-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mayeul d'Avezac
committed
Aug 12, 2015
1 parent
cd980ae
commit 5c91622
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef SOPT_EXCEPTION | ||
#define SOPT_EXCEPTION | ||
|
||
#include <exception> | ||
#include <sstream> | ||
#include <string> | ||
|
||
namespace sopt { | ||
//! Root exception for sopt | ||
class Exception : public std::exception { | ||
|
||
protected: | ||
//! Constructor for derived classes | ||
Exception(std::string const & name, std::string const &filename, size_t lineno) | ||
: std::exception(), message(header(name, filename, lineno)) {} | ||
|
||
public: | ||
//! Creates exception | ||
Exception(std::string const &filename, size_t lineno) | ||
: Exception("sopt::Exception", filename, lineno) {} | ||
|
||
//! Creates message | ||
const char* what() const noexcept override { return message.c_str(); } | ||
|
||
//! Header of the message | ||
static std::string header( | ||
std::string const &name, std::string const &filename, size_t lineno) { | ||
return (std::ostringstream() << name << " at " << filename << ":" << lineno).str(); | ||
} | ||
|
||
//! Adds to message | ||
template<class OBJECT> | ||
Exception& operator<<(OBJECT const &object) { | ||
message = (std::ostringstream(message) << object).str(); | ||
return *this; | ||
} | ||
|
||
private: | ||
//! Message to issue | ||
std::string message; | ||
}; | ||
|
||
# define SOPT_THROW(MSG) throw sopt::Exception(__FILE__, __LINE__) << "\n" << MSG | ||
|
||
} /* sopt */ | ||
#endif |