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-2015 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://image.diku.dk/shark/>
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 namespace shark {
39 
40  /**
41  * \brief Top-level exception class of the shark library.
42  */
43  class Exception : public std::exception {
44  public:
45  /**
46  * \brief Default c'tor.
47  * \param [in] what String that describes the exception.
48  * \param [in] file Filename the function that has thrown the exception resides in.
49  * \param [in] line Line of file that has thrown the exception.
50  */
51  Exception( const std::string & what = std::string(), const std::string & file = std::string(), unsigned int line = 0 ) : m_what( what ),
52  m_file( file ),
53  m_line( line ) {
54  }
55 
56  /**
57  * \brief Default d'tor.
58  */
59  ~Exception( ) throw() {}
60 
61  /**
62  * \brief Accesses the description of the exception.
63  */
64  inline const char* what() const throw() {
65  return m_what.c_str();
66  }
67 
68  /**
69  * \brief Accesses the name of the file the exception occurred in.
70  */
71  inline const std::string & file() const {
72  return( m_file );
73  }
74 
75  /**
76  * \brief Accesses the line of the file the exception occured in.
77  */
78  inline unsigned int line() const {
79  return( m_line );
80  }
81 
82  protected:
83  std::string m_what; ///< Description of the exception.
84  std::string m_file; ///< File name the exception occurred in.
85  unsigned int m_line; ///< Line of file the exception occurred in.
86  };
87 
88 }
89 
90 /**
91 * \brief Convenience macro that creates an instance of class shark::exception,
92 * injecting file and line information automatically.
93 */
94 #define SHARKEXCEPTION(message) shark::Exception(message, __FILE__, __LINE__)
95 
96 /// Break the execution and throw exception with @a message in case of predefined @a unexpectedCondition is true
97 /// @note This should not be replaced by SHARK_CHECK as we need always evaluate @a unexpectedCondition
98 inline void THROW_IF(bool unexpectedCondition, const std::string& message)
99 {
100  if (unexpectedCondition)
101  throw SHARKEXCEPTION(message);
102 }
103 
104 // some handy macros for special types of checks,
105 // throwing standard error messages
106 #ifndef NDEBUG
107 #define RANGE_CHECK(cond) do { if (!(cond)) throw SHARKEXCEPTION("range check error: "#cond); } while (false)
108 #define SIZE_CHECK(cond) do { if (!(cond)) throw SHARKEXCEPTION("size mismatch: "#cond); } while (false)
109 #define TYPE_CHECK(cond) do { if (!(cond)) throw SHARKEXCEPTION("type mismatch: "#cond); } while (false)
110 #define IO_CHECK(cond) do { if (!(cond)) throw SHARKEXCEPTION("I/O error "); } while (false)
111 #define SHARK_ASSERT(cond) do { if (!(cond)) throw SHARKEXCEPTION("assertion failed: "#cond); } while (false)
112 #define SHARK_CHECK(cond, error) do { if (!(cond)) throw SHARKEXCEPTION(error); } while (false)
113 #else
114 #define RANGE_CHECK(cond) do { (void)sizeof(cond); } while (false)
115 #define SIZE_CHECK(cond) do { (void)sizeof(cond); } while (false)
116 #define TYPE_CHECK(cond) do { (void)sizeof(cond); } while (false)
117 #define IO_CHECK(cond) do { (void)sizeof(cond); } while (false)
118 #define SHARK_ASSERT(cond) do { (void)sizeof(cond); } while (false)
119 #define SHARK_CHECK(cond, error) do { (void)sizeof(cond); (void)sizeof(error);} while (false)
120 #endif
121 
122 #endif // SHARK_CORE_EXCEPTION_H