//# 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 "print_to_stream.h" #include "node.h" #include "std_vector.h" #include "std_initializer_list.h" #include "header_start.h" TOML_DISABLE_ARITHMETIC_WARNINGS; TOML_NAMESPACE_START { /// \brief A view of a node. /// /// \detail A node_view is like a std::optional (if such a construct were legal), 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" ] /// /// [[products]] /// name = "Nail" /// sku = 284758393 /// color = "gray" /// /// )"sv); /// /// 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"; /// /// tbl["products"][0]["keywords"].as_array()->push_back("heavy"); /// 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"; /// \ecpp /// /// \out /// "my hardware store" /// "Hammer" /// [ "hammer", "construction", "build" ] /// "build" /// [ "hammer", "construction", "build", "heavy" ] /// has product[2]: false /// product[2]: /// \eout template class TOML_TRIVIAL_ABI node_view { static_assert(impl::is_one_of, "A toml::node_view<> must wrap toml::node or const toml::node."); public: /// \brief The node type being viewed - either `node` or `const node`. using viewed_type = ViewedType; private: template friend class TOML_NAMESPACE::node_view; mutable viewed_type* node_ = nullptr; template static constexpr bool visit_is_nothrow = noexcept(std::declval()->visit(std::declval())); public: /// \brief Constructs an empty node view. TOML_NODISCARD_CTOR node_view() noexcept = default; /// \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 } {} /// \brief Copy constructor. TOML_NODISCARD_CTOR node_view(const node_view&) noexcept = default; /// \brief Move constructor. TOML_NODISCARD_CTOR node_view(node_view&&) noexcept = default; /// \brief Copy-assignment operator. node_view& operator=(const node_view&) & noexcept = default; /// \brief Move-assignment operator. node_view& operator=(node_view&&) & noexcept = default; /// \brief Returns true if the view references a node. TOML_PURE_INLINE_GETTER explicit operator bool() const noexcept { return node_ != nullptr; } /// \brief Returns the node that's being referenced by the view. TOML_PURE_INLINE_GETTER viewed_type* node() const noexcept { return node_; } /// \name Type checks /// @{ /// \brief Returns the type identifier for the viewed node. TOML_PURE_GETTER node_type type() const noexcept { return node_ ? node_->type() : node_type::none; } /// \brief Returns true if the viewed node is a toml::table. TOML_PURE_GETTER bool is_table() const noexcept { return node_ && node_->is_table(); } /// \brief Returns true if the viewed node is a toml::array. TOML_PURE_GETTER bool is_array() const noexcept { return node_ && node_->is_array(); } /// \brief Returns true if the viewed node is a toml::value<>. TOML_PURE_GETTER bool is_value() const noexcept { return node_ && node_->is_value(); } /// \brief Returns true if the viewed node is a toml::value. TOML_PURE_GETTER bool is_string() const noexcept { return node_ && node_->is_string(); } /// \brief Returns true if the viewed node is a toml::value. TOML_PURE_GETTER bool is_integer() const noexcept { return node_ && node_->is_integer(); } /// \brief Returns true if the viewed node is a toml::value. TOML_PURE_GETTER bool is_floating_point() const noexcept { return node_ && node_->is_floating_point(); } /// \brief Returns true if the viewed node is a toml::value or toml::value. TOML_PURE_GETTER bool is_number() const noexcept { return node_ && node_->is_number(); } /// \brief Returns true if the viewed node is a toml::value. TOML_PURE_GETTER bool is_boolean() const noexcept { return node_ && node_->is_boolean(); } /// \brief Returns true if the viewed node is a toml::value. TOML_PURE_GETTER bool is_date() const noexcept { return node_ && node_->is_date(); } /// \brief Returns true if the viewed node is a toml::value