BalauException.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_EXCEPTION__BALAU_EXCEPTION
18 #define COM_BORA_SOFTWARE__BALAU_EXCEPTION__BALAU_EXCEPTION
19 
20 #include <Balau/Type/ToString.hpp>
21 #include <Balau/Util/Macros.hpp>
22 
23 #ifdef BALAU_ENABLE_STACKTRACES
24  #include <boost/stacktrace.hpp>
25  #include <exception>
26  #include <Balau/Util/Impl/StringsImpl.hpp>
27 
28  #define _ThrowBalauException_generateStackTrace \
29  std::ostringstream stStream; \
30  stStream << boost::stacktrace::stacktrace(); \
31  std::string st = stStream.str(); \
32  std::string match = std::string(BalauString(BALAU_BASE_FOLDER_)) + "/"; \
33  std::string replacement; \
34  ::Balau::Util::Impl::replaceAllImpl(st, match, replacement, nullptr);
35 #else
36  #define _ThrowBalauException_generateStackTrace std::string st;
37 #endif
38 
45 #define ThrowBalauException(ExceptionClass, ...) { \
46  _ThrowBalauException_generateStackTrace \
47  throw ExceptionClass(__FILE__, __LINE__, st, __VA_ARGS__); \
48 } _Balau_SwallowSemiColon_()
49 
50 namespace Balau::Exception {
51 
55 class BalauException : public std::exception {
59  public: const std::string message;
60 
64  public: const std::string fullMessage;
65 
66  public: const char * what() const noexcept override {
67  return fullMessage.c_str();
68  }
69 
70  protected: BalauException(const char * file,
71  int line,
72  const std::string & st,
73  const std::string & name,
74  const std::string & text)
75  : message(::toString(name, " - ", text))
76  , fullMessage(generateFullMessage(file, line, st, name, text)) {}
77 
78  private: static std::string generateFullMessage(const char * file,
79  int line,
80  const std::string & st,
81  const std::string & name,
82  const std::string & text) {
83  std::ostringstream stream;
84  stream << stripFilePath(file) << ":" << line << " - " << name << " - " << text;
85 
86  if (!st.empty()) {
87  stream << "\n" << st;
88  }
89 
90  return stream.str();
91  }
92 
93  // This will need to be reimplemented for the Windows platform.
94  private: static std::string stripFilePath(const char * file) {
95  const char * lastForward = strrchr(file, '/');
96 
97  if (lastForward != nullptr) {
98  return std::string(lastForward + 1);
99  } else {
100  return file;
101  }
102  }
103 };
104 
112 inline bool operator == (const BalauException & lhs, const BalauException & rhs) {
113  return typeid(lhs) == typeid(rhs) && lhs.message == rhs.message;
114 }
115 
121 template <typename AllocatorT>
123  return toString<AllocatorT>(e.fullMessage);
124 }
125 
131 inline std::string toString(const BalauException & e) {
132  return e.fullMessage;
133 }
134 
139  public: IllegalArgumentException(const char * file, int line, const std::string & st, const std::string & text)
140  : BalauException(file, line, st, "IllegalArgument", text) {}
141 };
142 
147  public: IllegalStateException(const char * file, int line, const std::string & st, const std::string & text)
148  : BalauException(file, line, st, "IllegalState", text) {}
149 };
150 
155  public: UnsupportedOperationException(const char * file, int line, const std::string & st, const std::string & text)
156  : BalauException(file, line, st, "UnsupportedOperation", text) {}
157 };
158 
163  public: NotImplementedException(const char * file, int line, const std::string & st, const std::string & text)
164  : BalauException(file, line, st, "NotImplemented", text) {}
165 };
166 
171  public: ConversionException(const char * file, int line, const std::string & st, const std::string & text)
172  : BalauException(file, line, st, "Conversion", text) {}
173 };
174 
178 class BugException : public BalauException {
179  public: BugException(const char * file, int line, const std::string & st, const std::string & text)
180  : BalauException(file, line, st, "Bug", text) {}
181 };
182 
183 } // namespace Balau::Exception
184 
188 inline std::string toString(const std::exception & e) {
189  return e.what();
190 }
191 
195 template <typename AllocatorT>
196 inline Balau::U8String<AllocatorT> toString(const std::exception & e) {
197  return toString<AllocatorT>(e.what());
198 }
199 
200 #endif // COM_BORA_SOFTWARE__BALAU_EXCEPTION__BALAU_EXCEPTION
bool operator==(const BalauException &lhs, const BalauException &rhs)
Base class comparison function for Balau exceptions.
Definition: BalauException.hpp:112
All exception classes.
Definition: BalauException.hpp:50
Pre-defined universal to-string functions.
Balau::U8String< AllocatorT > toString(const BalauException &e)
Base class toString<AllocatorT> function for Balau exceptions.
Definition: BalauException.hpp:122
Thrown when a feature is not yet implemented.
Definition: BalauException.hpp:162
Thrown when a bug is encountered (mainly unimplemented switch cases).
Definition: BalauException.hpp:178
Thrown when an illegal argument is passed to a function or method.
Definition: BalauException.hpp:138
const std::string fullMessage
The full message (includes file and line information).
Definition: BalauException.hpp:64
Thrown when a conversion fails.
Definition: BalauException.hpp:170
const std::string message
The message.
Definition: BalauException.hpp:59
Utilities for macros.
std::basic_string< char, std::char_traits< char >, AllocatorT > U8String
UTF-8 string type with selectable allocator.
Definition: ToStringA.hpp:41
Thrown when a variable is not in a valid state or when a section of code has been executed at an inap...
Definition: BalauException.hpp:146
Base class of all Balau exceptions.
Definition: BalauException.hpp:55
Thrown when an operation is deliberately not implemented.
Definition: BalauException.hpp:154