vSMC
vSMC: Scalable Monte Carlo
assert.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/internal/assert.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_INTERNAL_ASSERT_HPP
33 #define VSMC_INTERNAL_ASSERT_HPP
34 
35 #include <vsmc/internal/config.hpp>
36 
37 #include <cassert>
38 #include <cstdio>
39 #include <stdexcept>
40 #include <string>
41 
42 #if VSMC_NO_STATIC_ASSERT
43 #define VSMC_STATIC_ASSERT(cond, msg)
44 #else // VSMC_NO_STATIC_ASSERT
45 #if VSMC_HAS_CXX11_STATIC_ASSERT
46 #define VSMC_STATIC_ASSERT(cond, msg) static_assert(cond, #msg)
47 #else // VSMC_HAS_CXX11_STATIC_ASSERT
48 #define VSMC_STATIC_ASSERT(cond, msg) \
49 { \
50  struct VSMC_STATIC_ASSERT_FAILURE {enum msg {err};}; \
51  vsmc::internal::StaticAssert<static_cast<bool>(cond)>::test( \
52  VSMC_STATIC_ASSERT_FAILURE::err); \
53 }
54 #endif // VSMC_HAS_CXX11_STATIC_ASSERT
55 #endif // VSMC_NO_STATIC_ASSERT
56 
57 #if VSMC_NO_RUNTIME_ASSERT
58 #define VSMC_RUNTIME_ASSERT(cond, msg)
59 #else // VSMC_NO_RUNTIME_ASSERT
60 #if VSMC_RUNTIME_ASSERT_AS_EXCEPTION
61 #define VSMC_RUNTIME_ASSERT(cond, msg) \
62 { if (!(cond)) { throw vsmc::RuntimeAssert(msg); }; }
63 #else // VSMC_RUNTIME_ASSERT_AS_EXCEPTION
64 #define VSMC_RUNTIME_ASSERT(cond, msg) \
65 { \
66  if (!(cond)) { \
67  std::fprintf(stderr, \
68  "vSMC runtime assertion failed; File: %s; Line: %d\n%s\n", \
69  __FILE__, __LINE__, msg); \
70  std::fflush(stderr); \
71  }; \
72  assert(cond); \
73 }
74 #endif // VSMC_RUNTIME_ASSERT_AS_EXCEPTION
75 #endif // VSMC_NO_RUNTIME_ASSERT
76 
77 #if VSMC_NO_RUNTIME_WARNING
78 #define VSMC_RUNTIME_WARNING(cond, msg)
79 #else // VSMC_NO_RUNTIME_WARNING
80 #if VSMC_RUNTIME_WARNING_AS_EXCEPTION
81 #define VSMC_RUNTIME_WARNING(cond, msg) \
82 { if (!(cond)) { throw vsmc::RuntimeWarning(msg); }; }
83 #else // VSMC_RUNTIME_WARNING_AS_EXCEPTION
84 #define VSMC_RUNTIME_WARNING(cond, msg) \
85 { \
86  if (!(cond)) { \
87  std::fprintf(stderr, \
88  "vSMC runtime warning; File: %s; Line: %d\n%s\n", \
89  __FILE__, __LINE__, msg); \
90  std::fflush(stderr); \
91  }; \
92 }
93 #endif // VSMC_RUNTIME_WARNING_AS_EXCEPTION
94 #endif // VSMC_NO_RUNTIME_WARNING
95 
96 namespace vsmc {
97 
98 namespace internal {
99 
100 template <bool> struct StaticAssert {static void test (int *) {}};
101 
102 template <>
103 struct StaticAssert<true> {static void test (...) {}};
104 
105 } // namespace vsmc::internal
106 
107 class RuntimeAssert : public std::runtime_error
108 {
109  public :
110 
111  explicit RuntimeAssert (const char *msg) :
112  std::runtime_error(msg) {}
113 
114  explicit RuntimeAssert (const std::string &msg) :
115  std::runtime_error(msg) {}
116 }; // class RuntimeAssert
117 
118 class RuntimeWarning : public std::runtime_error
119 {
120  public :
121 
122  explicit RuntimeWarning (const char *msg) :
123  std::runtime_error(msg) {}
124 
125  explicit RuntimeWarning (const std::string &msg) :
126  std::runtime_error(msg) {}
127 }; // class RuntimeWarning
128 
129 } // namespace vsmc
130 
131 #endif // VSMC_INTERNAL_ASSERT_HPP
Definition: adapter.hpp:37
RuntimeAssert(const std::string &msg)
Definition: assert.hpp:114
STL namespace.
RuntimeWarning(const char *msg)
Definition: assert.hpp:122
RuntimeAssert(const char *msg)
Definition: assert.hpp:111
static void test(int *)
Definition: assert.hpp:100
RuntimeWarning(const std::string &msg)
Definition: assert.hpp:125