versatile-mcmc  0.1.7
exception.hpp
Go to the documentation of this file.
1 
13 #ifndef VMCMC_EXCEPTION_H_
14 #define VMCMC_EXCEPTION_H_
15 
16 #include <exception>
17 #include <sstream>
18 #include <string>
19 
20 namespace vmcmc
21 {
22 
28 class Exception : public std::exception
29 {
30 public:
31  Exception() { }
32  Exception(const Exception& copy);
33  virtual ~Exception() { }
34 
35  void operator=(const Exception& copy);
36 
37  const char* what() const throw() override;
38 
39  template <typename XValue>
40  Exception& operator<<(const XValue& toAppend);
41 
42  Exception& Nest(const std::exception& toNest);
43 
44 protected:
45  std::ostringstream fMessage;
46  std::string fNestedMessage;
47 
48  mutable std::string fWhat;
49 };
50 
51 inline Exception::Exception( const Exception& toCopy ) :
52  std::exception( toCopy ),
53  fMessage( toCopy.fMessage.str() ),
54  fNestedMessage( toCopy.fNestedMessage )
55 { }
56 
57 inline void Exception::operator=( const Exception& toCopy )
58 {
59  std::exception::operator=( toCopy );
60  fMessage.str( toCopy.fMessage.str() );
61  fNestedMessage = toCopy.fNestedMessage;
62 }
63 
64 inline const char* Exception::what() const throw()
65 {
66  fWhat = fMessage.str();
67  if( !fNestedMessage.empty() ) {
68  fWhat.append( " [" + fNestedMessage + "]" );
69  }
70  return fWhat.c_str();
71 }
72 
73 template<typename XValue>
74 inline Exception& Exception::operator<<(const XValue& toAppend)
75 {
76  fMessage << toAppend;
77  return (*this);
78 }
79 
80 inline Exception& Exception::Nest(const std::exception& toNest)
81 {
82  fNestedMessage = toNest.what();
83  return (*this);
84 }
85 
86 inline std::ostream& operator<<(std::ostream& os, const Exception& e)
87 {
88  return os << e.what();
89 }
90 
91 } /* namespace vmcmc */
92 
93 #endif /* VMCMC_EXCEPTION_H_ */
Definition: algorithm.cpp:28
Base class for exceptions thrown by FastMCMC.
Definition: exception.hpp:28