jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
assert.hpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #ifndef JUBATUS_CORE_COMMON_ASSERT_HPP_
18 #define JUBATUS_CORE_COMMON_ASSERT_HPP_
19 
20 #ifndef JUBATUS_DISABLE_ASSERTIONS
21 
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <iostream>
25 
26 #include <string>
27 #include "jubatus/util/lang/cast.h"
28 
29 #define JUBA_CHECK(a, op, b, message) { \
30  using jubatus::util::lang::lexical_cast; \
31  if (!((a)op(b))) { \
32  std::cerr << "ASSERTION FAILED: " << message << ": " \
33  << #a << " " << #op << " " << #b \
34  << " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \
35  std::cerr << "... where " << #a << " = " \
36  << lexical_cast<std::string>(a) << std::endl \
37  << " and " << #b << " = " \
38  << lexical_cast<std::string>(b) << std::endl; \
39  std::terminate(); \
40  }}
41 
42 // declares expr to be true; if false, messages are shown
43 #define JUBATUS_ASSERT_MSG(expr, message) \
44  do { \
45  if (!(expr)) { \
46  std::cerr << "ASSERTION FAILED: " << message << ": " << #expr \
47  << " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \
48  std::terminate(); \
49  } \
50  } while (0)
51 
52 // declares expr to be true
53 // unlike glog CHECK, this macro cannot take trailing `<<';
54 // please use JUBATUS_ASSERT_MSG if you need extra messages.
55 #define JUBATUS_ASSERT(expr) \
56  do { JUBATUS_ASSERT_MSG(expr, ""); } while (0)
57 
58 // declares control flow not to reach here
59 #define JUBATUS_ASSERT_UNREACHABLE() \
60  do { JUBATUS_ASSERT_MSG(0, "control flow not to reach here"); } while (0)
61 
62 // helpers to compare values
63 #define JUBATUS_ASSERT_EQ(a, b, messages) \
64  do { JUBA_CHECK(a, ==, b, messages); } while (0)
65 #define JUBATUS_ASSERT_NE(a, b, messages) \
66  do { JUBA_CHECK(a, !=, b, messages); } while (0)
67 #define JUBATUS_ASSERT_LE(a, b, messages) \
68  do { JUBA_CHECK(a, <=, b, messages); } while (0)
69 #define JUBATUS_ASSERT_LT(a, b, messages) \
70  do { JUBA_CHECK(a, <, b, messages); } while (0)
71 #define JUBATUS_ASSERT_GE(a, b, messages) \
72  do { JUBA_CHECK(a, >=, b, messages); } while (0)
73 #define JUBATUS_ASSERT_GT(a, b, messages) \
74  do { JUBA_CHECK(a, >, b, messages); } while (0)
75 
76 #else // #ifndef JUBATUS_DISABLE_ASSERTIONS
77 
78 #define JUBATUS_ASSERT(expr) ((void)0)
79 #define JUBATUS_ASSERT_MSG(expr, msg) ((void)0)
80 
81 #ifndef __has_builtin
82  #define __has_builtin(x) 0
83 #endif
84 
85 #if __has_builtin(__builtin_unreachable) || \
86  __GNUC__ == 4 && __GNUC_MINOR__ >= 5
87  // TODO(gintenlabo): Add __GUNC__ >= 5 if GCC 5.x has __builtin_unreachable
88  #define JUBATUS_ASSERT_UNREACHABLE() __builtin_unreachable()
89 #else
90  #include <exception> // NOLINT
91  #define JUBATUS_ASSERT_UNREACHABLE() std::terminate()
92 #endif
93 
94 #define JUBATUS_ASSERT_EQ(a, b, messages) ((void)0)
95 #define JUBATUS_ASSERT_NE(a, b, messages) ((void)0)
96 #define JUBATUS_ASSERT_LE(a, b, messages) ((void)0)
97 #define JUBATUS_ASSERT_LT(a, b, messages) ((void)0)
98 #define JUBATUS_ASSERT_GE(a, b, messages) ((void)0)
99 #define JUBATUS_ASSERT_GT(a, b, messages) ((void)0)
100 
101 #endif // #ifndef JUBATUS_DISABLE_ASSERTIONS
102 
103 #endif // JUBATUS_CORE_COMMON_ASSERT_HPP_