ʻOhana
Population structure, admixture history, and selection using learning methods.
jade.error.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_ERROR_HPP__
8 #define JADE_ERROR_HPP__
9 
10 #include "jade.assert.hpp"
11 
12 namespace jade
13 {
14  ///
15  /// A template for a class representing an exception thrown from this
16  /// namespace.
17  ///
18  template <typename TChar>
19  class basic_error : public std::exception
20  {
21  public:
22  /// The character type.
23  typedef TChar char_type;
24 
25  /// The string type.
26  typedef std::basic_string<char_type> string_type;
27 
28  ///
29  /// Initializes a new instance of the class with no message.
30  ///
31  inline basic_error()
32  : _m ()
33  {
34  }
35 
36  ///
37  /// Initializes a new instance of the class with the specified message.
38  ///
39  inline explicit basic_error(
40  const string_type & message) ///< The initial message.
41  : _m (message)
42  {
43  }
44 
45  ///
46  /// Initializes a new instance of the class with the specified message.
47  ///
48  inline explicit basic_error(
49  char_type const * const message) ///< The initial message.
50  : _m (message)
51  {
52  assert(message != nullptr);
53  }
54 
55  ///
56  /// \return The message.
57  ///
58  inline const string_type & str() const
59  {
60  return _m;
61  }
62 
63  ///
64  /// \return An explanatory string.
65  ///
66  inline virtual const char * what() const throw();
67 
68  ///
69  /// Concatenates the specified value with the current message.
70  ///
71  /// \param rhs The value to concatenate.
72  /// \return This instance.
73  ///
74  template <typename TArgValue>
75  basic_error & operator << (TArgValue rhs)
76  {
77  std::basic_ostringstream<char_type> out;
78  out << rhs;
79  _m += out.str();
80  return *this;
81  }
82 
83  private:
84  string_type _m;
85  };
86 
87  #ifndef DOXYGEN_IGNORE
88 
89  // ------------------------------------------------------------------------
90  template <>
91  inline const char * basic_error<char>::what() const throw()
92  {
93  return _m.c_str();
94  }
95 
96  #endif // DOXYGEN_IGNORE
97 
98  /// A class representing an exception thrown from this namespace.
99  typedef basic_error<char> error;
100 }
101 
102 #endif // JADE_ERROR_HPP__
jade::basic_error::basic_error
basic_error()
Initializes a new instance of the class with no message.
Definition: jade.error.hpp:31
jade::basic_error::str
const string_type & str() const
Definition: jade.error.hpp:58
jade::basic_error::basic_error
basic_error(const string_type &message)
Initializes a new instance of the class with the specified message.
Definition: jade.error.hpp:39
jade::basic_error::char_type
TChar char_type
The character type.
Definition: jade.error.hpp:23
jade::basic_error::what
virtual const char * what() const
jade::basic_error::string_type
std::basic_string< char_type > string_type
The string type.
Definition: jade.error.hpp:26
jade::basic_error::basic_error
basic_error(char_type const *const message)
Initializes a new instance of the class with the specified message.
Definition: jade.error.hpp:48
jade::basic_error
A template for a class representing an exception thrown from this namespace.
Definition: jade.error.hpp:20