NeBuild dev
Loading...
Searching...
No Matches
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 "table.hpp"
11#include "parse_error.hpp"
12#include "header_start.hpp"
13
15{
17
53 {
54 private:
55 struct storage_t
56 {
57 static constexpr size_t size =
58 (sizeof(toml::table) < sizeof(parse_error) ? sizeof(parse_error) : sizeof(toml::table));
59 static constexpr size_t align =
60 (alignof(toml::table) < alignof(parse_error) ? alignof(parse_error) : alignof(toml::table));
61
62 alignas(align) unsigned char bytes[size];
63 };
64
66 bool err_;
67
68 template <typename Type>
71 static Type* get_as(storage_t& s) noexcept
72 {
73 return TOML_LAUNDER(reinterpret_cast<Type*>(s.bytes));
74 }
75
76 void destroy() noexcept
77 {
78 if (err_)
79 get_as<parse_error>(storage_)->~parse_error();
80 else
81 get_as<toml::table>(storage_)->~table();
82 }
83
84 public:
87 parse_result() noexcept //
88 : err_{ true }
89 {
90 ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::string{}, source_region{} };
91 }
92
94 explicit parse_result(toml::table&& tbl) noexcept //
95 : err_{ false }
96 {
97 ::new (static_cast<void*>(storage_.bytes)) toml::table{ std::move(tbl) };
98 }
99
101 explicit parse_result(parse_error&& err) noexcept //
102 : err_{ true }
103 {
104 ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::move(err) };
105 }
106
109 parse_result(parse_result&& res) noexcept //
110 : err_{ res.err_ }
111 {
112 if (err_)
113 ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::move(res).error() };
114 else
115 ::new (static_cast<void*>(storage_.bytes)) toml::table{ std::move(res).table() };
116 }
117
120 {
121 if (err_ != rhs.err_)
122 {
123 destroy();
124 err_ = rhs.err_;
125 if (err_)
126 ::new (static_cast<void*>(storage_.bytes)) parse_error{ std::move(rhs).error() };
127 else
128 ::new (static_cast<void*>(storage_.bytes)) toml::table{ std::move(rhs).table() };
129 }
130 else
131 {
132 if (err_)
133 error() = std::move(rhs).error();
134 else
135 table() = std::move(rhs).table();
136 }
137 return *this;
138 }
139
141 ~parse_result() noexcept
142 {
143 destroy();
144 }
145
148
151 bool succeeded() const noexcept
152 {
153 return !err_;
154 }
155
158 bool failed() const noexcept
159 {
160 return err_;
161 }
162
165 explicit operator bool() const noexcept
166 {
167 return !err_;
168 }
169
171
176
179 toml::table& table() & noexcept
180 {
182 return *get_as<toml::table>(storage_);
183 }
184
187 toml::table&& table() && noexcept
188 {
190 return static_cast<toml::table&&>(*get_as<toml::table>(storage_));
191 }
192
195 const toml::table& table() const& noexcept
196 {
198 return *get_as<const toml::table>(storage_);
199 }
200
203 /* implicit */ operator toml::table&() noexcept
204 {
205 return table();
206 }
207
210 /* implicit */ operator toml::table&&() noexcept
211 {
212 return std::move(table());
213 }
214
217 /* implicit */ operator const toml::table&() const noexcept
218 {
219 return table();
220 }
221
223
228
231 parse_error& error() & noexcept
232 {
234 return *get_as<parse_error>(storage_);
235 }
236
239 parse_error&& error() && noexcept
240 {
242 return static_cast<parse_error&&>(*get_as<parse_error>(storage_));
243 }
244
247 const parse_error& error() const& noexcept
248 {
250 return *get_as<const parse_error>(storage_);
251 }
252
255 explicit operator parse_error&() noexcept
256 {
257 return error();
258 }
259
262 explicit operator parse_error&&() noexcept
263 {
264 return std::move(error());
265 }
266
269 explicit operator const parse_error&() const noexcept
270 {
271 return error();
272 }
273
275
278
280 using iterator = table_iterator;
281
284
288 table_iterator begin() noexcept
289 {
290 return err_ ? table_iterator{} : table().begin();
291 }
292
297 {
298 return err_ ? const_table_iterator{} : table().begin();
299 }
300
305 {
306 return err_ ? const_table_iterator{} : table().cbegin();
307 }
308
311 table_iterator end() noexcept
312 {
313 return err_ ? table_iterator{} : table().end();
314 }
315
318 const_table_iterator end() const noexcept
319 {
320 return err_ ? const_table_iterator{} : table().end();
321 }
322
325 const_table_iterator cend() const noexcept
326 {
327 return err_ ? const_table_iterator{} : table().cend();
328 }
329
331
334
339 node_view<node> at_path(std::string_view path) noexcept
340 {
341 return err_ ? node_view<node>{} : table().at_path(path);
342 }
343
348 node_view<const node> at_path(std::string_view path) const noexcept
349 {
350 return err_ ? node_view<const node>{} : table().at_path(path);
351 }
352
357 node_view<node> at_path(const toml::path& path) noexcept
358 {
359 return err_ ? node_view<node>{} : table().at_path(path);
360 }
361
366 node_view<const node> at_path(const toml::path& path) const noexcept
367 {
368 return err_ ? node_view<const node>{} : table().at_path(path);
369 }
370
371#if TOML_ENABLE_WINDOWS_COMPAT
372
379 node_view<node> at_path(std::wstring_view path)
380 {
381 return err_ ? node_view<node>{} : table().at_path(path);
382 }
383
390 node_view<const node> at_path(std::wstring_view path) const
391 {
392 return err_ ? node_view<const node>{} : table().at_path(path);
393 }
394
395#endif
396
401 node_view<node> operator[](const toml::path& path) noexcept
402 {
403 return err_ ? node_view<node>{} : table()[path];
404 }
405
410 node_view<const node> operator[](const toml::path& path) const noexcept
411 {
412 return err_ ? node_view<const node>{} : table()[path];
413 }
414
424 node_view<node> operator[](std::string_view key) noexcept
425 {
426 return err_ ? node_view<node>{} : table()[key];
427 }
428
438 node_view<const node> operator[](std::string_view key) const noexcept
439 {
440 return err_ ? node_view<const node>{} : table()[key];
441 }
442
443#if TOML_ENABLE_WINDOWS_COMPAT
444
456 node_view<node> operator[](std::wstring_view key)
457 {
458 return err_ ? node_view<node>{} : table()[key];
459 }
460
472 node_view<const node> operator[](std::wstring_view key) const
473 {
474 return err_ ? node_view<const node>{} : table()[key];
475 }
476
477#endif // TOML_ENABLE_WINDOWS_COMPAT
478
480
481#if TOML_ENABLE_FORMATTERS
482
486 friend std::ostream& operator<<(std::ostream& os, const parse_result& result)
487 {
488 return result.err_ ? (os << result.error()) : (os << result.table());
489 }
490
491#endif
492 };
493
495}
497
498#include "header_end.hpp"
499#endif // TOML_ENABLE_PARSER && !TOML_EXCEPTIONS
The result of a parsing operation.
Definition parse_result.hpp:53
TOML_NODISCARD toml::table && table() &&noexcept
Returns the internal toml::table (rvalue overload).
Definition parse_result.hpp:187
TOML_NODISCARD toml::table & table() &noexcept
Returns the internal toml::table.
Definition parse_result.hpp:179
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".
Definition parse_result.hpp:410
TOML_NODISCARD node_view< node > at_path(std::string_view path) noexcept
Returns a view of the subnode matching a fully-qualified "TOML path".
Definition parse_result.hpp:339
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).
Definition parse_result.hpp:472
TOML_NODISCARD node_view< node > at_path(const toml::path &path) noexcept
Returns a view of the subnode matching a fully-qualified "TOML path".
Definition parse_result.hpp:357
TOML_NODISCARD const toml::table & table() const &noexcept
Returns the internal toml::table (const lvalue overload).
Definition parse_result.hpp:195
TOML_NODISCARD const_table_iterator begin() const noexcept
Returns an iterator to the first key-value pair in the wrapped table.
Definition parse_result.hpp:296
TOML_NODISCARD bool failed() const noexcept
Returns true if parsing failed.
Definition parse_result.hpp:158
friend std::ostream & operator<<(std::ostream &os, const parse_result &result)
Prints the held error or table object out to a text stream.
Definition parse_result.hpp:486
TOML_NODISCARD_CTOR parse_result(parse_result &&res) noexcept
Move constructor.
Definition parse_result.hpp:109
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".
Definition parse_result.hpp:366
TOML_NODISCARD table_iterator end() noexcept
Returns an iterator to one-past-the-last key-value pair in the wrapped table.
Definition parse_result.hpp:311
~parse_result() noexcept
Destructor.
Definition parse_result.hpp:141
TOML_NODISCARD_CTOR parse_result(toml::table &&tbl) noexcept
Definition parse_result.hpp:94
table_iterator iterator
A BidirectionalIterator for iterating over key-value pairs in a wrapped toml::table.
Definition parse_result.hpp:280
TOML_NODISCARD table_iterator begin() noexcept
Returns an iterator to the first key-value pair in the wrapped table.
Definition parse_result.hpp:288
const_table_iterator const_iterator
A BidirectionalIterator for iterating over const key-value pairs in a wrapped toml::table.
Definition parse_result.hpp:283
parse_result & operator=(parse_result &&rhs) noexcept
Move-assignment operator.
Definition parse_result.hpp:119
TOML_NODISCARD_CTOR parse_result() noexcept
Default constructs an 'error' result.
Definition parse_result.hpp:87
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.
Definition parse_result.hpp:424
TOML_NODISCARD const_table_iterator end() const noexcept
Returns an iterator to one-past-the-last key-value pair in the wrapped table.
Definition parse_result.hpp:318
TOML_NODISCARD node_view< node > at_path(std::wstring_view path)
Returns a view of the subnode matching a fully-qualified "TOML path".
Definition parse_result.hpp:379
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).
Definition parse_result.hpp:438
TOML_NODISCARD node_view< node > operator[](std::wstring_view key)
Gets a node_view for the selected key-value pair in the wrapped table.
Definition parse_result.hpp:456
TOML_NODISCARD static TOML_ALWAYS_INLINE Type * get_as(storage_t &s) noexcept
Definition parse_result.hpp:71
TOML_NODISCARD_CTOR parse_result(parse_error &&err) noexcept
Definition parse_result.hpp:101
void destroy() noexcept
Definition parse_result.hpp:76
TOML_NODISCARD const_table_iterator cend() const noexcept
Returns an iterator to one-past-the-last key-value pair in the wrapped table.
Definition parse_result.hpp:325
TOML_NODISCARD node_view< node > operator[](const toml::path &path) noexcept
Returns a view of the subnode matching a fully-qualified "TOML path".
Definition parse_result.hpp:401
TOML_NODISCARD parse_error && error() &&noexcept
Returns the internal toml::parse_error (rvalue overload).
Definition parse_result.hpp:239
TOML_NODISCARD const parse_error & error() const &noexcept
Returns the internal toml::parse_error (const lvalue overload).
Definition parse_result.hpp:247
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".
Definition parse_result.hpp:348
bool err_
Definition parse_result.hpp:66
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".
Definition parse_result.hpp:390
TOML_NODISCARD bool succeeded() const noexcept
Returns true if parsing succeeeded.
Definition parse_result.hpp:151
TOML_NODISCARD parse_error & error() &noexcept
Returns the internal toml::parse_error.
Definition parse_result.hpp:231
storage_t storage_
Definition parse_result.hpp:65
TOML_NODISCARD const_table_iterator cbegin() const noexcept
Returns an iterator to the first key-value pair in the wrapped table.
Definition parse_result.hpp:304
A TOML path.
Definition path.hpp:239
A TOML table.
Definition table.hpp:220
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:797
TOML_PURE_INLINE_GETTER const_iterator cbegin() const noexcept
Returns an iterator to the first key-value pair.
Definition table.hpp:811
TOML_PURE_INLINE_GETTER iterator end() noexcept
Returns an iterator to one-past-the-last key-value pair.
Definition table.hpp:818
TOML_PURE_INLINE_GETTER const_iterator cend() const noexcept
Returns an iterator to one-past-the-last key-value pair.
Definition table.hpp:832
#define TOML_ASSERT_ASSUME(expr)
Definition preprocessor.hpp:1190
The root namespace for all toml++ functions and types.
Definition forward_declarations.hpp:199
TOML_NAMESPACE_START
Definition parse_result.hpp:15
TOML_ABI_NAMESPACE_END
Definition parse_result.hpp:494
TOML_NAMESPACE_END
Definition parse_result.hpp:496
#define TOML_NODISCARD_CTOR
Definition preprocessor.hpp:446
#define TOML_NODISCARD
Definition preprocessor.hpp:439
#define TOML_ABI_NAMESPACE_START(name)
Definition preprocessor.hpp:1322
#define TOML_ALWAYS_INLINE
Definition preprocessor.hpp:405
#define TOML_LAUNDER(x)
Definition std_new.hpp:17
Definition parse_result.hpp:56
unsigned char bytes[size]
Definition parse_result.hpp:62
static constexpr size_t align
Definition parse_result.hpp:59
static constexpr size_t size
Definition parse_result.hpp:57
A source document region.
Definition source_region.hpp:167
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:186