stator
A math, geometry, and utility library
exception.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 Marcus N Campbell Bannerman <[email protected]>
3 
4  This file is part of stator.
5 
6  stator is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  stator is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with stator. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 // C++
23 #include <exception>
24 #include <sstream>
25 
26 #define stator_throw() \
27  throw stator::Exception(__LINE__,__FILE__, __func__)
28 
29 namespace stator {
66  class Exception : public std::exception {
67  public:
74  Exception(int line, const char* file, const char* funcname) throw() :
75  message_(), messageStr_() {
76 
77  message_ << "Exception thrown in [" << funcname << "] (" << file <<
78  ":" << line << ")" << std::endl;
79  }
80 
86  Exception(const Exception& e) : message_(e.message_.str()), messageStr_() {
87  }
88 
89  inline ~Exception() throw() {}
90 
93  template<class T>
94  Exception& operator<<(const T& m) {
95  message_ << m;
96  return *this;
97  }
98 
109  const char* what() const throw() {
110  messageStr_ = message_.str();
111  return messageStr_.c_str();
112  }
113 
114  private:
116  std::ostringstream message_;
118  mutable std::string messageStr_;
119  };
120 } // namespace stator
Exception & operator<<(const T &m)
The stream operator engine for the class.
Definition: exception.hpp:94
An exception class that also appears to function like an ostream object.
Definition: exception.hpp:66
detail::C_wrap< stator::constant_ratio::e >::type e
A symbolic/compile-time rational approximation of .
Definition: constants.hpp:59
Exception(int line, const char *file, const char *funcname)
Constructor called by the stator_throw() macro.
Definition: exception.hpp:74
const char * what() const
Get the stored message from the class.
Definition: exception.hpp:109
Exception(const Exception &e)
Copy constructor.
Definition: exception.hpp:86
The stator library namespace.
Definition: frontpage.dox:243