2021-10-23 09:22:41 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
//# {{
|
|
|
|
#include "preprocessor.h"
|
|
|
|
#if !TOML_IMPLEMENTATION
|
2021-10-24 22:04:23 +00:00
|
|
|
#error This is an implementation-only header.
|
2021-10-23 09:22:41 +00:00
|
|
|
#endif
|
|
|
|
//# }}
|
|
|
|
|
|
|
|
#include "table.h"
|
|
|
|
#include "node_view.h"
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_start.h"
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
TOML_NAMESPACE_START
|
|
|
|
{
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table::table() noexcept
|
|
|
|
{
|
|
|
|
#if TOML_LIFETIME_HOOKS
|
|
|
|
TOML_TABLE_CREATED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table::~table() noexcept
|
|
|
|
{
|
|
|
|
#if TOML_LIFETIME_HOOKS
|
|
|
|
TOML_TABLE_DESTROYED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-10-26 18:03:56 +00:00
|
|
|
table::table(const impl::table_init_pair* b, const impl::table_init_pair* e)
|
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
#if TOML_LIFETIME_HOOKS
|
|
|
|
TOML_TABLE_CREATED;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TOML_ASSERT_ASSUME(b);
|
|
|
|
TOML_ASSERT_ASSUME(e);
|
|
|
|
TOML_ASSERT_ASSUME(b <= e);
|
|
|
|
|
|
|
|
if TOML_UNLIKELY(b == e)
|
|
|
|
return;
|
|
|
|
|
2021-10-26 18:03:56 +00:00
|
|
|
for (; b != e; b++)
|
|
|
|
{
|
|
|
|
if (!b->value) // empty node_views
|
|
|
|
continue;
|
|
|
|
|
|
|
|
map_.insert_or_assign(std::move(b->key), std::move(b->value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table::table(const table& other) //
|
2021-10-23 09:22:41 +00:00
|
|
|
: node(other),
|
|
|
|
inline_{ other.inline_ }
|
|
|
|
{
|
2021-11-10 09:17:15 +00:00
|
|
|
for (auto&& [k, v] : other.map_)
|
|
|
|
map_.emplace_hint(map_.end(), k, impl::make_node(*v));
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
#if TOML_LIFETIME_HOOKS
|
|
|
|
TOML_TABLE_CREATED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table::table(table && other) noexcept //
|
|
|
|
: node(std::move(other)),
|
2021-10-24 22:04:23 +00:00
|
|
|
map_{ std::move(other.map_) },
|
2021-10-23 09:22:41 +00:00
|
|
|
inline_{ other.inline_ }
|
|
|
|
{
|
|
|
|
#if TOML_LIFETIME_HOOKS
|
|
|
|
TOML_TABLE_CREATED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-10-26 18:03:56 +00:00
|
|
|
table& table::operator=(const table& rhs)
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
if (&rhs != this)
|
|
|
|
{
|
|
|
|
node::operator=(rhs);
|
2021-10-24 22:04:23 +00:00
|
|
|
map_.clear();
|
2021-11-10 09:17:15 +00:00
|
|
|
for (auto&& [k, v] : rhs.map_)
|
|
|
|
map_.emplace_hint(map_.end(), k, impl::make_node(*v));
|
2021-10-23 09:22:41 +00:00
|
|
|
inline_ = rhs.inline_;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table& table::operator=(table&& rhs) noexcept
|
|
|
|
{
|
|
|
|
if (&rhs != this)
|
|
|
|
{
|
|
|
|
node::operator=(std::move(rhs));
|
2022-04-18 09:27:54 +00:00
|
|
|
map_ = std::move(rhs.map_);
|
|
|
|
inline_ = rhs.inline_;
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
bool table::is_homogeneous(node_type ntype) const noexcept
|
|
|
|
{
|
2021-10-24 22:04:23 +00:00
|
|
|
if (map_.empty())
|
2021-10-23 09:22:41 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ntype == node_type::none)
|
2021-10-24 22:04:23 +00:00
|
|
|
ntype = map_.cbegin()->second->type();
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-11-10 09:17:15 +00:00
|
|
|
for (auto&& [k, v] : map_)
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
TOML_UNUSED(k);
|
2021-10-23 09:22:41 +00:00
|
|
|
if (v->type() != ntype)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-10-24 22:04:23 +00:00
|
|
|
bool table::is_homogeneous(node_type ntype, node * &first_nonmatch) noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
2021-11-07 19:25:42 +00:00
|
|
|
if (map_.empty())
|
|
|
|
{
|
|
|
|
first_nonmatch = {};
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ntype == node_type::none)
|
|
|
|
ntype = map_.cbegin()->second->type();
|
|
|
|
for (const auto& [k, v] : map_)
|
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
TOML_UNUSED(k);
|
2021-11-07 19:25:42 +00:00
|
|
|
if (v->type() != ntype)
|
|
|
|
{
|
|
|
|
first_nonmatch = v.get();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-10-24 22:04:23 +00:00
|
|
|
bool table::is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
2021-11-07 19:25:42 +00:00
|
|
|
node* fnm = nullptr;
|
|
|
|
const auto result = const_cast<table&>(*this).is_homogeneous(ntype, fnm);
|
|
|
|
first_nonmatch = fnm;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
node* table::get(std::string_view key) noexcept
|
|
|
|
{
|
|
|
|
if (auto it = map_.find(key); it != map_.end())
|
|
|
|
return it->second.get();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-11-07 20:33:02 +00:00
|
|
|
node& table::at(std::string_view key)
|
2021-11-07 19:25:42 +00:00
|
|
|
{
|
2021-11-07 20:33:02 +00:00
|
|
|
auto n = get(key);
|
2021-11-07 19:25:42 +00:00
|
|
|
|
|
|
|
#if TOML_COMPILER_EXCEPTIONS
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
auto err = "key '"s;
|
|
|
|
err.append(key);
|
|
|
|
err.append("' not found in table"sv);
|
|
|
|
throw std::out_of_range{ err };
|
|
|
|
}
|
|
|
|
|
2021-11-07 20:33:02 +00:00
|
|
|
#else
|
2021-11-07 19:25:42 +00:00
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
TOML_ASSERT_ASSUME(n && "key not found in table!");
|
2021-11-07 19:25:42 +00:00
|
|
|
|
2021-11-07 20:33:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return *n;
|
|
|
|
}
|
2021-11-07 19:25:42 +00:00
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-11-14 17:24:19 +00:00
|
|
|
table::map_iterator table::get_lower_bound(std::string_view key) noexcept
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
return map_.lower_bound(key);
|
|
|
|
}
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table::iterator table::find(std::string_view key) noexcept
|
|
|
|
{
|
2021-11-25 18:20:22 +00:00
|
|
|
return iterator{ map_.find(key) };
|
2021-11-14 17:24:19 +00:00
|
|
|
}
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table::const_iterator table::find(std::string_view key) const noexcept
|
|
|
|
{
|
2021-11-25 18:20:22 +00:00
|
|
|
return const_iterator{ map_.find(key) };
|
2021-11-14 17:24:19 +00:00
|
|
|
}
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-11-14 17:24:19 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table::map_iterator table::erase(const_map_iterator pos) noexcept
|
|
|
|
{
|
|
|
|
return map_.erase(pos);
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
2021-11-10 09:17:15 +00:00
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-11-14 17:24:19 +00:00
|
|
|
table::map_iterator table::erase(const_map_iterator begin, const_map_iterator end) noexcept
|
2021-11-10 09:17:15 +00:00
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
return map_.erase(begin, end);
|
2021-11-10 09:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2021-11-14 17:24:19 +00:00
|
|
|
size_t table::erase(std::string_view key) noexcept
|
2021-11-10 09:17:15 +00:00
|
|
|
{
|
2021-11-14 17:24:19 +00:00
|
|
|
if (auto it = map_.find(key); it != map_.end())
|
|
|
|
{
|
|
|
|
map_.erase(it);
|
|
|
|
return size_t{ 1 };
|
|
|
|
}
|
|
|
|
return size_t{};
|
2021-11-10 09:17:15 +00:00
|
|
|
}
|
2021-11-12 17:37:02 +00:00
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table& table::prune(bool recursive)& noexcept
|
|
|
|
{
|
|
|
|
if (map_.empty())
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
for (auto it = map_.begin(); it != map_.end();)
|
|
|
|
{
|
|
|
|
if (auto arr = it->second->as_array())
|
|
|
|
{
|
|
|
|
if (recursive)
|
|
|
|
arr->prune(true);
|
|
|
|
|
|
|
|
if (arr->empty())
|
|
|
|
{
|
|
|
|
it = map_.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (auto tbl = it->second->as_table())
|
|
|
|
{
|
|
|
|
if (recursive)
|
|
|
|
tbl->prune(true);
|
|
|
|
|
|
|
|
if (tbl->empty())
|
|
|
|
{
|
|
|
|
it = map_.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2021-11-14 17:24:19 +00:00
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
void table::clear() noexcept
|
|
|
|
{
|
|
|
|
map_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
|
|
|
table::map_iterator table::insert_with_hint(const_iterator hint, key && k, impl::node_ptr && v)
|
|
|
|
{
|
2021-11-25 18:20:22 +00:00
|
|
|
return map_.emplace_hint(const_map_iterator{ hint }, std::move(k), std::move(v));
|
2021-11-14 17:24:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TOML_EXTERNAL_LINKAGE
|
2022-06-06 09:21:46 +00:00
|
|
|
bool TOML_CALLCONV table::equal(const table& lhs, const table& rhs) noexcept
|
2021-11-14 17:24:19 +00:00
|
|
|
{
|
|
|
|
if (&lhs == &rhs)
|
|
|
|
return true;
|
|
|
|
if (lhs.map_.size() != rhs.map_.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (auto l = lhs.map_.begin(), r = rhs.map_.begin(), e = lhs.map_.end(); l != e; l++, r++)
|
|
|
|
{
|
|
|
|
if (l->first != r->first)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto lhs_type = l->second->type();
|
|
|
|
const node& rhs_ = *r->second;
|
|
|
|
const auto rhs_type = rhs_.type();
|
|
|
|
if (lhs_type != rhs_type)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const bool equal = l->second->visit(
|
|
|
|
[&](const auto& lhs_) noexcept
|
|
|
|
{ return lhs_ == *reinterpret_cast<std::remove_reference_t<decltype(lhs_)>*>(&rhs_); });
|
|
|
|
if (!equal)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
TOML_NAMESPACE_END;
|
|
|
|
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_end.h"
|