Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
path.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
8#include "header_start.hpp"
9#include "std_vector.hpp"
10
13 enum class TOML_CLOSED_ENUM path_component_type : uint8_t { key = 0x1, array_index = 0x2 };
14
18 struct storage_t {
19 static constexpr size_t size =
20 (sizeof(size_t) < sizeof(std::string) ? sizeof(std::string) : sizeof(size_t));
21 static constexpr size_t align =
22 (alignof(size_t) < alignof(std::string) ? alignof(std::string) : alignof(size_t));
23
24 alignas(align) unsigned char bytes[size];
25 };
26 alignas(storage_t::align) mutable storage_t value_storage_;
27
28 path_component_type type_;
29
32 static bool TOML_CALLCONV equal(const path_component&, const path_component&) noexcept;
33
34 template <typename Type>
35 TOML_PURE_INLINE_GETTER static Type* get_as(storage_t& s) noexcept {
36 return TOML_LAUNDER(reinterpret_cast<Type*>(s.bytes));
37 }
38
39 static void store_key(std::string_view key, storage_t& storage_) {
40 ::new (static_cast<void*>(storage_.bytes)) std::string{key};
41 }
42
43 static void store_index(size_t index, storage_t& storage_) noexcept {
44 ::new (static_cast<void*>(storage_.bytes)) std::size_t{index};
45 }
46
47 void destroy() noexcept {
48 if (type_ == path_component_type::key) get_as<std::string>(value_storage_)->~basic_string();
49 }
50
52 size_t& index_ref() noexcept {
53 TOML_ASSERT_ASSUME(type_ == path_component_type::array_index);
54 return *get_as<size_t>(value_storage_);
55 }
56
58 std::string& key_ref() noexcept {
59 TOML_ASSERT_ASSUME(type_ == path_component_type::key);
60 return *get_as<std::string>(value_storage_);
61 }
63
64 public:
69
73 path_component(size_t index) noexcept;
74
78 path_component(std::string_view key);
79
80#if TOML_ENABLE_WINDOWS_COMPAT
81
88 path_component(std::wstring_view key);
89
90#endif
91
96
101
105
109
112 path_component& operator=(size_t new_index) noexcept;
113
116 path_component& operator=(std::string_view new_key);
117
118#if TOML_ENABLE_WINDOWS_COMPAT
119
124 path_component& operator=(std::wstring_view new_key);
125
126#endif
127
129 ~path_component() noexcept { destroy(); }
130
135
138 size_t index() const noexcept {
139 TOML_ASSERT_ASSUME(type_ == path_component_type::array_index);
140 return *get_as<const size_t>(value_storage_);
141 }
142
145 explicit operator size_t() const noexcept { return index(); }
146
148
153
156 const std::string& key() const noexcept {
157 TOML_ASSERT_ASSUME(type_ == path_component_type::key);
158 return *get_as<const std::string>(value_storage_);
159 }
160
163 explicit operator const std::string&() const noexcept { return key(); }
164
166
170 path_component_type type() const noexcept { return type_; }
171
174
177 friend bool operator==(const path_component& lhs, const path_component& rhs) noexcept {
178 return equal(lhs, rhs);
179 }
180
183 friend bool operator!=(const path_component& lhs, const path_component& rhs) noexcept {
184 return !equal(lhs, rhs);
185 }
186
188 };
189
210 private:
212
213 std::vector<path_component> components_;
214
216 void print_to(std::ostream&) const;
217
220 static bool TOML_CALLCONV equal(const path&, const path&) noexcept;
221
223
224 public:
227 path() noexcept = default;
228
232 explicit path(std::string_view);
233
234#if TOML_ENABLE_WINDOWS_COMPAT
235
242 explicit path(std::wstring_view);
243
244#endif
245
247 ~path() noexcept = default;
248
251 path(const path&) = default;
252
255 path(path&&) noexcept = default;
256
259 size_t size() const noexcept { return components_.size(); }
260
263 explicit operator bool() const noexcept { return !components_.empty(); }
264
267 bool empty() const noexcept { return components_.empty(); }
268
271 path_component& operator[](size_t index) noexcept {
273 return components_[index];
274 }
275
278 const path_component& operator[](size_t index) const noexcept {
280 return components_[index];
281 }
282
285
287 path& operator=(const path&) = default;
288
290 path& operator=(path&&) noexcept = default;
291
294 path& operator=(std::string_view);
295
296#if TOML_ENABLE_WINDOWS_COMPAT
297
302 path& operator=(std::wstring_view);
303
304#endif
305
308 path& assign(const path& p) { return *this = p; }
309
312 path& assign(path&& p) noexcept { return *this = std::move(p); }
313
316 path& assign(std::string_view str) { return *this = str; }
317
318#if TOML_ENABLE_WINDOWS_COMPAT
319
324 path& assign(std::wstring_view str) { return *this = str; }
325
326#endif
327
329
332
336
340
343 path& operator+=(std::string_view);
344
345#if TOML_ENABLE_WINDOWS_COMPAT
346
351 path& operator+=(std::wstring_view);
352
353#endif
354
357 path& append(const path& p) { return *this += p; }
358
361 path& append(path&& p) { return *this += std::move(p); }
362
365 path& append(std::string_view str) { return *this += str; }
366
367#if TOML_ENABLE_WINDOWS_COMPAT
368
373 path& append(std::wstring_view str) { return *this += str; }
374
375#endif
376
378
381
384 path& prepend(const path&);
385
389
392 path& prepend(std::string_view);
393
394#if TOML_ENABLE_WINDOWS_COMPAT
395
400 path& prepend(std::wstring_view);
401
402#endif
403
405
408
411 friend path operator+(const path& lhs, const path& rhs) {
412 path result = lhs;
413 result += rhs;
414 return result;
415 }
416
419 friend path operator+(const path& lhs, std::string_view rhs) {
420 path result = lhs;
421 result += rhs;
422 return result;
423 }
424
427 friend path operator+(std::string_view lhs, const path& rhs) {
428 path result = rhs;
430 return result;
431 }
432
433#if TOML_ENABLE_WINDOWS_COMPAT
434
439 friend path operator+(const path& lhs, std::wstring_view rhs) {
440 path result = lhs;
441 result += rhs;
442 return result;
443 }
444
449 friend path operator+(std::wstring_view lhs, const path& rhs) {
450 path result = rhs;
452 return result;
453 }
454
455#endif
456
458
461
464 friend std::ostream& operator<<(std::ostream& os, const path& rhs) {
465 rhs.print_to(os);
466 return os;
467 }
468
472 std::string str() const;
473
477 explicit operator std::string() const { return str(); }
478
479#if TOML_ENABLE_WINDOWS_COMPAT
480
486 std::wstring wide_str() const;
487
493 explicit operator std::wstring() const { return wide_str(); }
494
495#endif
496
498
501
504 friend bool operator==(const path& lhs, const path& rhs) noexcept { return equal(lhs, rhs); }
505
508 friend bool operator!=(const path& lhs, const path& rhs) noexcept { return !equal(lhs, rhs); }
509
513 friend bool operator==(const path& lhs, std::string_view rhs) { return lhs == path{rhs}; }
514
518 friend bool operator==(std::string_view lhs, const path& rhs) { return rhs == lhs; }
519
523 friend bool operator!=(const path& lhs, std::string_view rhs) { return lhs != path{rhs}; }
524
528 friend bool operator!=(std::string_view lhs, const path& rhs) { return rhs != lhs; }
529
530#if TOML_ENABLE_WINDOWS_COMPAT
531
537 friend bool operator==(const path& lhs, std::wstring_view rhs) { return lhs == path{rhs}; }
538
544 friend bool operator==(std::wstring_view lhs, const path& rhs) { return rhs == lhs; }
545
551 friend bool operator!=(const path& lhs, std::wstring_view rhs) { return lhs != path{rhs}; }
552
558 friend bool operator!=(std::wstring_view lhs, const path& rhs) { return rhs != lhs; }
559
560#endif // TOML_ENABLE_WINDOWS_COMPAT
561
563
566
569 using iterator = std::vector<path_component>::iterator;
570
573 using const_iterator = std::vector<path_component>::const_iterator;
574
578 iterator begin() noexcept { return components_.begin(); }
579
583 iterator end() noexcept { return components_.end(); }
584
588 const_iterator begin() const noexcept { return components_.begin(); }
589
593 const_iterator end() const noexcept { return components_.end(); }
594
598 const_iterator cbegin() const noexcept { return components_.begin(); }
599
603 const_iterator cend() const noexcept { return components_.end(); }
604
606
609
612 void clear() noexcept;
613
616 path& truncate(size_t n);
617
621 path truncated(size_t n) const;
622
626 path parent() const;
627
631 path leaf(size_t n = 1) const;
632
638
643 path subpath(size_t start, size_t length) const;
644
646 };
647
648 inline namespace literals {
668 path operator"" _tpath(const char* str, size_t len) {
669 return path(std::string_view{str, len});
670 }
671 } // namespace literals
672
715 node_view<node> TOML_CALLCONV at_path(node & root, const toml::path& path) noexcept;
716
722 node_view<const node> TOML_CALLCONV at_path(const node& root, const toml::path& path) noexcept;
723}
725
726#include "header_end.hpp"
#define rhs
Definition 17.names.cpp:536
#define start
Definition 17.names.cpp:547
#define os
Definition 17.names.cpp:511
#define index
Definition 17.names.cpp:482
#define lhs
Definition 17.names.cpp:489
#define s
#define len
Definition 17.names.cpp:488
#define n
Definition 17.names.cpp:130
#define p
Definition 17.names.cpp:132
volatile int result
bool equal(const T &x, const U &y)
TOML_NODISCARD TOML_EXPORTED_FREE_FUNCTION node_view< const node > TOML_CALLCONV at_path(const node &root, std::string_view path) noexcept
Returns a const view of the node matching a fully-qualified "TOML path".
Represents a single component of a complete 'TOML-path': either a key or an array index.
Definition path.hpp:16
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION path_component()
Default constructor (creates an empty key).
TOML_EXPORTED_MEMBER_FUNCTION path_component & operator=(const path_component &rhs)
Copy-assignment operator.
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION path_component(const path_component &pc)
Copy constructor.
TOML_PURE_GETTER const std::string & key() const noexcept
Returns the key string.
Definition path.hpp:156
TOML_PURE_INLINE_GETTER friend bool operator==(const path_component &lhs, const path_component &rhs) noexcept
Returns true if two path components represent the same key or array index.
Definition path.hpp:177
~path_component() noexcept
Destructor.
Definition path.hpp:129
TOML_PURE_INLINE_GETTER friend bool operator!=(const path_component &lhs, const path_component &rhs) noexcept
Returns true if two path components do not represent the same key or array index.
Definition path.hpp:183
TOML_EXPORTED_MEMBER_FUNCTION path_component & operator=(std::string_view new_key)
Assigns a path key to this path component.
TOML_EXPORTED_MEMBER_FUNCTION path_component & operator=(size_t new_index) noexcept
Assigns an array index to this path component.
TOML_PURE_GETTER size_t index() const noexcept
Returns the array index (const lvalue overload).
Definition path.hpp:138
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION path_component(size_t index) noexcept
Constructor for a path component that is an array index.
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION path_component(std::string_view key)
Constructor for a path component that is a key string.
TOML_PURE_INLINE_GETTER path_component_type type() const noexcept
Retrieve the type of this path component, either path_component::key or path_component::array_index.
Definition path.hpp:170
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION path_component(std::wstring_view key)
Constructor for a path component that is a key string.
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION path_component(path_component &&pc) noexcept
Move constructor.
TOML_EXPORTED_MEMBER_FUNCTION path_component & operator=(path_component &&rhs) noexcept
Move-assignment operator.
TOML_EXPORTED_MEMBER_FUNCTION path_component & operator=(std::wstring_view new_key)
Assigns a path key to this path component.
A TOML path.
Definition path.hpp:209
TOML_NODISCARD TOML_ALWAYS_INLINE friend bool operator!=(std::string_view lhs, const path &rhs)
Returns whether two paths are not the same.
Definition path.hpp:528
TOML_NODISCARD friend path operator+(std::wstring_view lhs, const path &rhs)
Concatenates two paths.
Definition path.hpp:449
TOML_NODISCARD TOML_EXPORTED_MEMBER_FUNCTION std::wstring wide_str() const
Returns a string representation of this path.
TOML_EXPORTED_MEMBER_FUNCTION void clear() noexcept
Erases the contents of the path.
TOML_PURE_INLINE_GETTER friend bool operator!=(const path &lhs, const path &rhs) noexcept
Returns whether two paths are not the same.
Definition path.hpp:508
TOML_NODISCARD TOML_ALWAYS_INLINE friend bool operator!=(const path &lhs, std::string_view rhs)
Returns whether two paths are not the same.
Definition path.hpp:523
std::vector< path_component >::const_iterator const_iterator
Definition path.hpp:573
TOML_PURE_INLINE_GETTER friend bool operator==(const path &lhs, const path &rhs) noexcept
Returns whether two paths are the same.
Definition path.hpp:504
TOML_PURE_INLINE_GETTER const path_component & operator[](size_t index) const noexcept
Fetch a path component by index (const overload).
Definition path.hpp:278
TOML_ALWAYS_INLINE friend std::ostream & operator<<(std::ostream &os, const path &rhs)
Prints the string representation of a #toml::path out to a stream.
Definition path.hpp:464
TOML_PURE_INLINE_GETTER iterator end() noexcept
Returns an iterator to one-past-the-last component in the path.
Definition path.hpp:583
TOML_ALWAYS_INLINE path & append(std::string_view str)
Parses a path and appends it onto the end of this one.
Definition path.hpp:365
TOML_EXPORTED_MEMBER_FUNCTION path & prepend(path &&)
Prepends another path onto the beginning of this one.
std::vector< path_component >::iterator iterator
Definition path.hpp:569
TOML_NODISCARD friend path operator+(const path &lhs, std::string_view rhs)
Concatenates two paths.
Definition path.hpp:419
TOML_PURE_INLINE_GETTER size_t size() const noexcept
Returns the number of components in the path.
Definition path.hpp:259
TOML_NODISCARD friend path operator+(std::string_view lhs, const path &rhs)
Concatenates two paths.
Definition path.hpp:427
TOML_NODISCARD TOML_EXPORTED_MEMBER_FUNCTION std::string str() const
Returns a string representation of this path.
TOML_NODISCARD TOML_ALWAYS_INLINE friend bool operator!=(std::wstring_view lhs, const path &rhs)
Returns whether two paths are not the same.
Definition path.hpp:558
TOML_EXPORTED_MEMBER_FUNCTION path & operator+=(path &&)
Appends another path onto the end of this one.
TOML_NODISCARD_CTOR path() noexcept=default
Default constructor.
TOML_ALWAYS_INLINE path & append(std::wstring_view str)
Parses a path and appends it onto the end of this one.
Definition path.hpp:373
TOML_PURE_INLINE_GETTER iterator begin() noexcept
Returns an iterator to the first component in the path.
Definition path.hpp:578
TOML_ALWAYS_INLINE path & assign(std::wstring_view str)
Replaces the contents of the path object by a new path.
Definition path.hpp:324
TOML_PURE_INLINE_GETTER const_iterator begin() const noexcept
Returns a const iterator to the first component in the path.
Definition path.hpp:588
TOML_EXPORTED_MEMBER_FUNCTION path & operator+=(std::wstring_view)
Parses a path and appends it onto the end of this one.
TOML_EXPORTED_MEMBER_FUNCTION path & prepend(std::wstring_view)
Parses a path and prepends it onto the beginning of this one.
path & operator=(path &&) noexcept=default
Move-assignment operator.
TOML_NODISCARD friend path operator+(const path &lhs, std::wstring_view rhs)
Concatenates two paths.
Definition path.hpp:439
TOML_PURE_INLINE_GETTER path_component & operator[](size_t index) noexcept
Fetch a path component by index.
Definition path.hpp:271
TOML_EXPORTED_MEMBER_FUNCTION path & prepend(std::string_view)
Parses a path and prepends it onto the beginning of this one.
TOML_ALWAYS_INLINE path & append(const path &p)
Appends another path onto the end of this one.
Definition path.hpp:357
TOML_EXPORTED_MEMBER_FUNCTION path & prepend(const path &)
Prepends another path onto the beginning of this one.
TOML_PURE_INLINE_GETTER const_iterator end() const noexcept
Returns a const iterator to one-past-the-last component in the path.
Definition path.hpp:593
TOML_NODISCARD TOML_ALWAYS_INLINE friend bool operator==(std::string_view lhs, const path &rhs)
Returns whether two paths are the same.
Definition path.hpp:518
TOML_EXPORTED_MEMBER_FUNCTION path & operator=(std::wstring_view)
Replaces the contents of the path by parsing from a string.
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION path(std::wstring_view)
Construct a path by parsing from a string.
TOML_PURE_INLINE_GETTER const_iterator cbegin() const noexcept
Returns a const iterator to the first component in the path.
Definition path.hpp:598
TOML_NODISCARD TOML_ALWAYS_INLINE friend bool operator!=(const path &lhs, std::wstring_view rhs)
Returns whether two paths are not the same.
Definition path.hpp:551
TOML_EXPORTED_MEMBER_FUNCTION path & operator+=(const path &)
Appends another path onto the end of this one.
TOML_PURE_INLINE_GETTER bool empty() const noexcept
Whether (true) or not (false) the path is empty.
Definition path.hpp:267
TOML_ALWAYS_INLINE path & assign(const path &p)
Replaces the contents of the path with that of another.
Definition path.hpp:308
TOML_PURE_INLINE_GETTER const_iterator cend() const noexcept
Returns a const iterator to one-past-the-last component in the path.
Definition path.hpp:603
TOML_NODISCARD friend path operator+(const path &lhs, const path &rhs)
Concatenates two paths.
Definition path.hpp:411
TOML_EXPORTED_MEMBER_FUNCTION path & operator+=(std::string_view)
Parses a path and appends it onto the end of this one.
TOML_NODISCARD TOML_ALWAYS_INLINE friend bool operator==(const path &lhs, std::wstring_view rhs)
Returns whether two paths are the same.
Definition path.hpp:537
TOML_ALWAYS_INLINE path & assign(path &&p) noexcept
Replaces the contents of the path with that of another.
Definition path.hpp:312
TOML_ALWAYS_INLINE path & append(path &&p)
Appends another path onto the end of this one.
Definition path.hpp:361
path & operator=(const path &)=default
Copy-assignment operator.
TOML_NODISCARD TOML_ALWAYS_INLINE friend bool operator==(std::wstring_view lhs, const path &rhs)
Returns whether two paths are the same.
Definition path.hpp:544
TOML_ALWAYS_INLINE path & assign(std::string_view str)
Replaces the contents of the path object by a new path.
Definition path.hpp:316
TOML_NODISCARD TOML_ALWAYS_INLINE friend bool operator==(const path &lhs, std::string_view rhs)
Returns whether two paths are the same.
Definition path.hpp:513
~path() noexcept=default
Default destructor.
const char * str
Definition exec.cpp:170
#define TOML_CALLCONV
Calling convention to apply to exported free/static functions. \detail Not defined by default (let th...
#define TOML_EXPORTED_CLASS
An 'export' annotation to add to classes. \detail Not defined by default.
#define TOML_EXPORTED_STATIC_FUNCTION
An 'export' annotation to add to static class member functions. \detail Not defined by default.
#define TOML_ASSERT(expr)
Sets the assert function used by the library. \detail Defaults to the standard C assert().
#define TOML_ASSERT_ASSUME(expr)
#define TOML_EXPORTED_FREE_FUNCTION
An 'export' annotation to add to free functions. \detail Not defined by default.
#define TOML_EXPORTED_MEMBER_FUNCTION
An 'export' annotation to add to non-static class member functions. \detail Not defined by default.
Definition json.h:5363
TOML_NAMESPACE_START
Definition path.hpp:11
TOML_NAMESPACE_END
Definition path.hpp:724
#define TOML_NODISCARD_CTOR
#define TOML_CLOSED_ENUM
#define TOML_NODISCARD
#define TOML_PURE_GETTER
#define TOML_PURE_INLINE_GETTER
#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