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-04-10 16:46:00 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-03-12 15:23:25 +00:00
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#pragma once
|
|
|
|
#include "toml_table.h"
|
|
|
|
#include "toml_array.h"
|
|
|
|
#include "toml_value.h"
|
|
|
|
|
2020-07-18 12:10:19 +00:00
|
|
|
TOML_PUSH_WARNINGS
|
|
|
|
TOML_DISABLE_ARITHMETIC_WARNINGS
|
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
TOML_NAMESPACE_START
|
2020-01-04 14:21:38 +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);
|
|
|
|
///
|
2020-08-11 13:34:03 +00:00
|
|
|
/// std::cout << tbl["title"] << "\n";
|
|
|
|
/// std::cout << tbl["products"][0]["name"] << "\n";
|
|
|
|
/// std::cout << tbl["products"][0]["keywords"] << "\n";
|
|
|
|
/// std::cout << tbl["products"][0]["keywords"][2] << "\n";
|
2020-02-20 21:08:20 +00:00
|
|
|
///
|
|
|
|
/// tbl["products"][0]["keywords"].as_array()->push_back("heavy");
|
2020-08-11 13:34:03 +00:00
|
|
|
/// std::cout << tbl["products"][0]["keywords"] << "\n";
|
|
|
|
/// std::cout << "has product[2]: "sv << !!tbl["products"][2] << "\n";
|
|
|
|
/// std::cout << "product[2]: "sv << tbl["products"][2] << "\n";
|
2020-07-13 18:18:04 +00:00
|
|
|
/// \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-07-18 12:10:19 +00:00
|
|
|
template <typename ViewedType>
|
2020-04-10 16:46:00 +00:00
|
|
|
class TOML_API TOML_TRIVIAL_ABI node_view
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-08-13 11:02:40 +00:00
|
|
|
static_assert(
|
|
|
|
impl::is_one_of<ViewedType, toml::node, const toml::node>,
|
|
|
|
"A toml::node_view<> must wrap toml::node or const toml::node."
|
|
|
|
);
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
public:
|
2020-07-18 12:10:19 +00:00
|
|
|
using viewed_type = ViewedType;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
|
|
private:
|
2020-07-27 22:04:52 +00:00
|
|
|
template <typename T> friend class TOML_NAMESPACE::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
|
|
|
|
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-08-13 11:02:40 +00:00
|
|
|
/// \brief Constructs node_view of a specific node.
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
explicit node_view(viewed_type* node) noexcept
|
|
|
|
: node_{ node }
|
|
|
|
{}
|
|
|
|
|
|
|
|
/// \brief Constructs node_view of a specific node.
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
explicit node_view(viewed_type& node) noexcept
|
|
|
|
: node_{ &node }
|
|
|
|
{}
|
|
|
|
|
2020-07-27 22:04:52 +00:00
|
|
|
///// \brief Copy constructor.
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
node_view(const node_view&) noexcept = default;
|
|
|
|
|
|
|
|
///// \brief Copy-assignment operator.
|
2020-08-08 23:12:17 +00:00
|
|
|
node_view& operator= (const node_view&) & noexcept = default;
|
2020-07-27 22:04:52 +00:00
|
|
|
|
|
|
|
///// \brief Move constructor.
|
2020-08-08 23:12:17 +00:00
|
|
|
TOML_NODISCARD_CTOR
|
2020-07-27 22:04:52 +00:00
|
|
|
node_view(node_view&&) noexcept = default;
|
|
|
|
|
2020-08-08 23:12:17 +00:00
|
|
|
///// \brief Move-assignment operator.
|
|
|
|
node_view& operator= (node_view&&) & noexcept = default;
|
2020-07-27 22:04:52 +00:00
|
|
|
|
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-07-13 18:18:04 +00:00
|
|
|
[[nodiscard]] viewed_type* node() const noexcept { return node_; }
|
|
|
|
|
2020-07-20 14:13:52 +00:00
|
|
|
[[nodiscard, deprecated("use node_view::node() instead")]]
|
2020-07-13 18:18:04 +00:00
|
|
|
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.
|
|
|
|
///
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \tparam T A TOML node or value type.
|
2020-02-20 21:08:20 +00:00
|
|
|
///
|
|
|
|
/// \returns Returns true if the viewed node is an instance of the specified type.
|
|
|
|
///
|
|
|
|
/// \see toml::node::is()
|
2020-07-18 12:10:19 +00:00
|
|
|
template <typename T>
|
2020-02-20 21:08:20 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
bool is() const noexcept
|
|
|
|
{
|
2020-07-18 12:10:19 +00:00
|
|
|
return node_ ? node_->template is<T>() : false;
|
2020-02-20 21:08:20 +00:00
|
|
|
}
|
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.
|
|
|
|
///
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \tparam T The node type or TOML value type to cast to.
|
2020-02-20 21:08:20 +00:00
|
|
|
///
|
|
|
|
/// \returns A pointer to the node as the given type, or nullptr if it was a different type.
|
|
|
|
///
|
|
|
|
/// \see toml::node::as()
|
2020-07-18 12:10:19 +00:00
|
|
|
template <typename T>
|
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
|
|
|
{
|
2020-07-20 14:13:52 +00:00
|
|
|
return node_ ? node_->template as<T>() : 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-07-18 12:10:19 +00:00
|
|
|
[[nodiscard]] auto as_string() const noexcept { return as<std::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-08-02 14:02:10 +00:00
|
|
|
/// \brief Checks if the viewed node contains values/elements of only one type.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto cfg = toml::parse("arr = [ 1, 2, 3, 4.0 ]");
|
|
|
|
///
|
|
|
|
/// toml::node* nonmatch{};
|
|
|
|
/// if (cfg["arr"].is_homogeneous(toml::node_type::integer, nonmatch))
|
|
|
|
/// std::cout << "array was homogeneous"sv << "\n";
|
|
|
|
/// else
|
|
|
|
/// std::cout << "array was not homogeneous!\n"
|
|
|
|
/// << "first non-match was a "sv << nonmatch->type() << " at " << nonmatch->source() << "\n";
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// array was not homogeneous!
|
|
|
|
/// first non-match was a floating-point at line 1, column 18
|
|
|
|
/// \eout
|
|
|
|
///
|
|
|
|
/// \param ntype A TOML node type. <br>
|
|
|
|
/// <strong><em>toml::node_type::none: </em></strong> "is every element the same type?" <br>
|
|
|
|
/// <strong><em>Anything else:</em></strong> "is every element one of these?"
|
|
|
|
/// \param first_nonmatch Reference to a pointer in which the address of the first non-matching element
|
|
|
|
/// will be stored if the return value is false.
|
|
|
|
///
|
|
|
|
/// \returns True if the viewed node was homogeneous.
|
|
|
|
///
|
|
|
|
/// \remarks Always returns `false` if the view does not reference a node, or if the viewed node is
|
|
|
|
/// an empty table or array.
|
|
|
|
[[nodiscard]]
|
|
|
|
bool is_homogeneous(node_type ntype, viewed_type*& first_nonmatch) const noexcept
|
|
|
|
{
|
|
|
|
if (!node_)
|
|
|
|
{
|
|
|
|
first_nonmatch = {};
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return node_->is_homogeneous(ntype, first_nonmatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Checks if the viewed node contains values/elements of only one type.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto cfg = toml::parse("arr = [ 1, 2, 3 ]");
|
|
|
|
/// std::cout << "homogenous: "sv << cfg["arr"].is_homogeneous(toml::node_type::none) << "\n";
|
|
|
|
/// std::cout << "all floats: "sv << cfg["arr"].is_homogeneous(toml::node_type::floating_point) << "\n";
|
|
|
|
/// std::cout << "all arrays: "sv << cfg["arr"].is_homogeneous(toml::node_type::array) << "\n";
|
|
|
|
/// std::cout << "all ints: "sv << cfg["arr"].is_homogeneous(toml::node_type::integer) << "\n";
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// homogeneous: true
|
|
|
|
/// all floats: false
|
|
|
|
/// all arrays: false
|
|
|
|
/// all ints: true
|
|
|
|
/// \eout
|
|
|
|
///
|
|
|
|
/// \param ntype A TOML node type. <br>
|
|
|
|
/// <strong><em>toml::node_type::none: </em></strong> "is every element the same type?" <br>
|
|
|
|
/// <strong><em>Anything else:</em></strong> "is every element one of these?"
|
|
|
|
///
|
|
|
|
/// \returns True if the viewed node was homogeneous.
|
|
|
|
///
|
|
|
|
/// \remarks Always returns `false` if the view does not reference a node, or if the viewed node is
|
|
|
|
/// an empty table or array.
|
|
|
|
[[nodiscard]]
|
|
|
|
bool is_homogeneous(node_type ntype) const noexcept
|
|
|
|
{
|
|
|
|
return node_ ? node_->is_homogeneous(ntype) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Checks if the viewed node contains values/elements of only one type.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto cfg = toml::parse("arr = [ 1, 2, 3 ]");
|
|
|
|
/// std::cout << "homogenous: "sv << cfg["arr"].is_homogeneous() << "\n";
|
|
|
|
/// std::cout << "all doubles: "sv << cfg["arr"].is_homogeneous<double>() << "\n";
|
|
|
|
/// std::cout << "all arrays: "sv << cfg["arr"].is_homogeneous<toml::array>() << "\n";
|
|
|
|
/// std::cout << "all integers: "sv << cfg["arr"].is_homogeneous<int64_t>() << "\n";
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// homogeneous: true
|
|
|
|
/// all floats: false
|
|
|
|
/// all arrays: false
|
|
|
|
/// all ints: true
|
|
|
|
/// \eout
|
|
|
|
///
|
|
|
|
/// \tparam ElemType A TOML node or value type. <br>
|
|
|
|
/// <strong><em>Left as `void`:</em></strong> "is every element the same type?" <br>
|
|
|
|
/// <strong><em>Explicitly specified:</em></strong> "is every element a T?"
|
|
|
|
///
|
|
|
|
/// \returns True if the viewed node was homogeneous.
|
|
|
|
///
|
|
|
|
/// \remarks Always returns `false` if the view does not reference a node, or if the viewed node is
|
|
|
|
/// an empty table or array.
|
|
|
|
template <typename ElemType = void>
|
|
|
|
[[nodiscard]]
|
|
|
|
bool is_homogeneous() const noexcept
|
|
|
|
{
|
|
|
|
return node_ ? node_->template is_homogeneous<impl::unwrap_node<ElemType>>() : false;
|
|
|
|
}
|
|
|
|
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \brief Gets the value contained by the referenced node.
|
2020-07-25 17:37:30 +00:00
|
|
|
///
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \detail This function has 'exact' retrieval semantics; the only return value types allowed are the
|
|
|
|
/// TOML native value types, or types that can losslessly represent a native value type (e.g.
|
|
|
|
/// std::wstring on Windows).
|
|
|
|
///
|
|
|
|
/// \tparam T One of the native TOML value types, or a type capable of losslessly representing one.
|
|
|
|
///
|
|
|
|
/// \returns The underlying value if the node was a value of the
|
|
|
|
/// matching type (or losslessly convertible to it), or an empty optional.
|
|
|
|
///
|
|
|
|
/// \see node_view::value()
|
|
|
|
template <typename T>
|
2020-07-17 13:33:56 +00:00
|
|
|
[[nodiscard]]
|
2020-07-18 12:10:19 +00:00
|
|
|
optional<T> value_exact() const noexcept
|
2020-07-17 13:33:56 +00:00
|
|
|
{
|
|
|
|
if (node_)
|
2020-07-18 12:10:19 +00:00
|
|
|
return node_->template value_exact<T>();
|
2020-07-17 13:33:56 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-06 12:57:49 +00:00
|
|
|
TOML_PUSH_WARNINGS
|
|
|
|
TOML_DISABLE_INIT_WARNINGS
|
|
|
|
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \brief Gets the value contained by the referenced node.
|
|
|
|
///
|
|
|
|
/// \detail This function has 'permissive' retrieval semantics; some value types are allowed
|
|
|
|
/// to convert to others (e.g. retrieving a boolean as an integer), and the specified return value
|
|
|
|
/// type can be any type where a reasonable conversion from a native TOML value exists
|
|
|
|
/// (e.g. std::wstring on Windows). If the source value cannot be represented by
|
|
|
|
/// the destination type, an empty optional is returned. See node::value() for examples.
|
2020-02-22 14:10:32 +00:00
|
|
|
///
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \tparam T One of the native TOML value types, or a type capable of convertible to one.
|
2020-03-01 14:56:40 +00:00
|
|
|
///
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \returns The underlying value if the node was a value of the matching type (or convertible to it)
|
|
|
|
/// and within the range of the output type, or an empty optional.
|
2020-02-22 14:10:32 +00:00
|
|
|
///
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \attention If you want strict value retrieval semantics that do not allow for any type conversions,
|
|
|
|
/// use node_view::value_exact() instead.
|
|
|
|
///
|
|
|
|
/// \see
|
|
|
|
/// - node_view::value()
|
|
|
|
/// - node_view::value_exact()
|
|
|
|
template <typename T>
|
2020-07-17 13:33:56 +00:00
|
|
|
[[nodiscard]]
|
2020-07-18 12:10:19 +00:00
|
|
|
optional<T> value() const noexcept
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
|
|
|
if (node_)
|
2020-07-18 12:10:19 +00:00
|
|
|
return node_->template value<T>();
|
2020-03-01 14:56:40 +00:00
|
|
|
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-07-18 12:10:19 +00:00
|
|
|
/// \tparam T Default value type. Must be one of the native TOML value types,
|
|
|
|
/// or convertible to it.
|
|
|
|
/// \param default_value The default value to return 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-07-18 12:10:19 +00:00
|
|
|
/// \returns The underlying value if the node was a value of the matching type (or convertible to it)
|
|
|
|
/// and within the range of the output type, or the provided default.
|
|
|
|
///
|
|
|
|
/// \attention This function has the same permissive retrieval semantics as node::value(). If you want strict
|
|
|
|
/// value retrieval semantics that do not allow for any type conversions, use node_view::value_exact()
|
|
|
|
/// instead.
|
2020-03-01 14:56:40 +00:00
|
|
|
///
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \see
|
|
|
|
/// - node_view::value()
|
|
|
|
/// - node_view::value_exact()
|
|
|
|
template <typename T>
|
2020-07-17 13:33:56 +00:00
|
|
|
[[nodiscard]]
|
2020-07-18 12:10:19 +00:00
|
|
|
auto value_or(T&& default_value) const noexcept
|
2020-02-22 14:10:32 +00:00
|
|
|
{
|
2020-07-17 13:33:56 +00:00
|
|
|
using namespace ::toml::impl;
|
|
|
|
|
|
|
|
static_assert(
|
2020-07-18 12:10:19 +00:00
|
|
|
!is_wide_string<T> || TOML_WINDOWS_COMPAT,
|
|
|
|
"Retrieving values as wide-character strings is only "
|
|
|
|
"supported on Windows with TOML_WINDOWS_COMPAT enabled."
|
2020-07-17 13:33:56 +00:00
|
|
|
);
|
|
|
|
|
2020-07-18 12:10:19 +00:00
|
|
|
if constexpr (is_wide_string<T>)
|
2020-07-17 13:33:56 +00:00
|
|
|
{
|
|
|
|
#if TOML_WINDOWS_COMPAT
|
|
|
|
|
|
|
|
if (node_)
|
2020-07-18 12:10:19 +00:00
|
|
|
return node_->value_or(std::forward<T>(default_value));
|
|
|
|
return std::wstring{ std::forward<T>(default_value) };
|
2020-07-17 13:33:56 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-07-18 12:10:19 +00:00
|
|
|
static_assert(impl::dependent_false<T>, "Evaluated unreachable branch!");
|
2020-07-17 13:33:56 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
using value_type = std::conditional_t<
|
2020-07-18 12:10:19 +00:00
|
|
|
std::is_pointer_v<std::decay_t<T>>,
|
|
|
|
std::add_pointer_t<std::add_const_t<std::remove_pointer_t<std::decay_t<T>>>>,
|
|
|
|
std::decay_t<T>
|
2020-07-17 13:33:56 +00:00
|
|
|
>;
|
|
|
|
|
|
|
|
if (node_)
|
2020-07-18 12:10:19 +00:00
|
|
|
return node_->value_or(std::forward<T>(default_value));
|
2020-07-17 13:33:56 +00:00
|
|
|
if constexpr (std::is_pointer_v<value_type>)
|
|
|
|
return value_type{ default_value };
|
|
|
|
else
|
2020-07-18 12:10:19 +00:00
|
|
|
return std::forward<T>(default_value);
|
2020-07-17 13:33:56 +00:00
|
|
|
}
|
2020-02-22 14:10:32 +00:00
|
|
|
}
|
|
|
|
|
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"(
|
2020-06-28 12:26:18 +00:00
|
|
|
/// min = 32
|
|
|
|
/// max = 45
|
2020-03-05 11:07:26 +00:00
|
|
|
/// )"sv);
|
2020-02-20 21:08:20 +00:00
|
|
|
///
|
2020-06-28 12:26:18 +00:00
|
|
|
/// int64_t& min_ref = tbl["min"].ref<int64_t>(); // matching type
|
|
|
|
/// double& max_ref = tbl["max"].ref<double>(); // mismatched type, hits assert()
|
|
|
|
/// int64_t& foo_ref = tbl["foo"].ref<int64_t>(); // nonexistent key, hits assert()
|
2020-03-05 11:07:26 +00:00
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-07-18 12:10:19 +00:00
|
|
|
/// \tparam T One of the TOML value types.
|
2020-03-05 11:07:26 +00:00
|
|
|
///
|
|
|
|
/// \returns A reference to the underlying data.
|
2020-07-18 12:10:19 +00:00
|
|
|
template <typename T>
|
|
|
|
[[nodiscard]]
|
|
|
|
decltype(auto) ref() const noexcept
|
2020-02-20 21:08:20 +00:00
|
|
|
{
|
2020-03-05 11:07:26 +00:00
|
|
|
TOML_ASSERT(
|
|
|
|
node_
|
|
|
|
&& "toml::node_view::ref() called on a node_view that did not reference a node"
|
|
|
|
);
|
2020-07-20 14:13:52 +00:00
|
|
|
return node_->template ref<impl::unwrap_node<T>>();
|
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.
|
2020-07-18 12:10:19 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
friend bool operator == (const node_view& lhs, const table& rhs) noexcept
|
2020-02-20 21:08:20 +00:00
|
|
|
{
|
|
|
|
if (lhs.node_ == &rhs)
|
|
|
|
return true;
|
|
|
|
const auto tbl = lhs.as<table>();
|
|
|
|
return tbl && *tbl == rhs;
|
|
|
|
}
|
2020-04-10 16:46:00 +00:00
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const table&, )
|
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.
|
2020-07-18 12:10:19 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
friend bool operator == (const node_view& lhs, const array& rhs) noexcept
|
2020-02-20 21:08:20 +00:00
|
|
|
{
|
|
|
|
if (lhs.node_ == &rhs)
|
|
|
|
return true;
|
|
|
|
const auto arr = lhs.as<array>();
|
|
|
|
return arr && *arr == rhs;
|
|
|
|
}
|
2020-04-10 16:46:00 +00:00
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const array&, )
|
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-07-18 12:10:19 +00:00
|
|
|
template <typename T>
|
|
|
|
[[nodiscard]]
|
|
|
|
friend bool operator == (const node_view& lhs, const toml::value<T>& rhs) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-02-20 21:08:20 +00:00
|
|
|
if (lhs.node_ == &rhs)
|
|
|
|
return true;
|
2020-07-18 12:10:19 +00:00
|
|
|
const auto val = lhs.as<T>();
|
2020-02-20 21:08:20 +00:00
|
|
|
return val && *val == rhs;
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-07-18 12:10:19 +00:00
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const toml::value<T>&, template <typename T>)
|
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-07-18 12:10:19 +00:00
|
|
|
template <typename T, typename = std::enable_if_t<
|
|
|
|
impl::is_native<T>
|
|
|
|
|| impl::is_losslessly_convertible_to_native<T>
|
2020-07-17 13:33:56 +00:00
|
|
|
>>
|
2020-07-18 12:10:19 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
friend bool operator == (const node_view& lhs, const T& rhs) noexcept
|
2020-02-20 21:08:20 +00:00
|
|
|
{
|
2020-07-13 18:18:04 +00:00
|
|
|
static_assert(
|
2020-07-18 12:10:19 +00:00
|
|
|
!impl::is_wide_string<T> || TOML_WINDOWS_COMPAT,
|
|
|
|
"Comparison with wide-character strings is only "
|
|
|
|
"supported on Windows with TOML_WINDOWS_COMPAT enabled."
|
2020-07-13 18:18:04 +00:00
|
|
|
);
|
|
|
|
|
2020-07-18 12:10:19 +00:00
|
|
|
if constexpr (impl::is_wide_string<T>)
|
2020-07-17 13:33:56 +00:00
|
|
|
{
|
|
|
|
#if TOML_WINDOWS_COMPAT
|
2020-07-13 18:18:04 +00:00
|
|
|
return lhs == impl::narrow(rhs);
|
2020-07-17 13:33:56 +00:00
|
|
|
#else
|
2020-07-18 12:10:19 +00:00
|
|
|
static_assert(impl::dependent_false<T>, "Evaluated unreachable branch!");
|
2020-07-17 13:33:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
2020-07-13 18:18:04 +00:00
|
|
|
else
|
|
|
|
{
|
2020-07-18 12:10:19 +00:00
|
|
|
const auto val = lhs.as<impl::native_type_of<T>>();
|
2020-07-13 18:18:04 +00:00
|
|
|
return val && *val == rhs;
|
|
|
|
}
|
2020-02-20 21:08:20 +00:00
|
|
|
}
|
2020-04-10 16:46:00 +00:00
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(
|
2020-07-13 18:18:04 +00:00
|
|
|
const node_view&,
|
2020-07-18 12:10:19 +00:00
|
|
|
const T&,
|
|
|
|
template <typename T, typename = std::enable_if_t<
|
|
|
|
impl::is_native<T>
|
|
|
|
|| impl::is_losslessly_convertible_to_native<T>
|
2020-07-17 13:33:56 +00:00
|
|
|
>>
|
2020-04-10 16:46:00 +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 initializer list.
|
2020-07-18 12:10:19 +00:00
|
|
|
template <typename T>
|
|
|
|
[[nodiscard]]
|
|
|
|
friend bool operator == (const node_view& lhs, const std::initializer_list<T>& 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-07-18 12:10:19 +00:00
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::initializer_list<T>&, template <typename T>)
|
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-07-18 12:10:19 +00:00
|
|
|
template <typename T>
|
|
|
|
[[nodiscard]]
|
|
|
|
friend bool operator == (const node_view& lhs, const std::vector<T>& rhs) noexcept
|
2020-02-20 21:08:20 +00:00
|
|
|
{
|
|
|
|
const auto arr = lhs.as<array>();
|
|
|
|
return arr && *arr == rhs;
|
|
|
|
}
|
2020-07-18 12:10:19 +00:00
|
|
|
TOML_ASYMMETRICAL_EQUALITY_OPS(const node_view&, const std::vector<T>&, template <typename T>)
|
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-07-18 12:10:19 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
node_view operator[] (std::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())
|
2020-08-13 11:02:40 +00:00
|
|
|
return node_view{ tbl->get(key) };
|
|
|
|
return node_view{ nullptr };
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 18:18:04 +00:00
|
|
|
#if TOML_WINDOWS_COMPAT
|
|
|
|
|
|
|
|
/// \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.
|
|
|
|
///
|
|
|
|
/// \attention This overload is only available when #TOML_WINDOWS_COMPAT is enabled.
|
2020-07-18 12:10:19 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
node_view operator[] (std::wstring_view key) const noexcept
|
2020-07-13 18:18:04 +00:00
|
|
|
{
|
|
|
|
if (auto tbl = this->as_table())
|
2020-08-13 11:02:40 +00:00
|
|
|
return node_view{ tbl->get(key) };
|
|
|
|
return node_view{ nullptr };
|
2020-07-13 18:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TOML_WINDOWS_COMPAT
|
|
|
|
|
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-07-18 12:10:19 +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())
|
2020-08-13 11:02:40 +00:00
|
|
|
return node_view{ arr->get(index) };
|
|
|
|
return node_view{ nullptr };
|
2020-02-20 21:08:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 12:10:19 +00:00
|
|
|
template <typename Char, typename T>
|
|
|
|
friend std::basic_ostream<Char>& operator << (std::basic_ostream<Char>&, const node_view<T>&);
|
2020-01-04 14:21:38 +00:00
|
|
|
};
|
2020-08-13 11:02:40 +00:00
|
|
|
template <typename T> node_view(const value<T>&) -> node_view<const node>;
|
|
|
|
node_view(const table&) -> node_view<const node>;
|
|
|
|
node_view(const array&) -> node_view<const node>;
|
|
|
|
template <typename T> node_view(value<T>&) -> node_view<node>;
|
|
|
|
node_view(table&) -> node_view<node>;
|
|
|
|
node_view(array&) -> node_view<node>;
|
|
|
|
template <typename T> node_view(const T*) -> node_view<const node>;
|
|
|
|
template <typename T> node_view(T*) -> node_view<node>;
|
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-07-18 12:10:19 +00:00
|
|
|
inline 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
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
#if !defined(DOXYGEN) && !TOML_HEADER_ONLY
|
2020-07-18 12:10:19 +00:00
|
|
|
|
|
|
|
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>&);
|
|
|
|
|
|
|
|
#define TOML_EXTERN(name, T) \
|
|
|
|
extern template TOML_API optional<T> node_view<node>::name<T>() const noexcept; \
|
|
|
|
extern template TOML_API optional<T> node_view<const node>::name<T>() const noexcept
|
|
|
|
TOML_EXTERN(value_exact, std::string_view);
|
|
|
|
TOML_EXTERN(value_exact, std::string);
|
|
|
|
TOML_EXTERN(value_exact, const char*);
|
|
|
|
TOML_EXTERN(value_exact, int64_t);
|
|
|
|
TOML_EXTERN(value_exact, double);
|
|
|
|
TOML_EXTERN(value_exact, date);
|
|
|
|
TOML_EXTERN(value_exact, time);
|
|
|
|
TOML_EXTERN(value_exact, date_time);
|
|
|
|
TOML_EXTERN(value_exact, bool);
|
|
|
|
TOML_EXTERN(value, std::string_view);
|
|
|
|
TOML_EXTERN(value, std::string);
|
|
|
|
TOML_EXTERN(value, const char*);
|
|
|
|
TOML_EXTERN(value, signed char);
|
|
|
|
TOML_EXTERN(value, signed short);
|
|
|
|
TOML_EXTERN(value, signed int);
|
|
|
|
TOML_EXTERN(value, signed long);
|
|
|
|
TOML_EXTERN(value, signed long long);
|
|
|
|
TOML_EXTERN(value, unsigned char);
|
|
|
|
TOML_EXTERN(value, unsigned short);
|
|
|
|
TOML_EXTERN(value, unsigned int);
|
|
|
|
TOML_EXTERN(value, unsigned long);
|
|
|
|
TOML_EXTERN(value, unsigned long long);
|
|
|
|
TOML_EXTERN(value, double);
|
|
|
|
TOML_EXTERN(value, float);
|
|
|
|
TOML_EXTERN(value, date);
|
|
|
|
TOML_EXTERN(value, time);
|
|
|
|
TOML_EXTERN(value, date_time);
|
|
|
|
TOML_EXTERN(value, bool);
|
|
|
|
#ifdef __cpp_lib_char8_t
|
|
|
|
TOML_EXTERN(value_exact, std::u8string_view);
|
|
|
|
TOML_EXTERN(value_exact, std::u8string);
|
|
|
|
TOML_EXTERN(value_exact, const char8_t*);
|
|
|
|
TOML_EXTERN(value, std::u8string_view);
|
|
|
|
TOML_EXTERN(value, std::u8string);
|
|
|
|
TOML_EXTERN(value, const char8_t*);
|
2020-04-02 21:39:21 +00:00
|
|
|
#endif
|
2020-07-18 12:10:19 +00:00
|
|
|
#if TOML_WINDOWS_COMPAT
|
|
|
|
TOML_EXTERN(value_exact, std::wstring);
|
|
|
|
TOML_EXTERN(value, std::wstring);
|
|
|
|
#endif
|
|
|
|
#undef TOML_EXTERN
|
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
#endif // !TOML_HEADER_ONLY
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-07-25 17:37:30 +00:00
|
|
|
TOML_NAMESPACE_END
|
2020-03-28 16:56:59 +00:00
|
|
|
|
2020-07-18 12:10:19 +00:00
|
|
|
TOML_POP_WARNINGS // TOML_DISABLE_ARITHMETIC_WARNINGS
|