12 #ifdef GARLIC_USE_CONCEPTS
13 #define GARLIC_VIEW garlic::ViewLayer
14 #define GARLIC_REF garlic::RefLayer
15 #define GARLIC_ITERATOR_WRAPPER garlic::IteratorWrapper
17 #define GARLIC_VIEW typename
18 #define GARLIC_REF typename
19 #define GARLIC_ITERATOR_WRAPPER typename
24 enum TypeFlag : uint8_t {
34 template<
typename T>
using ConstValueIteratorOf =
typename std::decay_t<T>::ConstValueIterator;
35 template<
typename T>
using ValueIteratorOf =
typename std::decay_t<T>::ValueIterator;
36 template<
typename T>
using ConstMemberIteratorOf =
typename std::decay_t<T>::ConstMemberIterator;
37 template<
typename T>
using MemberIteratorOf =
typename std::decay_t<T>::MemberIterator;
170 #ifdef GARLIC_USE_CONCEPTS
173 concept member_pair = requires(T pair) {
179 concept forward_pair_iterator = std::forward_iterator<T> && requires(T it) {
180 { *it } -> member_pair;
183 template<
typename T> concept
ViewLayer = requires(
const T& t) {
184 typename ConstValueIteratorOf<T>;
185 typename ConstMemberIteratorOf<T>;
187 { t.is_null() } -> std::convertible_to<bool>;
188 { t.is_int() } -> std::convertible_to<bool>;
189 { t.is_string() } -> std::convertible_to<bool>;
190 { t.is_double() } -> std::convertible_to<bool>;
191 { t.is_object() } -> std::convertible_to<bool>;
192 { t.is_list() } -> std::convertible_to<bool>;
193 { t.is_bool() } -> std::convertible_to<bool>;
195 { t.get_int() } -> std::convertible_to<int>;
196 { t.get_cstr() } -> std::convertible_to<const char*>;
197 { t.get_string() } -> std::convertible_to<std::string>;
198 { t.get_string_view() } -> std::convertible_to<std::string_view>;
199 { t.get_double() } -> std::convertible_to<double>;
200 { t.get_bool() } -> std::convertible_to<bool>;
202 { t.begin_list() } -> std::forward_iterator;
203 { t.end_list() } -> std::forward_iterator;
204 { t.get_list() } -> std::ranges::range;
206 { t.begin_member() } -> forward_pair_iterator;
207 { t.end_member() } -> forward_pair_iterator;
208 { t.find_member(std::declval<text>()) } -> forward_pair_iterator;
209 { t.get_object() } -> std::ranges::range;
215 typename ValueIteratorOf<T>;
216 typename MemberIteratorOf<T>;
218 { t.set_string(std::declval<text>()) };
219 { t.set_bool(std::declval<bool>()) };
220 { t.set_int(std::declval<int>()) };
221 { t.set_double(std::declval<double>()) };
226 { t.begin_list() } -> std::forward_iterator;
227 { t.end_list() } -> std::forward_iterator;
228 { t.get_list() } -> std::ranges::range;
230 { t.begin_member() } -> forward_pair_iterator;
231 { t.end_member() } -> forward_pair_iterator;
232 { t.find_member(std::declval<text>()) } -> forward_pair_iterator;
233 { t.get_object() } -> std::ranges::range;
238 { t.push_back(std::declval<const char*>()) };
239 { t.push_back(std::declval<text>()) };
240 { t.push_back(std::declval<bool>()) };
241 { t.push_back(std::declval<int>()) };
242 { t.push_back(std::declval<double>()) };
243 { t.push_back_builder(std::declval<
void(*)(T)>()) };
245 { t.erase(std::declval<ValueIteratorOf<T>>()) };
246 { t.erase(std::declval<ValueIteratorOf<T>>(), std::declval<ValueIteratorOf<T>>()) };
248 { t.add_member(std::declval<text>()) };
249 { t.add_member(std::declval<text>(), std::declval<const char*>()) };
250 { t.add_member(std::declval<text>(), std::declval<text>()) };
251 { t.add_member(std::declval<text>(), std::declval<bool>()) };
252 { t.add_member(std::declval<text>(), std::declval<int>()) };
253 { t.add_member(std::declval<text>(), std::declval<double>()) };
254 { t.add_member_builder(std::declval<text>(), std::declval<
void(*)(T)>()) };
255 { t.remove_member(std::declval<text>()) };
256 { t.erase_member(std::declval<MemberIteratorOf<T>>()) };
258 { t.get_reference() };
262 concept TypeAbstraction = requires {
272 concept TypeProvider = requires (
const T& value) {
273 { T::type_flag } -> TypeAbstraction;
274 { value.get_type() } -> std::same_as<typename T::type_flag>;
279 typename T::output_type;
280 typename T::iterator_type;
282 { container.iterator };
295 template<
typename ValueType,
typename Iterator>
297 using output_type = ValueType;
298 using iterator_type = Iterator;
300 iterator_type iterator;
302 inline ValueType wrap()
const {
303 return ValueType { *iterator };
308 template<GARLIC_ITERATOR_WRAPPER Container,
typename DifferenceType = std::ptrdiff_t>
311 using difference_type = DifferenceType;
312 using value_type =
typename Container::output_type;
313 using reference =
typename Container::output_type&;
314 using pointer =
typename Container::output_type*;
315 using iterator_category = std::forward_iterator_tag;
316 using iterator_type =
typename Container::iterator_type;
321 : container_(std::move(container)) {}
323 self& operator ++ () {
324 ++container_.iterator;
328 self operator ++ (
int) {
330 ++container_.iterator;
334 bool operator == (
const self& other)
const {
335 return other.container_.iterator == container_.iterator;
338 bool operator != (
const self& other)
const {
return !(other == *
this); }
347 inline value_type
operator * ()
const {
return container_.wrap(); }
350 Container container_;
354 template<GARLIC_ITERATOR_WRAPPER Container,
typename DifferenceType = std::ptrdiff_t>
357 using difference_type = DifferenceType;
358 using value_type =
typename Container::output_type;
359 using reference_type = value_type;
360 using pointer_type = value_type*;
361 using iterator_category = std::random_access_iterator_tag;
362 using iterator_type =
typename Container::iterator_type;
367 : container_(container) {}
369 self& operator ++ () {
370 ++container_.iterator;
374 self operator ++ (
int) {
376 ++container_.iterator;
380 self& operator -- () {
381 --container_.iterator;
385 self operator -- (
int) {
387 --container_.iterator;
391 bool operator == (
const self& other)
const {
392 return container_.iterator == other.container_.iterator;
395 bool operator != (
const self& other)
const {
396 return container_.iterator != other.container_.iterator;
399 bool operator < (
const self& other)
const {
400 return container_.iterator < other.container_.iterator;
403 bool operator > (
const self& other)
const {
404 return container_.iterator > other.container_.iterator;
407 bool operator <= (
const self& other)
const {
408 return container_.iterator <= other.container_.iterator;
411 bool operator >= (
const self& other)
const {
412 return container_.iterator >= other.container_.iterator;
415 self operator + (
int value)
const {
417 it.container_.iterator += value;
421 self& operator += (
int value) {
422 container_.iterator += value;
426 self operator - (
int value)
const {
428 it.container_.iterator -= value;
432 self& operator -= (
int value) {
433 container_.iterator -= value;
437 reference_type operator [] (
int index)
const {
438 return *(*
this + index);
446 return container_.iterator;
451 return container_.wrap();
458 return right += left;
461 friend inline RandomAccessIterator<Container, DifferenceType>
464 RandomAccessIterator<Container, DifferenceType> right) {
465 return right -= left;
468 friend inline DifferenceType
470 const RandomAccessIterator<Container, DifferenceType>& left,
471 const RandomAccessIterator<Container, DifferenceType>& right) {
476 Container container_;
493 template<GARLIC_REF LayerType>
496 using difference_type = ptrdiff_t;
497 using value_type = void;
498 using reference_type = void;
499 using pointer_type = void;
500 using iterator_category = std::output_iterator_tag;
501 using container_type = LayerType;
506 inline void operator = (T&& value) noexcept {
507 layer_.push_back(std::forward<T>(value));
519 template<GARLIC_REF LayerType>
530 template<
typename ValueType,
typename Iterator>
531 using BasicForwardIterator = ForwardIterator<
532 BasicIteratorWrapper<ValueType, Iterator>
540 template<
typename ValueType,
typename Iterator>
546 template<
typename ValueType,
typename KeyType = ValueType>
560 template <
typename LayerType>
562 const LayerType& layer;
564 ConstValueIteratorOf<LayerType> begin()
const {
return layer.begin_list(); }
565 ConstValueIteratorOf<LayerType> end()
const {
return layer.end_list(); }
576 template <
typename LayerType>
580 ValueIteratorOf<LayerType> begin() {
return layer.begin_list(); }
581 ValueIteratorOf<LayerType> end() {
return layer.end_list(); }
592 template <
typename LayerType>
594 const LayerType& layer;
596 ConstMemberIteratorOf<LayerType> begin()
const {
return layer.begin_member(); }
597 ConstMemberIteratorOf<LayerType> end()
const {
return layer.end_member(); }
608 template <
typename LayerType>
612 MemberIteratorOf<LayerType> begin() {
return layer.begin_member(); }
613 MemberIteratorOf<LayerType> end() {
return layer.end_member(); }