Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ArrayView.hpp
1#pragma once
2
3#include "util/Assert.hpp"
4#include "util/config/ConfigDefinition.hpp"
5#include "util/config/ObjectView.hpp"
6#include "util/config/ValueView.hpp"
7
8#include <cstddef>
9#include <functional>
10#include <iterator>
11#include <string>
12#include <string_view>
13
14namespace util::config {
15
23class ArrayView {
24public:
28 template <typename T>
30 using iterator_category = std::forward_iterator_tag;
31 using pointer = T const*;
32 using reference = T const&;
33 using value_type = T;
34
41 ArrayIterator(ArrayView const& arr, std::size_t index) : arr_{arr}, index_{index}
42 {
43 if (arr_.clioConfig_.get().contains(arr_.prefix_)) {
44 ASSERT((std::is_same_v<T, ValueView>), "Array iterator must be ValueView");
45 } else {
46 ASSERT((std::is_same_v<T, ObjectView>), "Array iterator must be ObjectView");
47 }
48 }
49
57 {
58 if (index_ < arr_.size())
59 ++index_;
60 return *this;
61 }
62
70 {
71 ArrayIterator temp = *this;
72 if (index_ < arr_.size())
73 ++index_;
74 return temp;
75 }
76
82 T
84 {
85 if constexpr (std::is_same_v<T, ObjectView>) {
86 return ObjectView{arr_.prefix_, index_, arr_.clioConfig_.get()};
87 } else {
88 return arr_.clioConfig_.get().getValueInArray(arr_.prefix_, index_);
89 }
90 }
91
99 bool
100 operator==(ArrayIterator const& other) const
101 {
102 return &arr_ == &(other.arr_) && index_ == other.index_;
103 }
104
111 bool
112 operator!=(ArrayIterator const& other) const
113 {
114 return &arr_ != &(other.arr_) || index_ != other.index_;
115 }
116
117 private:
118 ArrayView const& arr_;
119 std::size_t index_ = 0;
120 };
121
127 template <typename T>
128 [[nodiscard]] auto
129 begin() const
130 {
131 return ArrayIterator<T>(*this, 0);
132 }
133
139 template <typename T>
140 [[nodiscard]] auto
141 end() const
142 {
143 return ArrayIterator<T>(*this, this->size());
144 }
145
152 ArrayView(std::string_view prefix, ClioConfigDefinition const& configDef);
153
160 [[nodiscard]] ObjectView
161 objectAt(std::size_t idx) const;
162
169 [[nodiscard]] ValueView
170 valueAt(std::size_t idx) const;
171
177 [[nodiscard]] size_t
178 size() const;
179
180private:
181 std::string prefix_;
182 std::reference_wrapper<ClioConfigDefinition const> clioConfig_;
183};
184
185} // namespace util::config
ObjectView objectAt(std::size_t idx) const
Returns an ObjectView at the specified index.
Definition ArrayView.cpp:39
auto end() const
Returns an iterator to the end of the Array.
Definition ArrayView.hpp:141
auto begin() const
Returns an iterator to the beginning of the Array.
Definition ArrayView.hpp:129
size_t size() const
Returns the number of elements in the array.
Definition ArrayView.cpp:33
ArrayView(std::string_view prefix, ClioConfigDefinition const &configDef)
Constructs an ArrayView with the given prefix and config definition.
Definition ArrayView.cpp:15
ValueView valueAt(std::size_t idx) const
Returns a ValueView at the specified index.
Definition ArrayView.cpp:21
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
Provides a view into a subset of configuration data defined by a prefix.
Definition ObjectView.hpp:21
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:27
Custom iterator class which contains config object or value underneath ArrayView.
Definition ArrayView.hpp:29
ArrayIterator & operator++()
Prefix increment operator.
Definition ArrayView.hpp:56
bool operator!=(ArrayIterator const &other) const
Inequality operator.
Definition ArrayView.hpp:112
bool operator==(ArrayIterator const &other) const
Equality operator.
Definition ArrayView.hpp:100
ArrayIterator(ArrayView const &arr, std::size_t index)
Constructs an ArrayIterator with underlying ArrayView and index value.
Definition ArrayView.hpp:41
T operator*()
Dereference operator to get a ValueView or ObjectView.
Definition ArrayView.hpp:83
ArrayIterator operator++(int)
Postfix increment operator.
Definition ArrayView.hpp:69