|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// Author: Torarin Hals Bakke (2012) |
| 4 | + |
| 5 | +// Boost Software License - Version 1.0 - August 17th, 2003 |
| 6 | + |
| 7 | +// Permission is hereby granted, free of charge, to any person or organization |
| 8 | +// obtaining a copy of the software and accompanying documentation covered by |
| 9 | +// this license (the "Software") to use, reproduce, display, distribute, |
| 10 | +// execute, and transmit the Software, and to prepare derivative works of the |
| 11 | +// Software, and to permit third-parties to whom the Software is furnished to |
| 12 | +// do so, all subject to the following: |
| 13 | + |
| 14 | +// The copyright notices in the Software and this entire statement, including |
| 15 | +// the above license grant, this restriction and the following disclaimer, |
| 16 | +// must be included in all copies of the Software, in whole or in part, and |
| 17 | +// all derivative works of the Software, unless such copies or derivative |
| 18 | +// works are solely in the form of machine-executable object code generated by |
| 19 | +// a source language processor. |
| 20 | + |
| 21 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 24 | +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 25 | +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 26 | +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 27 | +// DEALINGS IN THE SOFTWARE. |
| 28 | + |
| 29 | +#include <boost/config.hpp> |
| 30 | +#include <string> |
| 31 | +#include <utility> |
| 32 | +#include <type_traits> |
| 33 | +#include <exception> |
| 34 | + |
| 35 | +class Enforce_error : public std::exception { |
| 36 | + std::string what_; |
| 37 | + std::string user_friendly_what_; |
| 38 | +public: |
| 39 | + template<typename T, typename U> |
| 40 | + explicit Enforce_error(T&& what_arg, U&& user_friendly_what_arg) |
| 41 | + : what_(std::forward<T>(what_arg)), |
| 42 | + user_friendly_what_(std::forward<U>(user_friendly_what_arg)) |
| 43 | + {} |
| 44 | + |
| 45 | +#if __GNUC__ == 4 && __GNUC_MINOR__ == 6 |
| 46 | + ~Enforce_error() throw() {} |
| 47 | +#endif |
| 48 | + |
| 49 | + const char* what() const BOOST_NOEXCEPT { |
| 50 | + return what_.c_str(); |
| 51 | + } |
| 52 | + |
| 53 | + const std::string& user_friendly_what() const BOOST_NOEXCEPT { |
| 54 | + return user_friendly_what_; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +template <typename T, typename U> |
| 59 | +void enforce(bool condition, T&& msg, U&& user_friendly_msg) { |
| 60 | + static_assert(std::is_convertible<T, std::string>::value, |
| 61 | + "msg must be convertible to std::string"); |
| 62 | + static_assert(std::is_convertible<U, std::string>::value, |
| 63 | + "user_friendly_msg must be convertible to std::string"); |
| 64 | + if (!condition) |
| 65 | + throw Enforce_error(std::forward<T>(msg), std::forward<U>(user_friendly_msg)); |
| 66 | +} |
| 67 | + |
| 68 | +#define ENFORCE_MESSAGE "Enforcement failed in " __FILE__ "(" BOOST_STRINGIZE(__LINE__) ")" |
| 69 | + |
| 70 | +#define ENFORCE(condition) enforce(condition, ENFORCE_MESSAGE, "") |
| 71 | +#define ENFORCE2(condition, user_friendly_msg) enforce(condition, ENFORCE_MESSAGE, user_friendly_msg) |
0 commit comments