Kudu C++ client API
stubs.h
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 #ifndef KUDU_CLIENT_STUBS_H
18 #define KUDU_CLIENT_STUBS_H
19 
20 #include <stdlib.h> // for exit()
21 
22 #include <iostream>
23 
24 //
25 // GCC can be told that a certain branch is not likely to be taken (for
26 // instance, a CHECK failure), and use that information in static analysis.
27 // Giving it this information can help it optimize for the common case in
28 // the absence of better information (ie. -fprofile-arcs).
29 //
30 #ifndef PREDICT_FALSE
31 #if defined(__GNUC__)
32 #define PREDICT_FALSE(x) (__builtin_expect(x, 0))
33 #else
34 #define PREDICT_FALSE(x) x
35 #endif
36 #endif
37 #ifndef PREDICT_TRUE
38 #if defined(__GNUC__)
39 #define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
40 #else
41 #define PREDICT_TRUE(x) x
42 #endif
43 #endif
44 
45 // Annotate a function indicating the caller must examine the return value.
46 // Use like:
47 // int foo() WARN_UNUSED_RESULT;
48 // To explicitly ignore a result, see |ignore_result()| in <base/basictypes.h>.
49 #ifndef WARN_UNUSED_RESULT
50 #if defined(__GNUC__)
51 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
52 #else
53 #define WARN_UNUSED_RESULT
54 #endif
55 #endif
56 
57 #if (defined(__GNUC__) || defined(__APPLE__)) && !defined(SWIG)
58 #undef ATTRIBUTE_UNUSED
59 #define ATTRIBUTE_UNUSED __attribute__ ((unused))
60 #else
61 #ifndef ATTRIBUTE_UNUSED
62 #define ATTRIBUTE_UNUSED
63 #endif
64 #endif
65 
66 // For deprecated functions or variables, generate a warning at usage sites.
67 // Verified to work as early as GCC 3.1.1 and clang 3.2 (so we'll assume any
68 // clang is new enough).
69 #ifndef ATTRIBUTE_DEPRECATED
70 #if defined(__clang__) || \
71  (defined(COMPILER_GCC) && \
72  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 30200)
73 #define ATTRIBUTE_DEPRECATED(msg) __attribute__ ((deprecated (msg) ))
74 #else
75 #define ATTRIBUTE_DEPRECATED(msg)
76 #endif
77 #endif // #ifndef ATTRIBUTE_DEPRECATED
78 
79 #ifndef COMPILE_ASSERT
80 // The COMPILE_ASSERT macro can be used to verify that a compile time
81 // expression is true. For example, you could use it to verify the
82 // size of a static array:
83 //
84 // COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
85 // content_type_names_incorrect_size);
86 //
87 // or to make sure a struct is smaller than a certain size:
88 //
89 // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
90 //
91 // The second argument to the macro is the name of the variable. If
92 // the expression is false, most compilers will issue a warning/error
93 // containing the name of the variable.
94 
96 template <bool>
97 struct StubsCompileAssert {
98 };
100 
101 #define COMPILE_ASSERT(expr, msg) \
102  typedef StubsCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED // NOLINT(*)
103 #endif
104 
105 // Annotate a virtual method indicating it must be overriding a virtual
106 // method in the parent class.
107 // Use like:
108 // virtual void foo() OVERRIDE;
109 #ifndef OVERRIDE
110 # if defined(COMPILER_MSVC)
111 # define OVERRIDE override
112 # elif defined(__clang__) && __cplusplus >= 201103
113  // LLVM/Clang supports explicit virtual overrides, but warns about C++11
114  // extensions if compiling in pre-C++11 mode since the '-Wc++11-extensions'
115  // option is enabled by default.
116 # define OVERRIDE override
117 # elif defined(COMPILER_GCC) && __cplusplus >= 201103 && \
118  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
119  // GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
120 # define OVERRIDE override
121 # else
122 # define OVERRIDE
123 # endif
124 #endif // #ifndef OVERRIDE
125 
126 #ifndef DISALLOW_COPY_AND_ASSIGN
127 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
128  TypeName(const TypeName&); \
129  void operator=(const TypeName&)
130 #endif
131 
132 #ifndef FRIEND_TEST
133 #define FRIEND_TEST(test_case_name, test_name) \
134  friend class test_case_name##_##test_name##_Test
135 #endif
136 
137 // Stubbed versions of macros defined in glog/logging.h, intended for
138 // environments where glog headers aren't available.
139 //
140 // Add more as needed.
141 
142 #define KUDU_DCHECK(condition) while (false) kudu::internal_logging::NullLog()
143 #define KUDU_DCHECK_EQ(val1, val2) while (false) kudu::internal_logging::NullLog()
144 #define KUDU_DCHECK_NE(val1, val2) while (false) kudu::internal_logging::NullLog()
145 #define KUDU_DCHECK_LE(val1, val2) while (false) kudu::internal_logging::NullLog()
146 #define KUDU_DCHECK_LT(val1, val2) while (false) kudu::internal_logging::NullLog()
147 #define KUDU_DCHECK_GE(val1, val2) while (false) kudu::internal_logging::NullLog()
148 #define KUDU_DCHECK_GT(val1, val2) while (false) kudu::internal_logging::NullLog()
149 #define KUDU_DCHECK_NOTNULL(val) (val)
150 #define KUDU_DCHECK_STREQ(str1, str2) while (false) kudu::internal_logging::NullLog()
151 #define KUDU_DCHECK_STRCASEEQ(str1, str2) while (false) kudu::internal_logging::NullLog()
152 #define KUDU_DCHECK_STRNE(str1, str2) while (false) kudu::internal_logging::NullLog()
153 #define KUDU_DCHECK_STRCASENE(str1, str2) while (false) kudu::internal_logging::NullLog()
154 
155 // Log levels. LOG ignores them, so their values are abitrary.
156 
157 #define KUDU_INFO 0
158 #define KUDU_WARNING 1
159 #define KUDU_ERROR 2
160 #define KUDU_FATAL 3
161 
162 #ifdef NDEBUG
163 #define KUDU_DFATAL KUDU_WARNING
164 #else
165 #define KUDU_DFATAL KUDU_FATAL
166 #endif // NDEBUG
167 
168 #define KUDU_LOG_INTERNAL(level) kudu::internal_logging::CerrLog(level)
169 #define KUDU_LOG(level) KUDU_LOG_INTERNAL(KUDU_##level)
170 
171 #define KUDU_CHECK(condition) \
172  (condition) ? 0 : KUDU_LOG(FATAL) << "Check failed: " #condition " "
173 
174 namespace kudu {
175 
176 namespace internal_logging {
177 
182 class NullLog {
183  public:
189  template<class T>
190  NullLog& operator<<(const T& t) {
191  return *this;
192  }
193 };
194 
196 class CerrLog {
197  public:
202  CerrLog(int severity) // NOLINT(runtime/explicit)
203  : severity_(severity),
204  has_logged_(false) {
205  }
206 
207  ~CerrLog() {
208  if (has_logged_) {
209  std::cerr << std::endl;
210  }
211  if (severity_ == KUDU_FATAL) {
212  exit(1);
213  }
214  }
215 
221  template<class T>
222  CerrLog& operator<<(const T& t) {
223  has_logged_ = true;
224  std::cerr << t;
225  return *this;
226  }
227 
228  private:
229  const int severity_;
230  bool has_logged_;
231 };
232 
233 } // namespace internal_logging
234 } // namespace kudu
235 
236 #endif
A helper for the nil log sink.
Definition: stubs.h:182
Definition: callbacks.h:28
CerrLog(int severity)
Definition: stubs.h:202
A helper for stderr log sink.
Definition: stubs.h:196
CerrLog & operator<<(const T &t)
Definition: stubs.h:222
NullLog & operator<<(const T &t)
Definition: stubs.h:190