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-2015, 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.h>
36 
37 #include <cassert>
38 #include <cstdio>
39 #include <stdexcept>
40 #include <string>
41 
42 #define VSMC_STATIC_ASSERT(cond, msg) static_assert(cond, msg)
43 
44 #if VSMC_NO_RUNTIME_ASSERT
45 #define VSMC_RUNTIME_ASSERT(cond, msg)
46 #else // VSMC_NO_RUNTIME_ASSERT
47 #if VSMC_RUNTIME_ASSERT_AS_EXCEPTION
48 #define VSMC_RUNTIME_ASSERT(cond, msg) \
49  { \
50  if (!(cond)) { \
51  throw vsmc::RuntimeAssert(msg); \
52  }; \
53  }
54 #else // VSMC_RUNTIME_ASSERT_AS_EXCEPTION
55 #define VSMC_RUNTIME_ASSERT(cond, msg) \
56  { \
57  if (!(cond)) { \
58  std::fprintf(stderr, "vSMC runtime assertion failded:%s:%d:%s\n", \
59  __FILE__, __LINE__, msg); \
60  }; \
61  assert(cond); \
62  }
63 #endif // VSMC_RUNTIME_ASSERT_AS_EXCEPTION
64 #endif // VSMC_NO_RUNTIME_ASSERT
65 
66 #if VSMC_NO_RUNTIME_WARNING
67 #define VSMC_RUNTIME_WARNING(cond, msg)
68 #else // VSMC_NO_RUNTIME_WARNING
69 #if VSMC_RUNTIME_WARNING_AS_EXCEPTION
70 #define VSMC_RUNTIME_WARNING(cond, msg) \
71  { \
72  if (!(cond)) { \
73  throw vsmc::RuntimeWarning(msg); \
74  }; \
75  }
76 #else // VSMC_RUNTIME_WARNING_AS_EXCEPTION
77 #define VSMC_RUNTIME_WARNING(cond, msg) \
78  { \
79  if (!(cond)) { \
80  std::fprintf(stderr, \
81  "vSMC runtime warning; File: %s; Line: %d\n%s\n", __FILE__, \
82  __LINE__, msg); \
83  std::fflush(stderr); \
84  }; \
85  }
86 #endif // VSMC_RUNTIME_WARNING_AS_EXCEPTION
87 #endif // VSMC_NO_RUNTIME_WARNING
88 
89 namespace vsmc
90 {
91 
92 namespace internal
93 {
94 
95 template <bool>
97 {
98  public:
99  static void test(int *) {}
100 }; // class StaticAssert
101 
102 template <>
103 class StaticAssert<true>
104 {
105  public:
106  static void test(...) {}
107 }; // class StaticAssert
108 
109 } // namespace vsmc::internal
110 
111 class RuntimeAssert : public std::runtime_error
112 {
113  public:
114  explicit RuntimeAssert(const char *msg) : std::runtime_error(msg) {}
115 
116  explicit RuntimeAssert(const std::string &msg) : std::runtime_error(msg) {}
117 }; // class RuntimeAssert
118 
119 class RuntimeWarning : public std::runtime_error
120 {
121  public:
122  explicit RuntimeWarning(const char *msg) : std::runtime_error(msg) {}
123 
124  explicit RuntimeWarning(const std::string &msg) : std::runtime_error(msg)
125  {
126  }
127 }; // class RuntimeWarning
128 
129 } // namespace vsmc
130 
131 #endif // VSMC_INTERNAL_ASSERT_HPP
Definition: monitor.hpp:49
RuntimeAssert(const std::string &msg)
Definition: assert.hpp:116
STL namespace.
RuntimeWarning(const char *msg)
Definition: assert.hpp:122
RuntimeAssert(const char *msg)
Definition: assert.hpp:114
static void test(int *)
Definition: assert.hpp:99
RuntimeWarning(const std::string &msg)
Definition: assert.hpp:124