//# This file is a part of toml++ and is subject to the the terms of the MIT license. //# Copyright (c) 2019-2020 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_INTERFACE TOML_API node { private: friend class TOML_PARSER_TYPENAME; source_region source_{}; 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 impl::wrap_node& ref_cast() & noexcept { return *reinterpret_cast*>(this); } template [[nodiscard]] TOML_ALWAYS_INLINE impl::wrap_node&& ref_cast() && noexcept { return std::move(*reinterpret_cast*>(this)); } template [[nodiscard]] TOML_ALWAYS_INLINE 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; /// \brief Returns the node's type identifier. [[nodiscard]] virtual node_type type() const noexcept = 0; /// \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]] 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 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