blob: be31e03465ee12b5731b5f06224e76687cd1bb09 [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 Shapiroa2e18e12011-06-21 18:57:55 -070055// The arraysize(arr) macro returns the # of elements in an array arr.
56// The expression is a compile-time constant, and therefore can be
57// used in defining new arrays, for example. If you use arraysize on
58// a pointer by mistake, you will get a compile-time error.
59//
60// One caveat is that arraysize() doesn't accept any array of an
61// anonymous type or a type defined inside a function. In these rare
62// cases, you have to use the unsafe ARRAYSIZE() macro below. This is
63// due to a limitation in C++'s template system. The limitation might
64// eventually be removed, but it hasn't happened yet.
65
66// This template function declaration is used in defining arraysize.
67// Note that the function doesn't need an implementation, as we only
68// use its type.
69template <typename T, size_t N>
70char (&ArraySizeHelper(T (&array)[N]))[N];
71
72#define arraysize(array) (sizeof(ArraySizeHelper(array)))
73
Carl Shapiro59e85cd2011-06-21 10:16:23 -070074#define SIZEOF_MEMBER(t, f) sizeof(((t*) 4096)->f)
75
76#define OFFSETOF_MEMBER(t, f) \
77 (reinterpret_cast<char*>( \
78 &reinterpret_cast<t*>(16)->f) - \
79 reinterpret_cast<char*>(16))
80
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070081#endif // ART_SRC_MACROS_H_