1 /* 2 * Copyright (C) 2000-2013 The Exult Team 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 */ 18 19 #ifndef EXCEPTIONS_H 20 #define EXCEPTIONS_H 21 22 #include <cerrno> 23 #include <stdexcept> 24 #include <string> 25 #include <utility> 26 27 /* 28 * Base class of all our exceptions, providing a storage for the error message 29 */ 30 31 class exult_exception : public std::exception { 32 std::string what_; 33 int errno_; 34 public: exult_exception(std::string what_arg)35 explicit exult_exception(std::string what_arg): what_(std::move(what_arg)), errno_(errno) { } what()36 const char *what() const noexcept override { 37 return what_.c_str(); 38 } get_errno()39 int get_errno() const { 40 return errno_; 41 } 42 }; 43 44 45 /* 46 * A quit exception can be thrown to quit the program 47 */ 48 49 class quit_exception : public exult_exception { 50 int result_; 51 public: 52 explicit quit_exception(int result = 0): exult_exception("Quit"), result_(result) { } get_result()53 int get_result() const { 54 return result_; 55 } 56 }; 57 58 //Per analogiam to boost::noncopyable 59 class nonreplicatable { 60 protected: 61 constexpr nonreplicatable() = default; 62 nonreplicatable(const nonreplicatable&) = delete; 63 nonreplicatable(nonreplicatable&&) = delete; 64 nonreplicatable& operator=(const nonreplicatable&) = delete; 65 nonreplicatable& operator=(nonreplicatable&&) = delete; 66 ~nonreplicatable() = default; 67 }; 68 69 70 /* 71 * File errors 72 */ 73 74 class file_exception : public exult_exception { 75 public: file_exception(std::string what_arg)76 explicit file_exception(std::string what_arg): exult_exception(std::move(what_arg)) { } 77 }; 78 79 class file_open_exception : public file_exception { 80 static const std::string prefix_; 81 public: file_open_exception(const std::string & file)82 explicit file_open_exception(const std::string &file): file_exception("Error opening file " + file) { } 83 }; 84 85 class file_write_exception : public file_exception { 86 static const std::string prefix_; 87 public: file_write_exception(const std::string & file)88 explicit file_write_exception(const std::string &file): file_exception("Error writing to file " + file) { } 89 }; 90 91 class file_read_exception : public file_exception { 92 static const std::string prefix_; 93 public: file_read_exception(const std::string & file)94 explicit file_read_exception(const std::string &file): file_exception("Error reading from file " + file) { } 95 }; 96 97 class wrong_file_type_exception : public file_exception { 98 public: wrong_file_type_exception(const std::string & file,const std::string & type)99 explicit wrong_file_type_exception(const std::string &file, const std::string &type): file_exception("File " + file + " is not of type " + type) { } 100 }; 101 102 103 /* 104 * Exception that gets fired when the user aborts something 105 */ 106 class UserBreakException { 107 }; 108 109 #endif 110