GMP 0.4.0
Generative Metaprogramming library for C++
Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1// ___ __ __ ___
2// / __| \/ | _ \ GMP(Generative Metaprogramming)
3// | (_ | |\/| | _/ version 0.4.0
4// \___|_| |_|_| https://github.com/lkimuk/gmp
5//
6// SPDX-FileCopyrightText: 2023-2026 Miles Li <https://www.cppmore.com/>
7// SPDX-License-Identifier: MIT
8//
9// This file is part of the GMP (Generative Metaprogramming) library.
10// Full project source: https://github.com/lkimuk/gmp
11
12#ifndef GMP_SERIALIZATION_ERROR_HPP_
13#define GMP_SERIALIZATION_ERROR_HPP_
14
15#include <cstddef>
16#include <limits>
17#include <optional>
18#include <stdexcept>
19#include <string>
20#include <string_view>
21#include <utility>
22#include <variant>
23
24namespace gmp {
25
47
49 static constexpr std::size_t unknown_offset = std::numeric_limits<std::size_t>::max();
51 std::string message;
52 std::string path;
53 std::size_t offset = unknown_offset;
54
55 [[nodiscard]] std::string describe() const {
56 std::string result = message;
57 if (!path.empty()) {
58 result += " at ";
59 result += path;
60 }
61 if (offset != unknown_offset) {
62 result += " (byte ";
63 result += std::to_string(offset);
64 result += ')';
65 }
66 return result;
67 }
68};
69
73
74template <typename T> class serialization_result {
75public:
76 serialization_result(T value) : storage_(std::in_place_index<0>, std::move(value)) {}
77
80
82 return storage_.index() == 0;
83 }
84
85 explicit operator bool() const noexcept {
86 return has_value();
87 }
88
89 T &value() & {
90 return std::get<0>(storage_);
91 }
92
93 const T &value() const & {
94 return std::get<0>(storage_);
95 }
96
97 T &&value() && {
98 return std::get<0>(std::move(storage_));
99 }
100
101 T &operator*() & {
102 return value();
103 }
104
105 const T &operator*() const & {
106 return value();
107 }
108
109 T &&operator*() && {
110 return std::move(*this).value();
111 }
112
114 return &value();
115 }
116
117 const T *operator->() const {
118 return &value();
119 }
120
122 return std::get<1>(storage_).error;
123 }
124
126 return std::get<1>(storage_).error;
127 }
128
129private:
130 std::variant<T, serialization_unexpected> storage_;
131};
132
133template <> class serialization_result<void> {
134public:
136
138
140 return !error_.has_value();
141 }
142
143 explicit operator bool() const noexcept {
144 return has_value();
145 }
146
148 return error_.value();
149 }
150
152 return error_.value();
153 }
154
155private:
156 std::optional<serialization_error> error_;
157};
158
159inline serialization_error
160make_serialization_error(serialization_errc code, std::string message, std::string path = {},
161 std::size_t offset = serialization_error::unknown_offset) {
162 return {code, std::move(message), std::move(path), offset};
163}
164
166 std::string_view segment) {
167 if (!segment.empty()) {
168 std::string path(segment);
169 if (!error.path.empty()) {
170 path.push_back('.');
171 path += error.path;
172 }
173 error.path = std::move(path);
174 }
175 return error;
176}
177
178class serialization_exception : public std::runtime_error {
179public:
181 : std::runtime_error(error.describe()), error_(error) {}
182
184 return error_;
185 }
186
187private:
188 serialization_error error_;
189};
190
191} // namespace gmp
192
193#endif // GMP_SERIALIZATION_ERROR_HPP_
const serialization_error & error() const noexcept
Definition error.hpp:183
serialization_exception(const serialization_error &error)
Definition error.hpp:180
serialization_result() noexcept=default
bool has_value() const noexcept
Definition error.hpp:139
serialization_error & error() &
Definition error.hpp:147
const serialization_error & error() const &
Definition error.hpp:151
const serialization_error & error() const &
Definition error.hpp:125
const T * operator->() const
Definition error.hpp:117
const T & value() const &
Definition error.hpp:93
const T & operator*() const &
Definition error.hpp:105
serialization_result(T value)
Definition error.hpp:76
bool has_value() const noexcept
Definition error.hpp:81
serialization_result(serialization_error error)
Definition error.hpp:78
serialization_error & error() &
Definition error.hpp:121
consteval auto enum_values()
Get all enumerator values of an enumeration type at compile-time.
Definition meta.hpp:193
Definition lock.hpp:21
serialization_error make_serialization_error(serialization_errc code, std::string message, std::string path={}, std::size_t offset=serialization_error::unknown_offset)
Definition error.hpp:160
serialization_error prepend_serialization_path(serialization_error error, std::string_view segment)
Definition error.hpp:165
serialization_errc
Definition error.hpp:26
std::string message
Definition error.hpp:51
static constexpr std::size_t unknown_offset
Definition error.hpp:49
serialization_errc code
Definition error.hpp:50
std::string describe() const
Definition error.hpp:55
serialization_error error
Definition error.hpp:71