Code_TYMPAN  4.4.0
Industrial site acoustic simulation
exceptions.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) 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  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
25 #ifndef TY_EXCEPTIONS
26 #define TY_EXCEPTIONS
27 
28 #include <string>
29 #include <sstream>
30 #include <cassert>
31 #include <exception>
32 #include <deque>
33 
34 #include <boost/exception/all.hpp>
35 #include <boost/current_function.hpp>
36 #include <boost/throw_exception.hpp>
37 #include <boost/config.hpp>
38 
39 // Beware : Stuff::operator<< must be defined in the same namespace as Stuff
40 
41 // For GTest related pretty-printing please see :
42 // http://code.google.com/p/googletest/wiki/AdvancedGuide#Teaching_Google_Test_How_to_Print_Your_Values
43 
44 // For boost::exception diagnostic please see:
45 // http://www.boost.org/doc/libs/1_54_0/libs/exception/doc/diagnostic_information.html
46 
47 template <typename T> std::string tympan_to_string(const T& o)
48 {
49  using namespace std;
50  stringstream ss;
51  ss << o;
52  return ss.str();
53 }
54 
55 namespace tympan
56 {
57 
58 // We use the boost::exception to add information to exception classes.
59 // Cf. http://www.boost.org/doc/libs/1_54_0/libs/exception/doc/tutorial_transporting_data.html
60 
62 struct exception : /* virtual std::exception,*/ virtual boost::exception
63 {
64 };
65 
67 struct logic_error : /*virtual*/ std::logic_error, virtual tympan::exception
68 {
69  logic_error() : std::logic_error("Code_TYMPAN internal logic error"){};
70  logic_error(const std::string& desc) : std::logic_error(desc){};
71 };
72 
74 struct invalid_data : /*virtual*/ std::runtime_error, virtual tympan::exception
75 {
76  invalid_data() : std::runtime_error("Code_TYMPAN invalid data encountered"){};
77  invalid_data(const std::string& desc) : std::runtime_error(desc){};
78 };
79 
80 /* NB : the commented-out virtual inheritance in logic_error and invalid_data is
81  * important : otherwise the classe derived from those would need to call the
82  * std::logic_error or std::runtime_error constructor.
83  * The point is std::logic_error and std::runtime_error inherits *non*-virtualy
84  * from std::exception, so that the inheritance of tympan::exception from
85  * has std::exception to be removed even if semantically it would be relevant.
86  */
87 
88 } // namespace tympan
89 
91 #define tympan_source_loc \
92  ::boost::throw_function(BOOST_CURRENT_FUNCTION) \
93  << ::boost::throw_file(__FILE__) << ::boost::throw_line((int)__LINE__)
94 
95 #endif // TY_EXCEPTIONS
std::stringstream ss
Definition: Logger.cpp:21
std::string tympan_to_string(const T &o)
Definition: exceptions.h:47
The base exception class for all exceptions specific to Code_TYMPAN.
Definition: exceptions.h:63
The base exception class for errors due to invalid data.
Definition: exceptions.h:75
invalid_data(const std::string &desc)
Definition: exceptions.h:77
The base exception class for internal logic / algorithmic errors.
Definition: exceptions.h:68
logic_error(const std::string &desc)
Definition: exceptions.h:70