All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Enforce.h
Go to the documentation of this file.
1 #ifndef ENFORCE_H_
2 #define ENFORCE_H_
3 
4 #include <cassert>
5 #include <stdexcept>
6 #include <string>
7 
8 namespace Aux {
9 
16 template<typename Exception = std::runtime_error>
17 inline void enforce(bool b, const char* msg = "") {
18  assert(msg && "Message to enforce must not be nullptr");
19  if (!b) {
20  throw Exception{msg};
21  }
22 }
23 
28 template<typename Exception = std::runtime_error>
29 inline void enforce(bool b, const std::string& msg) {
30  enforce<Exception>(b, msg.c_str());
31 }
32 
36 template<typename Stream>
37 inline void enforceOpened(const Stream& stream) {
38  enforce(stream.is_open());
39 }
40 
45 namespace Checkers {
46 
50  struct Asserter {
51  static void enforce(bool b) {
52  assert(b);
53  (void) b; // prevent warnings in release-builds
54  }
55  };
56 
60  struct Enforcer {
61  static void enforce(bool b) {
62  ::Aux::enforce(b);
63  }
64  };
65 
69  struct Terminator {
70  static void enforce(bool b) {
71  if (!b) {
72  std::terminate();
73  }
74  }
75  };
76 
80  struct Ignorer{
81  static void enforce(bool) {}
82  };
83 }
84 
85 } // namespace Aux
86 
87 
88 
89 #endif // ENFORCE_H_
static void enforce(bool b)
Definition: Enforce.h:51
static void enforce(bool b)
Definition: Enforce.h:70
static void enforce(bool b)
Definition: Enforce.h:61
void enforceOpened(const Stream &stream)
Checks that the provided fstream is opened and throws an exception otherwise.
Definition: Enforce.h:37
Won't look at the bool (not even in debug-mode, which is how this differs from Asserter) ...
Definition: Enforce.h:80
Checks to bool via enforce.
Definition: Enforce.h:60
static void enforce(bool)
Definition: Enforce.h:81
void enforce(bool b, const std::string &msg)
Overload that accepts a std::string.
Definition: Enforce.h:29
Checks the bool via assert.
Definition: Enforce.h:50
void enforce(bool b, const char *msg="")
Enforces that b is true and throws an Exception otherwise.
Definition: Enforce.h:17
Calls std::terminate if the bool is false.
Definition: Enforce.h:69