59 template <
typename ViewedType>
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.");
67 using viewed_type = ViewedType;
71 friend class node_view;
73 mutable viewed_type* node_ =
nullptr;
78 node_view()
noexcept =
default;
82 explicit node_view(viewed_type* node)
noexcept
88 explicit node_view(viewed_type& node)
noexcept
94 node_view(
const node_view&)
noexcept =
default;
98 node_view(node_view&&)
noexcept =
default;
101 node_view& operator=(
const node_view&) &
noexcept =
default;
104 node_view& operator=(node_view&&) &
noexcept =
default;
108 explicit operator bool()
const noexcept
110 return node_ !=
nullptr;
115 viewed_type* node()
const noexcept
125 node_type type()
const noexcept
127 return node_ ? node_->type() : node_type::none;
132 bool is_table()
const noexcept
134 return node_ && node_->is_table();
141 return node_ && node_->is_array();
148 return node_ && node_->is_value();
155 return node_ && node_->is_string();
162 return node_ && node_->is_integer();
169 return node_ && node_->is_floating_point();
176 return node_ && node_->is_number();
183 return node_ && node_->is_boolean();
190 return node_ && node_->is_date();
197 return node_ && node_->is_time();
204 return node_ && node_->is_date_time();
209 bool is_array_of_tables()
const noexcept
211 return node_ && node_->is_array_of_tables();
221 template <
typename T>
223 bool is()
const noexcept
225 return node_ ? node_->template is<impl::unwrap_node<impl::remove_cvref<T>>>() :
false;
258 bool is_homogeneous(node_type ntype, viewed_type*& first_nonmatch)
const noexcept
265 return node_->is_homogeneous(ntype, first_nonmatch);
294 bool is_homogeneous(node_type ntype)
const noexcept
296 return node_ ? node_->is_homogeneous(ntype) :
false;
324 template <
typename ElemType =
void>
326 bool is_homogeneous()
const noexcept
328 return node_ ? node_->template is_homogeneous<impl::unwrap_node<impl::remove_cvref<ElemType>>>() :
false;
343 template <
typename T>
345 auto* as()
const noexcept
347 return node_ ? node_->template as<T>() :
nullptr;
352 auto* as_table()
const noexcept
359 auto* as_array()
const noexcept
366 auto* as_string()
const noexcept
368 return as<std::string>();
373 auto* as_integer()
const noexcept
375 return as<int64_t>();
380 auto* as_floating_point()
const noexcept
387 auto* as_boolean()
const noexcept
394 auto* as_date()
const noexcept
401 auto* as_time()
const noexcept
408 auto* as_date_time()
const noexcept
410 return as<date_time>();
430 template <
typename T>
432 optional<T> value_exact()
const noexcept(impl::value_retrieval_is_nothrow<T>)
435 return node_->template value_exact<T>();
458 template <
typename T>
460 optional<T> value()
const noexcept(impl::value_retrieval_is_nothrow<T>)
463 return node_->template value<T>();
484 template <
typename T>
486 auto value_or(T&& default_value)
const noexcept(impl::value_retrieval_is_nothrow<T>)
488 using namespace ::toml::impl;
491 "Retrieving values as wide-character strings is only "
492 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
494 if constexpr (is_wide_string<T>)
496#if TOML_ENABLE_WINDOWS_COMPAT
499 return node_->value_or(
static_cast<T&&
>(default_value));
500 return std::wstring{
static_cast<T&&
>(default_value) };
504 static_assert(impl::always_false<T>,
"Evaluated unreachable branch!");
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>>>>,
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 };
520 return static_cast<T&&
>(default_value);
554 template <
typename T>
556 decltype(
auto) ref()
const noexcept
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>();
569 template <
typename Func>
570 static constexpr bool visit_is_nothrow =
noexcept(std::declval<viewed_type*>()->visit(std::declval<Func>()));
579 template <
typename Func>
580 decltype(
auto) visit(Func&& visitor)
const noexcept(visit_is_nothrow<Func&&>)
582 using return_type =
decltype(node_->visit(
static_cast<Func&&
>(visitor)));
584 return node_->visit(
static_cast<Func&&
>(visitor));
585 if constexpr (!std::is_void_v<return_type>)
586 return return_type{};
596 template <
typename T>
598 friend bool operator==(
const node_view& lhs,
const node_view<T>& rhs)
noexcept
600 return impl::node_deep_equality(lhs.node_, rhs.node_);
604 template <
typename T>
606 friend bool operator!=(
const node_view& lhs,
const node_view<T>& rhs)
noexcept
608 return !impl::node_deep_equality(lhs.node_, rhs.node_);
613 friend bool operator==(
const node_view& lhs,
const table& rhs)
noexcept
615 if (lhs.node_ == &rhs)
617 const auto tbl = lhs.as<
table>();
618 return tbl && *tbl == rhs;
624 friend bool operator==(
const node_view& lhs,
const array& rhs)
noexcept
626 if (lhs.node_ == &rhs)
628 const auto arr = lhs.as<
array>();
629 return arr && *arr == rhs;
634 template <
typename T>
636 friend bool operator==(
const node_view& lhs,
const toml::value<T>& rhs)
noexcept
638 if (lhs.node_ == &rhs)
640 const auto val = lhs.as<T>();
641 return val && *val == rhs;
648 friend bool operator==(
const node_view& lhs,
const T& rhs)
noexcept(!impl::is_wide_string<T>)
651 "Comparison with wide-character strings is only "
652 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
654 if constexpr (impl::is_wide_string<T>)
656#if TOML_ENABLE_WINDOWS_COMPAT
657 return lhs == impl::narrow(rhs);
659 static_assert(impl::always_false<T>,
"Evaluated unreachable branch!");
664 const auto val = lhs.as<impl::native_type_of<T>>();
665 return val && *val == rhs;
674 template <
typename T>
677 const std::initializer_list<T>& rhs)
noexcept(!impl::is_wide_string<T>)
679 const auto arr = lhs.as<
array>();
680 return arr && *arr == rhs;
685 template <
typename T>
687 friend bool operator==(
const node_view& lhs,
const std::vector<T>& rhs)
noexcept(!impl::is_wide_string<T>)
689 const auto arr = lhs.as<
array>();
690 return arr && *arr == rhs;
706 node_view operator[](std::string_view key)
const noexcept
708 if (
auto tbl = this->as_table())
709 return node_view{ tbl->get(key) };
720 node_view operator[](
const toml::path&
path)
const noexcept
722 return node_ ? node_->at_path(
path) : node_view{};
729 node_view
at_path(std::string_view
path)
const noexcept
731 return node_ ? node_->at_path(
path) : node_view{};
738 node_view
at_path(
const toml::path&
path)
const noexcept
740 return node_ ? node_->at_path(
path) : node_view{};
743#if TOML_ENABLE_WINDOWS_COMPAT
754 node_view operator[](std::wstring_view key)
const
756 if (
auto tbl = this->as_table())
757 return node_view{ tbl->get(key) };
769 return node_ ? node_->at_path(
path) : node_view{};
781 node_view operator[](
size_t index)
const noexcept
783 if (
auto arr = this->as_array())
784 return node_view{ arr->get(index) };
790#if TOML_ENABLE_FORMATTERS
795 friend std::ostream&
operator<<(std::ostream& os,
const node_view& nv)
798 nv.node_->visit([&os](
const auto& n) { os << n; });
807 template <
typename T>
808 node_view(
const T&) -> node_view<const node>;
810 template <
typename T>
811 node_view(
const T*) -> node_view<const node>;
813 template <
typename T>
814 node_view(T&) -> node_view<node>;
816 template <
typename T>
817 node_view(T*) -> node_view<node>;
826 inline node::operator node_view<node>()
noexcept
828 return node_view<node>{
this };
831 inline node::operator node_view<const node>()
const noexcept
833 return node_view<const node>{
this };
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