RflySimSDK v4.10
RflySimSDK说明文档
载入中...
搜索中...
未找到
exceptions.hpp
1// __ _____ _____ _____
2// __| | __| | | | JSON for Modern C++
3// | | |__ | | | | | | version 3.12.0
4// |_____|_____|_____|_|___| https://github.com/nlohmann/json
5//
6// SPDX-FileCopyrightText: 2013-2025 Niels Lohmann <https://nlohmann.me>
7// SPDX-License-Identifier: MIT
8
9#pragma once
10
11#include <cstddef> // nullptr_t
12#include <exception> // exception
13#if JSON_DIAGNOSTICS
14 #include <numeric> // accumulate
15#endif
16#include <stdexcept> // runtime_error
17#include <string> // to_string
18#include <vector> // vector
19
20#include <nlohmann/detail/value_t.hpp>
21#include <nlohmann/detail/string_escape.hpp>
22#include <nlohmann/detail/input/position_t.hpp>
23#include <nlohmann/detail/macro_scope.hpp>
24#include <nlohmann/detail/meta/cpp_future.hpp>
25#include <nlohmann/detail/meta/type_traits.hpp>
26#include <nlohmann/detail/string_concat.hpp>
27
28// With -Wweak-vtables, Clang will complain about the exception classes as they
29// have no out-of-line virtual method definitions and their vtable will be
30// emitted in every translation unit. This issue cannot be fixed with a
31// header-only library as there is no implementation file to move these
32// functions to. As a result, we suppress this warning here to avoid client
33// code stumbling over this. See https://github.com/nlohmann/json/issues/4087
34// for a discussion.
35#if defined(__clang__)
36 #pragma clang diagnostic push
37 #pragma clang diagnostic ignored "-Wweak-vtables"
38#endif
39
40NLOHMANN_JSON_NAMESPACE_BEGIN
41namespace detail
42{
43
44////////////////
45// exceptions //
46////////////////
47
48/// @brief general exception of the @ref basic_json class
49/// @sa https://json.nlohmann.me/api/basic_json/exception/
50class exception : public std::exception
51{
52 public:
53 /// returns the explanatory string
54 const char* what() const noexcept override
55 {
56 return m.what();
57 }
58
59 /// the id of the exception
60 const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
61
62 protected:
63 JSON_HEDLEY_NON_NULL(3)
64 exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
65
66 static std::string name(const std::string& ename, int id_)
67 {
68 return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
69 }
70
71 static std::string diagnostics(std::nullptr_t /*leaf_element*/)
72 {
73 return "";
74 }
75
76 template<typename BasicJsonType>
77 static std::string diagnostics(const BasicJsonType* leaf_element)
78 {
79#if JSON_DIAGNOSTICS
80 std::vector<std::string> tokens;
81 for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
82 {
83 switch (current->m_parent->type())
84 {
85 case value_t::array:
86 {
87 for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
88 {
89 if (&current->m_parent->m_data.m_value.array->operator[](i) == current)
90 {
91 tokens.emplace_back(std::to_string(i));
92 break;
93 }
94 }
95 break;
96 }
97
98 case value_t::object:
99 {
100 for (const auto& element : *current->m_parent->m_data.m_value.object)
101 {
102 if (&element.second == current)
103 {
104 tokens.emplace_back(element.first.c_str());
105 break;
106 }
107 }
108 break;
109 }
110
111 case value_t::null: // LCOV_EXCL_LINE
112 case value_t::string: // LCOV_EXCL_LINE
113 case value_t::boolean: // LCOV_EXCL_LINE
114 case value_t::number_integer: // LCOV_EXCL_LINE
115 case value_t::number_unsigned: // LCOV_EXCL_LINE
116 case value_t::number_float: // LCOV_EXCL_LINE
117 case value_t::binary: // LCOV_EXCL_LINE
118 case value_t::discarded: // LCOV_EXCL_LINE
119 default: // LCOV_EXCL_LINE
120 break; // LCOV_EXCL_LINE
121 }
122 }
123
124 if (tokens.empty())
125 {
126 return "";
127 }
128
129 auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
130 [](const std::string & a, const std::string & b)
131 {
132 return concat(a, '/', detail::escape(b));
133 });
134
135 return concat('(', str, ") ", get_byte_positions(leaf_element));
136#else
137 return get_byte_positions(leaf_element);
138#endif
139 }
140
141 private:
142 /// an exception object as storage for error messages
143 std::runtime_error m;
144#if JSON_DIAGNOSTIC_POSITIONS
145 template<typename BasicJsonType>
146 static std::string get_byte_positions(const BasicJsonType* leaf_element)
147 {
148 if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
149 {
150 return concat("(bytes ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos()), ") ");
151 }
152 return "";
153 }
154#else
155 template<typename BasicJsonType>
156 static std::string get_byte_positions(const BasicJsonType* leaf_element)
157 {
158 static_cast<void>(leaf_element);
159 return "";
160 }
161#endif
162};
163
164/// @brief exception indicating a parse error
165/// @sa https://json.nlohmann.me/api/basic_json/parse_error/
166class parse_error : public exception
167{
168 public:
169 /*!
170 @brief create a parse error exception
171 @param[in] id_ the id of the exception
172 @param[in] pos the position where the error occurred (or with
173 chars_read_total=0 if the position cannot be
174 determined)
175 @param[in] what_arg the explanatory string
176 @return parse_error object
177 */
178 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
179 static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
180 {
181 const std::string w = concat(exception::name("parse_error", id_), "parse error",
182 position_string(pos), ": ", exception::diagnostics(context), what_arg);
183 return {id_, pos.chars_read_total, w.c_str()};
184 }
185
186 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
187 static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
188 {
189 const std::string w = concat(exception::name("parse_error", id_), "parse error",
190 (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
191 ": ", exception::diagnostics(context), what_arg);
192 return {id_, byte_, w.c_str()};
193 }
194
195 /*!
196 @brief byte index of the parse error
197
198 The byte index of the last read character in the input file.
199
200 @note For an input with n bytes, 1 is the index of the first character and
201 n+1 is the index of the terminating null byte or the end of file.
202 This also holds true when reading a byte vector (CBOR or MessagePack).
203 */
204 const std::size_t byte;
205
206 private:
207 parse_error(int id_, std::size_t byte_, const char* what_arg)
208 : exception(id_, what_arg), byte(byte_) {}
209
210 static std::string position_string(const position_t& pos)
211 {
212 return concat(" at line ", std::to_string(pos.lines_read + 1),
213 ", column ", std::to_string(pos.chars_read_current_line));
214 }
215};
216
217/// @brief exception indicating errors with iterators
218/// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
219class invalid_iterator : public exception
220{
221 public:
222 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
223 static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
224 {
225 const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
226 return {id_, w.c_str()};
227 }
228
229 private:
230 JSON_HEDLEY_NON_NULL(3)
231 invalid_iterator(int id_, const char* what_arg)
232 : exception(id_, what_arg) {}
233};
234
235/// @brief exception indicating executing a member function with a wrong type
236/// @sa https://json.nlohmann.me/api/basic_json/type_error/
237class type_error : public exception
238{
239 public:
240 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
241 static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
242 {
243 const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
244 return {id_, w.c_str()};
245 }
246
247 private:
248 JSON_HEDLEY_NON_NULL(3)
249 type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
250};
251
252/// @brief exception indicating access out of the defined range
253/// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
254class out_of_range : public exception
255{
256 public:
257 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
258 static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
259 {
260 const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
261 return {id_, w.c_str()};
262 }
263
264 private:
265 JSON_HEDLEY_NON_NULL(3)
266 out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
267};
268
269/// @brief exception indicating other library errors
270/// @sa https://json.nlohmann.me/api/basic_json/other_error/
271class other_error : public exception
272{
273 public:
274 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
275 static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
276 {
277 const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
278 return {id_, w.c_str()};
279 }
280
281 private:
282 JSON_HEDLEY_NON_NULL(3)
283 other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
284};
285
286} // namespace detail
287NLOHMANN_JSON_NAMESPACE_END
288
289#if defined(__clang__)
290 #pragma clang diagnostic pop
291#endif
const int id
the id of the exception
定义 exceptions.hpp:60
const char * what() const noexcept override
returns the explanatory string
定义 exceptions.hpp:54
exception indicating a parse error
定义 exceptions.hpp:167
const std::size_t byte
byte index of the parse error
定义 exceptions.hpp:204
static parse_error create(int id_, const position_t &pos, const std::string &what_arg, BasicJsonContext context)
create a parse error exception
定义 exceptions.hpp:179
detail namespace with internal helper functions
定义 from_json.hpp:43
@ null
null value
定义 value_t.hpp:55
@ number_integer
number value (signed integer)
定义 value_t.hpp:60
@ boolean
boolean value
定义 value_t.hpp:59
@ discarded
discarded by the parser callback function
定义 value_t.hpp:64
@ binary
binary array (ordered collection of bytes)
定义 value_t.hpp:63
@ object
object (unordered set of name/value pairs)
定义 value_t.hpp:56
@ string
string value
定义 value_t.hpp:58
@ number_float
number value (floating-point)
定义 value_t.hpp:62
@ number_unsigned
number value (unsigned integer)
定义 value_t.hpp:61
@ array
array (ordered collection of values)
定义 value_t.hpp:57
struct to capture the start position of the current token
定义 position_t.hpp:21
std::size_t chars_read_total
the total number of characters read
定义 position_t.hpp:23
std::size_t chars_read_current_line
the number of characters read in the current line
定义 position_t.hpp:25
std::size_t lines_read
the number of lines read
定义 position_t.hpp:27