tomlplusplus/include/toml++/toml_table.hpp
Mark Gillard fd07301bae improved support for __fp16, _Float16 and __float128
also:
- fixed a bunch of doxygen parsing issues
- added `#define` leak detection to the single-header script
- renamed `TOML_ALL_INLINE` to `TOML_HEADER_ONLY` (the old one still works too)
- simplified abi namespace definitions
2020-07-25 20:50:24 +03:00

241 lines
6.2 KiB
C++

//# This file is a part of toml++ and is subject to the the terms of the MIT license.
//# Copyright (c) 2019-2020 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
//# {{
#include "toml_preprocessor.h"
#if !TOML_IMPLEMENTATION
#error This is an implementation-only header.
#endif
//# }}
#include "toml_table.h"
#include "toml_node_view.h"
TOML_PUSH_WARNINGS
TOML_DISABLE_SUGGEST_WARNINGS
TOML_DISABLE_PADDING_WARNINGS
TOML_NAMESPACE_START
{
TOML_EXTERNAL_LINKAGE
table::table(impl::table_init_pair* pairs, size_t count) noexcept
{
for (size_t i = 0; i < count; i++)
{
values.insert_or_assign(
std::move(pairs[i].key),
std::move(pairs[i].value)
);
}
}
TOML_EXTERNAL_LINKAGE
table::table() noexcept {}
TOML_EXTERNAL_LINKAGE
table::table(table&& other) noexcept
: node{ std::move(other) },
values{ std::move(other.values) },
inline_{ other.inline_ }
{}
TOML_EXTERNAL_LINKAGE
table& table::operator = (table&& rhs) noexcept
{
node::operator=(std::move(rhs));
values = std::move(rhs.values);
inline_ = rhs.inline_;
return *this;
}
#define TOML_MEMBER_ATTR(attr) TOML_EXTERNAL_LINKAGE TOML_ATTR(attr)
TOML_MEMBER_ATTR(const) node_type table::type() const noexcept { return node_type::table; }
TOML_MEMBER_ATTR(const) bool table::is_table() const noexcept { return true; }
TOML_MEMBER_ATTR(const) bool table::is_array() const noexcept { return false; }
TOML_MEMBER_ATTR(const) bool table::is_value() const noexcept { return false; }
TOML_MEMBER_ATTR(const) const table* table::as_table() const noexcept { return this; }
TOML_MEMBER_ATTR(const) table* table::as_table() noexcept { return this; }
TOML_MEMBER_ATTR(pure) bool table::is_inline() const noexcept { return inline_; }
TOML_EXTERNAL_LINKAGE void table::is_inline(bool val) noexcept { inline_ = val; }
TOML_EXTERNAL_LINKAGE table::const_iterator table::begin() const noexcept { return { values.begin() }; }
TOML_EXTERNAL_LINKAGE table::const_iterator table::end() const noexcept { return { values.end() }; }
TOML_EXTERNAL_LINKAGE table::const_iterator table::cbegin() const noexcept { return { values.cbegin() }; }
TOML_EXTERNAL_LINKAGE table::const_iterator table::cend() const noexcept { return { values.cend() }; }
TOML_EXTERNAL_LINKAGE table::iterator table::begin() noexcept { return { values.begin() }; }
TOML_EXTERNAL_LINKAGE table::iterator table::end() noexcept { return { values.end() }; }
TOML_MEMBER_ATTR(pure) bool table::empty() const noexcept { return values.empty(); }
TOML_MEMBER_ATTR(pure) size_t table::size() const noexcept { return values.size(); }
TOML_EXTERNAL_LINKAGE void table::clear() noexcept { values.clear(); }
#undef TOML_MEMBER_ATTR
TOML_EXTERNAL_LINKAGE
node_view<node> table::operator[] (std::string_view key) noexcept
{
return { this->get(key) };
}
TOML_EXTERNAL_LINKAGE
node_view<const node> table::operator[] (std::string_view key) const noexcept
{
return { this->get(key) };
}
TOML_EXTERNAL_LINKAGE
table::iterator table::erase(iterator pos) noexcept
{
return { values.erase(pos.raw_) };
}
TOML_EXTERNAL_LINKAGE
table::iterator table::erase(const_iterator pos) noexcept
{
return { values.erase(pos.raw_) };
}
TOML_EXTERNAL_LINKAGE
table::iterator table::erase(const_iterator first, const_iterator last) noexcept
{
return { values.erase(first.raw_, last.raw_) };
}
TOML_EXTERNAL_LINKAGE
bool table::erase(std::string_view key) noexcept
{
if (auto it = values.find(key); it != values.end())
{
values.erase(it);
return true;
}
return false;
}
TOML_EXTERNAL_LINKAGE
node* table::get(std::string_view key) noexcept
{
return do_get(values, key);
}
TOML_EXTERNAL_LINKAGE
const node* table::get(std::string_view key) const noexcept
{
return do_get(values, key);
}
TOML_EXTERNAL_LINKAGE
table::iterator table::find(std::string_view key) noexcept
{
return { values.find(key) };
}
TOML_EXTERNAL_LINKAGE
table::const_iterator table::find(std::string_view key) const noexcept
{
return { values.find(key) };
}
TOML_EXTERNAL_LINKAGE
bool table::contains(std::string_view key) const noexcept
{
return do_contains(values, key);
}
#if TOML_WINDOWS_COMPAT
TOML_EXTERNAL_LINKAGE
node_view<node> table::operator[] (std::wstring_view key) noexcept
{
return { this->get(key) };
}
TOML_EXTERNAL_LINKAGE
node_view<const node> table::operator[] (std::wstring_view key) const noexcept
{
return { this->get(key) };
}
TOML_EXTERNAL_LINKAGE
bool table::erase(std::wstring_view key) noexcept
{
return erase(impl::narrow(key));
}
TOML_EXTERNAL_LINKAGE
node* table::get(std::wstring_view key) noexcept
{
return get(impl::narrow(key));
}
TOML_EXTERNAL_LINKAGE
const node* table::get(std::wstring_view key) const noexcept
{
return get(impl::narrow(key));
}
TOML_EXTERNAL_LINKAGE
table::iterator table::find(std::wstring_view key) noexcept
{
return find(impl::narrow(key));
}
TOML_EXTERNAL_LINKAGE
table::const_iterator table::find(std::wstring_view key) const noexcept
{
return find(impl::narrow(key));
}
TOML_EXTERNAL_LINKAGE
bool table::contains(std::wstring_view key) const noexcept
{
return contains(impl::narrow(key));
}
#endif // TOML_WINDOWS_COMPAT
TOML_API
TOML_EXTERNAL_LINKAGE
bool operator == (const table& lhs, const table& rhs) noexcept
{
if (&lhs == &rhs)
return true;
if (lhs.values.size() != rhs.values.size())
return false;
for (auto l = lhs.values.begin(), r = rhs.values.begin(), e = lhs.values.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;
}
TOML_API
TOML_EXTERNAL_LINKAGE
bool operator != (const table& lhs, const table& rhs) noexcept
{
return !(lhs == rhs);
}
}
TOML_NAMESPACE_END
TOML_POP_WARNINGS // TOML_DISABLE_SUGGEST_WARNINGS, TOML_DISABLE_PADDING_WARNINGS