NeBuild dev
Loading...
Searching...
No Matches
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 "std_vector.hpp"
9#include "header_start.hpp"
10
12{
14 enum class TOML_CLOSED_ENUM path_component_type : uint8_t
15 {
16 key = 0x1,
17 array_index = 0x2
18 };
19
22 {
24 struct storage_t
25 {
26 static constexpr size_t size =
27 (sizeof(size_t) < sizeof(std::string) ? sizeof(std::string) : sizeof(size_t));
28 static constexpr size_t align =
29 (alignof(size_t) < alignof(std::string) ? alignof(std::string) : alignof(size_t));
30
31 alignas(align) unsigned char bytes[size];
32 };
33 alignas(storage_t::align) mutable storage_t value_storage_;
34
35 path_component_type type_;
36
39 static bool TOML_CALLCONV equal(const path_component&, const path_component&) noexcept;
40
41 template <typename Type>
43 static Type* get_as(storage_t& s) noexcept
44 {
45 return TOML_LAUNDER(reinterpret_cast<Type*>(s.bytes));
46 }
47
48 static void store_key(std::string_view key, storage_t& storage_)
49 {
50 ::new (static_cast<void*>(storage_.bytes)) std::string{ key };
51 }
52
53 static void store_index(size_t index, storage_t& storage_) noexcept
54 {
55 ::new (static_cast<void*>(storage_.bytes)) std::size_t{ index };
56 }
57
58 void destroy() noexcept
59 {
60 if (type_ == path_component_type::key)
61 get_as<std::string>(value_storage_)->~basic_string();
62 }
63
65 size_t& index_ref() noexcept
66 {
67 TOML_ASSERT_ASSUME(type_ == path_component_type::array_index);
68 return *get_as<size_t>(value_storage_);
69 }
70
72 std::string& key_ref() noexcept
73 {
74 TOML_ASSERT_ASSUME(type_ == path_component_type::key);
75 return *get_as<std::string>(value_storage_);
76 }
78
79 public:
84
88 path_component(size_t index) noexcept;
89
93 path_component(std::string_view key);
94
95#if TOML_ENABLE_WINDOWS_COMPAT
96
102 path_component(std::wstring_view key);
103
104#endif
105
110
115
119
123
126 path_component& operator=(size_t new_index) noexcept;
127
130 path_component& operator=(std::string_view new_key);
131
132#if TOML_ENABLE_WINDOWS_COMPAT
133
138 path_component& operator=(std::wstring_view new_key);
139
140#endif
141
144 {
145 destroy();
146 }
147
152
155 size_t index() const noexcept
156 {
157 TOML_ASSERT_ASSUME(type_ == path_component_type::array_index);
158 return *get_as<const size_t>(value_storage_);
159 }
160
163 explicit operator size_t() const noexcept
164 {
165 return index();
166 }
167
169
174
177 const std::string& key() const noexcept
178 {
179 TOML_ASSERT_ASSUME(type_ == path_component_type::key);
180 return *get_as<const std::string>(value_storage_);
181 }
182
185 explicit operator const std::string&() const noexcept
186 {
187 return key();
188 }
189
191
194 path_component_type type() const noexcept
195 {
196 return type_;
197 }
198
201
204 friend bool operator==(const path_component& lhs, const path_component& rhs) noexcept
205 {
206 return equal(lhs, rhs);
207 }
208
211 friend bool operator!=(const path_component& lhs, const path_component& rhs) noexcept
212 {
213 return !equal(lhs, rhs);
214 }
215
217 };
218
239 {
240 private:
242
243 std::vector<path_component> components_;
244
246 void print_to(std::ostream&) const;
247
250 static bool TOML_CALLCONV equal(const path&, const path&) noexcept;
251
253
254 public:
257 path() noexcept = default;
258
262 explicit path(std::string_view);
263
264#if TOML_ENABLE_WINDOWS_COMPAT
265
271 explicit path(std::wstring_view);
272
273#endif
274
276 ~path() noexcept = default;
277
280 path(const path&) = default;
281
284 path(path&&) noexcept = default;
285
288 size_t size() const noexcept
289 {
290 return components_.size();
291 }
292
295 explicit operator bool() const noexcept
296 {
297 return !components_.empty();
298 }
299
302 bool empty() const noexcept
303 {
304 return components_.empty();
305 }
306
309 path_component& operator[](size_t index) noexcept
310 {
311 TOML_ASSERT(index < size());
312 return components_[index];
313 }
314
317 const path_component& operator[](size_t index) const noexcept
318 {
319 TOML_ASSERT(index < size());
320 return components_[index];
321 }
322
325
327 path& operator=(const path&) = default;
328
330 path& operator=(path&&) noexcept = default;
331
334 path& operator=(std::string_view);
335
336#if TOML_ENABLE_WINDOWS_COMPAT
337
342 path& operator=(std::wstring_view);
343
344#endif
345
348 path& assign(const path& p)
349 {
350 return *this = p;
351 }
352
355 path& assign(path&& p) noexcept
356 {
357 return *this = std::move(p);
358 }
359
362 path& assign(std::string_view str)
363 {
364 return *this = str;
365 }
366
367#if TOML_ENABLE_WINDOWS_COMPAT
368
373 path& assign(std::wstring_view str)
374 {
375 return *this = str;
376 }
377
378#endif
379
381
384
388
392
395 path& operator+=(std::string_view);
396
397#if TOML_ENABLE_WINDOWS_COMPAT
398
403 path& operator+=(std::wstring_view);
404
405#endif
406
409 path& append(const path& p)
410 {
411 return *this += p;
412 }
413
417 {
418 return *this += std::move(p);
419 }
420
423 path& append(std::string_view str)
424 {
425 return *this += str;
426 }
427
428#if TOML_ENABLE_WINDOWS_COMPAT
429
434 path& append(std::wstring_view str)
435 {
436 return *this += str;
437 }
438
439#endif
440
442
445
448 path& prepend(const path&);
449
453
456 path& prepend(std::string_view);
457
458#if TOML_ENABLE_WINDOWS_COMPAT
459
464 path& prepend(std::wstring_view);
465
466#endif
467
469
472
475 friend path operator+(const path& lhs, const path& rhs)
476 {
477 path result = lhs;
478 result += rhs;
479 return result;
480 }
481
484 friend path operator+(const path& lhs, std::string_view rhs)
485 {
486 path result = lhs;
487 result += rhs;
488 return result;
489 }
490
493 friend path operator+(std::string_view lhs, const path& rhs)
494 {
495 path result = rhs;
496 result.prepend(lhs);
497 return result;
498 }
499
500#if TOML_ENABLE_WINDOWS_COMPAT
501
506 friend path operator+(const path& lhs, std::wstring_view rhs)
507 {
508 path result = lhs;
509 result += rhs;
510 return result;
511 }
512
517 friend path operator+(std::wstring_view lhs, const path& rhs)
518 {
519 path result = rhs;
520 result.prepend(lhs);
521 return result;
522 }
523
524#endif
525
527
530
533 friend std::ostream& operator<<(std::ostream& os, const path& rhs)
534 {
535 rhs.print_to(os);
536 return os;
537 }
538
542 std::string str() const;
543
547 explicit operator std::string() const
548 {
549 return str();
550 }
551
552#if TOML_ENABLE_WINDOWS_COMPAT
553
559 std::wstring wide_str() const;
560
566 explicit operator std::wstring() const
567 {
568 return wide_str();
569 }
570
571#endif
572
574
577
580 friend bool operator==(const path& lhs, const path& rhs) noexcept
581 {
582 return equal(lhs, rhs);
583 }
584
587 friend bool operator!=(const path& lhs, const path& rhs) noexcept
588 {
589 return !equal(lhs, rhs);
590 }
591
595 friend bool operator==(const path& lhs, std::string_view rhs)
596 {
597 return lhs == path{ rhs };
598 }
599
603 friend bool operator==(std::string_view lhs, const path& rhs)
604 {
605 return rhs == lhs;
606 }
607
611 friend bool operator!=(const path& lhs, std::string_view rhs)
612 {
613 return lhs != path{ rhs };
614 }
615
619 friend bool operator!=(std::string_view lhs, const path& rhs)
620 {
621 return rhs != lhs;
622 }
623
624#if TOML_ENABLE_WINDOWS_COMPAT
625
631 friend bool operator==(const path& lhs, std::wstring_view rhs)
632 {
633 return lhs == path{ rhs };
634 }
635
641 friend bool operator==(std::wstring_view lhs, const path& rhs)
642 {
643 return rhs == lhs;
644 }
645
651 friend bool operator!=(const path& lhs, std::wstring_view rhs)
652 {
653 return lhs != path{ rhs };
654 }
655
661 friend bool operator!=(std::wstring_view lhs, const path& rhs)
662 {
663 return rhs != lhs;
664 }
665
666#endif // TOML_ENABLE_WINDOWS_COMPAT
667
669
672
675 using iterator = std::vector<path_component>::iterator;
676
679 using const_iterator = std::vector<path_component>::const_iterator;
680
684 iterator begin() noexcept
685 {
686 return components_.begin();
687 }
688
692 iterator end() noexcept
693 {
694 return components_.end();
695 }
696
700 const_iterator begin() const noexcept
701 {
702 return components_.begin();
703 }
704
708 const_iterator end() const noexcept
709 {
710 return components_.end();
711 }
712
716 const_iterator cbegin() const noexcept
717 {
718 return components_.begin();
719 }
720
724 const_iterator cend() const noexcept
725 {
726 return components_.end();
727 }
728
730
733
736 void clear() noexcept;
737
740 path& truncate(size_t n);
741
745 path truncated(size_t n) const;
746
750 path parent() const;
751
755 path leaf(size_t n = 1) const;
756
761 path subpath(const_iterator start, const_iterator end) const;
762
767 path subpath(size_t start, size_t length) const;
768
770 };
771
772 inline namespace literals
773 {
793 path operator"" _tpath(const char* str, size_t len)
794 {
795 return path(std::string_view{ str, len });
796 }
797 }
798
840 node_view<node> TOML_CALLCONV at_path(node & root, const toml::path& path) noexcept;
841
847 node_view<const node> TOML_CALLCONV at_path(const node& root, const toml::path& path) noexcept;
848}
850
851#include "header_end.hpp"
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:22
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:177
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:204
~path_component() noexcept
Destructor.
Definition path.hpp:143
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:211
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:155
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:194
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:239
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:619
TOML_NODISCARD friend path operator+(std::wstring_view lhs, const path &rhs)
Concatenates two paths.
Definition path.hpp:517
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:587
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:611
std::vector< path_component >::const_iterator const_iterator
Definition path.hpp:679
TOML_PURE_INLINE_GETTER friend bool operator==(const path &lhs, const path &rhs) noexcept
Returns whether two paths are the same.
Definition path.hpp:580
TOML_PURE_INLINE_GETTER const path_component & operator[](size_t index) const noexcept
Fetch a path component by index (const overload).
Definition path.hpp:317
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:533
TOML_PURE_INLINE_GETTER iterator end() noexcept
Returns an iterator to one-past-the-last component in the path.
Definition path.hpp:692
TOML_ALWAYS_INLINE path & append(std::string_view str)
Parses a path and appends it onto the end of this one.
Definition path.hpp:423
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:675
TOML_NODISCARD friend path operator+(const path &lhs, std::string_view rhs)
Concatenates two paths.
Definition path.hpp:484
TOML_PURE_INLINE_GETTER size_t size() const noexcept
Returns the number of components in the path.
Definition path.hpp:288
TOML_NODISCARD friend path operator+(std::string_view lhs, const path &rhs)
Concatenates two paths.
Definition path.hpp:493
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:661
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:434
TOML_PURE_INLINE_GETTER iterator begin() noexcept
Returns an iterator to the first component in the path.
Definition path.hpp:684
TOML_ALWAYS_INLINE path & assign(std::wstring_view str)
Replaces the contents of the path object by a new path.
Definition path.hpp:373
TOML_PURE_INLINE_GETTER const_iterator begin() const noexcept
Returns a const iterator to the first component in the path.
Definition path.hpp:700
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:506
TOML_PURE_INLINE_GETTER path_component & operator[](size_t index) noexcept
Fetch a path component by index.
Definition path.hpp:309
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:409
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:708
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:603
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:716
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:651
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:302
TOML_ALWAYS_INLINE path & assign(const path &p)
Replaces the contents of the path with that of another.
Definition path.hpp:348
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:724
TOML_NODISCARD friend path operator+(const path &lhs, const path &rhs)
Concatenates two paths.
Definition path.hpp:475
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:631
TOML_ALWAYS_INLINE path & assign(path &&p) noexcept
Replaces the contents of the path with that of another.
Definition path.hpp:355
TOML_ALWAYS_INLINE path & append(path &&p)
Appends another path onto the end of this one.
Definition path.hpp:416
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:641
TOML_ALWAYS_INLINE path & assign(std::string_view str)
Replaces the contents of the path object by a new path.
Definition path.hpp:362
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:595
~path() noexcept=default
Default destructor.
#define TOML_CALLCONV
Calling convention to apply to exported free/static functions. \detail Not defined by default (let th...
Definition preprocessor.hpp:1134
#define TOML_EXPORTED_CLASS
An 'export' annotation to add to classes. \detail Not defined by default.
Definition preprocessor.hpp:979
#define TOML_EXPORTED_STATIC_FUNCTION
An 'export' annotation to add to static class member functions. \detail Not defined by default.
Definition preprocessor.hpp:985
#define TOML_ASSERT(expr)
Sets the assert function used by the library. \detail Defaults to the standard C assert().
Definition preprocessor.hpp:1185
#define TOML_ASSERT_ASSUME(expr)
Definition preprocessor.hpp:1190
#define TOML_EXPORTED_FREE_FUNCTION
An 'export' annotation to add to free functions. \detail Not defined by default.
Definition preprocessor.hpp:988
#define TOML_EXPORTED_MEMBER_FUNCTION
An 'export' annotation to add to non-static class member functions. \detail Not defined by default.
Definition preprocessor.hpp:982
Definition json.h:24463
Definition json.h:5363
TOML_NAMESPACE_START
Definition path.hpp:12
TOML_NAMESPACE_END
Definition path.hpp:849
#define TOML_NODISCARD_CTOR
Definition preprocessor.hpp:446
#define TOML_CLOSED_ENUM
Definition preprocessor.hpp:557
#define TOML_NODISCARD
Definition preprocessor.hpp:439
#define TOML_PURE_GETTER
Definition preprocessor.hpp:474
#define TOML_PURE_INLINE_GETTER
Definition preprocessor.hpp:479
#define TOML_ALWAYS_INLINE
Definition preprocessor.hpp:405
#define TOML_LAUNDER(x)
Definition std_new.hpp:17