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#include <utility>
14
15namespace util::config {
16
24class ArrayView {
25public:
29 template <typename T>
31 // Required by the std iterator contract.
32 // NOLINTBEGIN(readability-identifier-naming)
33 using iterator_category = std::forward_iterator_tag;
34 using pointer = T const*;
35 using reference = T const&;
36 using value_type = T;
37 // NOLINTEND(readability-identifier-naming)
38
45 ArrayIterator(ArrayView const& arr, std::size_t index) : arr_{arr}, index_{index}
46 {
47 if (arr_.clioConfig_.get().contains(arr_.prefix_)) {
48 ASSERT((std::is_same_v<T, ValueView>), "Array iterator must be ValueView");
49 } else {
50 ASSERT((std::is_same_v<T, ObjectView>), "Array iterator must be ObjectView");
51 }
52 }
53
61 {
62 if (index_ < arr_.size())
63 ++index_;
64 return *this;
65 }
66
74 {
75 ArrayIterator temp = *this;
76 if (index_ < arr_.size())
77 ++index_;
78 return temp;
79 }
80
86 T
88 {
89 if constexpr (std::is_same_v<T, ObjectView>) {
90 return ObjectView{arr_.prefix_, index_, arr_.clioConfig_.get()};
91 } else {
92 return arr_.clioConfig_.get().getValueInArray(arr_.prefix_, index_);
93 }
94 }
95
103 bool
104 operator==(ArrayIterator const& other) const
105 {
106 return &arr_ == &other.arr_ && index_ == other.index_;
107 }
108
115 bool
116 operator!=(ArrayIterator const& other) const
117 {
118 return &arr_ != &other.arr_ || index_ != other.index_;
119 }
120
121 private:
122 ArrayView const& arr_;
123 std::size_t index_ = 0;
124 };
125
131 template <typename T>
132 [[nodiscard]] auto
133 begin() const
134 {
135 return ArrayIterator<T>(*this, 0);
136 }
137
143 template <typename T>
144 [[nodiscard]] auto
145 end() const
146 {
147 return ArrayIterator<T>(*this, this->size());
148 }
149
153 template <typename T>
154 class Elements;
155
161 [[nodiscard]] Elements<ValueView>
162 values() const;
163
169 [[nodiscard]] Elements<ObjectView>
170 objects() const;
171
178 ArrayView(std::string_view prefix, ClioConfigDefinition const& configDef);
179
186 [[nodiscard]] ObjectView
187 objectAt(std::size_t idx) const;
188
195 [[nodiscard]] ValueView
196 valueAt(std::size_t idx) const;
197
203 [[nodiscard]] size_t
204 size() const;
205
206private:
207 std::string prefix_;
208 std::reference_wrapper<ClioConfigDefinition const> clioConfig_;
209};
210
211template <typename T>
213public:
219 explicit Elements(ArrayView array) : array_{std::move(array)}
220 {
221 }
222
228 [[nodiscard]] ArrayIterator<T>
229 begin() const
230 {
231 return array_.begin<T>();
232 }
233
239 [[nodiscard]] ArrayIterator<T>
240 end() const
241 {
242 return array_.end<T>();
243 }
244
245private:
246 ArrayView array_;
247};
248
249[[nodiscard]] inline ArrayView::Elements<ValueView>
251{
252 return Elements<ValueView>{*this};
253}
254
255[[nodiscard]] inline ArrayView::Elements<ObjectView>
257{
258 return Elements<ObjectView>{*this};
259}
260
261} // namespace util::config
A range over the elements of an array, viewed as T (ValueView or ObjectView).
Definition ArrayView.hpp:212
ArrayIterator< T > end() const
Returns an iterator past the last element.
Definition ArrayView.hpp:240
Elements(ArrayView array)
Constructs the range over the given array.
Definition ArrayView.hpp:219
ArrayIterator< T > begin() const
Returns an iterator to the first element.
Definition ArrayView.hpp:229
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:145
auto begin() const
Returns an iterator to the beginning of the Array.
Definition ArrayView.hpp:133
size_t size() const
Returns the number of elements in the array.
Definition ArrayView.cpp:33
Elements< ObjectView > objects() const
Returns a range over the elements of an array of objects.
Definition ArrayView.hpp:256
Elements< ValueView > values() const
Returns a range over the elements of an array of values.
Definition ArrayView.hpp:250
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:30
ArrayIterator & operator++()
Prefix increment operator.
Definition ArrayView.hpp:60
bool operator!=(ArrayIterator const &other) const
Inequality operator.
Definition ArrayView.hpp:116
bool operator==(ArrayIterator const &other) const
Equality operator.
Definition ArrayView.hpp:104
ArrayIterator(ArrayView const &arr, std::size_t index)
Constructs an ArrayIterator with underlying ArrayView and index value.
Definition ArrayView.hpp:45
T operator*()
Dereference operator to get a ValueView or ObjectView.
Definition ArrayView.hpp:87
ArrayIterator operator++(int)
Postfix increment operator.
Definition ArrayView.hpp:73