From 3626abb35aaa351d2dd80bd6c64e60a51f94dd84 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Wed, 14 Oct 2026 16:21:48 +0800 Subject: [PATCH] Refactor: migrate TOML parser from toml11 to toml++ - Replace vendored single-header toml11 (includes/toml.hpp) with the toml++ library (includes/toml++) - Rewrite Config to use toml::parse_file and expose typed get/getArray/getPath/contains accessors, storing parse errors instead of logging them - Update bundled license to toml++ --- includes/JuiceAgent/Config.hpp | 301 +- includes/toml++/impl/array.hpp | 1790 ++ includes/toml++/impl/array.inl | 382 + includes/toml++/impl/at_path.hpp | 97 + includes/toml++/impl/at_path.inl | 290 + includes/toml++/impl/date_time.hpp | 468 + includes/toml++/impl/formatter.hpp | 194 + includes/toml++/impl/formatter.inl | 523 + includes/toml++/impl/forward_declarations.hpp | 1097 + includes/toml++/impl/header_end.hpp | 13 + includes/toml++/impl/header_start.hpp | 15 + includes/toml++/impl/json_formatter.hpp | 142 + includes/toml++/impl/json_formatter.inl | 121 + includes/toml++/impl/key.hpp | 335 + includes/toml++/impl/make_node.hpp | 182 + includes/toml++/impl/node.hpp | 1115 + includes/toml++/impl/node.inl | 141 + includes/toml++/impl/node_view.hpp | 839 + includes/toml++/impl/parse_error.hpp | 139 + includes/toml++/impl/parse_result.hpp | 499 + includes/toml++/impl/parser.hpp | 390 + includes/toml++/impl/parser.inl | 3917 ++++ includes/toml++/impl/path.hpp | 851 + includes/toml++/impl/path.inl | 523 + includes/toml++/impl/preprocessor.hpp | 1412 ++ includes/toml++/impl/print_to_stream.hpp | 129 + includes/toml++/impl/print_to_stream.inl | 490 + includes/toml++/impl/simd.hpp | 35 + includes/toml++/impl/source_region.hpp | 223 + includes/toml++/impl/std_except.hpp | 12 + includes/toml++/impl/std_initializer_list.hpp | 10 + includes/toml++/impl/std_map.hpp | 11 + includes/toml++/impl/std_new.hpp | 18 + includes/toml++/impl/std_optional.hpp | 32 + includes/toml++/impl/std_string.hpp | 53 + includes/toml++/impl/std_string.inl | 99 + includes/toml++/impl/std_utility.hpp | 10 + includes/toml++/impl/std_variant.hpp | 10 + includes/toml++/impl/std_vector.hpp | 11 + includes/toml++/impl/table.hpp | 1996 ++ includes/toml++/impl/table.inl | 318 + includes/toml++/impl/toml_formatter.hpp | 153 + includes/toml++/impl/toml_formatter.inl | 405 + includes/toml++/impl/unicode.hpp | 197 + includes/toml++/impl/unicode.inl | 60 + .../toml++/impl/unicode_autogenerated.hpp | 182 + includes/toml++/impl/value.hpp | 1270 ++ includes/toml++/impl/version.hpp | 13 + includes/toml++/impl/yaml_formatter.hpp | 139 + includes/toml++/impl/yaml_formatter.inl | 165 + includes/toml++/toml.h | 7 + includes/toml++/toml.hpp | 231 + includes/toml.hpp | 18162 ---------------- licenses/toml++.txt | 16 + licenses/toml11.txt | 21 - 55 files changed, 21945 insertions(+), 18309 deletions(-) create mode 100644 includes/toml++/impl/array.hpp create mode 100644 includes/toml++/impl/array.inl create mode 100644 includes/toml++/impl/at_path.hpp create mode 100644 includes/toml++/impl/at_path.inl create mode 100644 includes/toml++/impl/date_time.hpp create mode 100644 includes/toml++/impl/formatter.hpp create mode 100644 includes/toml++/impl/formatter.inl create mode 100644 includes/toml++/impl/forward_declarations.hpp create mode 100644 includes/toml++/impl/header_end.hpp create mode 100644 includes/toml++/impl/header_start.hpp create mode 100644 includes/toml++/impl/json_formatter.hpp create mode 100644 includes/toml++/impl/json_formatter.inl create mode 100644 includes/toml++/impl/key.hpp create mode 100644 includes/toml++/impl/make_node.hpp create mode 100644 includes/toml++/impl/node.hpp create mode 100644 includes/toml++/impl/node.inl create mode 100644 includes/toml++/impl/node_view.hpp create mode 100644 includes/toml++/impl/parse_error.hpp create mode 100644 includes/toml++/impl/parse_result.hpp create mode 100644 includes/toml++/impl/parser.hpp create mode 100644 includes/toml++/impl/parser.inl create mode 100644 includes/toml++/impl/path.hpp create mode 100644 includes/toml++/impl/path.inl create mode 100644 includes/toml++/impl/preprocessor.hpp create mode 100644 includes/toml++/impl/print_to_stream.hpp create mode 100644 includes/toml++/impl/print_to_stream.inl create mode 100644 includes/toml++/impl/simd.hpp create mode 100644 includes/toml++/impl/source_region.hpp create mode 100644 includes/toml++/impl/std_except.hpp create mode 100644 includes/toml++/impl/std_initializer_list.hpp create mode 100644 includes/toml++/impl/std_map.hpp create mode 100644 includes/toml++/impl/std_new.hpp create mode 100644 includes/toml++/impl/std_optional.hpp create mode 100644 includes/toml++/impl/std_string.hpp create mode 100644 includes/toml++/impl/std_string.inl create mode 100644 includes/toml++/impl/std_utility.hpp create mode 100644 includes/toml++/impl/std_variant.hpp create mode 100644 includes/toml++/impl/std_vector.hpp create mode 100644 includes/toml++/impl/table.hpp create mode 100644 includes/toml++/impl/table.inl create mode 100644 includes/toml++/impl/toml_formatter.hpp create mode 100644 includes/toml++/impl/toml_formatter.inl create mode 100644 includes/toml++/impl/unicode.hpp create mode 100644 includes/toml++/impl/unicode.inl create mode 100644 includes/toml++/impl/unicode_autogenerated.hpp create mode 100644 includes/toml++/impl/value.hpp create mode 100644 includes/toml++/impl/version.hpp create mode 100644 includes/toml++/impl/yaml_formatter.hpp create mode 100644 includes/toml++/impl/yaml_formatter.inl create mode 100644 includes/toml++/toml.h create mode 100644 includes/toml++/toml.hpp delete mode 100644 includes/toml.hpp create mode 100644 licenses/toml++.txt delete mode 100644 licenses/toml11.txt diff --git a/includes/JuiceAgent/Config.hpp b/includes/JuiceAgent/Config.hpp index fc86e35..97297be 100644 --- a/includes/JuiceAgent/Config.hpp +++ b/includes/JuiceAgent/Config.hpp @@ -1,160 +1,209 @@ #pragma once #include +#include +#include +#include #include -#include - -#include -#include -#include -#include +#include +#include namespace JuiceAgent::Config { class Config { +private: + toml::table _config; + std::optional _last_error; + + std::filesystem::path _runtime_dir; + std::filesystem::path _config_path; + + bool _valid = false; + public: - explicit Config(const std::filesystem::path& runtime_dir = {}) - { - _runtime_dir = runtime_dir; - - // No runtime directory specified: do not auto-read a config file - // from the current working directory. + explicit Config(const std::filesystem::path& runtime_dir = {}) { + // Check if the runtime directory is empty if (runtime_dir.empty()) { - return; + // Use the default runtime directory + _runtime_dir = std::filesystem::current_path(); + } else { + _runtime_dir = runtime_dir; } - const auto result = toml::try_parse(_runtime_dir / "config.toml"); + _config_path = _runtime_dir / "config.toml"; - if (result.is_ok()) { - _config = result.unwrap(); + // Load the config file + try { + _config = toml::parse_file(_config_path.string()); _valid = true; + } catch (const toml::parse_error& e) { + // TODO: Log the error + _last_error = e; } - else { - spdlog::error("Failed to parse config.toml"); + } - for (const auto& err : result.unwrap_err()) { - spdlog::error("{}", toml::format_error(err)); + + template + T get(const std::string& path) const { + const auto* node = _config.at_path(path).node(); + + if (!node) { + // TODO: Log the error + throw std::runtime_error( + "Configuration key not found: " + path + ); + } + + auto value = node->value(); + + if (!value) { + // TODO: Log the error + throw std::runtime_error( + "Configuration type mismatch: " + path + ); + } + + return *value; + } + + template + T get(const std::string& path, T defaultValue) const { + const auto* node = _config.at_path(path).node(); + + if (!node) { + // Configuration key is missing; use the default value. + // TODO: Log the warning + return defaultValue; + } + + auto value = node->value(); + + if (!value) { + // Configuration type mismatch; use the default value. + // TODO: Log the warning + return defaultValue; + } + + return *value; + } + + std::string getPath(const std::string& key) const { + const std::filesystem::path value(get(key)); + + if (value.is_absolute()) { + return value.lexically_normal().string(); + } + + const std::string normalized = value.generic_string(); + + if (normalized == "." || normalized.starts_with("./")) { + return (_runtime_dir / value).lexically_normal().string(); + } + + return value.lexically_normal().string(); + } + + template + std::vector getArray(const std::string& path) const { + const auto* node = _config.at_path(path).node(); + + if (!node) { + throw std::runtime_error( + "Configuration key not found: " + path + ); + } + + const auto* array = node->as_array(); + + if (!array) { + throw std::runtime_error( + "Configuration type mismatch: expected array: " + path + ); + } + + std::vector result; + result.reserve(array->size()); + + for (std::size_t i = 0; i < array->size(); ++i) { + const auto& element = (*array)[i]; + auto value = element.value(); + + if (!value) { + throw std::runtime_error( + "Configuration array element type mismatch: " + + path + "[" + std::to_string(i) + "]" + ); } + + result.push_back(*value); } + + return result; } - bool is_valid() const noexcept - { - return _valid; + template + std::vector getArray( + const std::string& path, + std::vector defaultValue + ) const { + const auto* node = _config.at_path(path).node(); + + if (!node) { + return defaultValue; + } + + const auto* array = node->as_array(); + + if (!array) { + throw std::runtime_error( + "Configuration type mismatch: expected array: " + path + ); + } + + std::vector result; + result.reserve(array->size()); + + for (std::size_t i = 0; i < array->size(); ++i) { + const auto& element = (*array)[i]; + auto value = element.value(); + + if (!value) { + throw std::runtime_error( + "Configuration array element type mismatch: " + + path + "[" + std::to_string(i) + "]" + ); + } + + result.push_back(*value); + } + + return result; } - const toml::value& get_config() const noexcept - { + bool contains(const std::string& path) const { + return _config.at_path(path).node() != nullptr; + } + + // Getters + const toml::table& getConfig() const { return _config; } - const std::filesystem::path& runtime_dir() const noexcept - { + const std::filesystem::path& getRuntimeDir() const { return _runtime_dir; } - template - T get(const std::string& path, const T& default_value, bool is_path = false) const - { - if (!_valid) { - return default_value; - } - - try { - const toml::value* current = &_config; - std::size_t start = 0; - - while (start < path.size()) { - const std::size_t end = path.find('.', start); - const std::string key = path.substr( - start, - end == std::string::npos ? std::string::npos : end - start - ); - - current = ¤t->at(key); - - if (end == std::string::npos) { - break; - } - - start = end + 1; - } - - T value = read_value(*current); - - if constexpr (std::is_same_v) { - if (value.empty()) { - value = default_value; - } - - if (is_path && value.starts_with(".")) { - value = (_runtime_dir / value).lexically_normal().string(); - } - } - - return value; - } - catch (...) { - spdlog::warn("Config get failed: {}, using default value", path); - return default_value; - } + const std::filesystem::path& getConfigPath() const { + return _config_path; } -private: - template - static T read_value(const toml::value& v) - { - if constexpr (std::is_same_v) { - return v.as_string(); - } - else if constexpr (std::is_same_v) { - return v.as_boolean(); - } - else if constexpr (std::is_integral_v && !std::is_same_v) { - return static_cast(v.as_integer()); - } - else if constexpr (std::is_floating_point_v) { - return static_cast(v.as_floating()); - } - else { - static_assert(sizeof(T) == 0, "Unsupported config type"); - } + const std::optional& getLastError() const { + return _last_error; } - std::filesystem::path _runtime_dir; - toml::value _config; - bool _valid = false; + bool isValid() const { + return _valid; + } }; -namespace Utils { - inline LoaderConfig get_loader_config(const Config& config) - { - LoaderConfig loader_config; - loader_config.JuiceAgentNativeLibraryPath = config.get( - "JuiceAgent.Loader.JuiceAgentNativeLibraryPath", - (config.runtime_dir() / "libagent.dll").string(), - true - ); - loader_config.RuntimeDir = config.runtime_dir().string(); - - return loader_config; - } - - inline void print_loader_config(const LoaderConfig& config) - { - spdlog::info("JuiceAgentNativeLibraryPath: {}", config.JuiceAgentNativeLibraryPath); - spdlog::info("RuntimeDir: {}", config.RuntimeDir); - } - - inline std::string serialize_loader_config(const Config& config, const LoaderConfig& loader_config) - { - JuiceAgent::Utils::Serializer ser; - ser.add_kv("Version", config.get("JuiceAgent.Version", 1)); - ser.add_kv("JuiceAgentNativeLibraryPath", loader_config.JuiceAgentNativeLibraryPath); - ser.add_kv("RuntimeDir", loader_config.RuntimeDir); - - return ser.serialize(); - } -} - -} // namespace JuiceAgent::Config +} // namespace JuiceAgent::Config \ No newline at end of file diff --git a/includes/toml++/impl/array.hpp b/includes/toml++/impl/array.hpp new file mode 100644 index 0000000..ad79379 --- /dev/null +++ b/includes/toml++/impl/array.hpp @@ -0,0 +1,1790 @@ +//# 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_utility.hpp" +#include "std_vector.hpp" +#include "std_initializer_list.hpp" +#include "value.hpp" +#include "make_node.hpp" +#include "header_start.hpp" + +#ifndef TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN +#if TOML_GCC && TOML_GCC <= 7 +#define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN 1 +#else +#define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN 0 +#endif +#endif + +#if TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN && !defined(TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED) +#define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_MESSAGE \ + "If you're seeing this error it's because you're using one of toml++'s for_each() functions on a compiler with " \ + "known bugs in that area (e.g. GCC 7). On these compilers returning a bool (or bool-convertible) value from the " \ + "for_each() callable causes spurious compilation failures, while returning nothing (void) works fine. " \ + "If you believe this message is incorrect for your compiler, you can try your luck by #defining " \ + "TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN as 0 and recompiling - if it works, great! Let me know at " \ + "https://github.com/marzer/tomlplusplus/issues. Alternatively, if you don't have any need for early-exiting from " \ + "for_each(), you can suppress this error by #defining TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED " \ + "and moving on with your life." +#endif + +/// \cond +TOML_IMPL_NAMESPACE_START +{ + template + class TOML_TRIVIAL_ABI array_iterator + { + private: + template + friend class array_iterator; + + using mutable_vector_iterator = std::vector::iterator; + using const_vector_iterator = std::vector::const_iterator; + using vector_iterator = std::conditional_t; + + mutable vector_iterator iter_; + + public: + using value_type = std::conditional_t; + using reference = value_type&; + using pointer = value_type*; + using difference_type = ptrdiff_t; + using iterator_category = typename std::iterator_traits::iterator_category; + + TOML_NODISCARD_CTOR + array_iterator() noexcept = default; + + TOML_NODISCARD_CTOR + explicit array_iterator(mutable_vector_iterator iter) noexcept // + : iter_{ iter } + {} + + TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst) + TOML_NODISCARD_CTOR + explicit array_iterator(const_vector_iterator iter) noexcept // + : iter_{ iter } + {} + + TOML_CONSTRAINED_TEMPLATE(C, bool C = IsConst) + TOML_NODISCARD_CTOR + array_iterator(const array_iterator& other) noexcept // + : iter_{ other.iter_ } + {} + + TOML_NODISCARD_CTOR + array_iterator(const array_iterator&) noexcept = default; + + array_iterator& operator=(const array_iterator&) noexcept = default; + + array_iterator& operator++() noexcept // ++pre + { + ++iter_; + return *this; + } + + array_iterator operator++(int) noexcept // post++ + { + array_iterator out{ iter_ }; + ++iter_; + return out; + } + + array_iterator& operator--() noexcept // --pre + { + --iter_; + return *this; + } + + array_iterator operator--(int) noexcept // post-- + { + array_iterator out{ iter_ }; + --iter_; + return out; + } + + TOML_PURE_INLINE_GETTER + reference operator*() const noexcept + { + return *iter_->get(); + } + + TOML_PURE_INLINE_GETTER + pointer operator->() const noexcept + { + return iter_->get(); + } + + TOML_PURE_INLINE_GETTER + explicit operator const vector_iterator&() const noexcept + { + return iter_; + } + + TOML_CONSTRAINED_TEMPLATE(!C, bool C = IsConst) + TOML_PURE_INLINE_GETTER + explicit operator const const_vector_iterator() const noexcept + { + return iter_; + } + + array_iterator& operator+=(ptrdiff_t rhs) noexcept + { + iter_ += rhs; + return *this; + } + + array_iterator& operator-=(ptrdiff_t rhs) noexcept + { + iter_ -= rhs; + return *this; + } + + TOML_NODISCARD + friend array_iterator operator+(const array_iterator& lhs, ptrdiff_t rhs) noexcept + { + return array_iterator{ lhs.iter_ + rhs }; + } + + TOML_NODISCARD + friend array_iterator operator+(ptrdiff_t lhs, const array_iterator& rhs) noexcept + { + return array_iterator{ rhs.iter_ + lhs }; + } + + TOML_NODISCARD + friend array_iterator operator-(const array_iterator& lhs, ptrdiff_t rhs) noexcept + { + return array_iterator{ lhs.iter_ - rhs }; + } + + TOML_PURE_INLINE_GETTER + friend ptrdiff_t operator-(const array_iterator& lhs, const array_iterator& rhs) noexcept + { + return lhs.iter_ - rhs.iter_; + } + + TOML_PURE_INLINE_GETTER + friend bool operator==(const array_iterator& lhs, const array_iterator& rhs) noexcept + { + return lhs.iter_ == rhs.iter_; + } + + TOML_PURE_INLINE_GETTER + friend bool operator!=(const array_iterator& lhs, const array_iterator& rhs) noexcept + { + return lhs.iter_ != rhs.iter_; + } + + TOML_PURE_INLINE_GETTER + friend bool operator<(const array_iterator& lhs, const array_iterator& rhs) noexcept + { + return lhs.iter_ < rhs.iter_; + } + + TOML_PURE_INLINE_GETTER + friend bool operator<=(const array_iterator& lhs, const array_iterator& rhs) noexcept + { + return lhs.iter_ <= rhs.iter_; + } + + TOML_PURE_INLINE_GETTER + friend bool operator>(const array_iterator& lhs, const array_iterator& rhs) noexcept + { + return lhs.iter_ > rhs.iter_; + } + + TOML_PURE_INLINE_GETTER + friend bool operator>=(const array_iterator& lhs, const array_iterator& rhs) noexcept + { + return lhs.iter_ >= rhs.iter_; + } + + TOML_PURE_INLINE_GETTER + reference operator[](ptrdiff_t idx) const noexcept + { + return *(iter_ + idx)->get(); + } + }; + + struct array_init_elem + { + mutable node_ptr value; + + template + TOML_NODISCARD_CTOR + array_init_elem(T&& val, value_flags flags = preserve_source_value_flags) // + : value{ make_node(static_cast(val), flags) } + {} + }; +} +TOML_IMPL_NAMESPACE_END; +/// \endcond + +TOML_NAMESPACE_START +{ + /// \brief A RandomAccessIterator for iterating over elements in a toml::array. + using array_iterator = POXY_IMPLEMENTATION_DETAIL(impl::array_iterator); + + /// \brief A RandomAccessIterator for iterating over const elements in a toml::array. + using const_array_iterator = POXY_IMPLEMENTATION_DETAIL(impl::array_iterator); + + /// \brief A TOML array. + /// + /// \detail The interface of this type is modeled after std::vector, with some + /// additional considerations made for the heterogeneous nature of a + /// TOML array. + /// + /// \godbolt{sjK4da} + /// + /// \cpp + /// + /// toml::table tbl = toml::parse(R"( + /// arr = [1, 2, 3, 4, 'five'] + /// )"sv); + /// + /// // get the element as an array + /// toml::array& arr = *tbl.get_as("arr"); + /// std::cout << arr << "\n"; + /// + /// // increment each element with visit() + /// for (auto&& elem : arr) + /// { + /// elem.visit([](auto&& el) noexcept + /// { + /// if constexpr (toml::is_number) + /// (*el)++; + /// else if constexpr (toml::is_string) + /// el = "six"sv; + /// }); + /// } + /// std::cout << arr << "\n"; + /// + /// // add and remove elements + /// arr.push_back(7); + /// arr.push_back(8.0f); + /// arr.push_back("nine"sv); + /// arr.erase(arr.cbegin()); + /// std::cout << arr << "\n"; + /// + /// // emplace elements + /// arr.emplace_back("ten"); + /// arr.emplace_back(11, 12.0); + /// std::cout << arr << "\n"; + /// \ecpp + /// + /// \out + /// [ 1, 2, 3, 4, 'five' ] + /// [ 2, 3, 4, 5, 'six' ] + /// [ 3, 4, 5, 'six', 7, 8.0, 'nine' ] + /// [ 3, 4, 5, 'six', 7, 8.0, 'nine', 'ten', [ 11, 12.0 ] ] + /// \eout + class TOML_EXPORTED_CLASS array : public node + { + private: + /// \cond + + using vector_type = std::vector; + using vector_iterator = typename vector_type::iterator; + using const_vector_iterator = typename vector_type::const_iterator; + vector_type elems_; + + TOML_NODISCARD_CTOR + TOML_EXPORTED_MEMBER_FUNCTION + array(const impl::array_init_elem*, const impl::array_init_elem*); + + TOML_NODISCARD_CTOR + array(std::false_type, std::initializer_list elems) // + : array{ elems.begin(), elems.end() } + {} + + TOML_EXPORTED_MEMBER_FUNCTION + void preinsertion_resize(size_t idx, size_t count); + + TOML_EXPORTED_MEMBER_FUNCTION + void insert_at_back(impl::node_ptr&&); + + TOML_EXPORTED_MEMBER_FUNCTION + vector_iterator insert_at(const_vector_iterator, impl::node_ptr&&); + + template + void emplace_back_if_not_empty_view(T&& val, value_flags flags) + { + if constexpr (is_node_view) + { + if (!val) + return; + } + insert_at_back(impl::make_node(static_cast(val), flags)); + } + + TOML_NODISCARD + TOML_EXPORTED_MEMBER_FUNCTION + size_t total_leaf_count() const noexcept; + + TOML_EXPORTED_MEMBER_FUNCTION + void flatten_child(array&& child, size_t& dest_index) noexcept; + + /// \endcond + + public: + using value_type = node; + using size_type = size_t; + using difference_type = ptrdiff_t; + using reference = node&; + using const_reference = const node&; + + /// \brief Default constructor. + TOML_NODISCARD_CTOR + TOML_EXPORTED_MEMBER_FUNCTION + array() noexcept; + + TOML_EXPORTED_MEMBER_FUNCTION + ~array() noexcept; + + /// \brief Copy constructor. + TOML_NODISCARD_CTOR + TOML_EXPORTED_MEMBER_FUNCTION + array(const array&); + + /// \brief Move constructor. + TOML_NODISCARD_CTOR + TOML_EXPORTED_MEMBER_FUNCTION + array(array&& other) noexcept; + + /// \brief Constructs an array with one or more initial elements. + /// + /// \detail \cpp + /// auto arr = toml::array{ 1, 2.0, "three"sv, toml::array{ 4, 5 } }; + /// std::cout << arr << "\n"; + /// \ecpp + /// + /// \out + /// [ 1, 2.0, 'three', [ 4, 5 ] ] + /// \eout + /// + /// \remark \parblock If you need to construct an array with one child array element, the array's move constructor + /// will take precedence and perform a move-construction instead. You can use toml::inserter to + /// suppress this behaviour: \cpp + /// // desired result: [ [ 42 ] ] + /// auto bad = toml::array{ toml::array{ 42 } } + /// auto good = toml::array{ toml::inserter{ toml::array{ 42 } } } + /// std::cout << "bad: " << bad << "\n"; + /// std::cout << "good:" << good << "\n"; + /// \ecpp + /// + /// \out + /// bad: [ 42 ] + /// good: [ [ 42 ] ] + /// \eout + /// + /// \endparblock + /// + /// \tparam ElemType One of the TOML node or value types (or a type promotable to one). + /// \tparam ElemTypes One of the TOML node or value types (or a type promotable to one). + /// \param val The node or value used to initialize element 0. + /// \param vals The nodes or values used to initialize elements 1...N. + TOML_CONSTRAINED_TEMPLATE((sizeof...(ElemTypes) > 0 || !std::is_same_v, array>), + typename ElemType, + typename... ElemTypes) + TOML_NODISCARD_CTOR + explicit array(ElemType&& val, ElemTypes&&... vals) + : array{ std::false_type{}, + std::initializer_list{ static_cast(val), + static_cast(vals)... } } + {} + + /// \brief Copy-assignment operator. + TOML_EXPORTED_MEMBER_FUNCTION + array& operator=(const array&); + + /// \brief Move-assignment operator. + TOML_EXPORTED_MEMBER_FUNCTION + array& operator=(array&& rhs) noexcept; + + /// \name Type checks + /// @{ + + /// \brief Returns #toml::node_type::array. + TOML_CONST_INLINE_GETTER + node_type type() const noexcept final + { + return node_type::array; + } + + TOML_PURE_GETTER + TOML_EXPORTED_MEMBER_FUNCTION + bool is_homogeneous(node_type ntype) const noexcept final; + + TOML_PURE_GETTER + TOML_EXPORTED_MEMBER_FUNCTION + bool is_homogeneous(node_type ntype, node*& first_nonmatch) noexcept final; + + TOML_PURE_GETTER + TOML_EXPORTED_MEMBER_FUNCTION + 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::remove_cvref>; + static_assert(std::is_void_v || toml::is_value || toml::is_container, + "The template type argument of array::is_homogeneous() must be void or one " + "of:" TOML_SA_UNWRAPPED_NODE_TYPE_LIST); + + return is_homogeneous(impl::node_type_of); + } + /// \endcond + + /// \brief Returns `false`. + TOML_CONST_INLINE_GETTER + bool is_table() const noexcept final + { + return false; + } + + /// \brief Returns `true`. + TOML_CONST_INLINE_GETTER + bool is_array() const noexcept final + { + return true; + } + + /// \brief Returns `true` if the array contains only tables. + TOML_PURE_GETTER + bool is_array_of_tables() const noexcept final + { + return is_homogeneous(node_type::table); + } + + /// \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 `nullptr`. + TOML_CONST_INLINE_GETTER + table* as_table() noexcept final + { + return nullptr; + } + + /// \brief Returns a pointer to the array. + TOML_CONST_INLINE_GETTER + array* as_array() noexcept final + { + return this; + } + + /// \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