|
| 1 | +/* |
| 2 | + * Copyright 2021 Niket Naidu. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef ENV_ASSERT_THROW_H_ |
| 18 | +#define ENV_ASSERT_THROW_H_ |
| 19 | + |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include "logging.h" |
| 23 | + |
| 24 | +namespace buildcc::env { |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief Compile time expr asserts fatally when false |
| 28 | + */ |
| 29 | +template <bool expr> inline void assert_throw(const char *message) { |
| 30 | + if constexpr (!expr) { |
| 31 | + env::log_critical("assert", message); |
| 32 | + // TODO, If needed specialize this |
| 33 | + throw std::exception(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * @brief Compile time expr asserts fatally when false |
| 39 | + */ |
| 40 | +template <bool expr> inline void assert_throw(const std::string &message) { |
| 41 | + assert_throw<expr>(message.c_str()); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief Runtime expr asserts fatally when false |
| 46 | + */ |
| 47 | +inline void assert_throw(bool expression, const char *message) { |
| 48 | + if (!expression) { |
| 49 | + assert_throw<false>(message); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * @brief Runtime expr asserts fatally when false |
| 55 | + */ |
| 56 | +inline void assert_throw(bool expression, const std::string &message) { |
| 57 | + assert_throw(expression, message.c_str()); |
| 58 | +} |
| 59 | + |
| 60 | +} // namespace buildcc::env |
| 61 | + |
| 62 | +/** |
| 63 | + * @brief Runtime expr assert throws when false |
| 64 | + */ |
| 65 | +#define ASSERT_THROW(expr, message) \ |
| 66 | + ((expr) ? static_cast<void>(0) : buildcc::env::assert_throw<false>(message)) |
| 67 | + |
| 68 | +#endif |
0 commit comments