Exception.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Exception
5  *
6  *
7  *
8  * \author T.Voss
9  * \date 2010-2011
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_CORE_EXCEPTION_H
33 #define SHARK_CORE_EXCEPTION_H
34 
35 #include <string>
36 #include <exception>
37 
38 
39 
40 namespace shark {
41 
42  /**
43  * \brief Top-level exception class of the shark library.
44  */
45  class Exception : public std::exception {
46  public:
47  /**
48  * \brief Default c'tor.
49  * \param [in] what String that describes the exception.
50  * \param [in] file Filename the function that has thrown the exception resides in.
51  * \param [in] line Line of file that has thrown the exception.
52  * \param [in] func Name of the Function the error appeared in.
53  */
55  const std::string & what = "unknown reason",
56  const std::string & file = "unknown",
57  unsigned int line = 0,
58  const std::string & func = "function"
59  ): m_what( what )
60  , m_file( file )
61  , m_line( line )
62  , m_func( func )
63  {
64  m_message="["+m_file+"::"+m_func+","+std::to_string(line)+"] " + what;
65  }
66 
67  /**
68  * \brief Default d'tor.
69  */
70  ~Exception( ) throw() {}
71 
72  /**
73  * \brief Accesses the description of the exception.
74  */
75  inline const char* what() const throw() {
76  return m_message.c_str();
77  }
78 
79  /**
80  * \brief Accesses the name of the file the exception occurred in.
81  */
82  inline const std::string & file() const {
83  return m_file;
84  }
85 
86  /**
87  * \brief Accesses the line of the file the exception occured in.
88  */
89  inline unsigned int line() const {
90  return m_line;
91  }
92 
93  protected:
94  std::string m_what; ///< Description of the exception.
95  std::string m_file; ///< File name the exception occurred in.
96  unsigned int m_line; ///< Line of file the exception occurred in.
97  std::string m_func; ///< Function name the exception occured in
98  std::string m_message; ///< complete error message
99 
100  };
101 
102 }
103 
104 //MSVC does not have the __func__ so __FUNCTION__ has to be used instead.
105 #ifdef _MSC_VER
106 #define SHARKEXCEPTION(message) shark::Exception(message, __FILE__, __LINE__, __FUNCTION__)
107 #else
108 #define SHARKEXCEPTION(message) shark::Exception(message, __FILE__, __LINE__, __func__)
109 #endif
110 // some handy macros for special types of checks,
111 // throwing standard error messages
112 #ifndef NDEBUG
113 #define RANGE_CHECK(cond) assert(cond)
114 #define SIZE_CHECK(cond) assert(cond)
115 #define SHARK_ASSERT(cond) assert(cond)
116 #else
117 #define RANGE_CHECK(cond) do { (void)sizeof(cond); } while (false)
118 #define SIZE_CHECK(cond) do { (void)sizeof(cond); } while (false)
119 #define SHARK_ASSERT(cond) do { (void)sizeof(cond); } while (false)
120 #endif
121 #define SHARK_RUNTIME_CHECK(cond, message) do { if (!(cond)) throw SHARKEXCEPTION(message);} while (false)
122 
123 #endif // SHARK_CORE_EXCEPTION_H