assertions.h
1 /* Copyright (C) 2019-2020 IBM Corp.
2  * This program is Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software
7  * distributed under the License is distributed on an "AS IS" BASIS,
8  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  * See the License for the specific language governing permissions and
10  * limitations under the License. See accompanying LICENSE file.
11  */
12 
13 // Header guard
14 #ifndef HELIB_ASSERTIONS_H
15 #define HELIB_ASSERTIONS_H
16 
17 #include <helib/exceptions.h>
18 
47 namespace helib {
48 
60 template <typename ExceptionTy = ::helib::LogicError, typename T = void>
61 inline void assertTrue(const T& value, const std::string& message)
62 {
63  static_assert(std::is_base_of<::helib::Exception, ExceptionTy>::value,
64  "ExceptionTy must inherit from helib::Exception");
65  static_assert(std::is_same<bool, T>::value, "Type T is not boolean");
66  if (!value) {
67  throw ExceptionTy(message);
68  }
69 }
70 
82 template <typename ExceptionTy = ::helib::LogicError, typename T = void>
83 inline void assertFalse(T value, const std::string& message)
84 {
85  static_assert(std::is_same<bool, T>::value, "Type T is not boolean");
86  static_assert(std::is_base_of<::helib::Exception, ExceptionTy>::value,
87  "ExceptionTy must inherit from helib::Exception");
88  if (value) {
89  throw ExceptionTy(message);
90  }
91 }
92 
107 template <typename ExceptionTy = ::helib::LogicError, typename T = void>
108 inline void assertEq(const T& a, const T& b, const std::string& message)
109 {
110  static_assert(std::is_base_of<::helib::Exception, ExceptionTy>::value,
111  "ExceptionTy must inherit from helib::Exception");
112  bool value = a == b;
113  if (!value) {
114  throw ExceptionTy(message);
115  }
116 }
117 
132 template <typename ExceptionTy = ::helib::LogicError, typename T = void>
133 inline void assertNeq(const T& a, const T& b, const std::string& message)
134 {
135  static_assert(std::is_base_of<::helib::Exception, ExceptionTy>::value,
136  "ExceptionTy must inherit from helib::Exception");
137  bool value = a != b;
138  if (!value) {
139  throw ExceptionTy(message);
140  }
141 }
142 
155 template <typename ExceptionTy = ::helib::LogicError, typename T = void>
156 inline void assertNotNull(const T& p, const std::string& message)
157 {
158  static_assert(std::is_base_of<::helib::Exception, ExceptionTy>::value,
159  "ExceptionTy must inherit from helib::Exception");
160  if (p == nullptr) {
161  throw ExceptionTy(message);
162  }
163 }
164 
182 template <typename ExceptionTy = ::helib::OutOfRangeError, typename T = void>
183 inline void assertInRange(const T& elem,
184  const T& min,
185  const T& max,
186  const std::string& message,
187  bool right_inclusive = false)
188 {
189  static_assert(std::is_base_of<::helib::Exception, ExceptionTy>::value,
190  "ExceptionTy must inherit from helib::Exception");
191  bool min_less = min <= elem;
192  if (!min_less) {
193  throw ExceptionTy(message);
194  }
195  bool max_great = right_inclusive ? elem <= max : elem < max;
196  if (!max_great) {
197  throw ExceptionTy(message);
198  }
199 }
200 
201 } // namespace helib
202 #endif // End of header guard
void assertTrue(const T &value, const std::string &message)
Definition: assertions.h:61
void assertEq(const T &a, const T &b, const std::string &message)
Definition: assertions.h:108
void assertInRange(const T &elem, const T &min, const T &max, const std::string &message, bool right_inclusive=false)
Definition: assertions.h:183
void assertNeq(const T &a, const T &b, const std::string &message)
Definition: assertions.h:133
void assertNotNull(const T &p, const std::string &message)
Definition: assertions.h:156
Definition: apiAttributes.h:21
void assertFalse(T value, const std::string &message)
Definition: assertions.h:83