vSMC  v3.0.0
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-2016, 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 <limits>
40 #include <stdexcept>
41 #include <string>
42 
43 #ifdef VSMC_CLANG
44 #pragma clang diagnostic push
45 #pragma clang diagnostic ignored "-Wweak-vtables"
46 #endif
47 
48 #if VSMC_NO_RUNTIME_ASSERT
49 #define VSMC_RUNTIME_ASSERT(cond, msg)
50 #else // VSMC_NO_RUNTIME_ASSERT
51 #if VSMC_RUNTIME_ASSERT_AS_EXCEPTION
52 #define VSMC_RUNTIME_ASSERT(cond, msg) \
53  { \
54  if (!(cond)) { \
55  throw ::vsmc::RuntimeAssert(msg); \
56  }; \
57  }
58 #else // VSMC_RUNTIME_ASSERT_AS_EXCEPTION
59 #define VSMC_RUNTIME_ASSERT(cond, msg) \
60  { \
61  if (!(cond)) { \
62  std::fprintf(stderr, "vSMC runtime assertion failed:%s\n", msg); \
63  std::fflush(stderr); \
64  }; \
65  assert(cond); \
66  }
67 #endif // VSMC_RUNTIME_ASSERT_AS_EXCEPTION
68 #endif // VSMC_NO_RUNTIME_ASSERT
69 
70 #if VSMC_NO_RUNTIME_WARNING
71 #define VSMC_RUNTIME_WARNING(cond, msg)
72 #else // VSMC_NO_RUNTIME_WARNING
73 #if VSMC_RUNTIME_WARNING_AS_EXCEPTION
74 #define VSMC_RUNTIME_WARNING(cond, msg) \
75  { \
76  if (!(cond)) { \
77  throw ::vsmc::RuntimeWarning(msg); \
78  }; \
79  }
80 #else // VSMC_RUNTIME_WARNING_AS_EXCEPTION
81 #define VSMC_RUNTIME_WARNING(cond, msg) \
82  { \
83  if (!(cond)) { \
84  std::fprintf(stderr, "vSMC runtime warning:%s\n", msg); \
85  std::fflush(stderr); \
86  }; \
87  }
88 #endif // VSMC_RUNTIME_WARNING_AS_EXCEPTION
89 #endif // VSMC_NO_RUNTIME_WARNING
90 
91 namespace vsmc
92 {
93 
94 class RuntimeAssert : public std::runtime_error
95 {
96  public:
97  explicit RuntimeAssert(const char *msg) : std::runtime_error(msg) {}
98 
99  explicit RuntimeAssert(const std::string &msg) : std::runtime_error(msg) {}
100 }; // class RuntimeAssert
101 
102 class RuntimeWarning : public std::runtime_error
103 {
104  public:
105  explicit RuntimeWarning(const char *msg) : std::runtime_error(msg) {}
106 
107  explicit RuntimeWarning(const std::string &msg) : std::runtime_error(msg)
108  {
109  }
110 }; // class RuntimeWarning
111 
112 namespace internal
113 {
114 
115 #if VSMC_NO_RUNTIME_ASSERT
116 template <typename IntType, typename SizeType>
117 inline void size_check(SizeType, const char *)
118 {
119 }
120 #else // VSMC_NO_RUNTIME_ASSERT
121 template <typename IntType, typename SizeType>
122 inline void size_check(SizeType n, const char *f)
123 {
124  static constexpr std::uintmax_t nmax =
125  static_cast<std::uintmax_t>(std::numeric_limits<IntType>::max());
126  std::string msg;
127  msg += "**";
128  msg += f;
129  msg += "** INPUT SIZE TOO BIG";
130 
131  VSMC_RUNTIME_ASSERT((static_cast<std::uintmax_t>(n) <= nmax), msg.c_str());
132 }
133 #endif // VSMC_NO_RUNTIME_ASSERT
134 
135 } // namespace vsmc::internal
136 
137 } // namespace vsmc
138 
139 #ifdef VSMC_CLANG
140 #pragma clang diagnostic pop
141 #endif
142 
143 #endif // VSMC_INTERNAL_ASSERT_HPP
Definition: monitor.hpp:48
RuntimeAssert(const std::string &msg)
Definition: assert.hpp:99
void size_check(SizeType n, const char *f)
Definition: assert.hpp:122
#define VSMC_RUNTIME_ASSERT(cond, msg)
Definition: assert.hpp:59
STL namespace.
RuntimeWarning(const char *msg)
Definition: assert.hpp:105
RuntimeAssert(const char *msg)
Definition: assert.hpp:97
typename SizeTypeTrait< T >::type SizeType
Definition: traits.hpp:212
RuntimeWarning(const std::string &msg)
Definition: assert.hpp:107