StormByte 2.0.0
C++26 foundation of the StormByte suite
 
Loading...
Searching...
No Matches
exception.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
44
45#include <format>
46#include <string>
47#include <string_view>
48#include <utility>
49
54namespace StormByte {
73 public:
78 explicit Exception(std::string_view message);
79
84 explicit Exception(const Safe::String& message);
85
86 protected:
93 struct Path {
94 std::string_view text;
95
100 explicit constexpr Path(std::string_view text = {}) noexcept: text(text) {}
101 };
102
103 public:
111 template <typename... Args>
112 Exception(std::format_string<Args...> fmt, Args&&... args)
113 : Exception(Path{}, fmt, std::forward<Args>(args)...) {}
114
119 Exception(const Exception& e) = default;
120
125 Exception(Exception&& e) noexcept = default;
126
130 virtual ~Exception() noexcept;
131
137 Exception& operator=(const Exception& e) = default;
138
144 Exception& operator=(Exception&& e) noexcept = default;
145
150 virtual const char* what() const noexcept;
151
152 protected:
161 template <typename... Args>
162 Exception(Path path, std::format_string<Args...> fmt, Args&&... args) {
163 const std::string body = sizeof...(Args) == 0
164 ? std::string(fmt.get())
165 : std::format(fmt, std::forward<Args>(args)...);
166 if (path.text.empty())
167 Assign("StormByte", body);
168 else {
169 const std::string full = std::string("StormByte.") + std::string(path.text);
170 Assign(full, body);
171 }
172 }
173
174 private:
180 void Assign(std::string_view path, std::string_view body) {
181 std::string full;
182 full.reserve(path.size() + 2 + body.size());
183 full.append(path);
184 full.append(": ");
185 full.append(body);
186 m_what = Safe::String(std::string_view(full));
187 }
188
189 Safe::String m_what;
190 };
191
197 public:
198 using Exception::Exception;
199
203 ~DeserializeError() noexcept override;
204 };
205
211 public:
212 using Exception::Exception;
213
217 ~OutOfBoundsError() noexcept override;
218 };
219
225 public:
226 using Exception::Exception;
227
231 ~Base64Error() noexcept override;
232 };
233}
Thrown when Base64 encode or decode fails.
Definition exception.hxx:224
~Base64Error() noexcept override
Destructor.
Thrown when deserialization fails.
Definition exception.hxx:196
~DeserializeError() noexcept override
Destructor.
Base exception type for the suite.
Definition exception.hxx:72
Exception(Exception &&e) noexcept=default
Move constructor.
Exception(std::format_string< Args... > fmt, Args &&... args)
Constructs with std::format.
Definition exception.hxx:112
Exception(const Safe::String &message)
Copies owned text into StormByte: message.
Exception(const Exception &e)=default
Copy constructor.
Exception(std::string_view message)
Copies text into StormByte: message.
virtual ~Exception() noexcept
Destructor.
Thrown when an index or range is out of bounds.
Definition exception.hxx:210
~OutOfBoundsError() noexcept override
Destructor.
Owned UTF-8 text composed of StormByte::Safe::CString.
Definition string.hxx:97
Root namespace of the StormByte suite.
STL namespace.
Segments under StormByte, already joined with a dot.
Definition exception.hxx:93
constexpr Path(std::string_view text={}) noexcept
Wraps text.
Definition exception.hxx:100
std::string_view text
Joined segments. Empty means the root.
Definition exception.hxx:94
#define STORMBYTE_PUBLIC
Definition visibility.h:52