E-MailRelay
gexception.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 Graeme Walker <graeme_walker@users.sourceforge.net>
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 3 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, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gexception.h
19///
20
21#ifndef G_EXCEPTION_H
22#define G_EXCEPTION_H
23
24#include "gdef.h"
25#include "gstringview.h"
26#include "ggettext.h"
27#include <initializer_list>
28#include <string>
29#include <iostream>
30#include <stdexcept>
31
32namespace G
33{
34 class Exception ;
35}
36
37//| \class G::Exception
38/// A general-purpose exception class derived from std::exception and containing
39/// an error message. Provides constructors that simplify the assembly of multi-part
40/// error messages.
41///
42/// Usage:
43/// \code
44/// throw G::Exception( "initialisation error" , "no such file" , path ) ;
45/// \endcode
46///
47/// The G_EXCEPTION macro is normally used in class declarations, as follows:
48/// \code
49/// class Foo
50/// {
51/// public:
52/// G_EXCEPTION( Error , tx("foo error") ) ;
53/// ...
54/// };
55/// \endcode
56///
57/// The tx() identifies the string at build-time as requiring translation.
58/// The G_EXCEPTION macro adds a "G::" scope so that at compile-time the
59/// do-nothing function G::tx() from "ggettext.h" is used. The xgettext
60/// utility should be run as "xgettext --c++ --keyword=tx". Note that
61/// xgettext keywords cannot use scoped names.
62///
63class G::Exception : public std::runtime_error
64{
65public:
66 Exception( std::initializer_list<std::string_view> ) ;
67 ///< Constructor.
68
69 explicit Exception( std::string_view what ) ;
70 ///< Constructor.
71
72 Exception( std::string_view what , std::string_view more ) ;
73 ///< Constructor.
74
75 Exception( std::string_view what , std::string_view more1 , std::string_view more2 ) ;
76 ///< Constructor.
77
78 Exception( std::string_view what , std::string_view more1 , std::string_view more2 , std::string_view more3 ) ;
79 ///< Constructor.
80
81 Exception( std::string_view what , std::string_view more1 , std::string_view more2 , std::string_view more3 , std::string_view more4 ) ;
82 ///< Constructor.
83
84private:
85 static std::string join( std::initializer_list<std::string_view> ) ;
86} ;
87
88#define G_EXCEPTION_CLASS_( class_name , description ) \
89 class class_name : public G::Exception { public: /* NOLINT bugprone-macro-parentheses */ \
90 class_name() : G::Exception(description) {} \
91 explicit class_name( std::string_view more ) : G::Exception(description,more) {} \
92 class_name( std::string_view more1 , std::string_view more2 ) : G::Exception(description,more1,more2) {} \
93 class_name( std::string_view more1 , std::string_view more2 , std::string_view more3 ) : G::Exception(description,more1,more2,more3) {} } ;
94
95#define G_EXCEPTION_FUNCTION_( name , description ) \
96 inline static G::Exception name() { return G::Exception((description)) ; } \
97 inline static G::Exception name( std::string_view s ) { return G::Exception(description,s) ; } \
98 inline static G::Exception name( std::string_view s1 , std::string_view s2 ) { return G::Exception(description,s1,s2) ; } \
99 inline static G::Exception name( std::string_view s1 , std::string_view s2 , std::string_view s3 ) { return G::Exception(description,s1,s2,s3) ; } \
100 inline static G::Exception name( std::string_view s1 , std::string_view s2 , std::string_view s3 , std::string_view s4 ) { return G::Exception(description,s1,s2,s3,s4) ; }
101
102#define G_EXCEPTION_CLASS( class_name , tx_description ) G_EXCEPTION_CLASS_( class_name , G::tx_description )
103#define G_EXCEPTION_FUNCTION( name , tx_description ) G_EXCEPTION_FUNCTION_( name , G::tx_description )
104#define G_EXCEPTION( class_name , tx_description ) G_EXCEPTION_FUNCTION_( class_name , G::tx_description )
105
106#endif
A general-purpose exception class derived from std::exception and containing an error message.
Definition: gexception.h:64
Exception(std::initializer_list< std::string_view >)
Constructor.
Definition: gexception.cpp:25
Low-level classes.
Definition: garg.h:36