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