Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
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 "header_start.hpp"
8#include "node.hpp"
9#include "print_to_stream.hpp"
11#include "std_vector.hpp"
13
60 template <typename ViewedType>
61 class TOML_TRIVIAL_ABI node_view {
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
87 explicit node_view(viewed_type& node) noexcept //
88 : node_{&node} {}
89
92 node_view(const node_view&) noexcept = default;
93
96 node_view(node_view&&) noexcept = default;
97
99 node_view& operator=(const node_view&) & noexcept = default;
100
102 node_view& operator=(node_view&&) & noexcept = default;
103
106 explicit operator bool() const noexcept { return node_ != nullptr; }
107
110 viewed_type* node() const noexcept { return node_; }
111
114
117 node_type type() const noexcept { return node_ ? node_->type() : node_type::none; }
118
121 bool is_table() const noexcept { return node_ && node_->is_table(); }
122
125 bool is_array() const noexcept { return node_ && node_->is_array(); }
126
129 bool is_value() const noexcept { return node_ && node_->is_value(); }
130
133 bool is_string() const noexcept { return node_ && node_->is_string(); }
134
137 bool is_integer() const noexcept { return node_ && node_->is_integer(); }
138
141 bool is_floating_point() const noexcept { return node_ && node_->is_floating_point(); }
142
145 bool is_number() const noexcept { return node_ && node_->is_number(); }
146
149 bool is_boolean() const noexcept { return node_ && node_->is_boolean(); }
150
153 bool is_date() const noexcept { return node_ && node_->is_date(); }
154
157 bool is_time() const noexcept { return node_ && node_->is_time(); }
158
161 bool is_date_time() const noexcept { return node_ && node_->is_date_time(); }
162
165 bool is_array_of_tables() const noexcept { return node_ && node_->is_array_of_tables(); }
166
174 template <typename T>
175 TOML_PURE_GETTER bool is() const noexcept {
176 return node_ ? node_->template is<impl::unwrap_node<impl::remove_cvref<T>>>() : false;
177 }
178
209 bool is_homogeneous(node_type ntype, viewed_type*& first_nonmatch) const noexcept {
210 if (!node_) {
211 first_nonmatch = {};
212 return false;
213 }
214 return node_->is_homogeneous(ntype, first_nonmatch);
215 }
216
244 bool is_homogeneous(node_type ntype) const noexcept {
245 return node_ ? node_->is_homogeneous(ntype) : false;
246 }
247
273 template <typename ElemType = void>
274 TOML_PURE_GETTER bool is_homogeneous() const noexcept {
275 return node_
276 ? node_->template is_homogeneous<impl::unwrap_node<impl::remove_cvref<ElemType>>>()
277 : false;
278 }
279
281
284
292 template <typename T>
293 TOML_PURE_GETTER auto* as() const noexcept {
294 return node_ ? node_->template as<T>() : nullptr;
295 }
296
299 auto* as_table() const noexcept { return as<table>(); }
300
303 auto* as_array() const noexcept { return as<array>(); }
304
307 auto* as_string() const noexcept { return as<std::string>(); }
308
311 auto* as_integer() const noexcept { return as<int64_t>(); }
312
315 auto* as_floating_point() const noexcept { return as<double>(); }
316
319 auto* as_boolean() const noexcept { return as<bool>(); }
320
323 auto* as_date() const noexcept { return as<date>(); }
324
327 auto* as_time() const noexcept { return as<time>(); }
328
331 auto* as_date_time() const noexcept { return as<date_time>(); }
332
334
337
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>();
354 return {};
355 }
356
376 template <typename T>
377 TOML_NODISCARD optional<T> value() const noexcept(impl::value_retrieval_is_nothrow<T>) {
378 if (node_) return node_->template value<T>();
379 return {};
380 }
381
399 template <typename T>
400 TOML_NODISCARD auto value_or(T&& default_value) const
401 noexcept(impl::value_retrieval_is_nothrow<T>) {
402 using namespace ::toml::impl;
403
404 static_assert(!is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
405 "Retrieving values as wide-character strings is only "
406 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
407
408 if constexpr (is_wide_string<T>) {
409#if TOML_ENABLE_WINDOWS_COMPAT
410
411 if (node_) return node_->value_or(static_cast<T&&>(default_value));
412 return std::wstring{static_cast<T&&>(default_value)};
413
414#else
415
416 static_assert(impl::always_false<T>, "Evaluated unreachable branch!");
417
418#endif
419 } else {
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>>>>,
423 std::decay_t<T>>;
424
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};
428 else
429 return static_cast<T&&>(default_value);
430 }
431 }
432
464 template <typename T>
465 TOML_PURE_INLINE_GETTER decltype(auto) ref() const noexcept {
467 node_ && "toml::node_view::ref() called on a node_view that did not reference a node");
468 return node_->template ref<T>();
469 }
470
472
475
476 private:
478 template <typename Func>
479 static constexpr bool visit_is_nothrow =
480 noexcept(std::declval<viewed_type*>()->visit(std::declval<Func>()));
482
483 public:
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{};
494 }
495
497
500
501 public:
503 template <typename T>
504 TOML_PURE_GETTER friend bool operator==(const node_view& lhs,
505 const node_view<T>& rhs) noexcept {
506 return impl::node_deep_equality(lhs.node_, rhs.node_);
507 }
508
510 template <typename T>
511 TOML_PURE_GETTER friend bool operator!=(const node_view& lhs,
512 const node_view<T>& rhs) noexcept {
513 return !impl::node_deep_equality(lhs.node_, rhs.node_);
514 }
515
518 friend bool operator==(const node_view& lhs, const table& rhs) noexcept {
519 if (lhs.node_ == &rhs) return true;
520 const auto tbl = lhs.as<table>();
521 return tbl && *tbl == rhs;
522 }
523 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const table&, );
524
527 friend bool operator==(const node_view& lhs, const array& rhs) noexcept {
528 if (lhs.node_ == &rhs) return true;
529 const auto arr = lhs.as<array>();
530 return arr && *arr == rhs;
531 }
532 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const array&, );
533
535 template <typename T>
536 TOML_NODISCARD friend bool operator==(const node_view& lhs,
537 const toml::value<T>& rhs) noexcept {
538 if (lhs.node_ == &rhs) return true;
539 const auto val = lhs.as<T>();
540 return val && *val == rhs;
541 }
542 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const toml::value<T>&, template <typename T>);
543
545 TOML_CONSTRAINED_TEMPLATE(impl::is_losslessly_convertible_to_native<T>, typename T)
547 friend bool operator==(const node_view& lhs, const T& rhs) noexcept(!impl::is_wide_string<T>) {
548 static_assert(!impl::is_wide_string<T> || TOML_ENABLE_WINDOWS_COMPAT,
549 "Comparison with wide-character strings is only "
550 "supported on Windows with TOML_ENABLE_WINDOWS_COMPAT enabled.");
551
552 if constexpr (impl::is_wide_string<T>) {
553#if TOML_ENABLE_WINDOWS_COMPAT
554 return lhs == impl::narrow(rhs);
555#else
556 static_assert(impl::always_false<T>, "Evaluated unreachable branch!");
557#endif
558 } else {
559 const auto val = lhs.as<impl::native_type_of<T>>();
560 return val && *val == rhs;
561 }
562 }
564 const node_view&, const T&,
565 TOML_CONSTRAINED_TEMPLATE(impl::is_losslessly_convertible_to_native<T>, typename T));
566
569 template <typename T>
570 TOML_NODISCARD friend bool operator==(
571 const node_view& lhs,
572 const std::initializer_list<T>& rhs) noexcept(!impl::is_wide_string<T>) {
573 const auto arr = lhs.as<array>();
574 return arr && *arr == rhs;
575 }
576 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::initializer_list<T>&,
577 template <typename T>);
578
581 template <typename T>
582 TOML_NODISCARD friend bool operator==(const node_view& lhs, const std::vector<T>& rhs) noexcept(
583 !impl::is_wide_string<T>) {
584 const auto arr = lhs.as<array>();
585 return arr && *arr == rhs;
586 }
587 TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::vector<T>&, template <typename T>);
588
590
593
601 node_view operator[](std::string_view key) const noexcept {
602 if (auto tbl = this->as_table()) return node_view{tbl->get(key)};
603 return {};
604 }
605
613 node_view operator[](const toml::path& path) const noexcept {
614 return node_ ? node_->at_path(path) : node_view{};
615 }
616
621 node_view at_path(std::string_view path) const noexcept {
622 return node_ ? node_->at_path(path) : node_view{};
623 }
624
629 node_view at_path(const toml::path& path) const noexcept {
630 return node_ ? node_->at_path(path) : node_view{};
631 }
632
633#if TOML_ENABLE_WINDOWS_COMPAT
634
644 node_view operator[](std::wstring_view key) const {
645 if (auto tbl = this->as_table()) return node_view{tbl->get(key)};
646 return {};
647 }
648
655 node_view at_path(std::wstring_view path) const {
656 return node_ ? node_->at_path(path) : node_view{};
657 }
658
659#endif // TOML_ENABLE_WINDOWS_COMPAT
660
668 node_view operator[](size_t index) const noexcept {
669 if (auto arr = this->as_array()) return node_view{arr->get(index)};
670 return {};
671 }
672
674
675#if TOML_ENABLE_FORMATTERS
676
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; });
682 return os;
683 }
684
685#endif
686 };
687
689
690 template <typename T>
691 node_view(const T&) -> node_view<const node>;
692
693 template <typename T>
694 node_view(const T*) -> node_view<const node>;
695
696 template <typename T>
697 node_view(T&) -> node_view<node>;
698
699 template <typename T>
700 node_view(T*) -> node_view<node>;
701
703}
705
708 inline node::operator node_view<node>() noexcept {
709 return node_view<node>{this};
710 }
711
712 inline node::operator node_view<const node>() const noexcept {
713 return node_view<const node>{this};
714 }
715}
718
719#include "header_end.hpp"
#define T
Definition 17.names.cpp:163
#define rhs
Definition 17.names.cpp:536
#define os
Definition 17.names.cpp:511
#define index
Definition 17.names.cpp:482
#define lhs
Definition 17.names.cpp:489
#define n
Definition 17.names.cpp:130
#define val
Definition 17.names.cpp:570
T value(const T &val)
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:258
A TOML path.
Definition path.hpp:209
A TOML table.
Definition table.hpp:190
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
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:14
TOML_DISABLE_ARITHMETIC_WARNINGS
Definition node_view.hpp:12
TOML_NAMESPACE_END
#define TOML_NODISCARD_CTOR
#define TOML_TRIVIAL_ABI
#define TOML_CONSTRAINED_TEMPLATE(condition,...)
#define TOML_NODISCARD
#define TOML_PURE_GETTER
#define TOML_PURE_INLINE_GETTER
#define TOML_ASYMMETRICAL_EQUALITY_OPS(LHS, RHS,...)
#define bool
Definition stdbool.h:24