1 /* 2 * Copyright (C) 2002-2020 by the Widelands Development Team 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 * 18 */ 19 20 #ifndef WL_BASE_WEXCEPTION_H 21 #define WL_BASE_WEXCEPTION_H 22 23 #include <exception> 24 #include <string> 25 26 #include "base/macros.h" 27 28 #ifndef PRINTF_FORMAT 29 #ifdef __GNUC__ 30 #define PRINTF_FORMAT(b, c) __attribute__((__format__(__printf__, b, c))) 31 #else 32 #define PRINTF_FORMAT(b, c) 33 #endif 34 #endif 35 36 /** Stupid, simple exception class. 37 * 38 * It has the nice bonus that you can give it sprintf()-style format strings. 39 */ 40 class WException : public std::exception { 41 public: 42 explicit WException(char const* const file, uint32_t const line, char const* const fmt, ...) 43 PRINTF_FORMAT(4, 5); 44 45 /** 46 * The target of the returned pointer remains valid during the lifetime of 47 * the WException object. 48 */ 49 const char* what() const noexcept override; 50 51 protected: WException()52 WException() { 53 } 54 std::string what_; 55 }; 56 57 #define wexception(...) WException(__FILE__, __LINE__, __VA_ARGS__) 58 59 // Throws a wexception for unreachable code. 60 #define NEVER_HERE() throw WException(__FILE__, __LINE__, "Unreachable code was reached.") 61 62 #endif // end of include guard: WL_BASE_WEXCEPTION_H 63