File: exceptions.h

package info (click to toggle)
exult 1.12.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,608 kB
  • sloc: cpp: 169,917; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 539; objc: 416
file content (158 lines) | stat: -rw-r--r-- 4,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 *  Copyright (C) 2000-2022  The Exult Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef EXCEPTIONS_H
#define EXCEPTIONS_H

#include <cerrno>
#include <stdexcept>
#include <string>
#include <utility>

/*
 *  Base class of all our exceptions, providing a storage for the error message
 */

class exult_exception : public std::exception {
	std::string what_;
	int         errno_;
	const char* sourcefile_;
	int         line_;

public:
	explicit exult_exception(
			std::string what_arg, const char* sourcefile = nullptr,
			int line = 0)
			: what_(std::move(what_arg)), errno_(errno),
			  sourcefile_(sourcefile), line_(line) {}

	const char* what() const noexcept override {
		return what_.c_str();
	}

	const char* sourcefile() const {
		return sourcefile_;
	}

	int line() const {
		return line_;
	}

	int get_errno() const {
		return errno_;
	}
};

/*
 *  A quit exception can be thrown to quit the program
 */

class quit_exception : public exult_exception {
	int result_;

public:
	explicit quit_exception(int result = 0)
			: exult_exception("Quit"), result_(result) {}

	int get_result() const {
		return result_;
	}
};

// Per analogiam to boost::noncopyable
class nonreplicatable {
protected:
	constexpr nonreplicatable()                        = default;
	nonreplicatable(const nonreplicatable&)            = delete;
	nonreplicatable(nonreplicatable&&)                 = delete;
	nonreplicatable& operator=(const nonreplicatable&) = delete;
	nonreplicatable& operator=(nonreplicatable&&)      = delete;
	~nonreplicatable()                                 = default;
};

/*
 *  File errors
 */

class file_exception : public exult_exception {
public:
	explicit file_exception(
			std::string what_arg, const char* sourcefile, int line)
			: exult_exception(std::move(what_arg), sourcefile, line) {}
};

class file_open_exception : public file_exception {
	static const std::string prefix_;

public:
	explicit file_open_exception(
			const std::string& file, const char* sourcefile, int line)
			: file_exception("Error opening file " + file, sourcefile, line) {}
};

class file_write_exception : public file_exception {
	static const std::string prefix_;

public:
	explicit file_write_exception(
			const std::string& file, const char* sourcefile, int line)
			: file_exception(
					  "Error writing to file " + file, sourcefile, line) {}
};

class file_read_exception : public file_exception {
	static const std::string prefix_;

public:
	explicit file_read_exception(
			const std::string& file, const char* sourcefile, int line)
			: file_exception(
					  "Error reading from file " + file, sourcefile, line) {}
};

class wrong_file_type_exception : public file_exception {
public:
	explicit wrong_file_type_exception(
			const std::string& file, const std::string& type,
			const char* sourcefile, int line)
			: file_exception(
					  "File " + file + " is not of type " + type, sourcefile,
					  line) {}
};

//
// Macros to automatically set Sourcefile and line arguments
//
#define file_exception(what_arg)  file_exception(what_arg, __FILE__, __LINE__)
#define file_open_exception(file) file_open_exception(file, __FILE__, __LINE__)
#define file_write_exception(file) \
	file_write_exception(file, __FILE__, __LINE__)

#define file_read_exception(file) file_read_exception(file, __FILE__, __LINE__)

#define wrong_file_type_exception(file, type) \
	wrong_file_type_exception(file, type, __FILE__, __LINE__)

/*
 *  Exception that gets fired when the user aborts something
 */
class UserBreakException {};

class UserSkipException : public UserBreakException {};

#endif