Scarab  v3.5.3
Project 8 C++ Utility Library
base_exception.hh
Go to the documentation of this file.
1 /*
2  * base_exception.hh
3  *
4  * Created on: Mar 17, 2020
5  * Author: N.S. Oblath
6  */
7 
8 #ifndef SCARAB_BASE_EXCEPTION_HH_
9 #define SCARAB_BASE_EXCEPTION_HH_
10 
11 #include "scarab_api.hh"
12 
13 #include <exception>
14 #include <sstream>
15 
16 
17 namespace scarab
18 {
34  template< typename x_derived >
35  class SCARAB_API base_exception : public ::std::exception
36  {
37  public:
40  virtual ~base_exception() noexcept;
41 
42  base_exception< x_derived >& operator=( const base_exception< x_derived >& a_orig );
43 
44  template< class x_streamable >
45  x_derived& operator<<( x_streamable a_fragment );
46  x_derived& operator<<( const std::string& a_fragment );
47  x_derived& operator<<( const char* a_fragment );
48 
49  virtual const char* what() const noexcept;
50 
51  protected:
52  mutable std::string f_error;
53  };
54 
55  template< typename x_derived >
57  ::std::exception(),
58  f_error()
59  {}
60 
61  template< typename x_derived >
63  std::exception( a_orig ),
64  f_error( a_orig.f_error )
65  {}
66 
67  template< typename x_derived >
69  {}
70 
71  template< typename x_derived >
73  {
74  f_error = a_orig.f_error;
75  return *this;
76  }
77 
78  template< typename x_derived >
79  const char* base_exception< x_derived >::what() const noexcept
80  {
81  return f_error.c_str();
82  }
83 
84  template< typename x_derived >
85  template< class x_streamable >
86  x_derived& base_exception< x_derived >::operator<<( x_streamable a_fragment )
87  {
88  std::stringstream stream;
89  stream << a_fragment;
90  f_error += stream.str();
91  return *static_cast< x_derived* >(this);
92  }
93 
94  template< typename x_derived >
95  x_derived& base_exception< x_derived >::operator<<( const std::string& a_fragment )
96  {
97  f_error += a_fragment;
98  return *static_cast< x_derived* >(this);
99  }
100 
101  template< typename x_derived >
102  x_derived& base_exception< x_derived >::operator<<( const char* a_fragment )
103  {
104  f_error += std::string( a_fragment );
105  return *static_cast< x_derived* >(this);
106  }
107 
108 }
109 
110 #endif /* SCARAB_BASE_EXCEPTION_HH_ */
#define SCARAB_API
Definition: scarab_api.hh:24
STL namespace.
std::ostream & operator<<(std::ostream &in, const T &item)
output streaming for enumerations
Definition: CLI11.hpp:227
SCARAB_API std::ostream & operator<<(std::ostream &out, const param_array &a_value)
Definition: param_array.cc:167
Base class for exceptions with streaming operators.