NeBuild dev
Loading...
Searching...
No Matches
node_view.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 "std_vector.hpp"
9#include "print_to_stream.hpp"
10#include "node.hpp"
11#include "header_start.hpp"
13
15{
59 template <typename ViewedType>
60 class TOML_TRIVIAL_ABI node_view
61 {
62 static_assert(impl::is_one_of<ViewedType, toml::node, const toml::node>,
63 "A toml::node_view<> must wrap toml::node or const toml::node.");
64
65 public:
67 using viewed_type = ViewedType;
68
69 private:
70 template <typename T>
71 friend class node_view;
72
73 mutable viewed_type* node_ = nullptr;
74
75 public:
78 node_view() noexcept = default;
79
82 explicit node_view(viewed_type* node) noexcept //
83 : node_{ node }
84 {}
85
88 explicit node_view(viewed_type& node) noexcept //
89 : node_{ &node }
90 {}
91
94 node_view(const node_view&) noexcept = default;
95
98 node_view(node_view&&) noexcept = default;
99
101 node_view& operator=(const node_view&) & noexcept = default;
102
104 node_view& operator=(node_view&&) & noexcept = default;
105
108 explicit operator bool() const noexcept
109 {
110 return node_ != nullptr;
111 }
112
115 viewed_type* node() const noexcept
116 {
117 return node_;
118 }
119
122
125 node_type type() const noexcept
126 {
127 return node_ ? node_->type() : node_type::none;
128 }
129
132 bool is_table() const noexcept
133 {
134 return node_ && node_->is_table();
135 }
136
139 bool is_array() const noexcept
140 {
141 return node_ && node_->is_array();
142 }
143
146 bool is_value() const noexcept
147 {
148 return node_ && node_->is_value();
149 }
150
153 bool is_string() const noexcept
154 {
155 return node_ && node_->is_string();
156 }
157
160 bool is_integer() const noexcept
161 {
162 return node_ && node_->is_integer();
163 }
164
167 bool is_floating_point() const noexcept
168 {
169 return node_ && node_->is_floating_point();
170 }
171
174 bool is_number() const noexcept
175 {
176 return node_ && node_->is_number();
177 }
178
181 bool is_boolean() const noexcept
182 {
183 return node_ && node_->is_boolean();
184 }
185
188 bool is_date() const noexcept
189 {
190 return node_ && node_->is_date();
191 }
192
195 bool is_time() const noexcept
196 {
197 return node_ && node_->is_time();
198 }
199
202 bool is_date_time() const noexcept
203 {
204 return node_ && node_->is_date_time();
205 }
206
209 bool is_array_of_tables() const noexcept
210 {
211 return node_ && node_->is_array_of_tables();
212 }
213
221 template <typename T>
223 bool is() const noexcept
224 {
225 return node_ ? node_->template is<impl::unwrap_node<impl::remove_cvref<T>>>() : false;
226 }
227
258 bool is_homogeneous(node_type ntype, viewed_type*& first_nonmatch) const noexcept
259 {
260 if (!node_)
261 {
262 first_nonmatch = {};
263 return false;
264 }
265 return node_->is_homogeneous(ntype, first_nonmatch);
266 }
267
294 bool is_homogeneous(node_type ntype) const noexcept
295 {
296 return node_ ? node_->is_homogeneous(ntype) : false;
297 }
298
324 template <typename ElemType = void>
326 bool is_homogeneous() const noexcept
327 {
328 return node_ ? node_->template is_homogeneous<impl::unwrap_node<impl::remove_cvref<ElemType>>>() : false;
329 }
330
332
335
343 template <typename T>
345 auto* as() const noexcept
346 {
347 return node_ ? node_->template as<T>() : nullptr;
348 }
349
352 auto* as_table() const noexcept
353 {
354 return as<table>();
355 }
356
359 auto* as_array() const noexcept
360 {
361 return as<array>();
362 }
363
366 auto* as_string() const noexcept
367 {
368 return as<std::string>();
369 }
370
373 auto* as_integer() const noexcept
374 {
375 return as<int64_t>();
376 }
377
380 auto* as_floating_point() const noexcept
381 {
382 return as<double>();
383 }
384
387 auto* as_boolean() const noexcept
388 {
389 return as<bool>();
390 }
391
394 auto* as_date() const noexcept
395 {
396 return as<date>();
397 }
398
401 auto* as_time() const noexcept
402 {
403 return as<time>();
404 }
405
408 auto* as_date_time() const noexcept
409 {
410 return as<date_time>();
411 }
412
414
417
430 template <typename T>
432 optional<T> value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>)
433 {
434 if (node_)
435 return node_->template value_exact<T>();
436 return {};
437 }
438
458 template <typename T>
460 optional<T> value() const noexcept(impl::value_retrieval_is_nothrow<T>)
461 {
462 if (node_)
463 return node_->template value<T>();
464 return {};
465 }
466
484 template <typename T>
486 auto value_or(T&& default_value) const noexcept(impl::value_retrieval_is_nothrow<T>)
487 {
488 using namespace ::toml::impl;
489
490 static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
491 "Retrieving values as wide-character strings is only "
492 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
493
494 if constexpr (is_wide_string<T>)
495 {
496#if TOML_ENABLE_WINDOWS_COMPAT
497
498 if (node_)
499 return node_->value_or(static_cast<T&&>(default_value));
500 return std::wstring{ static_cast<T&&>(default_value) };
501
502#else
503
504 static_assert(impl::always_false<T>, "Evaluated unreachable branch!");
505
506#endif
507 }
508 else
509 {
510 using value_type =
511 std::conditional_t<std::is_pointer_v<std::decay_t<T>>,
512 std::add_pointer_t<std::add_const_t<std::remove_pointer_t<std::decay_t<T>>>>,
513 std::decay_t<T>>;
514
515 if (node_)
516 return node_->value_or(static_cast<T&&>(default_value));
517 if constexpr (std::is_pointer_v<value_type>)
518 return value_type{ default_value };
519 else
520 return static_cast<T&&>(default_value);
521 }
522 }
523
554 template <typename T>
556 decltype(auto) ref() const noexcept
557 {
558 TOML_ASSERT_ASSUME(node_ && "toml::node_view::ref() called on a node_view that did not reference a node");
559 return node_->template ref<T>();
560 }
561
563
566
567 private:
569 template <typename Func>
570 static constexpr bool visit_is_nothrow = noexcept(std::declval<viewed_type*>()->visit(std::declval<Func>()));
572
573 public:
579 template <typename Func>
580 decltype(auto) visit(Func&& visitor) const noexcept(visit_is_nothrow<Func&&>)
581 {
582 using return_type = decltype(node_->visit(static_cast<Func&&>(visitor)));
583 if (node_)
584 return node_->visit(static_cast<Func&&>(visitor));
585 if constexpr (!std::is_void_v<return_type>)
586 return return_type{};
587 }
588
590
593
594 public:
596 template <typename T>
598 friend bool operator==(const node_view& lhs, const node_view<T>& rhs) noexcept
599 {
600 return impl::node_deep_equality(lhs.node_, rhs.node_);
601 }
602
604 template <typename T>
606 friend bool operator!=(const node_view& lhs, const node_view<T>& rhs) noexcept
607 {
608 return !impl::node_deep_equality(lhs.node_, rhs.node_);
609 }
610
613 friend bool operator==(const node_view& lhs, const table& rhs) noexcept
614 {
615 if (lhs.node_ == &rhs)
616 return true;
617 const auto tbl = lhs.as<table>();
618 return tbl && *tbl == rhs;
619 }
620 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const table&, );
621
624 friend bool operator==(const node_view& lhs, const array& rhs) noexcept
625 {
626 if (lhs.node_ == &rhs)
627 return true;
628 const auto arr = lhs.as<array>();
629 return arr && *arr == rhs;
630 }
631 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const array&, );
632
634 template <typename T>
636 friend bool operator==(const node_view& lhs, const toml::value<T>& rhs) noexcept
637 {
638 if (lhs.node_ == &rhs)
639 return true;
640 const auto val = lhs.as<T>();
641 return val && *val == rhs;
642 }
643 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const toml::value<T>&, template <typename T>);
644
646 TOML_CONSTRAINED_TEMPLATE(impl::is_losslessly_convertible_to_native<T>, typename T)
648 friend bool operator==(const node_view& lhs, const T& rhs) noexcept(!impl::is_wide_string<T>)
649 {
650 static_assert(!impl::is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
651 "Comparison with wide-character strings is only "
652 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
653
654 if constexpr (impl::is_wide_string<T>)
655 {
656#if TOML_ENABLE_WINDOWS_COMPAT
657 return lhs == impl::narrow(rhs);
658#else
659 static_assert(impl::always_false<T>, "Evaluated unreachable branch!");
660#endif
661 }
662 else
663 {
664 const auto val = lhs.as<impl::native_type_of<T>>();
665 return val && *val == rhs;
666 }
667 }
668 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&,
669 const T&,
670 TOML_CONSTRAINED_TEMPLATE(impl::is_losslessly_convertible_to_native<T>,
671 typename T));
672
674 template <typename T>
676 friend bool operator==(const node_view& lhs,
677 const std::initializer_list<T>& rhs) noexcept(!impl::is_wide_string<T>)
678 {
679 const auto arr = lhs.as<array>();
680 return arr && *arr == rhs;
681 }
682 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::initializer_list<T>&, template <typename T>);
683
685 template <typename T>
687 friend bool operator==(const node_view& lhs, const std::vector<T>& rhs) noexcept(!impl::is_wide_string<T>)
688 {
689 const auto arr = lhs.as<array>();
690 return arr && *arr == rhs;
691 }
692 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::vector<T>&, template <typename T>);
693
695
698
706 node_view operator[](std::string_view key) const noexcept
707 {
708 if (auto tbl = this->as_table())
709 return node_view{ tbl->get(key) };
710 return {};
711 }
712
720 node_view operator[](const toml::path& path) const noexcept
721 {
722 return node_ ? node_->at_path(path) : node_view{};
723 }
724
729 node_view at_path(std::string_view path) const noexcept
730 {
731 return node_ ? node_->at_path(path) : node_view{};
732 }
733
738 node_view at_path(const toml::path& path) const noexcept
739 {
740 return node_ ? node_->at_path(path) : node_view{};
741 }
742
743#if TOML_ENABLE_WINDOWS_COMPAT
744
754 node_view operator[](std::wstring_view key) const
755 {
756 if (auto tbl = this->as_table())
757 return node_view{ tbl->get(key) };
758 return {};
759 }
760
767 node_view at_path(std::wstring_view path) const
768 {
769 return node_ ? node_->at_path(path) : node_view{};
770 }
771
772#endif // TOML_ENABLE_WINDOWS_COMPAT
773
781 node_view operator[](size_t index) const noexcept
782 {
783 if (auto arr = this->as_array())
784 return node_view{ arr->get(index) };
785 return {};
786 }
787
789
790#if TOML_ENABLE_FORMATTERS
791
795 friend std::ostream& operator<<(std::ostream& os, const node_view& nv)
796 {
797 if (nv.node_)
798 nv.node_->visit([&os](const auto& n) { os << n; });
799 return os;
800 }
801
802#endif
803 };
804
806
807 template <typename T>
808 node_view(const T&) -> node_view<const node>;
809
810 template <typename T>
811 node_view(const T*) -> node_view<const node>;
812
813 template <typename T>
814 node_view(T&) -> node_view<node>;
815
816 template <typename T>
817 node_view(T*) -> node_view<node>;
818
820}
822
825{
826 inline node::operator node_view<node>() noexcept
827 {
828 return node_view<node>{ this };
829 }
830
831 inline node::operator node_view<const node>() const noexcept
832 {
833 return node_view<const node>{ this };
834 }
835}
838
839#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".
A TOML array.
Definition array.hpp:285
A TOML path.
Definition path.hpp:239
A TOML table.
Definition table.hpp:220
constexpr bool is_number
Metafunction for determining if a type satisfies either toml::is_integer or toml::is_floating_point.
Definition forward_declarations.hpp:986
constexpr bool is_boolean
Metafunction for determining if a type is, or is a reference to, a bool or toml::value<bool>.
Definition forward_declarations.hpp:990
constexpr bool is_floating_point
Metafunction for determining if a type is, or is a reference to, a double or toml::value<double>.
Definition forward_declarations.hpp:980
constexpr bool is_date
Metafunction for determining if a type is, or is a reference to, a toml::date or toml::value<date>.
Definition forward_declarations.hpp:996
constexpr bool is_string
Metafunction for determining if a type is, or is a reference to, a std::string or toml::value<std::st...
Definition forward_declarations.hpp:968
constexpr bool is_array
Metafunction for determining if a type is, or is a reference to, a toml::array.
Definition forward_declarations.hpp:960
constexpr bool is_date_time
Metafunction for determining if a type is, or is a reference to, a toml::date_time or toml::value<dat...
Definition forward_declarations.hpp:1008
constexpr bool is_integer
Metafunction for determining if a type is, or is a reference to, a int64_t or toml::value<int64_t>.
Definition forward_declarations.hpp:974
std::basic_ostream< Char > & operator<<(std::basic_ostream< Char > &lhs, node_type rhs)
Pretty-prints the value of a node_type to a stream.
Definition forward_declarations.hpp:256
constexpr bool is_time
Metafunction for determining if a type is, or is a reference to, a toml::time or toml::value<time>.
Definition forward_declarations.hpp:1002
constexpr bool is_value
Metafunction for determining if a type is, or is a reference to, any of the toml value types....
Definition forward_declarations.hpp:1018
#define TOML_ENABLE_WINDOWS_COMPAT
Enables the use of wide strings (wchar_t, std::wstring) in various places throughout the library when...
Definition preprocessor.hpp:1074
#define TOML_ASSERT_ASSUME(expr)
Definition preprocessor.hpp:1190
bool operator==(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.h:14737
bool operator!=(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.h:14762
TOML_NAMESPACE_START
Definition node_view.hpp:15
TOML_DISABLE_ARITHMETIC_WARNINGS
Definition node_view.hpp:12
TOML_NAMESPACE_END
Definition node_view.hpp:821
#define TOML_NODISCARD_CTOR
Definition preprocessor.hpp:446
#define TOML_TRIVIAL_ABI
Definition preprocessor.hpp:430
#define TOML_CONSTRAINED_TEMPLATE(condition,...)
Definition preprocessor.hpp:1260
#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_ASYMMETRICAL_EQUALITY_OPS(LHS, RHS,...)
Definition preprocessor.hpp:611