//# This file is a part of toml++ and is subject to the the terms of the MIT license. //# Copyright (c) Mark Gillard //# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text. // SPDX-License-Identifier: MIT #pragma once #include "toml_common.h" #if defined(DOXYGEN) || TOML_SIMPLE_STATIC_ASSERT_MESSAGES #define TOML_SA_NEWLINE " " #define TOML_SA_LIST_SEP ", " #define TOML_SA_LIST_BEG " (" #define TOML_SA_LIST_END ")" #define TOML_SA_LIST_NEW " " #define TOML_SA_LIST_NXT ", " #else #define TOML_SA_NEWLINE "\n| " #define TOML_SA_LIST_SEP TOML_SA_NEWLINE " - " #define TOML_SA_LIST_BEG TOML_SA_LIST_SEP #define TOML_SA_LIST_END #define TOML_SA_LIST_NEW TOML_SA_NEWLINE TOML_SA_NEWLINE #define TOML_SA_LIST_NXT TOML_SA_LIST_NEW #endif #define TOML_SA_NATIVE_VALUE_TYPE_LIST \ TOML_SA_LIST_BEG "std::string" \ TOML_SA_LIST_SEP "int64_t" \ TOML_SA_LIST_SEP "double" \ TOML_SA_LIST_SEP "bool" \ TOML_SA_LIST_SEP "toml::date" \ TOML_SA_LIST_SEP "toml::time" \ TOML_SA_LIST_SEP "toml::date_time" \ TOML_SA_LIST_END #define TOML_SA_NODE_TYPE_LIST \ TOML_SA_LIST_BEG "toml::table" \ TOML_SA_LIST_SEP "toml::array" \ TOML_SA_LIST_SEP "toml::value" \ TOML_SA_LIST_SEP "toml::value" \ TOML_SA_LIST_SEP "toml::value" \ TOML_SA_LIST_SEP "toml::value" \ TOML_SA_LIST_SEP "toml::value" \ TOML_SA_LIST_SEP "toml::value" \ TOML_SA_LIST_SEP "toml::value" \ TOML_SA_LIST_END #define TOML_SA_UNWRAPPED_NODE_TYPE_LIST \ TOML_SA_LIST_NEW "A native TOML value type" \ TOML_SA_NATIVE_VALUE_TYPE_LIST \ \ TOML_SA_LIST_NXT "A TOML node type" \ TOML_SA_NODE_TYPE_LIST 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. class TOML_ABSTRACT_BASE TOML_API node { private: friend class TOML_PARSER_TYPENAME; source_region source_{}; /// \cond template [[nodiscard]] decltype(auto) get_value_exact() const noexcept; template [[nodiscard]] TOML_ATTR(pure) static decltype(auto) do_ref(N&& n) noexcept { using type = impl::unwrap_node; static_assert( (impl::is_native || impl::is_one_of) && !impl::is_cvref, "The template type argument of node::ref() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST ); TOML_ASSERT( n.template is() && "template type argument T provided to toml::node::ref() didn't match the node's actual type" ); if constexpr (impl::is_native) return std::forward(n).template ref_cast().get(); else return std::forward(n).template ref_cast(); } /// \endcond protected: node() noexcept = default; node(const node&) noexcept; node(node&&) noexcept; node& operator= (const node&) noexcept; node& operator= (node&&) noexcept; template [[nodiscard]] TOML_ALWAYS_INLINE TOML_ATTR(pure) impl::wrap_node& ref_cast() & noexcept { return *reinterpret_cast*>(this); } template [[nodiscard]] TOML_ALWAYS_INLINE TOML_ATTR(pure) impl::wrap_node&& ref_cast() && noexcept { return std::move(*reinterpret_cast*>(this)); } template [[nodiscard]] TOML_ALWAYS_INLINE TOML_ATTR(pure) const impl::wrap_node& ref_cast() const & noexcept { return *reinterpret_cast*>(this); } template using ref_cast_type = decltype(std::declval().template ref_cast()); public: virtual ~node() noexcept = default; /// \name Type checks /// @{ #if defined(DOXYGEN) || !TOML_ICC || TOML_ICC_CL /// \brief Returns the node's type identifier. [[nodiscard]] virtual node_type type() const noexcept = 0; #else [[nodiscard]] virtual node_type type() const noexcept { // Q: "what the fuck?" // A: https://github.com/marzer/tomlplusplus/issues/83 // tl,dr: go home ICC, you're drunk. return type(); } #endif /// \brief Returns true if this node is a table. [[nodiscard]] virtual bool is_table() const noexcept = 0; /// \brief Returns true if this node is an array. [[nodiscard]] virtual bool is_array() const noexcept = 0; /// \brief Returns true if this node is a value. [[nodiscard]] virtual bool is_value() const noexcept = 0; /// \brief Returns true if this node is a string value. [[nodiscard]] virtual bool is_string() const noexcept; /// \brief Returns true if this node is an integer value. [[nodiscard]] virtual bool is_integer() const noexcept; /// \brief Returns true if this node is an floating-point value. [[nodiscard]] virtual bool is_floating_point() const noexcept; /// \brief Returns true if this node is an integer or floating-point value. [[nodiscard]] virtual bool is_number() const noexcept; /// \brief Returns true if this node is a boolean value. [[nodiscard]] virtual bool is_boolean() const noexcept; /// \brief Returns true if this node is a local date value. [[nodiscard]] virtual bool is_date() const noexcept; /// \brief Returns true if this node is a local time value. [[nodiscard]] virtual bool is_time() const noexcept; /// \brief Returns true if this node is a date-time value. [[nodiscard]] virtual bool is_date_time() const noexcept; /// \brief Returns true if this node is an array containing only tables. [[nodiscard]] virtual bool is_array_of_tables() const noexcept; /// \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 [[nodiscard]] TOML_ATTR(pure) bool is() const noexcept { using type = impl::unwrap_node; static_assert( (impl::is_native || impl::is_one_of) && !impl::is_cvref, "The template type argument of node::is() must be one of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST ); if constexpr (std::is_same_v) return is_table(); else if constexpr (std::is_same_v) return is_array(); else if constexpr (std::is_same_v) return is_string(); else if constexpr (std::is_same_v) return is_integer(); else if constexpr (std::is_same_v) return is_floating_point(); else if constexpr (std::is_same_v) return is_boolean(); else if constexpr (std::is_same_v) return is_date(); else if constexpr (std::is_same_v) return is_time(); else if constexpr (std::is_same_v) return is_date_time(); } /// \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.
/// \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. [[nodiscard]] 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). [[nodiscard]] 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.
/// \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. [[nodiscard]] 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() << "\n"; /// std::cout << "all arrays: "sv << arr.is_homogeneous() << "\n"; /// std::cout << "all integers: "sv << arr.is_homogeneous() << "\n"; /// /// \ecpp /// /// \out /// homogeneous: true /// all floats: false /// all arrays: false /// all ints: true /// \eout /// /// \tparam ElemType A TOML node or value type.
/// \conditional_return{Left as `void`} /// "is every element the same type?"
/// \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 [[nodiscard]] TOML_ATTR(pure) bool is_homogeneous() const noexcept { using type = impl::unwrap_node; static_assert( std::is_void_v || ((impl::is_native || impl::is_one_of) && !impl::is_cvref), "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); } /// @} /// \name Type casts /// @{ /// \brief Returns a pointer to the node as a toml::table, if it is one. [[nodiscard]] virtual table* as_table() noexcept; /// \brief Returns a pointer to the node as a toml::array, if it is one. [[nodiscard]] virtual array* as_array() noexcept; /// \brief Returns a pointer to the node as a toml::value, if it is one. [[nodiscard]] virtual toml::value* as_string() noexcept; /// \brief Returns a pointer to the node as a toml::value, if it is one. [[nodiscard]] virtual toml::value* as_integer() noexcept; /// \brief Returns a pointer to the node as a toml::value, if it is one. [[nodiscard]] virtual toml::value* as_floating_point() noexcept; /// \brief Returns a pointer to the node as a toml::value, if it is one. [[nodiscard]] virtual toml::value* as_boolean() noexcept; /// \brief Returns a pointer to the node as a toml::value, if it is one. [[nodiscard]] virtual toml::value* as_date() noexcept; /// \brief Returns a pointer to the node as a toml::value