rcpputils  master
C++ API providing common utilities and data structures.
asserts.hpp
Go to the documentation of this file.
1 // Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
19 #ifndef RCPPUTILS__ASSERTS_HPP_
20 #define RCPPUTILS__ASSERTS_HPP_
21 
22 #include <exception>
23 #include <stdexcept>
24 #include <string>
25 
27 
28 // Needed to disable compiler warning for exporting a class that extends a
29 // non-DLL-interface class.
30 // This should be fine since its extending an STL class.
31 #ifdef _WIN32
32 # pragma warning(push)
33 # pragma warning(disable:4251)
34 # pragma warning(disable:4275)
35 #endif
36 
37 namespace rcpputils
38 {
39 
44 {
45  std::string msg_;
46 
47 public:
53  explicit AssertionException(const char * msg);
54 
60  virtual const char * what() const noexcept;
61 };
62 
68 {
69  std::string msg_;
70 
71 public:
78  explicit IllegalStateException(const char * msg);
79 
85  virtual const char * what() const noexcept;
86 };
87 
95 inline void require_true(bool condition, const std::string & msg = "invalid argument passed")
96 {
97  if (!condition) {
98  throw std::invalid_argument{msg};
99  }
100 }
101 
109 inline void check_true(bool condition, const std::string & msg = "check reported invalid state")
110 {
111  if (!condition) {
112  throw rcpputils::IllegalStateException{msg.c_str()};
113  }
114 }
115 
126 inline void assert_true(bool condition, const std::string & msg = "assertion failed")
127 {
128 // Same macro definition used by cassert
129 #ifndef NDEBUG
130  if (!condition) {
131  throw rcpputils::AssertionException{msg.c_str()};
132  }
133 #else
134  (void) condition;
135  (void) msg;
136 #endif
137 }
138 } // namespace rcpputils
139 
140 #ifdef _WIN32
141 # pragma warning(pop)
142 #endif
143 
144 #endif // RCPPUTILS__ASSERTS_HPP_
std::string
std::exception
rcpputils
Definition: asserts.hpp:37
rcpputils::check_true
void check_true(bool condition, const std::string &msg="check reported invalid state")
Check that a state condition passes.
Definition: asserts.hpp:109
rcpputils::IllegalStateException
An exception to be thrown when a state check fails.
Definition: asserts.hpp:67
rcpputils::assert_true
void assert_true(bool condition, const std::string &msg="assertion failed")
Assert that a condition passes.
Definition: asserts.hpp:126
std::invalid_argument
rcpputils::AssertionException
An assertion-like exception for halting tests if conditions are not met.
Definition: asserts.hpp:43
rcpputils::require_true
void require_true(bool condition, const std::string &msg="invalid argument passed")
Check that an argument condition passes.
Definition: asserts.hpp:95
visibility_control.hpp
Macros for controlling visibilty of exported iterfaces.
RCPPUTILS_PUBLIC
#define RCPPUTILS_PUBLIC
Declares symbols and functions will be visible for export.
Definition: visibility_control.hpp:81