Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
parse_result.hpp
Go to the documentation of this file.
1// # This file is a part of toml++ and is subject to the the terms of the MIT license.
2// # Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3// # See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4// SPDX-License-Identifier: MIT
5#pragma once
6
7#include "preprocessor.hpp"
8#if TOML_DOXYGEN || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
9
10#include "header_start.hpp"
11#include "parse_error.hpp"
12#include "table.hpp"
13
16
52 private:
53 struct storage_t {
54 static constexpr size_t size =
55 (sizeof(toml::table) < sizeof(parse_error) ? sizeof(parse_error) : sizeof(toml::table));
56 static constexpr size_t align =
57 (alignof(toml::table) < alignof(parse_error) ? alignof(parse_error)
58 : alignof(toml::table));
59
60 alignas(align) unsigned char bytes[size];
61 };
62
63 alignas(storage_t::align) mutable storage_t storage_;
64 bool err_;
65
66 template <typename Type>
67 TOML_NODISCARD TOML_ALWAYS_INLINE static Type* get_as(storage_t& s) noexcept {
68 return TOML_LAUNDER(reinterpret_cast<Type*>(s.bytes));
69 }
70
71 void destroy() noexcept {
72 if (err_)
73 get_as<parse_error>(storage_)->~parse_error();
74 else
75 get_as<toml::table>(storage_)->~table();
76 }
77
78 public:
81 parse_result() noexcept //
82 : err_{true} {
83 ::new (static_cast<void*>(storage_.bytes)) parse_error{std::string{}, source_region{}};
84 }
85
87 explicit parse_result(toml::table&& tbl) noexcept //
88 : err_{false} {
89 ::new (static_cast<void*>(storage_.bytes)) toml::table{std::move(tbl)};
90 }
91
93 explicit parse_result(parse_error&& err) noexcept //
94 : err_{true} {
95 ::new (static_cast<void*>(storage_.bytes)) parse_error{std::move(err)};
96 }
97
101 : err_{res.err_} {
102 if (err_)
103 ::new (static_cast<void*>(storage_.bytes)) parse_error{std::move(res).error()};
104 else
105 ::new (static_cast<void*>(storage_.bytes)) toml::table{std::move(res).table()};
106 }
107
110 if (err_ != rhs.err_) {
111 destroy();
112 err_ = rhs.err_;
113 if (err_)
114 ::new (static_cast<void*>(storage_.bytes)) parse_error{std::move(rhs).error()};
115 else
116 ::new (static_cast<void*>(storage_.bytes)) toml::table{std::move(rhs).table()};
117 } else {
118 if (err_)
119 error() = std::move(rhs).error();
120 else
121 table() = std::move(rhs).table();
122 }
123 return *this;
124 }
125
127 ~parse_result() noexcept { destroy(); }
128
131
134 bool succeeded() const noexcept { return !err_; }
135
138 bool failed() const noexcept { return err_; }
139
142 explicit operator bool() const noexcept { return !err_; }
143
145
151
154 toml::table& table() & noexcept {
155 TOML_ASSERT_ASSUME(!err_);
156 return *get_as<toml::table>(storage_);
157 }
158
161 toml::table&& table() && noexcept {
162 TOML_ASSERT_ASSUME(!err_);
163 return static_cast<toml::table&&>(*get_as<toml::table>(storage_));
164 }
165
168 const toml::table& table() const& noexcept {
169 TOML_ASSERT_ASSUME(!err_);
170 return *get_as<const toml::table>(storage_);
171 }
172
175 /* implicit */ operator toml::table&() noexcept { return table(); }
176
179 /* implicit */ operator toml::table&&() noexcept { return std::move(table()); }
180
183 /* implicit */ operator const toml::table&() const noexcept { return table(); }
184
186
192
195 parse_error& error() & noexcept {
196 TOML_ASSERT_ASSUME(err_);
197 return *get_as<parse_error>(storage_);
198 }
199
202 parse_error&& error() && noexcept {
203 TOML_ASSERT_ASSUME(err_);
204 return static_cast<parse_error&&>(*get_as<parse_error>(storage_));
205 }
206
209 const parse_error& error() const& noexcept {
210 TOML_ASSERT_ASSUME(err_);
211 return *get_as<const parse_error>(storage_);
212 }
213
216 explicit operator parse_error&() noexcept { return error(); }
217
220 explicit operator parse_error&&() noexcept { return std::move(error()); }
221
224 explicit operator const parse_error&() const noexcept { return error(); }
225
227
230
232 using iterator = table_iterator;
233
237
241 table_iterator begin() noexcept { return err_ ? table_iterator{} : table().begin(); }
242
246 const_table_iterator begin() const noexcept {
247 return err_ ? const_table_iterator{} : table().begin();
248 }
249
253 const_table_iterator cbegin() const noexcept {
254 return err_ ? const_table_iterator{} : table().cbegin();
255 }
256
259 table_iterator end() noexcept { return err_ ? table_iterator{} : table().end(); }
260
263 const_table_iterator end() const noexcept {
264 return err_ ? const_table_iterator{} : table().end();
265 }
266
269 const_table_iterator cend() const noexcept {
270 return err_ ? const_table_iterator{} : table().cend();
271 }
272
274
277
282 node_view<node> at_path(std::string_view path) noexcept {
283 return err_ ? node_view<node>{} : table().at_path(path);
284 }
285
290 node_view<const node> at_path(std::string_view path) const noexcept {
291 return err_ ? node_view<const node>{} : table().at_path(path);
292 }
293
298 node_view<node> at_path(const toml::path& path) noexcept {
299 return err_ ? node_view<node>{} : table().at_path(path);
300 }
301
306 node_view<const node> at_path(const toml::path& path) const noexcept {
307 return err_ ? node_view<const node>{} : table().at_path(path);
308 }
309
310#if TOML_ENABLE_WINDOWS_COMPAT
311
318 node_view<node> at_path(std::wstring_view path) {
319 return err_ ? node_view<node>{} : table().at_path(path);
320 }
321
328 node_view<const node> at_path(std::wstring_view path) const {
329 return err_ ? node_view<const node>{} : table().at_path(path);
330 }
331
332#endif
333
338 node_view<node> operator[](const toml::path& path) noexcept {
339 return err_ ? node_view<node>{} : table()[path];
340 }
341
346 node_view<const node> operator[](const toml::path& path) const noexcept {
347 return err_ ? node_view<const node>{} : table()[path];
348 }
349
359 node_view<node> operator[](std::string_view key) noexcept {
360 return err_ ? node_view<node>{} : table()[key];
361 }
362
373 node_view<const node> operator[](std::string_view key) const noexcept {
374 return err_ ? node_view<const node>{} : table()[key];
375 }
376
377#if TOML_ENABLE_WINDOWS_COMPAT
378
390 node_view<node> operator[](std::wstring_view key) {
391 return err_ ? node_view<node>{} : table()[key];
392 }
393
406 node_view<const node> operator[](std::wstring_view key) const {
407 return err_ ? node_view<const node>{} : table()[key];
408 }
409
410#endif // TOML_ENABLE_WINDOWS_COMPAT
411
413
414#if TOML_ENABLE_FORMATTERS
415
419 friend std::ostream& operator<<(std::ostream& os, const parse_result& result) {
420 return result.err_ ? (os << result.error()) : (os << result.table());
421 }
422
423#endif
424 };
425
427}
429
430#include "header_end.hpp"
431#endif // TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
#define rhs
Definition 17.names.cpp:536
#define os
Definition 17.names.cpp:511
#define res
Definition 17.names.cpp:531
#define s
volatile int result
The result of a parsing operation.
TOML_NODISCARD toml::table && table() &&noexcept
Returns the internal toml::table (rvalue overload).
TOML_NODISCARD toml::table & table() &noexcept
Returns the internal toml::table.
TOML_NODISCARD node_view< const node > operator[](const toml::path &path) const noexcept
Returns a const view of the subnode matching a fully-qualified "TOML path".
TOML_NODISCARD node_view< node > at_path(std::string_view path) noexcept
Returns a view of the subnode matching a fully-qualified "TOML path".
TOML_NODISCARD node_view< const node > operator[](std::wstring_view key) const
Gets a node_view for the selected key-value pair in the wrapped table (const overload).
TOML_NODISCARD node_view< node > at_path(const toml::path &path) noexcept
Returns a view of the subnode matching a fully-qualified "TOML path".
TOML_NODISCARD const toml::table & table() const &noexcept
Returns the internal toml::table (const lvalue overload).
TOML_NODISCARD const_table_iterator begin() const noexcept
Returns an iterator to the first key-value pair in the wrapped table.
TOML_NODISCARD bool failed() const noexcept
Returns true if parsing failed.
friend std::ostream & operator<<(std::ostream &os, const parse_result &result)
Prints the held error or table object out to a text stream.
TOML_NODISCARD_CTOR parse_result(parse_result &&res) noexcept
Move constructor.
TOML_NODISCARD node_view< const node > at_path(const toml::path &path) const noexcept
Returns a const view of the subnode matching a fully-qualified "TOML path".
TOML_NODISCARD table_iterator end() noexcept
Returns an iterator to one-past-the-last key-value pair in the wrapped table.
~parse_result() noexcept
Destructor.
TOML_NODISCARD_CTOR parse_result(toml::table &&tbl) noexcept
table_iterator iterator
A BidirectionalIterator for iterating over key-value pairs in a wrapped toml::table.
TOML_NODISCARD table_iterator begin() noexcept
Returns an iterator to the first key-value pair in the wrapped table.
const_table_iterator const_iterator
A BidirectionalIterator for iterating over const key-value pairs in a wrapped toml::table.
parse_result & operator=(parse_result &&rhs) noexcept
Move-assignment operator.
TOML_NODISCARD_CTOR parse_result() noexcept
Default constructs an 'error' result.
TOML_NODISCARD node_view< node > operator[](std::string_view key) noexcept
Gets a node_view for the selected key-value pair in the wrapped table.
TOML_NODISCARD const_table_iterator end() const noexcept
Returns an iterator to one-past-the-last key-value pair in the wrapped table.
TOML_NODISCARD node_view< node > at_path(std::wstring_view path)
Returns a view of the subnode matching a fully-qualified "TOML path".
TOML_NODISCARD node_view< const node > operator[](std::string_view key) const noexcept
Gets a node_view for the selected key-value pair in the wrapped table (const overload).
TOML_NODISCARD node_view< node > operator[](std::wstring_view key)
Gets a node_view for the selected key-value pair in the wrapped table.
TOML_NODISCARD_CTOR parse_result(parse_error &&err) noexcept
TOML_NODISCARD const_table_iterator cend() const noexcept
Returns an iterator to one-past-the-last key-value pair in the wrapped table.
TOML_NODISCARD node_view< node > operator[](const toml::path &path) noexcept
Returns a view of the subnode matching a fully-qualified "TOML path".
TOML_NODISCARD parse_error && error() &&noexcept
Returns the internal toml::parse_error (rvalue overload).
TOML_NODISCARD const parse_error & error() const &noexcept
Returns the internal toml::parse_error (const lvalue overload).
TOML_NODISCARD node_view< const node > at_path(std::string_view path) const noexcept
Returns a const view of the subnode matching a fully-qualified "TOML path".
TOML_NODISCARD node_view< const node > at_path(std::wstring_view path) const
Returns a const view of the subnode matching a fully-qualified "TOML path".
TOML_NODISCARD bool succeeded() const noexcept
Returns true if parsing succeeeded.
TOML_NODISCARD parse_error & error() &noexcept
Returns the internal toml::parse_error.
TOML_NODISCARD const_table_iterator cbegin() const noexcept
Returns an iterator to the first key-value pair in the wrapped table.
A TOML path.
Definition path.hpp:209
A TOML table.
Definition table.hpp:190
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION table() noexcept
Default constructor.
TOML_PURE_INLINE_GETTER iterator begin() noexcept
Returns an iterator to the first key-value pair.
Definition table.hpp:649
Iter end
Definition table.hpp:1430
TOML_PURE_INLINE_GETTER const_iterator cbegin() const noexcept
Returns an iterator to the first key-value pair.
Definition table.hpp:657
TOML_PURE_INLINE_GETTER const_iterator cend() const noexcept
Returns an iterator to one-past-the-last key-value pair.
Definition table.hpp:669
#define TOML_ASSERT_ASSUME(expr)
The root namespace for all toml++ functions and types.
TOML_NAMESPACE_START
TOML_ABI_NAMESPACE_END
TOML_NAMESPACE_END
#define TOML_NODISCARD_CTOR
#define TOML_NODISCARD
#define TOML_ABI_NAMESPACE_START(name)
#define TOML_ALWAYS_INLINE
UChar bytes[sizeof(int)]
Definition printf.cpp:83
#define TOML_LAUNDER(x)
Definition std_new.hpp:18
#define bool
Definition stdbool.h:24
A source document region.
POXY_IMPLEMENTATION_DETAIL(impl::table_iterator< true >) const_table_iterator
A BidirectionalIterator for iterating over const key-value pairs in a toml::table.
Definition table.hpp:157