blob: 90a446baa84916176f3e461eab543c3280267ecd [file] [log] [blame]
Carl Shapiro6c21dc12011-06-20 15:20:52 -07001// Copyright 2010 Google
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070014#ifndef ART_SRC_MACROS_H_
15#define ART_SRC_MACROS_H_
Carl Shapiro6c21dc12011-06-20 15:20:52 -070016
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070017// The COMPILE_ASSERT macro can be used to verify that a compile time
18// expression is true. For example, you could use it to verify the
19// size of a static array:
20//
21// COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
22// content_type_names_incorrect_size);
23//
24// or to make sure a struct is smaller than a certain size:
25//
26// COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
27//
28// The second argument to the macro is the name of the variable. If
29// the expression is false, most compilers will issue a warning/error
30// containing the name of the variable.
31
32template <bool>
33struct CompileAssert {
34};
35
36#define COMPILE_ASSERT(expr, msg) \
37 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
38
Carl Shapiro6c21dc12011-06-20 15:20:52 -070039// DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions.
40// It goes in the private: declarations in a class.
41#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
42 TypeName(const TypeName&); \
43 void operator=(const TypeName&)
44
45// A macro to disallow all the implicit constructors, namely the
46// default constructor, copy constructor and operator= functions.
47//
48// This should be used in the private: declarations for a class
49// that wants to prevent anyone from instantiating it. This is
50// especially useful for classes containing only static methods.
51#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
52 TypeName(); \
53 DISALLOW_COPY_AND_ASSIGN(TypeName)
54
Carl Shapiro59e85cd2011-06-21 10:16:23 -070055#define SIZEOF_MEMBER(t, f) sizeof(((t*) 4096)->f)
56
57#define OFFSETOF_MEMBER(t, f) \
58 (reinterpret_cast<char*>( \
59 &reinterpret_cast<t*>(16)->f) - \
60 reinterpret_cast<char*>(16))
61
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070062#endif // ART_SRC_MACROS_H_