1 /* 2 Exception classes and assertions 3 4 Copyright (C) 2013 AMPL Optimization Inc 5 6 Permission to use, copy, modify, and distribute this software and its 7 documentation for any purpose and without fee is hereby granted, 8 provided that the above copyright notice appear in all copies and that 9 both that the copyright notice and this permission notice and warranty 10 disclaimer appear in supporting documentation. 11 12 The author and AMPL Optimization Inc disclaim all warranties with 13 regard to this software, including all implied warranties of 14 merchantability and fitness. In no event shall the author be liable 15 for any special, indirect or consequential damages or any damages 16 whatsoever resulting from loss of use, data or profits, whether in an 17 action of contract, negligence or other tortious action, arising out 18 of or in connection with the use or performance of this software. 19 20 Author: Victor Zverovich 21 */ 22 23 #ifndef MP_ERROR_H_ 24 #define MP_ERROR_H_ 25 26 #include "mp/format.h" 27 28 namespace mp { 29 30 #ifndef MP_ASSERT 31 # define MP_ASSERT(condition, message) assert((condition) && message) 32 #endif 33 34 #define MP_RAISE(msg) throw std::runtime_error(msg) 35 #define MP_WARNING(msg) Print(msg) 36 // A general error. 37 class Error : public fmt::internal::RuntimeError { 38 protected: 39 Error() {} 40 41 void SetMessage(const std::string &message) { 42 std::runtime_error &base = *this; 43 base = std::runtime_error(message); 44 } 45 46 void init(fmt::CStringRef format_str, fmt::ArgList args) { 47 SetMessage(fmt::format(format_str, args)); 48 } 49 50 public: 51 FMT_VARIADIC_(char, , Error, init, fmt::CStringRef) 52 ~Error() throw() {} 53 }; 54 55 // The operation is not supported by the object. 56 class UnsupportedError : public Error { 57 public: 58 FMT_VARIADIC_(char, , UnsupportedError, init, fmt::CStringRef) 59 }; 60 61 // Makes UnsupportedError with prefix "unsupported: ". 62 inline UnsupportedError MakeUnsupportedError( 63 fmt::CStringRef format_str, fmt::ArgList args) { 64 return UnsupportedError("unsupported: {}", fmt::format(format_str, args)); 65 } 66 FMT_VARIADIC(UnsupportedError, MakeUnsupportedError, fmt::CStringRef) 67 } // namespace mp 68 69 #endif // MP_ERROR_H_ 70