Apache Mesos
check.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_CHECK_HPP__
14 #define __STOUT_CHECK_HPP__
15 
16 #include <ostream>
17 #include <sstream>
18 #include <string>
19 
20 #include <glog/logging.h>
21 
22 #include <stout/abort.hpp>
23 #include <stout/error.hpp>
24 #include <stout/none.hpp>
25 #include <stout/option.hpp>
26 #include <stout/result.hpp>
27 #include <stout/some.hpp>
28 #include <stout/try.hpp>
29 
30 // A generic macro to facilitate definitions of CHECK_*, akin to CHECK.
31 // This appends the error if possible to the end of the log message,
32 // so there's no need to append the error message explicitly.
33 // To define a new CHECK_*, provide the name, the function that performs the
34 // check, and the expression. See below for examples (e.g. CHECK_SOME).
35 #define CHECK_STATE(name, check, expression) \
36  for (const Option<Error> _error = check(expression); _error.isSome();) \
37  _CheckFatal(__FILE__, \
38  __LINE__, \
39  #name, \
40  #expression, \
41  _error.get()).stream()
42 
43 
44 #define CHECK_SOME(expression) \
45  CHECK_STATE(CHECK_SOME, _check_some, expression)
46 
47 
48 #define CHECK_NONE(expression) \
49  CHECK_STATE(CHECK_NONE, _check_none, expression)
50 
51 
52 #define CHECK_ERROR(expression) \
53  CHECK_STATE(CHECK_ERROR, _check_error, expression)
54 
55 
56 // A private helper for CHECK_NOTNONE which is similar to the
57 // CHECK_NOTNULL provided by glog.
58 template <typename T>
60  const char* file,
61  int line,
62  const char* message,
63  Option<T>&& t) {
64  if (t.isNone()) {
65  google::LogMessageFatal(file, line, new std::string(message));
66  }
67  return std::move(t).get();
68 }
69 
70 
71 template <typename T>
73  const char* file,
74  int line,
75  const char* message,
76  Option<T>& t) {
77  if (t.isNone()) {
78  google::LogMessageFatal(file, line, new std::string(message));
79  }
80  return t.get();
81 }
82 
83 
84 template <typename T>
85 const T& _check_not_none(
86  const char* file,
87  int line,
88  const char* message,
89  const Option<T>& t) {
90  if (t.isNone()) {
91  google::LogMessageFatal(file, line, new std::string(message));
92  }
93  return t.get();
94 }
95 
96 
97 #define CHECK_NOTNONE(expression) \
98  _check_not_none( \
99  __FILE__, \
100  __LINE__, \
101  "'" #expression "' Must be SOME", \
102  (expression))
103 
104 
105 // Private structs/functions used for CHECK_*.
106 
107 template <typename T>
109 {
110  if (o.isNone()) {
111  return Error("is NONE");
112  } else {
113  CHECK(o.isSome());
114  return None();
115  }
116 }
117 
118 
119 template <typename T>
121 {
122  if (t.isError()) {
123  return Error(t.error());
124  } else {
125  CHECK(t.isSome());
126  return None();
127  }
128 }
129 
130 
131 template <typename T>
133 {
134  if (r.isError()) {
135  return Error(r.error());
136  } else if (r.isNone()) {
137  return Error("is NONE");
138  } else {
139  CHECK(r.isSome());
140  return None();
141  }
142 }
143 
144 
145 template <typename T>
147 {
148  if (o.isSome()) {
149  return Error("is SOME");
150  } else {
151  CHECK(o.isNone());
152  return None();
153  }
154 }
155 
156 
157 template <typename T>
159 {
160  if (r.isError()) {
161  return Error("is ERROR");
162  } else if (r.isSome()) {
163  return Error("is SOME");
164  } else {
165  CHECK(r.isNone());
166  return None();
167  }
168 }
169 
170 
171 template <typename T>
173 {
174  if (t.isSome()) {
175  return Error("is SOME");
176  } else {
177  CHECK(t.isError());
178  return None();
179  }
180 }
181 
182 
183 template <typename T>
185 {
186  if (r.isNone()) {
187  return Error("is NONE");
188  } else if (r.isSome()) {
189  return Error("is SOME");
190  } else {
191  CHECK(r.isError());
192  return None();
193  }
194 }
195 
196 
198 {
199  _CheckFatal(const char* _file,
200  int _line,
201  const char* type,
202  const char* expression,
203  const Error& error)
204  : file(_file),
205  line(_line)
206  {
207  out << type << "(" << expression << "): " << error.message << " ";
208  }
209 
211  {
212  google::LogMessageFatal(file.c_str(), line).stream() << out.str();
213  }
214 
215  std::ostream& stream()
216  {
217  return out;
218  }
219 
220  const std::string file;
221  const int line;
222  std::ostringstream out;
223 };
224 
225 #endif // __STOUT_CHECK_HPP__
std::ostream & stream()
Definition: check.hpp:215
bool isNone() const
Definition: result.hpp:112
Option< Error > _check_some(const Option< T > &o)
Definition: check.hpp:108
Definition: errorbase.hpp:35
Definition: option.hpp:28
const int line
Definition: check.hpp:221
Definition: try.hpp:34
const std::string file
Definition: check.hpp:220
static Result< T > error(const std::string &message)
Definition: result.hpp:53
~_CheckFatal()
Definition: check.hpp:210
Definition: result.hpp:40
bool isSome() const
Definition: option.hpp:115
URI file(const std::string &path)
Creates a file URI with the given path on the local host.
Definition: file.hpp:33
Definition: check.hpp:197
_CheckFatal(const char *_file, int _line, const char *type, const char *expression, const Error &error)
Definition: check.hpp:199
Option< Error > _check_none(const Option< T > &o)
Definition: check.hpp:146
bool isSome() const
Definition: try.hpp:70
const T & get() const &
Definition: option.hpp:118
static Try error(const E &e)
Definition: try.hpp:42
Option< Error > _check_error(const Try< T > &t)
Definition: check.hpp:172
const std::string message
Definition: errorbase.hpp:45
Definition: none.hpp:27
bool isError() const
Definition: try.hpp:71
std::string error(const std::string &msg, uint32_t code)
Try< uint32_t > type(const std::string &path)
bool isNone() const
Definition: option.hpp:116
bool isSome() const
Definition: result.hpp:111
bool isError() const
Definition: result.hpp:113
std::ostringstream out
Definition: check.hpp:222
T && _check_not_none(const char *file, int line, const char *message, Option< T > &&t)
Definition: check.hpp:59