1 //
2 // Copyright (C) 2001-2013 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 <string>
26 #include <iostream>
27 
28 /// \namespace G
29 namespace G
30 {
31 	class Exception ;
32 }
33 
34 /// \class G::Exception
35 /// A general-purpose exception class derived from std::exception
36 /// and containing a std::string. Provides constructors that simplify the
37 /// assembly of multi-part error messages.
38 ///
39 /// Usage:
40 /// \code
41 ///	throw G::Exception( "initialisation error" , "no such file" , path ) ;
42 /// \endcode
43 ///
44 class G::Exception : public std::exception
45 {
46 protected:
47 	std::string m_what ;
48 
49 public:
50 	Exception() ;
51 		///< Default constructor.
52 
53 	explicit Exception( const char * what ) ;
54 		///< Constructor.
55 
56 	explicit Exception( const std::string & what ) ;
57 		///< Constructor.
58 
59 	Exception( const char * what , const std::string & more ) ;
60 		///< Constructor.
61 
62 	Exception( const std::string & what , const std::string & more ) ;
63 		///< Constructor.
64 
65 	Exception( const std::string & what , const std::string & more1 , const std::string & more2 ) ;
66 		///< Constructor.
67 
68 	virtual ~Exception() throw() ;
69 		///< Destructor.
70 
71 	virtual const char * what() const throw() ;
72 		///< Override from std::exception.
73 
74 	void prepend( const char * context ) ;
75 		///< Prepends context to the what string.
76 		///< Inserts a separator as needed.
77 
78 	void append( const char * more ) ;
79 		///< Appends 'more' to the what string.
80 		///< Inserts a separator as needed.
81 
82 	void append( const std::string & more ) ;
83 		///< Appends 'more' to the what string.
84 		///< Inserts a separator as needed.
85 } ;
86 
87 #define G_EXCEPTION_CLASS( class_name , description ) class class_name : public G::Exception { public: class_name() : G::Exception(description) {} explicit class_name(const char *more) : G::Exception(description,more) {} explicit class_name(const std::string &more) : G::Exception(description,more) {} class_name(const std::string &more1,const std::string &more2) : G::Exception(description,more1,more2) {} }
88 
89 /// define as a function rather than a type if optimising for size
90 #ifdef USE_SMALL_EXCEPTIONS
91  #define G_EXCEPTION( fn_name , description ) static inline int fn_name() { throw G::Exception(description) ; return 0 ; } static inline int fn_name( const std::string & more ) { throw G::Exception(description,more) ; return 0 ; }
92 #else
93  #define G_EXCEPTION( class_name , description ) G_EXCEPTION_CLASS( class_name , description )
94 #endif
95 
96 #endif
97