60 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
87 explicit node_view(viewed_type& node)
noexcept
92 node_view(
const node_view&)
noexcept =
default;
96 node_view(node_view&&)
noexcept =
default;
99 node_view& operator=(
const node_view&) &
noexcept =
default;
102 node_view& operator=(node_view&&) &
noexcept =
default;
106 explicit operator bool()
const noexcept {
return node_ !=
nullptr; }
110 viewed_type* node()
const noexcept {
return node_; }
117 node_type type()
const noexcept {
return node_ ? node_->type() : node_type::none; }
121 bool is_table()
const noexcept {
return node_ && node_->is_table(); }
125 bool is_array()
const noexcept {
return node_ && node_->is_array(); }
129 bool is_value()
const noexcept {
return node_ && node_->is_value(); }
133 bool is_string()
const noexcept {
return node_ && node_->is_string(); }
137 bool is_integer()
const noexcept {
return node_ && node_->is_integer(); }
141 bool is_floating_point()
const noexcept {
return node_ && node_->is_floating_point(); }
145 bool is_number()
const noexcept {
return node_ && node_->is_number(); }
149 bool is_boolean()
const noexcept {
return node_ && node_->is_boolean(); }
153 bool is_date()
const noexcept {
return node_ && node_->is_date(); }
157 bool is_time()
const noexcept {
return node_ && node_->is_time(); }
161 bool is_date_time()
const noexcept {
return node_ && node_->is_date_time(); }
165 bool is_array_of_tables()
const noexcept {
return node_ && node_->is_array_of_tables(); }
174 template <
typename T>
176 return node_ ? node_->template is<impl::unwrap_node<impl::remove_cvref<T>>>() :
false;
209 bool is_homogeneous(node_type ntype, viewed_type*& first_nonmatch)
const noexcept {
214 return node_->is_homogeneous(ntype, first_nonmatch);
244 bool is_homogeneous(node_type ntype)
const noexcept {
245 return node_ ? node_->is_homogeneous(ntype) :
false;
273 template <
typename ElemType =
void>
276 ? node_->template is_homogeneous<impl::unwrap_node<impl::remove_cvref<ElemType>>>()
292 template <
typename T>
294 return node_ ? node_->template as<T>() :
nullptr;
299 auto* as_table()
const noexcept {
return as<table>(); }
303 auto* as_array()
const noexcept {
return as<array>(); }
307 auto* as_string()
const noexcept {
return as<std::string>(); }
311 auto* as_integer()
const noexcept {
return as<int64_t>(); }
315 auto* as_floating_point()
const noexcept {
return as<double>(); }
319 auto* as_boolean()
const noexcept {
return as<bool>(); }
323 auto* as_date()
const noexcept {
return as<date>(); }
327 auto* as_time()
const noexcept {
return as<time>(); }
331 auto* as_date_time()
const noexcept {
return as<date_time>(); }
351 template <
typename T>
352 TOML_NODISCARD optional<T> value_exact()
const noexcept(impl::value_retrieval_is_nothrow<T>) {
353 if (node_)
return node_->template value_exact<T>();
376 template <
typename T>
378 if (node_)
return node_->template value<T>();
399 template <
typename T>
401 noexcept(impl::value_retrieval_is_nothrow<T>) {
402 using namespace ::toml::impl;
405 "Retrieving values as wide-character strings is only "
406 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
408 if constexpr (is_wide_string<T>) {
409#if TOML_ENABLE_WINDOWS_COMPAT
411 if (node_)
return node_->value_or(
static_cast<T&&
>(default_value));
412 return std::wstring{
static_cast<T&&
>(default_value)};
416 static_assert(impl::always_false<T>,
"Evaluated unreachable branch!");
420 using value_type = std::conditional_t<
421 std::is_pointer_v<std::decay_t<T>>,
422 std::add_pointer_t<std::add_const_t<std::remove_pointer_t<std::decay_t<T>>>>,
425 if (node_)
return node_->value_or(
static_cast<T&&
>(default_value));
426 if constexpr (std::is_pointer_v<value_type>)
427 return value_type{default_value};
429 return static_cast<T&&
>(default_value);
464 template <
typename T>
467 node_ &&
"toml::node_view::ref() called on a node_view that did not reference a node");
468 return node_->template ref<T>();
478 template <
typename Func>
479 static constexpr bool visit_is_nothrow =
480 noexcept(std::declval<viewed_type*>()->visit(std::declval<Func>()));
489 template <
typename Func>
490 decltype(
auto) visit(Func&& visitor)
const noexcept(visit_is_nothrow<Func&&>) {
491 using return_type =
decltype(node_->visit(
static_cast<Func&&
>(visitor)));
492 if (node_)
return node_->visit(
static_cast<Func&&
>(visitor));
493 if constexpr (!std::is_void_v<return_type>)
return return_type{};
503 template <
typename T>
505 const node_view<T>&
rhs)
noexcept {
506 return impl::node_deep_equality(
lhs.node_,
rhs.node_);
510 template <
typename T>
512 const node_view<T>&
rhs)
noexcept {
513 return !impl::node_deep_equality(
lhs.node_,
rhs.node_);
519 if (
lhs.node_ == &
rhs)
return true;
521 return tbl && *tbl ==
rhs;
528 if (
lhs.node_ == &
rhs)
return true;
530 return arr && *arr ==
rhs;
535 template <
typename T>
537 const toml::value<T>&
rhs)
noexcept {
538 if (
lhs.node_ == &
rhs)
return true;
547 friend bool operator==(
const node_view&
lhs,
const T&
rhs)
noexcept(!impl::is_wide_string<T>) {
549 "Comparison with wide-character strings is only "
550 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
552 if constexpr (impl::is_wide_string<T>) {
553#if TOML_ENABLE_WINDOWS_COMPAT
554 return lhs == impl::narrow(
rhs);
556 static_assert(impl::always_false<T>,
"Evaluated unreachable branch!");
559 const auto val =
lhs.as<impl::native_type_of<T>>();
564 const node_view&,
const T&,
569 template <
typename T>
571 const node_view&
lhs,
572 const std::initializer_list<T>&
rhs)
noexcept(!impl::is_wide_string<T>) {
574 return arr && *arr ==
rhs;
577 template <typename T>);
581 template <
typename T>
583 !impl::is_wide_string<T>) {
585 return arr && *arr ==
rhs;
601 node_view operator[](std::string_view key)
const noexcept {
602 if (
auto tbl = this->as_table())
return node_view{tbl->get(key)};
613 node_view operator[](
const toml::path&
path)
const noexcept {
614 return node_ ? node_->at_path(
path) : node_view{};
621 node_view
at_path(std::string_view
path)
const noexcept {
622 return node_ ? node_->at_path(
path) : node_view{};
629 node_view
at_path(
const toml::path&
path)
const noexcept {
630 return node_ ? node_->at_path(
path) : node_view{};
633#if TOML_ENABLE_WINDOWS_COMPAT
644 node_view operator[](std::wstring_view key)
const {
645 if (
auto tbl = this->as_table())
return node_view{tbl->get(key)};
656 return node_ ? node_->at_path(
path) : node_view{};
668 node_view operator[](
size_t index)
const noexcept {
669 if (
auto arr = this->as_array())
return node_view{arr->get(
index)};
675#if TOML_ENABLE_FORMATTERS
680 friend std::ostream&
operator<<(std::ostream&
os,
const node_view& nv) {
681 if (nv.node_) nv.node_->visit([&
os](
const auto&
n) {
os <<
n; });
690 template <
typename T>
691 node_view(
const T&) -> node_view<const node>;
693 template <
typename T>
694 node_view(
const T*) -> node_view<const node>;
696 template <
typename T>
697 node_view(
T&) -> node_view<node>;
699 template <
typename T>
700 node_view(
T*) -> node_view<node>;
708 inline node::operator node_view<node>()
noexcept {
709 return node_view<node>{
this};
712 inline node::operator node_view<const node>()
const noexcept {
713 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".
constexpr bool is_number
Metafunction for determining if a type satisfies either toml::is_integer or toml::is_floating_point.
constexpr bool is_boolean
Metafunction for determining if a type is, or is a reference to, a bool or toml::value<bool>.
constexpr bool is_floating_point
Metafunction for determining if a type is, or is a reference to, a double or toml::value<double>.
constexpr bool is_date
Metafunction for determining if a type is, or is a reference to, a toml::date or toml::value<date>.
constexpr bool is_string
Metafunction for determining if a type is, or is a reference to, a std::string or toml::value<std::st...
constexpr bool is_array
Metafunction for determining if a type is, or is a reference to, a toml::array.
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...
constexpr bool is_integer
Metafunction for determining if a type is, or is a reference to, a int64_t or toml::value<int64_t>.
std::basic_ostream< Char > & operator<<(std::basic_ostream< Char > &lhs, node_type rhs)
Pretty-prints the value of a node_type to a stream.
constexpr bool is_time
Metafunction for determining if a type is, or is a reference to, a toml::time or toml::value<time>.
constexpr bool is_value
Metafunction for determining if a type is, or is a reference to, any of the toml value types....
#define TOML_ENABLE_WINDOWS_COMPAT
Enables the use of wide strings (wchar_t, std::wstring) in various places throughout the library when...
#define TOML_ASSERT_ASSUME(expr)
bool operator==(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
bool operator!=(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
TOML_DISABLE_ARITHMETIC_WARNINGS
#define TOML_NODISCARD_CTOR
#define TOML_CONSTRAINED_TEMPLATE(condition,...)
#define TOML_PURE_INLINE_GETTER
#define TOML_ASYMMETRICAL_EQUALITY_OPS(LHS, RHS,...)