SOPT
Sparse OPTimisation
exception.h
Go to the documentation of this file.
1 #ifndef SOPT_EXCEPTION
2 #define SOPT_EXCEPTION
3 
4 #include "sopt/config.h"
5 #include <exception>
6 #include <sstream>
7 #include <string>
8 
9 namespace sopt {
11 class Exception : public std::exception {
12  protected:
14  Exception(std::string const &name, std::string const &filename, size_t lineno)
15  : std::exception(), message(header(name, filename, lineno)) {}
16 
17  public:
19  Exception(std::string const &filename, size_t lineno)
20  : Exception("sopt::Exception", filename, lineno) {}
21 
23  const char *what() const noexcept override { return message.c_str(); }
24 
26  static std::string header(std::string const &name, std::string const &filename, size_t lineno) {
27  std::ostringstream header;
28  header << name << " at " << filename << ":" << lineno;
29  return header.str();
30  }
31 
33  template <typename OBJECT>
34  Exception &operator<<(OBJECT const &object) {
35  std::ostringstream msg;
36  msg << message << object;
37  message = msg.str();
38  return *this;
39  }
40 
41  private:
43  std::string message;
44 };
45 
46 #define SOPT_THROW(MSG) throw(sopt::Exception(__FILE__, __LINE__) << "\n" << MSG)
47 
48 } // namespace sopt
49 #endif
Root exception for sopt.
Definition: exception.h:11
static std::string header(std::string const &name, std::string const &filename, size_t lineno)
Header of the message.
Definition: exception.h:26
Exception(std::string const &filename, size_t lineno)
Creates exception.
Definition: exception.h:19
const char * what() const noexcept override
Creates message.
Definition: exception.h:23
Exception & operator<<(OBJECT const &object)
Adds to message.
Definition: exception.h:34