SlHelpers
Exception.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <sstream>
6 #include <stdexcept>
7 #include <string_view>
8 
9 namespace SlHelpers {
10 
22 class [[nodiscard("Exception must be thrown")]] RuntimeException {
23 public:
24  RuntimeException() = default;
26  RuntimeException(std::string_view str) { m_oss << str; }
27 
28  struct ThrowNow {};
29 
31  template <typename T>
32  RuntimeException &operator<<(const T &msg) {
33  m_oss << msg;
34  return *this;
35  }
36 
38  std::string str() const { return m_oss.str(); }
39 
41  auto getRE() const { return std::runtime_error(str()); }
42 
44  [[noreturn]] void raise() const { throw getRE(); }
45 
47  [[noreturn]] void operator<<(const ThrowNow &) { raise(); }
48 
49 private:
50  std::ostringstream m_oss;
51 };
52 
53 inline constexpr RuntimeException::ThrowNow raise;
54 
55 }
Class for easier construction of std::runtime_error.
Definition: Exception.h:22
Definition: Exception.h:28
RuntimeException(std::string_view str)
Construct new RuntimeException, having str as the initial exception string.
Definition: Exception.h:26
void operator<<(const ThrowNow &)
Create and throw std::runtime_error.
Definition: Exception.h:47
auto getRE() const
Create and return std::runtime_error.
Definition: Exception.h:41
RuntimeException & operator<<(const T &msg)
Add more to the exception string.
Definition: Exception.h:32
std::string str() const
Return the stored exception string.
Definition: Exception.h:38
Definition: Color.h:8