1 # generate-exceptions: generate C++ files for Xapian's exception hierarchy.
3 # Copyright (C) 2003,2004,2006,2007,2008,2009,2011,2012,2013,2014,2015,2017 Olly Betts
4 # Copyright (C) 2007 Richard Boulton
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 use exception_data qw(
22 $copyright $generated_warning @baseclasses @classes %classcode
25 open HDR, ">include/xapian/error.h" or die $!;
26 open DISPATCH, ">include/xapian/errordispatch.h" or die $!;
30 * @brief Hierarchy of classes which Xapian can throw as exceptions.
34 print HDR $generated_warning;
35 print DISPATCH $generated_warning;
38 print DISPATCH $copyright;
42 #ifndef XAPIAN_INCLUDED_ERROR_H
43 #define XAPIAN_INCLUDED_ERROR_H
45 #if !defined XAPIAN_IN_XAPIAN_H && !defined XAPIAN_LIB_BUILD
46 # error "Never use <xapian/error.h> directly; include <xapian.h> instead."
50 #include <xapian/attributes.h>
51 #include <xapian/visibility.h>
55 /** All exceptions thrown by Xapian are subclasses of Xapian::Error.
57 * This class can not be instantiated directly - instead a subclass should
60 class XAPIAN_VISIBILITY_DEFAULT Error {
61 /// Message giving details of the error, intended for human consumption.
64 /** Optional context information.
66 * This context is intended for machine use (for example to know which
67 * remote server an error came from), but it is typically a
68 * plain-text string, and so also fit for human consumption.
72 /** The error string derived from my_errno.
74 * This string is generated from my_errno lazily.
76 mutable std::string error_string;
78 /// The type of this error (e.g. DocNotFoundError.)
81 /** Optional value of 'errno' associated with this error.
83 * If no value is associated, this member variable will be 0.
85 * On UNIX, if this value is < 0, it's an error code returned from
86 * getaddrinfo() (negated if such error codes are positive).
88 * On Windows, if this value is < 0, it's a negated Windows error code
89 * (as given by GetLastError()), while if it is >= WSABASEERR then it is a
90 * WinSock error code (as given by WSAGetLastError()). Prior to Xapian
91 * 1.2.20 and 1.3.3, WSAGetLastError() codes were also negated.
93 * NB We don't just call this member "errno" to avoid problems on
94 * platforms where errno is a preprocessor macro.
98 /// Don't allow assignment of the base class.
99 void operator=(const Error &o);
102 /** @private @internal
103 * @brief Constructor for use by constructors of derived classes.
105 Error(const std::string &msg_, const std::string &context_,
106 const char * type_, const char * error_string_);
108 /** @private @internal
109 * @brief Constructor for use by constructors of derived classes.
111 Error(const std::string &msg_, const std::string &context_,
112 const char * type_, int errno_)
113 : msg(msg_), context(context_), error_string(), type(type_),
117 /// The type of this error (e.g. "DocNotFoundError".)
118 const char * XAPIAN_NOTHROW(get_type() const) {
122 /// Message giving details of the error, intended for human consumption.
123 const std::string & XAPIAN_NOTHROW(get_msg() const) {
127 /** Optional context information.
129 * This context is intended for machine use (for example to know which
130 * remote server an error came from), but it is typically a
131 * plain-text string, and so also fit for human consumption.
133 const std::string & XAPIAN_NOTHROW(get_context() const) {
137 /** Returns any system error string associated with this exception.
139 * The system error string may come from errno, h_errno (on UNIX), or
140 * GetLastError() (on MS Windows). If there is no associated system
141 * error string, NULL is returned.
143 const char * get_error_string() const;
145 /// Return a string describing this object.
146 std::string get_description() const;
150 print DISPATCH <<'EOF';
152 /* Note that this file isn't an external header - it's located in
153 * include/xapian in the source tree because it's generated so this
154 * is the simplest way to make inclusion work in a VPATH build.
157 // DOXYGEN gets confused by this header-with-code.
163 my ($class, $parent, $comment) = split /\t/, $_, 3;
167 class XAPIAN_VISIBILITY_DEFAULT $class : public $parent {
169 /** \@private \@internal
170 * \@brief Constructor for use by constructors of derived classes.
172 $class(const std::string \&msg_, const std::string \&context_, const char * type_, const char * error_string_)
173 : $parent(msg_, context_, type_, error_string_) {}
175 /** \@private \@internal
176 * \@brief Constructor for use by constructors of derived classes.
178 $class(const std::string \&msg_, const std::string \&context_, const char * type_, int errno_)
179 : $parent(msg_, context_, type_, errno_) {}
186 my ($class, $parent, $comment) = split /\t/, $_, 3;
187 my $code = sprintf('\%03o', $classcode{$class});
189 print DISPATCH "case '$code': throw Xapian::$class(msg, context, error_string);\n";
194 class XAPIAN_VISIBILITY_DEFAULT $class : public $parent {
196 /** \@private \@internal
197 * \@brief Private constructor for use by remote backend.
199 * \@param error_string_ Optional string describing error. May be NULL.
201 $class(const std::string \&msg_, const std::string \&context_, const char * error_string_)
202 : $parent(msg_, context_, "$code$class", error_string_) {}
203 /** General purpose constructor.
205 * \@param msg_ Message giving details of the error, intended
206 * for human consumption.
207 * \@param context_ Optional context information for this error.
208 * \@param errno_ Optional errno value associated with this error.
210 explicit $class(const std::string \&msg_, const std::string \&context_ = std::string(), int errno_ = 0)
211 : $parent(msg_, context_, "$code$class", errno_) {}
212 /** Construct from message and errno value.
214 * \@param msg_ Message giving details of the error, intended
215 * for human consumption.
216 * \@param errno_ Optional errno value associated with this error.
218 $class(const std::string \&msg_, int errno_)
219 : $parent(msg_, std::string(), "$code$class", errno_) {}
221 /** \@private \@internal
222 * \@brief Constructor for use by constructors of derived classes.
224 $class(const std::string \&msg_, const std::string \&context_, const char * type_, const char * error_string_)
225 : $parent(msg_, context_, type_, error_string_) {}
227 /** \@private \@internal
228 * \@brief Constructor for use by constructors of derived classes.
230 $class(const std::string \&msg_, const std::string \&context_, const char * type_, int errno_)
231 : $parent(msg_, context_, type_, errno_) {}
240 #endif /* XAPIAN_INCLUDED_ERROR_H */
243 print DISPATCH <<'EOF';
248 close DISPATCH or die $!;
250 # vim: set syntax=perl: