SlHelpers
Loading...
Searching...
No Matches
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
9namespace SlHelpers {
10
22class [[nodiscard("Exception must be thrown")]] RuntimeException {
23public:
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
49private:
50 std::ostringstream m_oss;
51};
52
53inline constexpr RuntimeException::ThrowNow raise;
54
55}
void raise() const
Create and throw std::runtime_error.
Definition Exception.h:44
RuntimeException(std::string_view str)
Construct new RuntimeException, having str as the initial exception string.
Definition Exception.h:26
auto getRE() const
Create and return std::runtime_error.
Definition Exception.h:41
void operator<<(const ThrowNow &)
Create and throw std::runtime_error.
Definition Exception.h:47
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 Exception.h:28