Kudu C++ client API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
95 template <bool>
97 };
98 
99 #define COMPILE_ASSERT(expr, msg) \
100  typedef StubsCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED // NOLINT(*)
101 #endif
102 
103 // Annotate a virtual method indicating it must be overriding a virtual
104 // method in the parent class.
105 // Use like:
106 // virtual void foo() OVERRIDE;
107 #ifndef OVERRIDE
108 #if defined(COMPILER_MSVC)
109 #define OVERRIDE override
110 #elif defined(__clang__)
111 #define OVERRIDE override
112 #elif defined(COMPILER_GCC) && __cplusplus >= 201103 && \
113  (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40700
114 // GCC 4.7 supports explicit virtual overrides when C++11 support is enabled.
115 #define OVERRIDE override
116 #else
117 #define OVERRIDE
118 #endif
119 #endif
120 
121 #ifndef DISALLOW_COPY_AND_ASSIGN
122 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
123  TypeName(const TypeName&); \
124  void operator=(const TypeName&)
125 #endif
126 
127 #ifndef FRIEND_TEST
128 #define FRIEND_TEST(test_case_name, test_name) \
129  friend class test_case_name##_##test_name##_Test
130 #endif
131 
132 // Stubbed versions of macros defined in glog/logging.h, intended for
133 // environments where glog headers aren't available.
134 //
135 // Add more as needed.
136 
137 #define KUDU_DCHECK(condition) while (false) kudu::internal_logging::NullLog()
138 #define KUDU_DCHECK_EQ(val1, val2) while (false) kudu::internal_logging::NullLog()
139 #define KUDU_DCHECK_NE(val1, val2) while (false) kudu::internal_logging::NullLog()
140 #define KUDU_DCHECK_LE(val1, val2) while (false) kudu::internal_logging::NullLog()
141 #define KUDU_DCHECK_LT(val1, val2) while (false) kudu::internal_logging::NullLog()
142 #define KUDU_DCHECK_GE(val1, val2) while (false) kudu::internal_logging::NullLog()
143 #define KUDU_DCHECK_GT(val1, val2) while (false) kudu::internal_logging::NullLog()
144 #define KUDU_DCHECK_NOTNULL(val) (val)
145 #define KUDU_DCHECK_STREQ(str1, str2) while (false) kudu::internal_logging::NullLog()
146 #define KUDU_DCHECK_STRCASEEQ(str1, str2) while (false) kudu::internal_logging::NullLog()
147 #define KUDU_DCHECK_STRNE(str1, str2) while (false) kudu::internal_logging::NullLog()
148 #define KUDU_DCHECK_STRCASENE(str1, str2) while (false) kudu::internal_logging::NullLog()
149 
150 // Log levels. LOG ignores them, so their values are abitrary.
151 
152 #define KUDU_INFO 0
153 #define KUDU_WARNING 1
154 #define KUDU_ERROR 2
155 #define KUDU_FATAL 3
156 
157 #ifdef NDEBUG
158 #define KUDU_DFATAL KUDU_WARNING
159 #else
160 #define KUDU_DFATAL KUDU_FATAL
161 #endif // NDEBUG
162 
163 #define KUDU_LOG_INTERNAL(level) kudu::internal_logging::CerrLog(level)
164 #define KUDU_LOG(level) KUDU_LOG_INTERNAL(KUDU_##level)
165 
166 #define KUDU_CHECK(condition) \
167  (condition) ? 0 : KUDU_LOG(FATAL) << "Check failed: " #condition " "
168 
169 namespace kudu {
170 
171 namespace internal_logging {
172 
177 class NullLog {
178  public:
184  template<class T>
185  NullLog& operator<<(const T& t) {
186  return *this;
187  }
188 };
189 
191 class CerrLog {
192  public:
197  CerrLog(int severity) // NOLINT(runtime/explicit)
198  : severity_(severity),
199  has_logged_(false) {
200  }
201 
202  ~CerrLog() {
203  if (has_logged_) {
204  std::cerr << std::endl;
205  }
206  if (severity_ == KUDU_FATAL) {
207  exit(1);
208  }
209  }
210 
216  template<class T>
217  CerrLog& operator<<(const T& t) {
218  has_logged_ = true;
219  std::cerr << t;
220  return *this;
221  }
222 
223  private:
224  const int severity_;
225  bool has_logged_;
226 };
227 
228 } // namespace internal_logging
229 } // namespace kudu
230 
231 #endif
Definition: stubs.h:96
A helper for the nil log sink.
Definition: stubs.h:177
CerrLog(int severity)
Definition: stubs.h:197
A helper for stderr log sink.
Definition: stubs.h:191
CerrLog & operator<<(const T &t)
Definition: stubs.h:217
NullLog & operator<<(const T &t)
Definition: stubs.h:185