//# 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" TOML_PUSH_WARNINGS TOML_DISABLE_VTABLE_WARNINGS namespace toml { /// \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(node&& other) noexcept; node& operator= (node&& rhs) noexcept; template [[nodiscard]] TOML_ALWAYS_INLINE impl::node_of& ref_cast() & noexcept { return *reinterpret_cast*>(this); } template [[nodiscard]] TOML_ALWAYS_INLINE impl::node_of&& ref_cast() && noexcept { return std::move(*reinterpret_cast*>(this)); } template [[nodiscard]] TOML_ALWAYS_INLINE const impl::node_of& ref_cast() const & noexcept { return *reinterpret_cast*>(this); } template using ref_cast_type = decltype(std::declval().template ref_cast()); node() noexcept = default; node(const node&) = delete; node& operator= (const node&) = delete; 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]] TOML_ALWAYS_INLINE bool is() const noexcept { using type = impl::unwrapped>; static_assert( impl::is_value_or_node, "Template type parameter must be one of the TOML value types, a toml::table, or a toml::array" ); 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