blob: 078628fc41844a60b469ef6400077c0bf4dcd236 [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
Carl Shapirod2bdb572011-06-22 11:45:37 -070062// cases, you have to use the unsafe ARRAYSIZE_UNSAFE() macro below. This is
Carl Shapiroa2e18e12011-06-21 18:57:55 -070063// 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 Shapirod2bdb572011-06-22 11:45:37 -070074// ARRAYSIZE_UNSAFE performs essentially the same calculation as arraysize,
75// but can be used on anonymous types or types defined inside
76// functions. It's less safe than arraysize as it accepts some
77// (although not all) pointers. Therefore, you should use arraysize
78// whenever possible.
79//
80// The expression ARRAYSIZE_UNSAFE(a) is a compile-time constant of type
81// size_t.
82//
83// ARRAYSIZE_UNSAFE catches a few type errors. If you see a compiler error
84//
85// "warning: division by zero in ..."
86//
87// when using ARRAYSIZE_UNSAFE, you are (wrongfully) giving it a pointer.
88// You should only use ARRAYSIZE_UNSAFE on statically allocated arrays.
89//
90// The following comments are on the implementation details, and can
91// be ignored by the users.
92//
93// ARRAYSIZE_UNSAFE(arr) works by inspecting sizeof(arr) (the # of bytes in
94// the array) and sizeof(*(arr)) (the # of bytes in one array
95// element). If the former is divisible by the latter, perhaps arr is
96// indeed an array, in which case the division result is the # of
97// elements in the array. Otherwise, arr cannot possibly be an array,
98// and we generate a compiler error to prevent the code from
99// compiling.
100//
101// Since the size of bool is implementation-defined, we need to cast
102// !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
103// result has type size_t.
104//
105// This macro is not perfect as it wrongfully accepts certain
106// pointers, namely where the pointer size is divisible by the pointee
107// size. Since all our code has to go through a 32-bit compiler,
108// where a pointer is 4 bytes, this means all pointers to a type whose
109// size is 3 or greater than 4 will be (righteously) rejected.
110#define ARRAYSIZE_UNSAFE(a) \
111 ((sizeof(a) / sizeof(*(a))) / \
112 static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
113
Carl Shapiro59e85cd2011-06-21 10:16:23 -0700114#define SIZEOF_MEMBER(t, f) sizeof(((t*) 4096)->f)
115
116#define OFFSETOF_MEMBER(t, f) \
117 (reinterpret_cast<char*>( \
118 &reinterpret_cast<t*>(16)->f) - \
119 reinterpret_cast<char*>(16))
120
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700121#endif // ART_SRC_MACROS_H_