tomlplusplus/include/toml++/toml_table_impl.h
Mark Gillard 946a912cee added node::value() and node::value_or()
also:
- added `node_view::value`
- added relops for the date/time classes
- added `TOML_ALL_INLINE` and `TOML_IMPLEMENTATION` options
- fixed documentation header overflowing on narrow devices
2020-03-01 16:56:40 +02:00

178 lines
4.7 KiB
C++

#pragma once
#include "toml_table.h"
#include "toml_node_view.h"
#if TOML_IMPLEMENTATION
TOML_IMPL_START
{
template struct table_proxy_pair<true>;
template struct table_proxy_pair<false>;
template class table_iterator<true>;
template class table_iterator<false>;
}
TOML_IMPL_END
TOML_START
{
TOML_INLINE_FUNC_IMPL
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_INLINE_FUNC_IMPL
table::table() noexcept {}
TOML_INLINE_FUNC_IMPL
table::table(table&& other) noexcept
: node{ std::move(other) },
values{ std::move(other.values) },
inline_{ other.inline_ }
{}
TOML_INLINE_FUNC_IMPL
table& table::operator = (table&& rhs) noexcept
{
node::operator=(std::move(rhs));
values = std::move(rhs.values);
inline_ = rhs.inline_;
return *this;
}
TOML_INLINE_FUNC_IMPL node_type table::type() const noexcept { return node_type::table; }
TOML_INLINE_FUNC_IMPL bool table::is_table() const noexcept { return true; }
TOML_INLINE_FUNC_IMPL bool table::is_array() const noexcept { return false; }
TOML_INLINE_FUNC_IMPL bool table::is_value() const noexcept { return false; }
TOML_INLINE_FUNC_IMPL table* table::as_table() noexcept { return this; }
TOML_INLINE_FUNC_IMPL const table* table::as_table() const noexcept { return this; }
TOML_INLINE_FUNC_IMPL bool table::is_inline() const noexcept { return inline_; }
TOML_INLINE_FUNC_IMPL void table::is_inline(bool val) noexcept { inline_ = val; }
TOML_INLINE_FUNC_IMPL
node_view<node> table::operator[] (string_view key) noexcept
{
return { this->get(key) };
}
TOML_INLINE_FUNC_IMPL
node_view<const node> table::operator[] (string_view key) const noexcept
{
return { this->get(key) };
}
TOML_INLINE_FUNC_IMPL table::iterator table::begin() noexcept { return { values.begin() }; }
TOML_INLINE_FUNC_IMPL table::const_iterator table::begin() const noexcept { return { values.begin() }; }
TOML_INLINE_FUNC_IMPL table::const_iterator table::cbegin() const noexcept { return { values.cbegin() }; }
TOML_INLINE_FUNC_IMPL table::iterator table::end() noexcept { return { values.end() }; }
TOML_INLINE_FUNC_IMPL table::const_iterator table::end() const noexcept { return { values.end() }; }
TOML_INLINE_FUNC_IMPL table::const_iterator table::cend() const noexcept { return { values.cend() }; }
TOML_INLINE_FUNC_IMPL bool table::empty() const noexcept { return values.empty(); }
TOML_INLINE_FUNC_IMPL size_t table::size() const noexcept { return values.size(); }
TOML_INLINE_FUNC_IMPL void table::clear() noexcept { values.clear(); }
TOML_INLINE_FUNC_IMPL
table::iterator table::erase(iterator pos) noexcept
{
return { values.erase(pos.raw_) };
}
TOML_INLINE_FUNC_IMPL
table::iterator table::erase(const_iterator pos) noexcept
{
return { values.erase(pos.raw_) };
}
TOML_INLINE_FUNC_IMPL
table::iterator table::erase(const_iterator first, const_iterator last) noexcept
{
return { values.erase(first.raw_, last.raw_) };
}
TOML_INLINE_FUNC_IMPL
bool table::erase(string_view key) noexcept
{
if (auto it = values.find(key); it != values.end())
{
values.erase(it);
return true;
}
return false;
}
TOML_INLINE_FUNC_IMPL
node* table::get(string_view key) noexcept
{
return do_get(values, key);
}
TOML_INLINE_FUNC_IMPL
const node* table::get(string_view key) const noexcept
{
return do_get(values, key);
}
TOML_INLINE_FUNC_IMPL
table::iterator table::find(string_view key) noexcept
{
return { values.find(key) };
}
TOML_INLINE_FUNC_IMPL
table::const_iterator table::find(string_view key) const noexcept
{
return { values.find(key) };
}
TOML_INLINE_FUNC_IMPL
bool table::contains(string_view key) const noexcept
{
return do_contains(values, key);
}
[[nodiscard]] TOML_INLINE_FUNC_IMPL
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;
}
[[nodiscard]] TOML_INLINE_FUNC_IMPL
bool operator != (const table& lhs, const table& rhs) noexcept
{
return !(lhs == rhs);
}
}
TOML_END
#endif // TOML_IMPLEMENTATION