NeBuild dev
Loading...
Searching...
No Matches
table.inl
Go to the documentation of this file.
1// # This file is a part of toml++ and is subject to the the terms of the MIT license.
2// # Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3// # See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4// SPDX-License-Identifier: MIT
5#pragma once
6
7// # {{
8#include "preprocessor.hpp"
9#if !TOML_IMPLEMENTATION
10#error This is an implementation-only header.
11#endif
12// # }}
13
14#include "header_start.hpp"
15#include "node_view.hpp"
16#include "table.hpp"
17
20 table::table() noexcept {
21#if TOML_LIFETIME_HOOKS
22 TOML_TABLE_CREATED;
23#endif
24 }
25
27 table::~table() noexcept {
28#if TOML_LIFETIME_HOOKS
29 TOML_TABLE_DESTROYED;
30#endif
31 }
32
34 table::table(const impl::table_init_pair* b, const impl::table_init_pair* e) {
35#if TOML_LIFETIME_HOOKS
36 TOML_TABLE_CREATED;
37#endif
38
41 TOML_ASSERT_ASSUME(b <= e);
42
43 if TOML_UNLIKELY (b == e) return;
44
45 for (; b != e; b++) {
46 if (!b->value) // empty node_views
47 continue;
48
49 map_.insert_or_assign(std::move(b->key), std::move(b->value));
50 }
51 }
52
54 table::table(const table& other) //
55 : node(other), inline_{other.inline_} {
56 for (auto&& [k, v] : other.map_) map_.emplace_hint(map_.end(), k, impl::make_node(*v));
57
58#if TOML_LIFETIME_HOOKS
59 TOML_TABLE_CREATED;
60#endif
61 }
62
64 table::table(table && other) noexcept //
65 : node(std::move(other)), map_{std::move(other.map_)}, inline_{other.inline_} {
66#if TOML_LIFETIME_HOOKS
67 TOML_TABLE_CREATED;
68#endif
69 }
70
72 table& table::operator=(const table& rhs) {
73 if (&rhs != this) {
74 node::operator=(rhs);
75 map_.clear();
76 for (auto&& [k, v] : rhs.map_) map_.emplace_hint(map_.end(), k, impl::make_node(*v));
77 inline_ = rhs.inline_;
78 }
79 return *this;
80 }
81
83 table& table::operator=(table&& rhs) noexcept {
84 if (&rhs != this) {
85 node::operator=(std::move(rhs));
86 map_ = std::move(rhs.map_);
87 inline_ = rhs.inline_;
88 }
89 return *this;
90 }
91
94 bool table::is_homogeneous(node_type ntype) const noexcept {
95 if (map_.empty()) return false;
96
97 if (ntype == node_type::none) ntype = map_.cbegin()->second->type();
98
99 for (auto&& [k, v] : map_) {
100 TOML_UNUSED(k);
101 if (v->type() != ntype) return false;
102 }
103
104 return true;
105 }
106
109 bool table::is_homogeneous(node_type ntype, node * &first_nonmatch) noexcept {
110 if (map_.empty()) {
111 first_nonmatch = {};
112 return false;
113 }
114 if (ntype == node_type::none) ntype = map_.cbegin()->second->type();
115 for (const auto& [k, v] : map_) {
116 TOML_UNUSED(k);
117 if (v->type() != ntype) {
118 first_nonmatch = v.get();
119 return false;
120 }
121 }
122 return true;
123 }
124
127 bool table::is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept {
128 node* fnm = nullptr;
129 const auto result = const_cast<table&>(*this).is_homogeneous(ntype, fnm);
130 first_nonmatch = fnm;
131 return result;
132 }
133
136 node* table::get(std::string_view key) noexcept {
137 if (auto it = map_.find(key); it != map_.end()) return it->second.get();
138 return nullptr;
139 }
140
142 node& table::at(std::string_view key) {
143 auto n = get(key);
144
145#if TOML_COMPILER_HAS_EXCEPTIONS
146
147 if (!n) {
148 auto err = "key '"s;
149 err.append(key);
150 err.append("' not found in table"sv);
151 throw std::out_of_range{err};
152 }
153
154#else
155
156 TOML_ASSERT_ASSUME(n && "key not found in table!");
157
158#endif
159
160 return *n;
161 }
162
165 table::map_iterator table::get_lower_bound(std::string_view key) noexcept {
166 return map_.lower_bound(key);
167 }
168
171 table::iterator table::find(std::string_view key) noexcept {
172 return iterator{map_.find(key)};
173 }
174
177 table::const_iterator table::find(std::string_view key) const noexcept {
178 return const_iterator{map_.find(key)};
179 }
180
182 table::map_iterator table::erase(const_map_iterator pos) noexcept {
183 return map_.erase(pos);
184 }
185
187 table::map_iterator table::erase(const_map_iterator begin, const_map_iterator end) noexcept {
188 return map_.erase(begin, end);
189 }
190
192 size_t table::erase(std::string_view key) noexcept {
193 if (auto it = map_.find(key); it != map_.end()) {
194 map_.erase(it);
195 return size_t{1};
196 }
197 return size_t{};
198 }
199
201 table& table::prune(bool recursive)& noexcept {
202 if (map_.empty()) return *this;
203
204 for (auto it = map_.begin(); it != map_.end();) {
205 if (auto arr = it->second->as_array()) {
206 if (recursive) arr->prune(true);
207
208 if (arr->empty()) {
209 it = map_.erase(it);
210 continue;
211 }
212 } else if (auto tbl = it->second->as_table()) {
213 if (recursive) tbl->prune(true);
214
215 if (tbl->empty()) {
216 it = map_.erase(it);
217 continue;
218 }
219 }
220 it++;
221 }
222
223 return *this;
224 }
225
227 void table::clear() noexcept {
228 map_.clear();
229 }
230
232 table::map_iterator table::insert_with_hint(const_iterator hint, key && k, impl::node_ptr && v) {
233 return map_.emplace_hint(const_map_iterator{hint}, std::move(k), std::move(v));
234 }
235
238 bool TOML_CALLCONV table::equal(const table& lhs, const table& rhs) noexcept {
239 if (&lhs == &rhs) return true;
240 if (lhs.map_.size() != rhs.map_.size()) return false;
241
242 for (auto l = lhs.map_.begin(), r = rhs.map_.begin(), e = lhs.map_.end(); l != e; l++, r++) {
243 if (l->first != r->first) return false;
244
245 const auto lhs_type = l->second->type();
246 const node& rhs_ = *r->second;
247 const auto rhs_type = rhs_.type();
248 if (lhs_type != rhs_type) return false;
249
250 const bool equal = l->second->visit([&](const auto& lhs_) noexcept {
251 return lhs_ == *reinterpret_cast<std::remove_reference_t<decltype(lhs_)>*>(&rhs_);
252 });
253 if (!equal) return false;
254 }
255 return true;
256 }
257}
259
260#include "header_end.hpp"
A TOML table.
Definition table.hpp:220
TOML_EXPORTED_MEMBER_FUNCTION table & prune(bool recursive=true) &noexcept
Removes empty child arrays and tables.
KeyType && key
Definition table.hpp:1469
TOML_NODISCARD TOML_EXPORTED_MEMBER_FUNCTION node & at(std::string_view key)
Gets a reference to the element at a specific key, throwing std::out_of_range if none existed.
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION table() noexcept
Default constructor.
TOML_PURE_GETTER TOML_EXPORTED_MEMBER_FUNCTION node * get(std::string_view key) noexcept
Gets the node at a specific key.
toml::const_table_iterator const_iterator
A BidirectionalIterator for iterating over const key-value pairs in a toml::table.
Definition table.hpp:793
TOML_EXPORTED_MEMBER_FUNCTION void clear() noexcept
Removes all key-value pairs from the table.
toml::table_iterator iterator
A BidirectionalIterator for iterating over key-value pairs in a toml::table.
Definition table.hpp:790
TOML_PURE_GETTER TOML_EXPORTED_MEMBER_FUNCTION bool is_homogeneous(node_type ntype) const noexcept final
TOML_EXPORTED_MEMBER_FUNCTION table & operator=(const table &)
Copy-assignment operator.
TOML_PURE_INLINE_GETTER iterator begin() noexcept
Returns an iterator to the first key-value pair.
Definition table.hpp:797
TOML_EXPORTED_MEMBER_FUNCTION ~table() noexcept
TOML_PURE_GETTER TOML_EXPORTED_MEMBER_FUNCTION iterator find(std::string_view key) noexcept
Gets an iterator to the node at a specific key.
TOML_PURE_INLINE_GETTER const_iterator cbegin() const noexcept
Returns an iterator to the first key-value pair.
Definition table.hpp:811
TOML_PURE_INLINE_GETTER iterator end() noexcept
Returns an iterator to one-past-the-last key-value pair.
Definition table.hpp:818
iterator erase(iterator pos) noexcept
Removes the specified key-value pair from the table.
Definition table.hpp:1295
TOML_PURE_INLINE_GETTER size_t size() const noexcept
Returns the number of key-value pairs in the table.
Definition table.hpp:1119
#define TOML_CALLCONV
Calling convention to apply to exported free/static functions. \detail Not defined by default (let th...
Definition preprocessor.hpp:1134
#define TOML_ASSERT_ASSUME(expr)
Definition preprocessor.hpp:1190
#define TOML_UNUSED(...)
Definition preprocessor.hpp:603
#define TOML_UNLIKELY(...)
Definition preprocessor.hpp:538
#define TOML_EXTERNAL_LINKAGE
Definition preprocessor.hpp:1339
#define TOML_PURE_GETTER
Definition preprocessor.hpp:474
#define TOML_NAMESPACE_END
Definition preprocessor.hpp:1320
TOML_NAMESPACE_START
Definition table.inl:18