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-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 <stdexcept>
40 #include <string>
41 
42 #if VSMC_NO_RUNTIME_ASSERT
43 #define VSMC_RUNTIME_ASSERT(cond, msg)
44 #else // VSMC_NO_RUNTIME_ASSERT
45 #if VSMC_RUNTIME_ASSERT_AS_EXCEPTION
46 #define VSMC_RUNTIME_ASSERT(cond, msg) \
47  { \
48  if (!(cond)) { \
49  throw vsmc::RuntimeAssert(msg); \
50  }; \
51  }
52 #else // VSMC_RUNTIME_ASSERT_AS_EXCEPTION
53 #define VSMC_RUNTIME_ASSERT(cond, msg) \
54  { \
55  if (!(cond)) { \
56  std::fprintf(stderr, "vSMC runtime assertion failded:%s:%d:%s\n", \
57  __FILE__, __LINE__, msg); \
58  }; \
59  assert(cond); \
60  }
61 #endif // VSMC_RUNTIME_ASSERT_AS_EXCEPTION
62 #endif // VSMC_NO_RUNTIME_ASSERT
63 
64 #if VSMC_NO_RUNTIME_WARNING
65 #define VSMC_RUNTIME_WARNING(cond, msg)
66 #else // VSMC_NO_RUNTIME_WARNING
67 #if VSMC_RUNTIME_WARNING_AS_EXCEPTION
68 #define VSMC_RUNTIME_WARNING(cond, msg) \
69  { \
70  if (!(cond)) { \
71  throw vsmc::RuntimeWarning(msg); \
72  }; \
73  }
74 #else // VSMC_RUNTIME_WARNING_AS_EXCEPTION
75 #define VSMC_RUNTIME_WARNING(cond, msg) \
76  { \
77  if (!(cond)) { \
78  std::fprintf(stderr, \
79  "vSMC runtime warning; File: %s; Line: %d\n%s\n", __FILE__, \
80  __LINE__, msg); \
81  std::fflush(stderr); \
82  }; \
83  }
84 #endif // VSMC_RUNTIME_WARNING_AS_EXCEPTION
85 #endif // VSMC_NO_RUNTIME_WARNING
86 
87 namespace vsmc
88 {
89 
90 class RuntimeAssert : public std::runtime_error
91 {
92  public:
93  explicit RuntimeAssert(const char *msg) : std::runtime_error(msg) {}
94 
95  explicit RuntimeAssert(const std::string &msg) : std::runtime_error(msg) {}
96 }; // class RuntimeAssert
97 
98 class RuntimeWarning : public std::runtime_error
99 {
100  public:
101  explicit RuntimeWarning(const char *msg) : std::runtime_error(msg) {}
102 
103  explicit RuntimeWarning(const std::string &msg) : std::runtime_error(msg)
104  {
105  }
106 }; // class RuntimeWarning
107 
108 } // namespace vsmc
109 
110 #endif // VSMC_INTERNAL_ASSERT_HPP
Definition: monitor.hpp:49
RuntimeAssert(const std::string &msg)
Definition: assert.hpp:95
STL namespace.
RuntimeWarning(const char *msg)
Definition: assert.hpp:101
RuntimeAssert(const char *msg)
Definition: assert.hpp:93
RuntimeWarning(const std::string &msg)
Definition: assert.hpp:103