• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef GOOGLE_PROTOBUF_MACROS_H__
32 #define GOOGLE_PROTOBUF_MACROS_H__
33 
34 #include <google/protobuf/stubs/port.h>
35 
36 namespace google {
37 namespace protobuf {
38 
39 #undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
40 #define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)    \
41   TypeName(const TypeName&);                           \
42   void operator=(const TypeName&)
43 
44 #undef GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS
45 #define GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
46   TypeName();                                           \
47   TypeName(const TypeName&);                            \
48   void operator=(const TypeName&)
49 
50 // ===================================================================
51 // from google3/base/basictypes.h
52 
53 // The GOOGLE_ARRAYSIZE(arr) macro returns the # of elements in an array arr.
54 // The expression is a compile-time constant, and therefore can be
55 // used in defining new arrays, for example.
56 //
57 // GOOGLE_ARRAYSIZE catches a few type errors.  If you see a compiler error
58 //
59 //   "warning: division by zero in ..."
60 //
61 // when using GOOGLE_ARRAYSIZE, you are (wrongfully) giving it a pointer.
62 // You should only use GOOGLE_ARRAYSIZE on statically allocated arrays.
63 //
64 // The following comments are on the implementation details, and can
65 // be ignored by the users.
66 //
67 // ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in
68 // the array) and sizeof(*(arr)) (the # of bytes in one array
69 // element).  If the former is divisible by the latter, perhaps arr is
70 // indeed an array, in which case the division result is the # of
71 // elements in the array.  Otherwise, arr cannot possibly be an array,
72 // and we generate a compiler error to prevent the code from
73 // compiling.
74 //
75 // Since the size of bool is implementation-defined, we need to cast
76 // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
77 // result has type size_t.
78 //
79 // This macro is not perfect as it wrongfully accepts certain
80 // pointers, namely where the pointer size is divisible by the pointee
81 // size.  Since all our code has to go through a 32-bit compiler,
82 // where a pointer is 4 bytes, this means all pointers to a type whose
83 // size is 3 or greater than 4 will be (righteously) rejected.
84 //
85 // Kudos to Jorg Brown for this simple and elegant implementation.
86 
87 #undef GOOGLE_ARRAYSIZE
88 #define GOOGLE_ARRAYSIZE(a) \
89   ((sizeof(a) / sizeof(*(a))) / \
90    static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
91 
92 // The COMPILE_ASSERT macro can be used to verify that a compile time
93 // expression is true. For example, you could use it to verify the
94 // size of a static array:
95 //
96 //   COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
97 //                  content_type_names_incorrect_size);
98 //
99 // or to make sure a struct is smaller than a certain size:
100 //
101 //   COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
102 //
103 // The second argument to the macro is the name of the variable. If
104 // the expression is false, most compilers will issue a warning/error
105 // containing the name of the variable.
106 
107 namespace internal {
108 
109 template <bool>
110 struct CompileAssert {
111 };
112 
113 }  // namespace internal
114 
115 #define GOOGLE_COMPILE_ASSERT(expr, msg) static_assert(expr, #msg)
116 
117 }  // namespace protobuf
118 }  // namespace google
119 
120 #endif  // GOOGLE_PROTOBUF_MACROS_H__
121