1 /*
2 
3 Copyright (c) 2002-2008, Yauheni Akhotnikau
4 Copyright (c) 2008-2016, The SObjectizer Project
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 - Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 
13 - Redistributions in binary form must reproduce the above copyright notice, this
14 list of conditions and the following disclaimer in the documentation and/or
15 other materials provided with the distribution.
16 
17 - The name of the author may not be used to endorse or promote products derived
18 from this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
23 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
25 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29 OF SUCH DAMAGE.
30 
31 */
32 
33 /*!
34 	\file
35 	\brief Classes for representing exceptions.
36 */
37 
38 #if !defined( OESS_2_DEFS_EX_HPP )
39 #define OESS_2_DEFS_EX_HPP
40 
41 #include <oess_2/defs/h/declspec.hpp>
42 
43 #include <sstream>
44 #include <exception>
45 
46 #include <oess_2/defs/h/err_code.hpp>
47 
48 namespace oess_2 {
49 
50 #if defined( _MSC_VER )
51 	#pragma warning(push)
52 	#pragma warning(disable: 4251)
53 	#pragma warning(disable: 4275)
54 #endif
55 
56 //
57 // ex_t
58 //
59 
60 /*!
61 	\brief A base class for all exceptions.
62 */
63 class	OESS_2_DEFS_TYPE	ex_t : public std::exception {
64 	public :
65 		ex_t( const ex_t & ) = default;
66 		ex_t( ex_t && ) = default;
67 		ex_t & operator=( const ex_t & ) = default;
68 		ex_t & operator=( ex_t && ) = default;
69 
70 		ex_t( err_code_t err );
71 		virtual ~ex_t();
72 
73 		const err_code_t &
74 		query_err_code() const;
75 
76 		virtual const char *
77 		what() const noexcept override;
78 
79 	private :
80 		err_code_t	m_err;
81 
82 		/*! Textual description for what(). */
83 		std::string	m_text_representation;
84 
85 		/*! Creation of textual description for what(). */
86 		void
87 		make_text_representation();
88 };
89 
90 //
91 // logic_ex_t
92 //
93 
94 /*!
95 	\brief A base class for logical errors.
96 */
97 class	OESS_2_DEFS_TYPE	logic_ex_t : public ex_t {
98 	public :
99 		logic_ex_t( err_code_t err );
100 };
101 
102 //
103 // physic_ex_t
104 //
105 
106 /*!
107 	\brief A base class for physic (often fatal) errors.
108 */
109 class	OESS_2_DEFS_TYPE	physic_ex_t : public ex_t {
110 	public :
111 		physic_ex_t( err_code_t err );
112 };
113 
114 #if defined( _MSC_VER )
115 	#pragma warning(pop)
116 #endif
117 
118 //
119 // Helper macros.
120 //
121 
122 /*!
123 	Generate logic exception.
124 
125 	\param	c	Numeric error code for exception.
126 	\param	m	Description of an error.
127 */
128 #define OESS_THROW_LOGIC( c, m )\
129 { \
130 	std::ostringstream msg; \
131 	msg << m; \
132 	throw oess_2::logic_ex_t( oess_2_make_err( c, msg.str() ) ); \
133 }
134 
135 /*!
136 	Generate physic exception.
137 
138 	\param	c	Numeric error code for exception.
139 	\param	m	Description of an error.
140 */
141 #define OESS_THROW_PHYSIC( c, m )\
142 { \
143 	std::ostringstream msg; \
144 	msg << m; \
145 	throw oess_2::physic_ex_t( oess_2_make_err( c, msg.str() ) ); \
146 }
147 
148 } /* namespace oess_2 */
149 
150 #endif
151