1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/*
* This source code is released into the public domain.
*/
module;
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <format>
#include <memory>
#include <optional>
#include <string>
#include <system_error>
#include <ucl.h>
export module nihil.ucl:map;
import :object;
namespace nihil::ucl {
// Exception thrown when map::operator[] does not find the key.
export struct key_not_found : error {
key_not_found(std::string_view key)
: error(std::format("key '{}' not found in map", key))
, m_key(key)
{}
auto key(this key_not_found const &self) -> std::string_view
{
return self.m_key;
}
private:
std::string m_key;
};
export template<datatype T>
struct map;
template<datatype T>
struct map_iterator {
using difference_type = std::ptrdiff_t;
using value_type = std::pair<std::string_view, T>;
using reference = value_type &;
using const_reference = value_type const &;
using pointer = value_type *;
using const_pointer = value_type const *;
struct sentinel{};
[[nodiscard]] auto operator==(this map_iterator const &self, sentinel)
-> bool
{
return (self.m_state->cur == nullptr);
}
auto operator++(this map_iterator &self) -> map_iterator &
{
self.m_state->next();
return self;
}
auto operator++(this map_iterator &self, int) -> map_iterator &
{
self.m_state->next();
return self;
}
[[nodiscard]] auto operator*(this map_iterator const &self)
-> value_type
{
auto obj = T(ref, self.m_state->cur);
return {obj.key(), std::move(obj)};
}
private:
friend struct map<T>;
map_iterator(::ucl_object_t const *obj)
: m_state(std::make_shared<state>(obj))
{
++(*this);
}
struct state {
state(::ucl_object_t const *obj)
{
iter = ::ucl_object_iterate_new(obj);
if (iter == nullptr)
throw std::system_error(make_error_code(
std::errc(errno)));
}
state(state const &) = delete;
auto operator=(this state &, state const &) -> state& = delete;
~state()
{
if (iter != nullptr)
::ucl_object_iterate_free(iter);
}
auto next() -> void
{
cur = ::ucl_object_iterate_safe(iter, true);
}
ucl_object_iter_t iter = nullptr;
ucl_object_t const *cur = nullptr;
};
std::shared_ptr<state> m_state;
};
export template<datatype T = object>
struct map final : object {
inline static constexpr object_type ucl_type = object_type::object;
using value_type = std::pair<std::string_view, T>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using iterator = map_iterator<T>;
/*
* Create an empty map. Throws std::system_error on failure.
*/
map() : object(noref, [] {
auto *uobj = ::ucl_object_typed_new(UCL_OBJECT);
if (uobj == nullptr)
throw std::system_error(
std::make_error_code(std::errc(errno)));
return uobj;
}())
{
}
/*
* Create a map from a UCL object. Throws type_mismatch on failure.
*
* Unlike object_cast<>, this does not check the type of the contained
* elements, which means object access can throw type_mismatch.
*/
map(ref_t, ::ucl_object_t const *uobj)
: object(nihil::ucl::ref, [&] {
auto actual_type = static_cast<object_type>(
::ucl_object_type(uobj));
if (actual_type != map::ucl_type)
throw type_mismatch(map::ucl_type,
actual_type);
return uobj;
}())
{
if (type() != ucl_type)
throw type_mismatch(ucl_type, type());
}
map(noref_t, ::ucl_object_t *uobj)
: object(nihil::ucl::noref, [&] {
auto actual_type = static_cast<object_type>(
::ucl_object_type(uobj));
if (actual_type != map::ucl_type)
throw type_mismatch(map::ucl_type,
actual_type);
return uobj;
}())
{
}
/*
* Create a map from an iterator pair.
*/
template<std::input_iterator Iterator>
requires(std::convertible_to<std::iter_value_t<Iterator>, value_type>)
map(Iterator first, Iterator last)
: map()
{
// This is exception safe, because if we throw here the
// base class destructor will free the map.
while (first != last) {
insert(*first);
++first;
}
}
/*
* Create a map from a range.
*/
template<std::ranges::range Range>
requires(std::convertible_to<std::ranges::range_value_t<Range>,
value_type>)
map(std::from_range_t, Range &&range)
: map(std::ranges::begin(range),
std::ranges::end(range))
{
}
/*
* Create a map from an initializer_list.
*/
map(std::initializer_list<value_type> const &list)
: map(std::ranges::begin(list), std::ranges::end(list))
{
}
/*
* Map iterator access.
*/
[[nodiscard]] auto begin(this map const &self) -> iterator
{
return {self.get_ucl_object()};
}
[[nodiscard]] auto end(this map const &) -> iterator::sentinel
{
return {};
}
/*
* Reserve space for future insertions.
*/
auto reserve(this map &self, size_type nelems) -> void
{
::ucl_object_reserve(self.get_ucl_object(), nelems);
}
/*
* Add an element to the map.
*/
auto insert(this map &self, value_type const &v) -> void
{
auto uobj = ::ucl_object_ref(v.second.get_ucl_object());
::ucl_object_insert_key(self.get_ucl_object(), uobj,
v.first.data(), v.first.size(), true);
}
/*
* Access a map element by key.
*/
[[nodiscard]] auto find(this map const &self, std::string_view key)
-> std::optional<T>
{
auto const *obj = ::ucl_object_lookup_len(
self.get_ucl_object(),
key.data(), key.size());
if (obj == nullptr)
return {};
return {T(nihil::ucl::ref, obj)};
}
/*
* Remove an object from the map.
*/
auto remove(this map &self, std::string_view key) -> bool
{
return ::ucl_object_delete_keyl(self.get_ucl_object(),
key.data(), key.size());
}
/*
* Remove an object from the map and return it.
*/
auto pop(this map &self, std::string_view key)
-> std::optional<T>
{
auto *uobj = ::ucl_object_pop_keyl(self.get_ucl_object(),
key.data(), key.size());
if (uobj)
return T(noref, uobj);
return {};
}
/*
* Equivalent to find(), except it throws key_not_found if the key
* doesn't exist in the map.
*/
[[nodiscard]] auto operator[] (this map const &self,
std::string_view key)
-> T
{
auto obj = self.find(key);
if (obj)
return *obj;
throw key_not_found(key);
}
};
} // namespace nihil::ucl
|