//# 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 "std_map.h" #include "std_initializer_list.h" #include "array.h" #include "make_node.h" #include "node_view.h" #include "key.h" #include "header_start.h" /// \cond TOML_IMPL_NAMESPACE_START { template struct table_proxy_pair { using value_type = std::conditional_t; const toml::key& first; value_type& second; }; template class table_iterator { private: template friend class table_iterator; using proxy_type = table_proxy_pair; using mutable_map_iterator = std::map>::iterator; using const_map_iterator = std::map>::const_iterator; using map_iterator = std::conditional_t; mutable map_iterator iter_; mutable std::aligned_storage_t proxy_; mutable bool proxy_instantiated_ = false; TOML_NODISCARD proxy_type* get_proxy() const noexcept { if (!proxy_instantiated_) { auto p = ::new (static_cast(&proxy_)) proxy_type{ iter_->first, *iter_->second.get() }; proxy_instantiated_ = true; return p; } else return TOML_LAUNDER(reinterpret_cast(&proxy_)); } public: TOML_NODISCARD_CTOR table_iterator() noexcept = default; TOML_NODISCARD_CTOR explicit table_iterator(mutable_map_iterator iter) noexcept // : iter_{ iter } {} TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst) TOML_NODISCARD_CTOR explicit table_iterator(const_map_iterator iter) noexcept // : iter_{ iter } {} TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst) TOML_NODISCARD_CTOR table_iterator(const table_iterator& other) noexcept // : iter_{ other.iter_ } {} TOML_NODISCARD_CTOR table_iterator(const table_iterator& other) noexcept // : iter_{ other.iter_ } {} table_iterator& operator=(const table_iterator& rhs) noexcept { iter_ = rhs.iter_; proxy_instantiated_ = false; return *this; } using value_type = table_proxy_pair; using reference = value_type&; using pointer = value_type*; using difference_type = typename std::iterator_traits::difference_type; using iterator_category = typename std::iterator_traits::iterator_category; table_iterator& operator++() noexcept // ++pre { ++iter_; proxy_instantiated_ = false; return *this; } table_iterator operator++(int) noexcept // post++ { table_iterator out{ iter_ }; ++iter_; proxy_instantiated_ = false; return out; } table_iterator& operator--() noexcept // --pre { --iter_; proxy_instantiated_ = false; return *this; } table_iterator operator--(int) noexcept // post-- { table_iterator out{ iter_ }; --iter_; proxy_instantiated_ = false; return out; } TOML_PURE_INLINE_GETTER reference operator*() const noexcept { return *get_proxy(); } TOML_PURE_INLINE_GETTER pointer operator->() const noexcept { return get_proxy(); } TOML_PURE_INLINE_GETTER explicit operator const map_iterator&() const noexcept { return iter_; } TOML_CONSTRAINED_TEMPLATE(!C, bool C = IsConst) TOML_PURE_INLINE_GETTER explicit operator const const_map_iterator() const noexcept { return iter_; } TOML_PURE_INLINE_GETTER friend bool operator==(const table_iterator& lhs, const table_iterator& rhs) noexcept { return lhs.iter_ == rhs.iter_; } TOML_PURE_INLINE_GETTER friend bool operator!=(const table_iterator& lhs, const table_iterator& rhs) noexcept { return lhs.iter_ != rhs.iter_; } }; struct table_init_pair { mutable toml::key key; mutable node_ptr value; template TOML_NODISCARD_CTOR table_init_pair(K&& k, V&& v, value_flags flags = preserve_source_value_flags) // : key{ static_cast(k) }, value{ make_node(static_cast(v), flags) } {} }; } TOML_IMPL_NAMESPACE_END; /// \endcond TOML_NAMESPACE_START { /// \brief A BidirectionalIterator for iterating over key-value pairs in a toml::table. using table_iterator = POXY_IMPLEMENTATION_DETAIL(impl::table_iterator); /// \brief A BidirectionalIterator for iterating over const key-value pairs in a toml::table. using const_table_iterator = POXY_IMPLEMENTATION_DETAIL(impl::table_iterator); /// \brief A TOML table. /// /// \remarks The interface of this type is modeled after std::map, with some /// additional considerations made for the heterogeneous nature of a /// TOML table. class table : public node { private: /// \cond using map_type = std::map>; using map_iterator = typename map_type::iterator; using const_map_iterator = typename map_type::const_iterator; map_type map_; bool inline_ = false; TOML_NODISCARD_CTOR TOML_API table(const impl::table_init_pair*, const impl::table_init_pair*); /// \endcond public: /// \brief A BidirectionalIterator for iterating over key-value pairs in a toml::table. using iterator = toml::table_iterator; /// \brief A BidirectionalIterator for iterating over const key-value pairs in a toml::table. using const_iterator = toml::const_table_iterator; #if TOML_LIFETIME_HOOKS TOML_NODISCARD_CTOR table() noexcept { TOML_TABLE_CREATED; } ~table() noexcept { TOML_TABLE_DESTROYED; } #else /// \brief Default constructor. TOML_NODISCARD_CTOR table() noexcept = default; #endif /// \brief Copy constructor. TOML_NODISCARD_CTOR TOML_API table(const table&); /// \brief Move constructor. TOML_NODISCARD_CTOR TOML_API table(table&& other) noexcept; /// \brief Constructs a table with one or more initial key-value pairs. /// /// \detail \cpp /// auto tbl = toml::table{ /// { "foo", 1 }, /// { "bar", 2.0 }, /// { "kek", "three" } /// }; /// std::cout << tbl << "\n"; /// \ecpp /// /// \out /// { foo = 1, bar = 2.0, kek = "three" } /// \eout /// /// \param kvps A list of key-value pairs used to initialize the table. TOML_NODISCARD_CTOR TOML_API explicit table(std::initializer_list kvps) // : table(kvps.begin(), kvps.end()) {} /// \brief Copy-assignment operator. TOML_API table& operator=(const table&); /// \brief Move-assignment operator. TOML_API table& operator=(table&& rhs) noexcept; /// \name Type checks /// @{ /// \brief Returns #toml::node_type::table. TOML_CONST_INLINE_GETTER node_type type() const noexcept final { return node_type::table; } TOML_PURE_GETTER TOML_API bool is_homogeneous(node_type ntype) const noexcept final; TOML_PURE_GETTER TOML_API bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept final; TOML_PURE_GETTER TOML_API bool is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept final; /// \cond template TOML_PURE_GETTER bool is_homogeneous() const noexcept { using type = impl::unwrap_node; static_assert( std::is_void_v< type> || ((impl::is_native || impl::is_one_of)&&!impl::is_cvref), "The template type argument of table::is_homogeneous() must be void or one " "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST); return is_homogeneous(impl::node_type_of); } /// \endcond /// \brief Returns `true`. TOML_CONST_INLINE_GETTER bool is_table() const noexcept final { return true; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_array() const noexcept final { return false; } /// \brief Returns `false`. TOML_PURE_GETTER bool is_array_of_tables() const noexcept final { return false; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_value() const noexcept final { return false; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_string() const noexcept final { return false; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_integer() const noexcept final { return false; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_floating_point() const noexcept final { return false; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_number() const noexcept final { return false; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_boolean() const noexcept final { return false; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_date() const noexcept final { return false; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_time() const noexcept final { return false; } /// \brief Returns `false`. TOML_CONST_INLINE_GETTER bool is_date_time() const noexcept final { return false; } /// @} /// \name Type casts /// @{ /// \brief Returns a pointer to the table. TOML_CONST_INLINE_GETTER table* as_table() noexcept final { return this; } /// \brief Returns `nullptr`. TOML_CONST_INLINE_GETTER array* as_array() noexcept final { return nullptr; } /// \brief Returns `nullptr`. TOML_CONST_INLINE_GETTER toml::value* as_string() noexcept final { return nullptr; } /// \brief Returns `nullptr`. TOML_CONST_INLINE_GETTER toml::value* as_integer() noexcept final { return nullptr; } /// \brief Returns `nullptr`. TOML_CONST_INLINE_GETTER toml::value* as_floating_point() noexcept final { return nullptr; } /// \brief Returns `nullptr`. TOML_CONST_INLINE_GETTER toml::value* as_boolean() noexcept final { return nullptr; } /// \brief Returns `nullptr`. TOML_CONST_INLINE_GETTER toml::value* as_date() noexcept final { return nullptr; } /// \brief Returns `nullptr`. TOML_CONST_INLINE_GETTER toml::value