2020-03-12 15:23:25 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#pragma once
|
|
|
|
#include "toml_table.h"
|
|
|
|
#include "toml_array.h"
|
|
|
|
#include "toml_value.h"
|
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
namespace toml
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Char, typename T>
|
|
|
|
inline std::basic_ostream<Char>& operator << (std::basic_ostream<Char>&, const node_view<T>&);
|
2020-03-03 08:10:07 +00:00
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief A view of a node.
|
|
|
|
///
|
|
|
|
/// \detail A node_view is like a std::optional<toml::node> with lots of toml-specific stuff built-in.
|
|
|
|
/// It _may_ represent a node, and allows you to do many of the same operations that you'd do
|
|
|
|
/// on nodes directly, as well as easily traversing the node tree by creating
|
|
|
|
/// subviews (via node_view::operator[]). \cpp
|
|
|
|
///
|
|
|
|
/// auto tbl = toml::parse(R"(
|
|
|
|
///
|
|
|
|
/// title = "my hardware store"
|
|
|
|
///
|
|
|
|
/// [[products]]
|
|
|
|
/// name = "Hammer"
|
|
|
|
/// sku = 738594937
|
|
|
|
/// keywords = [ "hammer", "construction", "build" ]
|
2020-02-16 13:11:57 +00:00
|
|
|
///
|
2020-02-20 21:08:20 +00:00
|
|
|
/// [[products]]
|
|
|
|
/// name = "Nail"
|
|
|
|
/// sku = 284758393
|
|
|
|
/// color = "gray"
|
|
|
|
///
|
|
|
|
/// )"sv);
|
|
|
|
///
|
|
|
|
/// std::cout << tbl["title"] << std::endl;
|
|
|
|
/// std::cout << tbl["products"][0]["name"] << std::endl;
|
|
|
|
/// std::cout << tbl["products"][0]["keywords"] << std::endl;
|
|
|
|
/// std::cout << tbl["products"][0]["keywords"][2] << std::endl;
|
|
|
|
///
|
|
|
|
/// tbl["products"][0]["keywords"].as_array()->push_back("heavy");
|
|
|
|
/// std::cout << tbl["products"][0]["keywords"] << std::endl;
|
2020-02-23 11:13:18 +00:00
|
|
|
/// std::cout << "has product[2]: "sv << !!tbl["products"][2] << std::endl;
|
|
|
|
/// std::cout << "product[2]: "sv << tbl["products"][2] << std::endl;
|
|
|
|
/// \ecpp
|
2020-02-20 21:08:20 +00:00
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// "my hardware store"
|
|
|
|
/// "Hammer"
|
|
|
|
/// [ "hammer", "construction", "build" ]
|
2020-02-23 11:13:18 +00:00
|
|
|
/// "build"
|
2020-02-20 21:08:20 +00:00
|
|
|
/// [ "hammer", "construction", "build", "heavy" ]
|
2020-02-23 11:13:18 +00:00
|
|
|
/// has product[2]: false
|
|
|
|
/// product[2]:
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \eout
|
2020-01-04 14:21:38 +00:00
|
|
|
template <typename T>
|
2020-03-03 08:10:07 +00:00
|
|
|
class TOML_API node_view final
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-02-20 21:08:20 +00:00
|
|
|
using viewed_type = T;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
|
|
private:
|
2020-02-20 21:08:20 +00:00
|
|
|
friend class toml::table;
|
2020-03-03 08:10:07 +00:00
|
|
|
template <typename U> friend class toml::node_view;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-04-03 13:33:02 +00:00
|
|
|
mutable viewed_type* node_ = nullptr;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
2020-02-20 21:08:20 +00:00
|
|
|
node_view(viewed_type* node) noexcept
|
|
|
|
: node_{ node }
|
2020-01-04 14:21:38 +00:00
|
|
|
{}
|
|
|
|
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Func>
|
2020-03-05 11:07:26 +00:00
|
|
|
static constexpr bool visit_is_nothrow
|
2020-04-02 21:39:21 +00:00
|
|
|
= noexcept(std::declval<viewed_type*>()->visit(std::declval<Func&&>()));
|
2020-03-05 11:07:26 +00:00
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
public:
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-04-03 13:33:02 +00:00
|
|
|
/// \brief Constructs an empty node view.
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
node_view() noexcept = default;
|
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the view references a node.
|
|
|
|
[[nodiscard]] explicit operator bool() const noexcept { return node_ != nullptr; }
|
|
|
|
/// \brief Returns the node that's being referenced by the view.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] viewed_type* get() const noexcept { return node_; }
|
2020-02-20 21:08:20 +00:00
|
|
|
|
|
|
|
/// \brief Returns the type identifier for the viewed node.
|
|
|
|
[[nodiscard]] node_type type() const noexcept { return node_ ? node_->type() : node_type::none; }
|
|
|
|
|
|
|
|
/// \brief Returns true if the viewed node is a toml::table.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_table() const noexcept { return node_ && node_->is_table(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::array.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_array() const noexcept { return node_ && node_->is_array(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::value<>.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_value() const noexcept { return node_ && node_->is_value(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::value<string>.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_string() const noexcept { return node_ && node_->is_string(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::value<int64_t>.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_integer() const noexcept { return node_ && node_->is_integer(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::value<double>.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_floating_point() const noexcept { return node_ && node_->is_floating_point(); }
|
|
|
|
/// \brief Returns true if the viewed node is a toml::value<int64_t> or toml::value<double>.
|
|
|
|
[[nodiscard]] bool is_number() const noexcept { return node_ && node_->is_number(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::value<bool>.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_boolean() const noexcept { return node_ && node_->is_boolean(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::value<date>.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_date() const noexcept { return node_ && node_->is_date(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::value<time>.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_time() const noexcept { return node_ && node_->is_time(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::value<date_time>.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_date_time() const noexcept { return node_ && node_->is_date_time(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a toml::array that contains only tables.
|
2020-02-22 14:10:32 +00:00
|
|
|
[[nodiscard]] bool is_array_of_tables() const noexcept { return node_ && node_->is_array_of_tables(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
|
|
|
|
/// \brief Checks if this view references a node of a specific type.
|
|
|
|
///
|
|
|
|
/// \tparam U A TOML node or value type.
|
|
|
|
///
|
|
|
|
/// \returns Returns true if the viewed node is an instance of the specified type.
|
|
|
|
///
|
|
|
|
/// \see toml::node::is()
|
|
|
|
template <typename U>
|
|
|
|
[[nodiscard]]
|
|
|
|
bool is() const noexcept
|
|
|
|
{
|
|
|
|
return node_ ? node_->template is<U>() : false;
|
|
|
|
}
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Gets a pointer to the viewed node as a more specific node type.
|
|
|
|
///
|
|
|
|
/// \tparam U The node type or TOML value type to cast to.
|
|
|
|
///
|
|
|
|
/// \returns A pointer to the node as the given type, or nullptr if it was a different type.
|
|
|
|
///
|
|
|
|
/// \see toml::node::as()
|
2020-01-04 14:21:38 +00:00
|
|
|
template <typename U>
|
2020-02-20 21:08:20 +00:00
|
|
|
[[nodiscard]]
|
2020-03-05 11:07:26 +00:00
|
|
|
auto as() const noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
static_assert(
|
2020-02-03 09:12:43 +00:00
|
|
|
impl::is_value_or_node<impl::unwrapped<U>>,
|
2020-01-04 14:21:38 +00:00
|
|
|
"Template type parameter must be one of the basic value types, a toml::table, or a toml::array"
|
|
|
|
);
|
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
return node_ ? node_->template as<U>() : nullptr;
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a pointer to the viewed node as a toml::table, if it is one.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] auto as_table() const noexcept { return as<table>(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a pointer to the viewed node as a toml::array, if it is one.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] auto as_array() const noexcept { return as<array>(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a pointer to the viewed node as a toml::value<string>, if it is one.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] auto as_string() const noexcept { return as<string>(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a pointer to the viewed node as a toml::value<int64_t>, if it is one.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] auto as_integer() const noexcept { return as<int64_t>(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a pointer to the viewed node as a toml::value<double>, if it is one.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] auto as_floating_point() const noexcept { return as<double>(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a pointer to the viewed node as a toml::value<bool>, if it is one.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] auto as_boolean() const noexcept { return as<bool>(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a pointer to the viewed node as a toml::value<date>, if it is one.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] auto as_date() const noexcept { return as<date>(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a pointer to the viewed node as a toml::value<time>, if it is one.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] auto as_time() const noexcept { return as<time>(); }
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a pointer to the viewed node as a toml::value<date_time>, if it is one.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] auto as_date_time() const noexcept { return as<date_time>(); }
|
2020-03-01 14:56:40 +00:00
|
|
|
|
2020-04-06 12:57:49 +00:00
|
|
|
TOML_PUSH_WARNINGS
|
|
|
|
TOML_DISABLE_INIT_WARNINGS
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Gets the raw value contained by the referenced node.
|
2020-02-22 14:10:32 +00:00
|
|
|
///
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \tparam U One of the TOML value types. Can also be a string_view.
|
|
|
|
///
|
|
|
|
/// \returns The underlying value if the node was a value of the matching type (or convertible to it), or an empty optional.
|
2020-02-22 14:10:32 +00:00
|
|
|
///
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \see node::value()
|
|
|
|
template <typename U>
|
2020-03-03 21:28:24 +00:00
|
|
|
[[nodiscard]] optional<U> value() const noexcept
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
|
|
|
if (node_)
|
|
|
|
return node_->template value<U>();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-06 12:57:49 +00:00
|
|
|
TOML_POP_WARNINGS
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Gets the raw value contained by the referenced node, or a default.
|
|
|
|
///
|
2020-02-22 14:10:32 +00:00
|
|
|
/// \tparam U Default value type. Must be (or be promotable to) one of the TOML value types.
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \param default_value The default value to return if the view did not reference a node,
|
|
|
|
/// or if the node wasn't a value, wasn't the correct type, or no conversion was possible.
|
2020-02-22 14:10:32 +00:00
|
|
|
///
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \returns The node's underlying value, or the default if the node wasn't a value, wasn't the
|
|
|
|
/// correct type, or no conversion was possible.
|
|
|
|
///
|
|
|
|
/// \see node::value_or()
|
2020-02-22 14:10:32 +00:00
|
|
|
template <typename U>
|
|
|
|
[[nodiscard]] auto value_or(U&& default_value) const noexcept
|
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
using return_type = decltype(node_->value_or(std::forward<U>(default_value)));
|
|
|
|
if (node_)
|
|
|
|
return node_->value_or(std::forward<U>(default_value));
|
2020-02-22 14:10:32 +00:00
|
|
|
return return_type{ std::forward<U>(default_value) };
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Invokes a visitor on the viewed node based on its concrete type.
|
|
|
|
///
|
|
|
|
/// \remarks Has no effect if the view does not reference a node.
|
|
|
|
///
|
|
|
|
/// \see node::visit()
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Func>
|
|
|
|
decltype(auto) visit(Func&& visitor) const
|
|
|
|
noexcept(visit_is_nothrow<Func&&>)
|
2020-02-20 21:08:20 +00:00
|
|
|
{
|
2020-04-02 21:39:21 +00:00
|
|
|
using return_type = decltype(node_->visit(std::forward<Func>(visitor)));
|
2020-03-05 11:07:26 +00:00
|
|
|
if (node_)
|
2020-04-02 21:39:21 +00:00
|
|
|
return node_->visit(std::forward<Func>(visitor));
|
2020-03-05 11:07:26 +00:00
|
|
|
if constexpr (!std::is_void_v<return_type>)
|
|
|
|
return return_type{};
|
2020-02-20 21:08:20 +00:00
|
|
|
}
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-05 11:07:26 +00:00
|
|
|
/// \brief Gets a raw reference to the viewed node's underlying data.
|
|
|
|
///
|
|
|
|
/// \warning This function is dangerous if used carelessly and **WILL** break your code if the
|
|
|
|
/// node_view didn't reference a node, or the chosen value type doesn't match the node's
|
2020-03-23 15:55:32 +00:00
|
|
|
/// actual type. In debug builds an assertion will fire when invalid accesses are attempted: \cpp
|
2020-03-05 11:07:26 +00:00
|
|
|
///
|
|
|
|
/// auto tbl = toml::parse(R"(
|
|
|
|
///
|
|
|
|
/// min = 32
|
|
|
|
/// max = 45
|
2020-02-20 21:08:20 +00:00
|
|
|
///
|
2020-03-05 11:07:26 +00:00
|
|
|
/// )"sv);
|
2020-02-20 21:08:20 +00:00
|
|
|
///
|
2020-03-05 11:07:26 +00:00
|
|
|
/// auto& min_ref = tbl["min"].ref<int64_t>(); // this is OK
|
|
|
|
/// auto& max_ref = tbl["max"].ref<double>(); // hits assertion because the type is wrong
|
|
|
|
/// auto& min_ref = tbl["foo"].ref<int64_t>(); // hits assertion because "foo" didn't exist
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \tparam U One of the TOML value types.
|
|
|
|
///
|
|
|
|
/// \returns A reference to the underlying data.
|
|
|
|
template <typename U>
|
|
|
|
[[nodiscard]] decltype(auto) ref() const noexcept
|
2020-02-20 21:08:20 +00:00
|
|
|
{
|
2020-03-05 11:07:26 +00:00
|
|
|
using type = impl::unwrapped<U>;
|
|
|
|
static_assert(
|
|
|
|
impl::is_value_or_node<type>,
|
|
|
|
"Template type parameter must be one of the TOML value types, a toml::table, or a toml::array"
|
|
|
|
);
|
|
|
|
TOML_ASSERT(
|
|
|
|
node_
|
|
|
|
&& "toml::node_view::ref() called on a node_view that did not reference a node"
|
|
|
|
);
|
|
|
|
return node_->template ref<type>();
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a table with the same contents as RHS.
|
|
|
|
[[nodiscard]] friend bool operator == (const node_view& lhs, const table& rhs) noexcept
|
|
|
|
{
|
|
|
|
if (lhs.node_ == &rhs)
|
|
|
|
return true;
|
|
|
|
const auto tbl = lhs.as<table>();
|
|
|
|
return tbl && *tbl == rhs;
|
|
|
|
}
|
2020-04-01 12:53:10 +00:00
|
|
|
#ifndef DOXYGEN
|
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const table&, )
|
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is an array with the same contents as RHS.
|
|
|
|
[[nodiscard]] friend bool operator == (const node_view& lhs, const array& rhs) noexcept
|
|
|
|
{
|
|
|
|
if (lhs.node_ == &rhs)
|
|
|
|
return true;
|
|
|
|
const auto arr = lhs.as<array>();
|
|
|
|
return arr && *arr == rhs;
|
|
|
|
}
|
2020-04-01 12:53:10 +00:00
|
|
|
#ifndef DOXYGEN
|
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const array&, )
|
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a value with the same value as RHS.
|
2020-01-04 14:21:38 +00:00
|
|
|
template <typename U>
|
2020-03-01 14:56:40 +00:00
|
|
|
[[nodiscard]] friend bool operator == (const node_view& lhs, const toml::value<U>& rhs) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-02-20 21:08:20 +00:00
|
|
|
if (lhs.node_ == &rhs)
|
|
|
|
return true;
|
|
|
|
const auto val = lhs.as<U>();
|
|
|
|
return val && *val == rhs;
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-04-01 12:53:10 +00:00
|
|
|
#ifndef DOXYGEN
|
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const toml::value<U>&, template <typename U>)
|
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is a value with the same value as RHS.
|
|
|
|
template <typename U, typename = std::enable_if_t<impl::is_value_or_promotable<U>>>
|
|
|
|
[[nodiscard]] friend bool operator == (const node_view& lhs, const U& rhs) noexcept
|
|
|
|
{
|
|
|
|
const auto val = lhs.as<impl::promoted<U>>();
|
|
|
|
return val && *val == rhs;
|
|
|
|
}
|
2020-04-01 12:53:10 +00:00
|
|
|
#ifndef DOXYGEN
|
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(
|
|
|
|
const node_view&,
|
|
|
|
const U&,
|
|
|
|
template <typename U, typename = std::enable_if_t<impl::is_value_or_promotable<U>>>
|
|
|
|
)
|
|
|
|
#endif
|
2020-02-20 21:08:20 +00:00
|
|
|
|
|
|
|
/// \brief Returns true if the viewed node is an array with the same contents as the RHS initializer list.
|
2020-01-04 14:21:38 +00:00
|
|
|
template <typename U>
|
2020-02-20 21:08:20 +00:00
|
|
|
[[nodiscard]] friend bool operator == (const node_view& lhs, const std::initializer_list<U>& rhs) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-02-20 21:08:20 +00:00
|
|
|
const auto arr = lhs.as<array>();
|
|
|
|
return arr && *arr == rhs;
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-04-01 12:53:10 +00:00
|
|
|
#ifndef DOXYGEN
|
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::initializer_list<U>&, template <typename U>)
|
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns true if the viewed node is an array with the same contents as the RHS vector.
|
2020-01-04 14:21:38 +00:00
|
|
|
template <typename U>
|
2020-02-20 21:08:20 +00:00
|
|
|
[[nodiscard]] friend bool operator == (const node_view& lhs, const std::vector<U>& rhs) noexcept
|
|
|
|
{
|
|
|
|
const auto arr = lhs.as<array>();
|
|
|
|
return arr && *arr == rhs;
|
|
|
|
}
|
2020-04-01 12:53:10 +00:00
|
|
|
#ifndef DOXYGEN
|
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::vector<U>&, template <typename U>)
|
|
|
|
#endif
|
2020-02-20 21:08:20 +00:00
|
|
|
|
|
|
|
/// \brief Returns a view of the selected subnode.
|
|
|
|
///
|
|
|
|
/// \param key The key of the node to retrieve
|
|
|
|
///
|
|
|
|
/// \returns A view of the selected node if this node represented a table and it contained a
|
|
|
|
/// value at the given key, or an empty view.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] node_view operator[] (string_view key) const noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-02-20 21:08:20 +00:00
|
|
|
if (auto tbl = this->as_table())
|
|
|
|
return { tbl->get(key) };
|
|
|
|
return { nullptr };
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 21:08:20 +00:00
|
|
|
/// \brief Returns a view of the selected subnode.
|
|
|
|
///
|
|
|
|
/// \param index The index of the node to retrieve
|
|
|
|
///
|
|
|
|
/// \returns A view of the selected node if this node represented an array and it contained a
|
|
|
|
/// value at the given index, or an empty view.
|
2020-03-05 11:07:26 +00:00
|
|
|
[[nodiscard]] node_view operator[] (size_t index) const noexcept
|
2020-02-20 21:08:20 +00:00
|
|
|
{
|
2020-03-03 08:10:07 +00:00
|
|
|
if (auto arr = this->as_array())
|
|
|
|
return { arr->get(index) };
|
2020-02-20 21:08:20 +00:00
|
|
|
return { nullptr };
|
|
|
|
}
|
|
|
|
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Char, typename U>
|
|
|
|
friend std::basic_ostream<Char>& operator << (std::basic_ostream<Char>&, const node_view<U>&);
|
2020-01-04 14:21:38 +00:00
|
|
|
};
|
2020-03-03 08:10:07 +00:00
|
|
|
|
|
|
|
/// \brief Prints the viewed node out to a stream.
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Char, typename T>
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-04-02 21:39:21 +00:00
|
|
|
std::basic_ostream<Char>& operator << (std::basic_ostream<Char>& os, const node_view<T>& nv)
|
2020-03-03 08:10:07 +00:00
|
|
|
{
|
|
|
|
if (nv.node_)
|
|
|
|
{
|
2020-03-28 16:56:59 +00:00
|
|
|
nv.node_->visit([&os](const auto& n)
|
|
|
|
{
|
|
|
|
os << n;
|
|
|
|
});
|
2020-03-03 08:10:07 +00:00
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
2020-04-02 21:39:21 +00:00
|
|
|
|
|
|
|
#if !TOML_ALL_INLINE
|
|
|
|
extern template class TOML_API node_view<node>;
|
|
|
|
extern template class TOML_API node_view<const node>;
|
|
|
|
extern template TOML_API std::ostream& operator << (std::ostream&, const node_view<node>&);
|
|
|
|
extern template TOML_API std::ostream& operator << (std::ostream&, const node_view<const node>&);
|
|
|
|
#endif
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-03-28 16:56:59 +00:00
|
|
|
|