1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file |
3 | // distributed with this work for additional information |
4 | // regarding copyright ownership. The ASF licenses this file |
5 | // to you under the Apache License, Version 2.0 (the |
6 | // "License"); you may not use this file except in compliance |
7 | // with the License. You may obtain a copy of the License at |
8 | // |
9 | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | // |
11 | // Unless required by applicable law or agreed to in writing, |
12 | // software distributed under the License is distributed on an |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | // KIND, either express or implied. See the License for the |
15 | // specific language governing permissions and limitations |
16 | // under the License. |
17 | |
18 | #ifndef ARROW_UTIL_MACROS_H |
19 | #define ARROW_UTIL_MACROS_H |
20 | |
21 | #define ARROW_STRINGIFY(x) #x |
22 | |
23 | // From Google gutil |
24 | #ifndef ARROW_DISALLOW_COPY_AND_ASSIGN |
25 | #define ARROW_DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
26 | TypeName(const TypeName&) = delete; \ |
27 | void operator=(const TypeName&) = delete |
28 | #endif |
29 | |
30 | #define ARROW_UNUSED(x) (void)x |
31 | #define ARROW_ARG_UNUSED(x) |
32 | // |
33 | // GCC can be told that a certain branch is not likely to be taken (for |
34 | // instance, a CHECK failure), and use that information in static analysis. |
35 | // Giving it this information can help it optimize for the common case in |
36 | // the absence of better information (ie. -fprofile-arcs). |
37 | // |
38 | #if defined(__GNUC__) |
39 | #define ARROW_PREDICT_FALSE(x) (__builtin_expect(x, 0)) |
40 | #define ARROW_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) |
41 | #define ARROW_NORETURN __attribute__((noreturn)) |
42 | #define ARROW_PREFETCH(addr) __builtin_prefetch(addr) |
43 | #elif defined(_MSC_VER) |
44 | #define ARROW_NORETURN __declspec(noreturn) |
45 | #define ARROW_PREDICT_FALSE(x) x |
46 | #define ARROW_PREDICT_TRUE(x) x |
47 | #define ARROW_PREFETCH(addr) |
48 | #else |
49 | #define ARROW_NORETURN |
50 | #define ARROW_PREDICT_FALSE(x) x |
51 | #define ARROW_PREDICT_TRUE(x) x |
52 | #define ARROW_PREFETCH(addr) |
53 | #endif |
54 | |
55 | #if (defined(__GNUC__) || defined(__APPLE__)) |
56 | #define ARROW_MUST_USE_RESULT __attribute__((warn_unused_result)) |
57 | #elif defined(_MSC_VER) |
58 | #define ARROW_MUST_USE_RESULT |
59 | #else |
60 | #define ARROW_MUST_USE_RESULT |
61 | #endif |
62 | |
63 | // ---------------------------------------------------------------------- |
64 | // C++/CLI support macros (see ARROW-1134) |
65 | |
66 | #ifndef NULLPTR |
67 | |
68 | #ifdef __cplusplus_cli |
69 | #define NULLPTR __nullptr |
70 | #else |
71 | #define NULLPTR nullptr |
72 | #endif |
73 | |
74 | #endif // ifndef NULLPTR |
75 | |
76 | // ---------------------------------------------------------------------- |
77 | |
78 | // clang-format off |
79 | // [[deprecated]] is only available in C++14, use this for the time being |
80 | // This macro takes an optional deprecation message |
81 | #if __cplusplus <= 201103L |
82 | # ifdef __GNUC__ |
83 | # define ARROW_DEPRECATED(...) __attribute__((deprecated(__VA_ARGS__))) |
84 | # elif defined(_MSC_VER) |
85 | # define ARROW_DEPRECATED(...) __declspec(deprecated(__VA_ARGS__)) |
86 | # else |
87 | # define ARROW_DEPRECATED(...) |
88 | # endif |
89 | #else |
90 | # define ARROW_DEPRECATED(...) [[deprecated(__VA_ARGS__)]] |
91 | #endif |
92 | |
93 | // ---------------------------------------------------------------------- |
94 | |
95 | // macros to disable padding |
96 | // these macros are portable across different compilers and platforms |
97 | //[https://github.com/google/flatbuffers/blob/master/include/flatbuffers/flatbuffers.h#L1355] |
98 | #if !defined(MANUALLY_ALIGNED_STRUCT) |
99 | #if defined(_MSC_VER) |
100 | #define MANUALLY_ALIGNED_STRUCT(alignment) \ |
101 | __pragma(pack(1)); \ |
102 | struct __declspec(align(alignment)) |
103 | #define STRUCT_END(name, size) \ |
104 | __pragma(pack()); \ |
105 | static_assert(sizeof(name) == size, "compiler breaks packing rules") |
106 | #elif defined(__GNUC__) || defined(__clang__) |
107 | #define MANUALLY_ALIGNED_STRUCT(alignment) \ |
108 | _Pragma("pack(1)") struct __attribute__((aligned(alignment))) |
109 | #define STRUCT_END(name, size) \ |
110 | _Pragma("pack()") static_assert(sizeof(name) == size, "compiler breaks packing rules") |
111 | #else |
112 | #error Unknown compiler, please define structure alignment macros |
113 | #endif |
114 | #endif // !defined(MANUALLY_ALIGNED_STRUCT) |
115 | |
116 | // ---------------------------------------------------------------------- |
117 | // Convenience macro disabling a particular UBSan check in a function |
118 | |
119 | #if defined(__clang__) |
120 | #define ARROW_DISABLE_UBSAN(feature) __attribute__((no_sanitize(feature))) |
121 | #else |
122 | #define ARROW_DISABLE_UBSAN(feature) |
123 | #endif |
124 | |
125 | // ---------------------------------------------------------------------- |
126 | // From googletest |
127 | // (also in parquet-cpp) |
128 | |
129 | // When you need to test the private or protected members of a class, |
130 | // use the FRIEND_TEST macro to declare your tests as friends of the |
131 | // class. For example: |
132 | // |
133 | // class MyClass { |
134 | // private: |
135 | // void MyMethod(); |
136 | // FRIEND_TEST(MyClassTest, MyMethod); |
137 | // }; |
138 | // |
139 | // class MyClassTest : public testing::Test { |
140 | // // ... |
141 | // }; |
142 | // |
143 | // TEST_F(MyClassTest, MyMethod) { |
144 | // // Can call MyClass::MyMethod() here. |
145 | // } |
146 | |
147 | #define FRIEND_TEST(test_case_name, test_name) \ |
148 | friend class test_case_name##_##test_name##_Test |
149 | |
150 | #endif // ARROW_UTIL_MACROS_H |
151 | |