2021-10-23 09:22:41 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2021-11-03 16:19:48 +00:00
|
|
|
#include "std_utility.h"
|
2021-11-06 16:59:47 +00:00
|
|
|
#include "forward_declarations.h"
|
2021-10-24 22:04:23 +00:00
|
|
|
#include "source_region.h"
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_start.h"
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
TOML_NAMESPACE_START
|
|
|
|
{
|
|
|
|
/// \brief A TOML node.
|
|
|
|
///
|
|
|
|
/// \detail A parsed TOML document forms a tree made up of tables, arrays and values.
|
|
|
|
/// This type is the base of each of those, providing a lot of the polymorphic plumbing.
|
2022-02-12 12:25:43 +00:00
|
|
|
class TOML_ABSTRACT_BASE TOML_EXPORTED_CLASS node
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
private:
|
2021-10-24 22:04:23 +00:00
|
|
|
/// \cond
|
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
friend class TOML_PARSER_TYPENAME;
|
|
|
|
source_region source_{};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TOML_NODISCARD
|
2021-10-26 18:03:56 +00:00
|
|
|
decltype(auto) get_value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
template <typename T, typename N>
|
|
|
|
using ref_type_ = std::conditional_t< //
|
|
|
|
std::is_reference_v<T>, //
|
|
|
|
impl::copy_ref<impl::copy_cv<impl::unwrap_node<T>, std::remove_reference_t<N>>, T>, //
|
|
|
|
impl::copy_cvref<impl::unwrap_node<T>, N> //
|
|
|
|
>;
|
|
|
|
|
|
|
|
template <typename T, typename N>
|
|
|
|
using ref_type = std::conditional_t< //
|
|
|
|
std::is_reference_v<N>, //
|
|
|
|
ref_type_<T, N>, //
|
|
|
|
ref_type_<T, std::add_lvalue_reference_t<N>> //
|
|
|
|
>;
|
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
template <typename T, typename N>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_GETTER
|
2021-11-14 17:24:19 +00:00
|
|
|
static ref_type<T, N&&> do_ref(N&& n) noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
using unwrapped_type = impl::unwrap_node<T>;
|
|
|
|
static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
|
2021-10-23 09:22:41 +00:00
|
|
|
"The template type argument of node::ref() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
|
2021-11-14 17:24:19 +00:00
|
|
|
|
|
|
|
TOML_ASSERT_ASSUME(
|
|
|
|
n.template is<unwrapped_type>()
|
|
|
|
&& "template type argument provided to toml::node::ref() didn't match the node's actual type");
|
|
|
|
|
|
|
|
using node_ref = std::remove_volatile_t<std::remove_reference_t<N>>&;
|
|
|
|
using val_type = std::remove_volatile_t<unwrapped_type>;
|
|
|
|
using out_ref = ref_type<T, N&&>;
|
|
|
|
static_assert(std::is_reference_v<out_ref>);
|
|
|
|
|
|
|
|
if constexpr (toml::is_value<unwrapped_type>)
|
|
|
|
return static_cast<out_ref>(const_cast<node_ref>(n).template ref_cast<val_type>().get());
|
2021-10-23 09:22:41 +00:00
|
|
|
else
|
2021-11-14 17:24:19 +00:00
|
|
|
return static_cast<out_ref>(const_cast<node_ref>(n).template ref_cast<val_type>());
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
|
|
|
node() noexcept;
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-23 09:22:41 +00:00
|
|
|
node(const node&) noexcept;
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-23 09:22:41 +00:00
|
|
|
node(node&&) noexcept;
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-23 09:22:41 +00:00
|
|
|
node& operator=(const node&) noexcept;
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-23 09:22:41 +00:00
|
|
|
node& operator=(node&&) noexcept;
|
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
template <typename T, typename N>
|
|
|
|
using ref_cast_type_ = std::conditional_t< //
|
|
|
|
std::is_reference_v<T>, //
|
|
|
|
impl::copy_ref<impl::copy_cv<impl::wrap_node<T>, std::remove_reference_t<N>>, T>, //
|
|
|
|
impl::copy_cvref<impl::wrap_node<T>, N> //
|
|
|
|
>;
|
|
|
|
|
|
|
|
template <typename T, typename N>
|
|
|
|
using ref_cast_type = std::conditional_t< //
|
|
|
|
std::is_reference_v<N>, //
|
|
|
|
ref_cast_type_<T, N>, //
|
|
|
|
ref_cast_type_<T, std::add_lvalue_reference_t<N>> //
|
|
|
|
>;
|
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
template <typename T>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-11-14 17:24:19 +00:00
|
|
|
ref_cast_type<T, node&> ref_cast() & noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
using out_ref = ref_cast_type<T, node&>;
|
|
|
|
using out_type = std::remove_reference_t<out_ref>;
|
|
|
|
return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-11-14 17:24:19 +00:00
|
|
|
ref_cast_type<T, node&&> ref_cast() && noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
using out_ref = ref_cast_type<T, node&&>;
|
|
|
|
using out_type = std::remove_reference_t<out_ref>;
|
|
|
|
return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-11-14 17:24:19 +00:00
|
|
|
ref_cast_type<T, const node&> ref_cast() const& noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
using out_ref = ref_cast_type<T, const node&>;
|
|
|
|
using out_type = std::remove_reference_t<out_ref>;
|
|
|
|
return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
template <typename T>
|
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
ref_cast_type<T, const node&&> ref_cast() const&& noexcept
|
|
|
|
{
|
|
|
|
using out_ref = ref_cast_type<T, const node&&>;
|
|
|
|
using out_type = std::remove_reference_t<out_ref>;
|
|
|
|
return static_cast<out_ref>(*reinterpret_cast<out_type*>(this));
|
|
|
|
}
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
/// \endcond
|
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
public:
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
virtual ~node() noexcept;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
/// \name Type checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/// \brief Checks if a node contains values/elements of only one type.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto cfg = toml::parse("arr = [ 1, 2, 3, 4.0 ]");
|
|
|
|
/// toml::array& arr = *cfg["arr"].as_array();
|
|
|
|
///
|
|
|
|
/// toml::node* nonmatch{};
|
|
|
|
/// if (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>
|
|
|
|
/// \conditional_return{toml::node_type::none}
|
|
|
|
/// "is every element the same type?"
|
|
|
|
/// \conditional_return{Anything else}
|
|
|
|
/// "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 node was homogeneous.
|
|
|
|
///
|
|
|
|
/// \remarks Always returns `false` for empty tables and arrays.
|
2021-10-30 14:26:05 +00:00
|
|
|
TOML_PURE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
virtual bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Checks if a node contains values/elements of only one type (const overload).
|
2021-10-30 14:26:05 +00:00
|
|
|
TOML_PURE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
virtual bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Checks if the node contains values/elements of only one type.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto arr = toml::array{ 1, 2, 3 };
|
|
|
|
/// std::cout << "homogenous: "sv << arr.is_homogeneous(toml::node_type::none) << "\n";
|
|
|
|
/// std::cout << "all floats: "sv << arr.is_homogeneous(toml::node_type::floating_point) << "\n";
|
|
|
|
/// std::cout << "all arrays: "sv << arr.is_homogeneous(toml::node_type::array) << "\n";
|
|
|
|
/// std::cout << "all ints: "sv << 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>
|
|
|
|
/// \conditional_return{toml::node_type::none}
|
|
|
|
/// "is every element the same type?"
|
|
|
|
/// \conditional_return{Anything else}
|
|
|
|
/// "is every element one of these?"
|
|
|
|
///
|
|
|
|
/// \returns True if the node was homogeneous.
|
|
|
|
///
|
|
|
|
/// \remarks Always returns `false` for empty tables and arrays.
|
2021-10-30 14:26:05 +00:00
|
|
|
TOML_PURE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
virtual bool is_homogeneous(node_type ntype) const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Checks if the node contains values/elements of only one type.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto arr = toml::array{ 1, 2, 3 };
|
|
|
|
/// std::cout << "homogenous: "sv << arr.is_homogeneous() << "\n";
|
|
|
|
/// std::cout << "all doubles: "sv << arr.is_homogeneous<double>() << "\n";
|
|
|
|
/// std::cout << "all arrays: "sv << arr.is_homogeneous<toml::array>() << "\n";
|
|
|
|
/// std::cout << "all integers: "sv << 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>
|
|
|
|
/// \conditional_return{Left as `void`}
|
|
|
|
/// "is every element the same type?" <br>
|
|
|
|
/// \conditional_return{Explicitly specified}
|
|
|
|
/// "is every element a T?"
|
|
|
|
///
|
|
|
|
/// \returns True if the node was homogeneous.
|
|
|
|
///
|
|
|
|
/// \remarks Always returns `false` for empty tables and arrays.
|
|
|
|
template <typename ElemType = void>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
bool is_homogeneous() const noexcept
|
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
using unwrapped_type = impl::unwrap_node<impl::remove_cvref<ElemType>>;
|
|
|
|
static_assert(std::is_void_v<unwrapped_type> //
|
|
|
|
|| (toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>),
|
|
|
|
"The template type argument of node::is_homogeneous() must be void or one "
|
|
|
|
"of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
|
|
|
|
|
|
|
|
return is_homogeneous(impl::node_type_of<unwrapped_type>);
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns the node's type identifier.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual node_type type() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is a table.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_table() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is an array.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_array() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is an array containing only tables.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_array_of_tables() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is a value.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_value() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is a string value.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_string() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is an integer value.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_integer() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is an floating-point value.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_floating_point() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is an integer or floating-point value.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_number() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is a boolean value.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_boolean() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is a local date value.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_date() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is a local time value.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_time() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Returns true if this node is a date-time value.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual bool is_date_time() const noexcept = 0;
|
|
|
|
|
|
|
|
/// \brief Checks if a node is a specific type.
|
|
|
|
///
|
|
|
|
/// \tparam T A TOML node or value type.
|
|
|
|
///
|
|
|
|
/// \returns Returns true if this node is an instance of the specified type.
|
|
|
|
template <typename T>
|
|
|
|
TOML_PURE_INLINE_GETTER
|
|
|
|
bool is() const noexcept
|
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
using unwrapped_type = impl::unwrap_node<impl::remove_cvref<T>>;
|
|
|
|
static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
|
2021-10-30 14:26:05 +00:00
|
|
|
"The template type argument of node::is() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
|
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
if constexpr (std::is_same_v<unwrapped_type, table>)
|
2021-10-30 14:26:05 +00:00
|
|
|
return is_table();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, array>)
|
2021-10-30 14:26:05 +00:00
|
|
|
return is_array();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, std::string>)
|
2021-10-30 14:26:05 +00:00
|
|
|
return is_string();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, int64_t>)
|
2021-10-30 14:26:05 +00:00
|
|
|
return is_integer();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, double>)
|
2021-10-30 14:26:05 +00:00
|
|
|
return is_floating_point();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, bool>)
|
2021-10-30 14:26:05 +00:00
|
|
|
return is_boolean();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, date>)
|
2021-10-30 14:26:05 +00:00
|
|
|
return is_date();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, time>)
|
2021-10-30 14:26:05 +00:00
|
|
|
return is_time();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, date_time>)
|
2021-10-30 14:26:05 +00:00
|
|
|
return is_date_time();
|
|
|
|
}
|
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Type casts
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/// \brief Returns a pointer to the node as a toml::table, if it is one.
|
2021-10-30 14:26:05 +00:00
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual table* as_table() noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
/// \brief Returns a pointer to the node as a toml::array, if it is one.
|
2021-10-30 14:26:05 +00:00
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual array* as_array() noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a pointer to the node as a toml::value<std::string>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual toml::value<std::string>* as_string() noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
/// \brief Returns a pointer to the node as a toml::value<int64_t>, if it is one.
|
2021-10-30 14:26:05 +00:00
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual toml::value<int64_t>* as_integer() noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
/// \brief Returns a pointer to the node as a toml::value<double>, if it is one.
|
2021-10-30 14:26:05 +00:00
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual toml::value<double>* as_floating_point() noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
/// \brief Returns a pointer to the node as a toml::value<bool>, if it is one.
|
2021-10-30 14:26:05 +00:00
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual toml::value<bool>* as_boolean() noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a pointer to the node as a toml::value<toml::date>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual toml::value<date>* as_date() noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a pointer to the node as a toml::value<toml::time>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual toml::value<time>* as_time() noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a pointer to the node as a toml::value<toml::date_time>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual toml::value<date_time>* as_date_time() noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a const-qualified pointer to the node as a toml::table, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual const table* as_table() const noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a const-qualified pointer to the node as a toml::array, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual const array* as_array() const noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a const-qualified pointer to the node as a toml::value<std::string>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual const toml::value<std::string>* as_string() const noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a const-qualified pointer to the node as a toml::value<int64_t>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual const toml::value<int64_t>* as_integer() const noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a const-qualified pointer to the node as a toml::value<double>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual const toml::value<double>* as_floating_point() const noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a const-qualified pointer to the node as a toml::value<bool>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual const toml::value<bool>* as_boolean() const noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a const-qualified pointer to the node as a toml::value<toml::date>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual const toml::value<date>* as_date() const noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a const-qualified pointer to the node as a toml::value<toml::time>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual const toml::value<time>* as_time() const noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 14:26:05 +00:00
|
|
|
/// \brief Returns a const-qualified pointer to the node as a toml::value<toml::date_time>, if it is one.
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
virtual const toml::value<date_time>* as_date_time() const noexcept = 0;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
/// \brief Gets a pointer to the node as a more specific node type.
|
|
|
|
///
|
|
|
|
/// \details \cpp
|
|
|
|
///
|
|
|
|
/// toml::value<int64_t>* int_value = node->as<int64_t>();
|
|
|
|
/// toml::table* tbl = node->as<toml::table>();
|
|
|
|
/// if (int_value)
|
|
|
|
/// std::cout << "Node is a value<int64_t>\n";
|
|
|
|
/// else if (tbl)
|
|
|
|
/// std::cout << "Node is a table\n";
|
|
|
|
///
|
|
|
|
/// // fully-qualified value node types also work (useful for template code):
|
|
|
|
/// toml::value<int64_t>* int_value2 = node->as<toml::value<int64_t>>();
|
|
|
|
/// if (int_value2)
|
|
|
|
/// std::cout << "Node is a value<int64_t>\n";
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \tparam T 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.
|
|
|
|
template <typename T>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
impl::wrap_node<T>* as() noexcept
|
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
using unwrapped_type = impl::unwrap_node<impl::remove_cvref<T>>;
|
|
|
|
static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
|
2021-10-23 09:22:41 +00:00
|
|
|
"The template type argument of node::as() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
|
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
if constexpr (std::is_same_v<unwrapped_type, table>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_table();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, array>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_array();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, std::string>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_string();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, int64_t>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_integer();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, double>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_floating_point();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, bool>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_boolean();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, date>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_date();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, time>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_time();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, date_time>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_date_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Gets a pointer to the node as a more specific node type (const overload).
|
|
|
|
template <typename T>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
const impl::wrap_node<T>* as() const noexcept
|
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
using unwrapped_type = impl::unwrap_node<impl::remove_cvref<T>>;
|
|
|
|
static_assert(toml::is_value<unwrapped_type> || toml::is_container<unwrapped_type>,
|
2021-10-23 09:22:41 +00:00
|
|
|
"The template type argument of node::as() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST);
|
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
if constexpr (std::is_same_v<unwrapped_type, table>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_table();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, array>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_array();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, std::string>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_string();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, int64_t>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_integer();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, double>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_floating_point();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, bool>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_boolean();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, date>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_date();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, time>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_time();
|
2021-11-14 17:24:19 +00:00
|
|
|
else if constexpr (std::is_same_v<unwrapped_type, date_time>)
|
2021-10-23 09:22:41 +00:00
|
|
|
return as_date_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Value retrieval
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/// \brief Gets the value contained by this node.
|
|
|
|
///
|
|
|
|
/// \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::value()
|
|
|
|
template <typename T>
|
|
|
|
TOML_NODISCARD
|
2021-10-26 18:03:56 +00:00
|
|
|
optional<T> value_exact() const noexcept(impl::value_retrieval_is_nothrow<T>);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
/// \brief Gets the value contained by this 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.
|
|
|
|
///
|
|
|
|
/// \godbolt{zzG81K}
|
|
|
|
///
|
|
|
|
/// \cpp
|
|
|
|
/// auto tbl = toml::parse(R"(
|
|
|
|
/// int = -10
|
|
|
|
/// flt = 25.0
|
|
|
|
/// pi = 3.14159
|
|
|
|
/// bool = false
|
|
|
|
/// huge = 9223372036854775807
|
|
|
|
/// str = "foo"
|
|
|
|
/// )"sv);
|
|
|
|
///
|
|
|
|
/// const auto print_value_with_typename =
|
|
|
|
/// [&](std::string_view key, std::string_view type_name, auto* dummy)
|
|
|
|
/// {
|
|
|
|
/// std::cout << "- " << std::setw(18) << std::left << type_name;
|
|
|
|
/// using type = std::remove_pointer_t<decltype(dummy)>;
|
|
|
|
/// if (std::optional<type> val = tbl.get(key)->value<type>())
|
|
|
|
/// std::cout << *val << "\n";
|
|
|
|
/// else
|
|
|
|
/// std::cout << "n/a\n";
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// #define print_value(key, T) print_value_with_typename(key, #T, (T*)nullptr)
|
|
|
|
///
|
|
|
|
/// for (auto key : { "int", "flt", "pi", "bool", "huge", "str" })
|
|
|
|
/// {
|
|
|
|
/// std::cout << tbl[key].type() << " value '" << key << "' as:\n";
|
|
|
|
/// print_value(key, bool);
|
|
|
|
/// print_value(key, int);
|
|
|
|
/// print_value(key, unsigned int);
|
|
|
|
/// print_value(key, long long);
|
|
|
|
/// print_value(key, float);
|
|
|
|
/// print_value(key, double);
|
|
|
|
/// print_value(key, std::string);
|
|
|
|
/// print_value(key, std::string_view);
|
|
|
|
/// print_value(key, const char*);
|
|
|
|
/// std::cout << "\n";
|
|
|
|
/// }
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// integer value 'int' as:
|
|
|
|
/// - bool true
|
|
|
|
/// - int -10
|
|
|
|
/// - unsigned int n/a
|
|
|
|
/// - long long -10
|
|
|
|
/// - float -10
|
|
|
|
/// - double -10
|
|
|
|
/// - std::string n/a
|
|
|
|
/// - std::string_view n/a
|
|
|
|
/// - const char* n/a
|
|
|
|
///
|
|
|
|
/// floating-point value 'flt' as:
|
|
|
|
/// - bool n/a
|
|
|
|
/// - int 25
|
|
|
|
/// - unsigned int 25
|
|
|
|
/// - long long 25
|
|
|
|
/// - float 25
|
|
|
|
/// - double 25
|
|
|
|
/// - std::string n/a
|
|
|
|
/// - std::string_view n/a
|
|
|
|
/// - const char* n/a
|
|
|
|
///
|
|
|
|
/// floating-point value 'pi' as:
|
|
|
|
/// - bool n/a
|
|
|
|
/// - int n/a
|
|
|
|
/// - unsigned int n/a
|
|
|
|
/// - long long n/a
|
|
|
|
/// - float 3.14159
|
|
|
|
/// - double 3.14159
|
|
|
|
/// - std::string n/a
|
|
|
|
/// - std::string_view n/a
|
|
|
|
/// - const char* n/a
|
|
|
|
///
|
|
|
|
/// boolean value 'bool' as:
|
|
|
|
/// - bool false
|
|
|
|
/// - int 0
|
|
|
|
/// - unsigned int 0
|
|
|
|
/// - long long 0
|
|
|
|
/// - float n/a
|
|
|
|
/// - double n/a
|
|
|
|
/// - std::string n/a
|
|
|
|
/// - std::string_view n/a
|
|
|
|
/// - const char* n/a
|
|
|
|
///
|
|
|
|
/// integer value 'huge' as:
|
|
|
|
/// - bool true
|
|
|
|
/// - int n/a
|
|
|
|
/// - unsigned int n/a
|
|
|
|
/// - long long 9223372036854775807
|
|
|
|
/// - float n/a
|
|
|
|
/// - double n/a
|
|
|
|
/// - std::string n/a
|
|
|
|
/// - std::string_view n/a
|
|
|
|
/// - const char* n/a
|
|
|
|
///
|
|
|
|
/// string value 'str' as:
|
|
|
|
/// - bool n/a
|
|
|
|
/// - int n/a
|
|
|
|
/// - unsigned int n/a
|
|
|
|
/// - long long n/a
|
|
|
|
/// - float n/a
|
|
|
|
/// - double n/a
|
|
|
|
/// - std::string foo
|
|
|
|
/// - std::string_view foo
|
|
|
|
/// - const char* foo
|
|
|
|
/// \eout
|
|
|
|
///
|
|
|
|
/// \tparam T One of the native TOML value types, or a type capable of converting to one.
|
|
|
|
///
|
|
|
|
/// \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.
|
|
|
|
///
|
|
|
|
/// \note If you want strict value retrieval semantics that do not allow for any type conversions,
|
|
|
|
/// use node::value_exact() instead.
|
|
|
|
///
|
|
|
|
/// \see node::value_exact()
|
|
|
|
template <typename T>
|
|
|
|
TOML_NODISCARD
|
2021-10-26 18:03:56 +00:00
|
|
|
optional<T> value() const noexcept(impl::value_retrieval_is_nothrow<T>);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
/// \brief Gets the raw value contained by this node, or a default.
|
|
|
|
///
|
|
|
|
/// \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.
|
|
|
|
///
|
|
|
|
/// \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.
|
|
|
|
///
|
|
|
|
/// \note 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::value_exact()
|
|
|
|
/// instead.
|
|
|
|
///
|
|
|
|
/// \see
|
|
|
|
/// - node::value()
|
|
|
|
/// - node::value_exact()
|
|
|
|
template <typename T>
|
|
|
|
TOML_NODISCARD
|
2021-10-26 18:03:56 +00:00
|
|
|
auto value_or(T&& default_value) const noexcept(impl::value_retrieval_is_nothrow<T>);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-11-15 07:06:03 +00:00
|
|
|
/// \brief Gets a raw reference to a node's underlying data.
|
2021-11-14 17:24:19 +00:00
|
|
|
///
|
2021-10-23 09:22:41 +00:00
|
|
|
/// \warning This function is dangerous if used carelessly and **WILL** break your code if the
|
|
|
|
/// chosen value type doesn't match the node's actual type. In debug builds an assertion
|
|
|
|
/// will fire when invalid accesses are attempted: \cpp
|
|
|
|
///
|
|
|
|
/// auto tbl = toml::parse(R"(
|
|
|
|
/// min = 32
|
|
|
|
/// max = 45
|
|
|
|
/// )"sv);
|
|
|
|
///
|
2021-11-15 07:06:03 +00:00
|
|
|
/// int64_t& min_ref = tbl.at("min").ref<int64_t>(); // matching type
|
|
|
|
/// double& max_ref = tbl.at("max").ref<double>(); // mismatched type, hits assert()
|
2021-10-23 09:22:41 +00:00
|
|
|
/// \ecpp
|
|
|
|
///
|
2021-11-15 07:06:03 +00:00
|
|
|
/// \note Specifying explicit ref qualifiers acts as an explicit ref-category cast,
|
|
|
|
/// whereas specifying explicit cv-ref qualifiers merges them with whatever
|
|
|
|
/// the cv qualification of the node is (to ensure cv-correctness is propagated), e.g.:
|
|
|
|
/// | node | T | return type |
|
|
|
|
/// |-------------|------------------------|------------------------------|
|
|
|
|
/// | node& | std::string | std::string& |
|
|
|
|
/// | node& | std::string&& | std::string&& |
|
|
|
|
/// | const node& | volatile std::string | const volatile std::string& |
|
|
|
|
/// | const node& | volatile std::string&& | const volatile std::string&& |
|
|
|
|
///
|
|
|
|
/// \tparam T toml::table, toml::array, or one of the TOML value types.
|
2021-10-23 09:22:41 +00:00
|
|
|
///
|
|
|
|
/// \returns A reference to the underlying data.
|
|
|
|
template <typename T>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_GETTER
|
2021-11-14 17:24:19 +00:00
|
|
|
decltype(auto) ref() & noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
return do_ref<T>(*this);
|
|
|
|
}
|
|
|
|
|
2021-11-15 07:06:03 +00:00
|
|
|
/// \brief Gets a raw reference to a node's underlying data (rvalue overload).
|
2021-10-23 09:22:41 +00:00
|
|
|
template <typename T>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_GETTER
|
2021-11-14 17:24:19 +00:00
|
|
|
decltype(auto) ref() && noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
return do_ref<T>(std::move(*this));
|
|
|
|
}
|
|
|
|
|
2021-11-15 07:06:03 +00:00
|
|
|
/// \brief Gets a raw reference to a node's underlying data (const lvalue overload).
|
2021-10-23 09:22:41 +00:00
|
|
|
template <typename T>
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_GETTER
|
2021-11-14 17:24:19 +00:00
|
|
|
decltype(auto) ref() const& noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
return do_ref<T>(*this);
|
|
|
|
}
|
|
|
|
|
2021-11-15 07:06:03 +00:00
|
|
|
/// \brief Gets a raw reference to a node's underlying data (const rvalue overload).
|
2021-11-14 17:24:19 +00:00
|
|
|
template <typename T>
|
|
|
|
TOML_PURE_GETTER
|
|
|
|
decltype(auto) ref() const&& noexcept
|
|
|
|
{
|
|
|
|
return do_ref<T>(std::move(*this));
|
|
|
|
}
|
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Metadata
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/// \brief Returns the source region responsible for generating this node during parsing.
|
2021-10-27 13:03:05 +00:00
|
|
|
TOML_PURE_INLINE_GETTER
|
2021-10-23 09:22:41 +00:00
|
|
|
const source_region& source() const noexcept
|
|
|
|
{
|
|
|
|
return source_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// \cond
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
|
|
|
template <typename Func, typename N, typename T>
|
2021-11-14 17:24:19 +00:00
|
|
|
static constexpr bool can_visit = std::is_invocable_v<Func, ref_cast_type<T, N>>;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
template <typename Func, typename N>
|
|
|
|
static constexpr bool can_visit_any =
|
|
|
|
can_visit<Func, N, table>
|
|
|
|
|| can_visit<Func, N, array>
|
|
|
|
|| can_visit<Func, N, std::string>
|
|
|
|
|| can_visit<Func, N, int64_t>
|
|
|
|
|| can_visit<Func, N, double>
|
|
|
|
|| can_visit<Func, N, bool>
|
|
|
|
|| can_visit<Func, N, date>
|
|
|
|
|| can_visit<Func, N, time>
|
|
|
|
|| can_visit<Func, N, date_time>;
|
|
|
|
|
|
|
|
template <typename Func, typename N>
|
|
|
|
static constexpr bool can_visit_all =
|
|
|
|
can_visit<Func, N, table>
|
|
|
|
&& can_visit<Func, N, array>
|
|
|
|
&& can_visit<Func, N, std::string>
|
|
|
|
&& can_visit<Func, N, int64_t>
|
|
|
|
&& can_visit<Func, N, double>
|
|
|
|
&& can_visit<Func, N, bool>
|
|
|
|
&& can_visit<Func, N, date>
|
|
|
|
&& can_visit<Func, N, time>
|
|
|
|
&& can_visit<Func, N, date_time>;
|
|
|
|
|
|
|
|
template <typename Func, typename N, typename T>
|
|
|
|
static constexpr bool visit_is_nothrow_one =
|
|
|
|
!can_visit<Func, N, T>
|
2021-11-14 17:24:19 +00:00
|
|
|
|| std::is_nothrow_invocable_v<Func, ref_cast_type<T, N>>;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
template <typename Func, typename N>
|
|
|
|
static constexpr bool visit_is_nothrow =
|
|
|
|
visit_is_nothrow_one<Func, N, table>
|
|
|
|
&& visit_is_nothrow_one<Func, N, array>
|
|
|
|
&& visit_is_nothrow_one<Func, N, std::string>
|
|
|
|
&& visit_is_nothrow_one<Func, N, int64_t>
|
|
|
|
&& visit_is_nothrow_one<Func, N, double>
|
|
|
|
&& visit_is_nothrow_one<Func, N, bool>
|
|
|
|
&& visit_is_nothrow_one<Func, N, date>
|
|
|
|
&& visit_is_nothrow_one<Func, N, time>
|
|
|
|
&& visit_is_nothrow_one<Func, N, date_time>;
|
|
|
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
template <typename Func, typename N, typename T, bool = can_visit<Func, N, T>>
|
|
|
|
struct visit_return_type_
|
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
using type = decltype(std::declval<Func>()(std::declval<ref_cast_type<T, N>>()));
|
2021-10-23 09:22:41 +00:00
|
|
|
};
|
|
|
|
template <typename Func, typename N, typename T>
|
|
|
|
struct visit_return_type_<Func, N, T, false>
|
|
|
|
{
|
|
|
|
using type = void;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Func, typename N, typename T>
|
|
|
|
using visit_return_type = typename visit_return_type_<Func, N, T>::type;
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
using nonvoid = std::conditional_t<std::is_void_v<A>, B, A>;
|
|
|
|
|
|
|
|
template <typename N, typename Func>
|
|
|
|
static decltype(auto) do_visit(N&& n, Func&& visitor) noexcept(visit_is_nothrow<Func&&, N&&>)
|
|
|
|
{
|
|
|
|
static_assert(can_visit_any<Func&&, N&&>,
|
|
|
|
"TOML node visitors must be invocable for at least one of the toml::node "
|
|
|
|
"specializations:" TOML_SA_NODE_TYPE_LIST);
|
|
|
|
|
|
|
|
switch (n.type())
|
|
|
|
{
|
|
|
|
case node_type::table:
|
|
|
|
if constexpr (can_visit<Func&&, N&&, table>)
|
|
|
|
return static_cast<Func&&>(visitor)(static_cast<N&&>(n).template ref_cast<table>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::array:
|
|
|
|
if constexpr (can_visit<Func&&, N&&, array>)
|
|
|
|
return static_cast<Func&&>(visitor)(static_cast<N&&>(n).template ref_cast<array>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::string:
|
|
|
|
if constexpr (can_visit<Func&&, N&&, std::string>)
|
|
|
|
return static_cast<Func&&>(visitor)(static_cast<N&&>(n).template ref_cast<std::string>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::integer:
|
|
|
|
if constexpr (can_visit<Func&&, N&&, int64_t>)
|
|
|
|
return static_cast<Func&&>(visitor)(static_cast<N&&>(n).template ref_cast<int64_t>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::floating_point:
|
|
|
|
if constexpr (can_visit<Func&&, N&&, double>)
|
|
|
|
return static_cast<Func&&>(visitor)(static_cast<N&&>(n).template ref_cast<double>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::boolean:
|
|
|
|
if constexpr (can_visit<Func&&, N&&, bool>)
|
|
|
|
return static_cast<Func&&>(visitor)(static_cast<N&&>(n).template ref_cast<bool>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::date:
|
|
|
|
if constexpr (can_visit<Func&&, N&&, date>)
|
|
|
|
return static_cast<Func&&>(visitor)(static_cast<N&&>(n).template ref_cast<date>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::time:
|
|
|
|
if constexpr (can_visit<Func&&, N&&, time>)
|
|
|
|
return static_cast<Func&&>(visitor)(static_cast<N&&>(n).template ref_cast<time>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::date_time:
|
|
|
|
if constexpr (can_visit<Func&&, N&&, date_time>)
|
|
|
|
return static_cast<Func&&>(visitor)(static_cast<N&&>(n).template ref_cast<date_time>());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case node_type::none: TOML_UNREACHABLE;
|
|
|
|
default: TOML_UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if constexpr (can_visit_all<Func&&, N&&>)
|
|
|
|
TOML_UNREACHABLE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// clang-format off
|
|
|
|
|
|
|
|
using return_type =
|
|
|
|
nonvoid<visit_return_type<Func&&, N&&, table>,
|
|
|
|
nonvoid<visit_return_type<Func&&, N&&, array>,
|
|
|
|
nonvoid<visit_return_type<Func&&, N&&, std::string>,
|
|
|
|
nonvoid<visit_return_type<Func&&, N&&, int64_t>,
|
|
|
|
nonvoid<visit_return_type<Func&&, N&&, double>,
|
|
|
|
nonvoid<visit_return_type<Func&&, N&&, bool>,
|
|
|
|
nonvoid<visit_return_type<Func&&, N&&, date>,
|
|
|
|
nonvoid<visit_return_type<Func&&, N&&, time>,
|
|
|
|
visit_return_type<Func&&, N&&, date_time>
|
|
|
|
>>>>>>>>;
|
|
|
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
if constexpr (!std::is_void_v<return_type>)
|
|
|
|
{
|
|
|
|
static_assert(std::is_default_constructible_v<return_type>,
|
|
|
|
"Non-exhaustive visitors must return a default-constructible type, or void");
|
|
|
|
return return_type{};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \endcond
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// \name Visitation
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/// \brief Invokes a visitor on the node based on the node's concrete type.
|
|
|
|
///
|
|
|
|
/// \details Visitation is useful when you expect
|
|
|
|
/// a node to be one of a set number of types and need
|
|
|
|
/// to handle these types differently. Using `visit()` allows
|
|
|
|
/// you to eliminate some of the casting/conversion boilerplate: \cpp
|
|
|
|
///
|
|
|
|
/// node.visit([](auto&& n)
|
|
|
|
/// {
|
|
|
|
/// if constexpr (toml::is_string<decltype(n)>)
|
|
|
|
/// do_something_with_a_string(*n)); //n is a toml::value<std::string>
|
|
|
|
/// else if constexpr (toml::is_integer<decltype(n)>)
|
|
|
|
/// do_something_with_an_int(*n); //n is a toml::value<int64_t>
|
|
|
|
/// else
|
|
|
|
/// throw std::exception{ "Expected string or integer" };
|
|
|
|
/// });
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \tparam Func A callable type invocable with one or more of the
|
|
|
|
/// toml++ node types.
|
|
|
|
///
|
|
|
|
/// \param visitor The visitor object.
|
|
|
|
///
|
|
|
|
/// \returns The return value of the visitor.
|
|
|
|
/// Can be void. Non-exhaustive visitors must return a default-constructible type.
|
|
|
|
///
|
|
|
|
/// \see https://en.wikipedia.org/wiki/Visitor_pattern
|
|
|
|
template <typename Func>
|
|
|
|
decltype(auto) visit(Func&& visitor) & noexcept(visit_is_nothrow<Func&&, node&>)
|
|
|
|
{
|
|
|
|
return do_visit(*this, static_cast<Func&&>(visitor));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Invokes a visitor on the node based on the node's concrete type (rvalue overload).
|
|
|
|
template <typename Func>
|
|
|
|
decltype(auto) visit(Func&& visitor) && noexcept(visit_is_nothrow<Func&&, node&&>)
|
|
|
|
{
|
|
|
|
return do_visit(static_cast<node&&>(*this), static_cast<Func&&>(visitor));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Invokes a visitor on the node based on the node's concrete type (const lvalue overload).
|
|
|
|
template <typename Func>
|
|
|
|
decltype(auto) visit(Func&& visitor) const& noexcept(visit_is_nothrow<Func&&, const node&>)
|
|
|
|
{
|
|
|
|
return do_visit(*this, static_cast<Func&&>(visitor));
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
/// \brief Invokes a visitor on the node based on the node's concrete type (const rvalue overload).
|
|
|
|
template <typename Func>
|
|
|
|
decltype(auto) visit(Func&& visitor) const&& noexcept(visit_is_nothrow<Func&&, const node&&>)
|
|
|
|
{
|
|
|
|
return do_visit(static_cast<const node&&>(*this), static_cast<Func&&>(visitor));
|
|
|
|
}
|
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Node views
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/// \brief Creates a node_view pointing to this node.
|
|
|
|
TOML_NODISCARD
|
|
|
|
explicit operator node_view<node>() noexcept;
|
|
|
|
|
|
|
|
/// \brief Creates a node_view pointing to this node (const overload).
|
|
|
|
TOML_NODISCARD
|
|
|
|
explicit operator node_view<const node>() const noexcept;
|
|
|
|
|
2022-01-06 11:26:01 +00:00
|
|
|
/// \brief Returns a view of the subnode matching a fully-qualified "TOML path".
|
|
|
|
///
|
2022-01-13 10:09:07 +00:00
|
|
|
/// \detail \cpp
|
|
|
|
/// auto config = toml::parse(R"(
|
|
|
|
///
|
|
|
|
/// [foo]
|
|
|
|
/// bar = [ 0, 1, 2, [ 3 ], { kek = 4 } ]
|
|
|
|
///
|
|
|
|
/// )"sv);
|
|
|
|
///
|
|
|
|
/// std::cout << config.at_path("foo.bar[2]") << "\n";
|
|
|
|
/// std::cout << config.at_path("foo.bar[3][0]") << "\n";
|
|
|
|
/// std::cout << config.at_path("foo.bar[4].kek") << "\n";
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// 2
|
|
|
|
/// 3
|
|
|
|
/// 4
|
|
|
|
/// \eout
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// \note Keys in paths are interpreted literally, so whitespace (or lack thereof) matters:
|
|
|
|
/// \cpp
|
|
|
|
/// config.at_path( "foo.bar") // same as node_view{ config }["foo"]["bar"]
|
|
|
|
/// config.at_path( "foo. bar") // same as node_view{ config }["foo"][" bar"]
|
|
|
|
/// config.at_path( "foo..bar") // same as node_view{ config }["foo"][""]["bar"]
|
|
|
|
/// config.at_path( ".foo.bar") // same as node_view{ config }[""]["foo"]["bar"]
|
|
|
|
/// \ecpp
|
|
|
|
/// <br>
|
|
|
|
/// Additionally, TOML allows '.' (period) characters to appear in keys if they are quoted strings.
|
|
|
|
/// This function makes no allowance for this, instead treating all period characters as sub-table delimiters.
|
|
|
|
/// If you have periods in your table keys, first consider:
|
|
|
|
/// 1. Not doing that
|
|
|
|
/// 2. Using node_view::operator[] instead.
|
|
|
|
///
|
|
|
|
/// \param path The "TOML path" to traverse.
|
2022-01-06 11:26:01 +00:00
|
|
|
TOML_NODISCARD
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2022-01-06 11:26:01 +00:00
|
|
|
node_view<node> at_path(std::string_view path) noexcept;
|
|
|
|
|
|
|
|
/// \brief Returns a const view of the subnode matching a fully-qualified "TOML path".
|
|
|
|
///
|
2022-01-13 10:09:07 +00:00
|
|
|
/// \see #at_path(std::string_view)
|
2022-01-06 11:26:01 +00:00
|
|
|
TOML_NODISCARD
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2022-01-06 11:26:01 +00:00
|
|
|
node_view<const node> at_path(std::string_view path) const noexcept;
|
|
|
|
|
|
|
|
#if TOML_ENABLE_WINDOWS_COMPAT
|
|
|
|
|
|
|
|
/// \brief Returns a view of the subnode matching a fully-qualified "TOML path".
|
|
|
|
///
|
|
|
|
/// \availability This overload is only available when #TOML_ENABLE_WINDOWS_COMPAT is enabled.
|
|
|
|
///
|
2022-01-13 10:09:07 +00:00
|
|
|
/// \see #at_path(std::string_view)
|
2022-01-06 11:26:01 +00:00
|
|
|
TOML_NODISCARD
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2022-01-06 11:26:01 +00:00
|
|
|
node_view<node> at_path(std::wstring_view path);
|
|
|
|
|
|
|
|
/// \brief Returns a const view of the subnode matching a fully-qualified "TOML path".
|
|
|
|
///
|
|
|
|
/// \availability This overload is only available when #TOML_ENABLE_WINDOWS_COMPAT is enabled.
|
|
|
|
///
|
2022-01-13 10:09:07 +00:00
|
|
|
/// \see #at_path(std::string_view)
|
2022-01-06 11:26:01 +00:00
|
|
|
TOML_NODISCARD
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2022-01-06 11:26:01 +00:00
|
|
|
node_view<const node> at_path(std::wstring_view path) const;
|
|
|
|
|
|
|
|
#endif // TOML_ENABLE_WINDOWS_COMPAT
|
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
/// @}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
TOML_NAMESPACE_END;
|
2021-10-24 10:21:32 +00:00
|
|
|
|
2022-01-06 11:26:01 +00:00
|
|
|
/// \cond
|
|
|
|
TOML_IMPL_NAMESPACE_START
|
|
|
|
{
|
|
|
|
TOML_PURE_GETTER
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_FREE_FUNCTION
|
2022-01-06 11:26:01 +00:00
|
|
|
bool node_deep_equality(const node*, const node*) noexcept;
|
|
|
|
}
|
|
|
|
TOML_IMPL_NAMESPACE_END;
|
|
|
|
/// \endcond
|
|
|
|
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_end.h"
|