Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ArrayView.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/Assert.hpp"
23#include "util/newconfig/ConfigDefinition.hpp"
24#include "util/newconfig/ObjectView.hpp"
25#include "util/newconfig/ValueView.hpp"
26
27#include <cstddef>
28#include <functional>
29#include <iterator>
30#include <string>
31#include <string_view>
32
33namespace util::config {
34
42class ArrayView {
43public:
47 template <typename T>
49 using iterator_category = std::forward_iterator_tag;
50 using pointer = T const*;
51 using reference = T const&;
52 using value_type = T;
53
60 ArrayIterator(ArrayView const& arr, std::size_t index) : arr_{arr}, index_{index}
61 {
62 if (arr_.clioConfig_.get().contains(arr_.prefix_)) {
63 ASSERT((std::is_same_v<T, ValueView>), "Array iterator must be ValueView");
64 } else {
65 ASSERT((std::is_same_v<T, ObjectView>), "Array iterator must be ObjectView");
66 }
67 }
68
76 {
77 if (index_ < arr_.size())
78 ++index_;
79 return *this;
80 }
81
89 {
90 ArrayIterator temp = *this;
91 if (index_ < arr_.size())
92 ++index_;
93 return temp;
94 }
95
101 T
103 {
104 if constexpr (std::is_same_v<T, ObjectView>) {
105 return ObjectView{arr_.prefix_, index_, arr_.clioConfig_.get()};
106 } else {
107 return arr_.clioConfig_.get().getValueInArray(arr_.prefix_, index_);
108 }
109 }
110
117 bool
118 operator==(ArrayIterator const& other) const
119 {
120 return &arr_ == &(other.arr_) && index_ == other.index_;
121 }
122
129 bool
130 operator!=(ArrayIterator const& other) const
131 {
132 return &arr_ != &(other.arr_) || index_ != other.index_;
133 }
134
135 private:
136 ArrayView const& arr_;
137 std::size_t index_ = 0;
138 };
139
145 template <typename T>
146 [[nodiscard]] auto
147 begin() const
148 {
149 return ArrayIterator<T>(*this, 0);
150 }
151
157 template <typename T>
158 [[nodiscard]] auto
159 end() const
160 {
161 return ArrayIterator<T>(*this, this->size());
162 }
163
170 ArrayView(std::string_view prefix, ClioConfigDefinition const& configDef);
171
178 [[nodiscard]] ObjectView
179 objectAt(std::size_t idx) const;
180
187 [[nodiscard]] ValueView
188 valueAt(std::size_t idx) const;
189
195 [[nodiscard]] size_t
196 size() const;
197
198private:
199 std::string prefix_;
200 std::reference_wrapper<ClioConfigDefinition const> clioConfig_;
201};
202
203} // namespace util::config
View for array structure for config.
Definition ArrayView.hpp:42
ObjectView objectAt(std::size_t idx) const
Returns an ObjectView at the specified index.
Definition ArrayView.cpp:54
auto end() const
Returns an iterator to the end of the Array.
Definition ArrayView.hpp:159
auto begin() const
Returns an iterator to the beginning of the Array.
Definition ArrayView.hpp:147
size_t size() const
Returns the number of elements in the array.
Definition ArrayView.cpp:48
ArrayView(std::string_view prefix, ClioConfigDefinition const &configDef)
Constructs an ArrayView with the given prefix and config definition.
Definition ArrayView.cpp:34
ValueView valueAt(std::size_t idx) const
Returns a ValueView at the specified index.
Definition ArrayView.cpp:40
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
Provides a view into a subset of configuration data defined by a prefix.
Definition ObjectView.hpp:40
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:46
Custom iterator class which contains config object or value underneath ArrayView.
Definition ArrayView.hpp:48
ArrayIterator & operator++()
Prefix increment operator.
Definition ArrayView.hpp:75
bool operator!=(ArrayIterator const &other) const
Inequality operator.
Definition ArrayView.hpp:130
bool operator==(ArrayIterator const &other) const
Equality operator.
Definition ArrayView.hpp:118
ArrayIterator(ArrayView const &arr, std::size_t index)
Constructs an ArrayIterator with underlying ArrayView and index value.
Definition ArrayView.hpp:60
T operator*()
Dereference operator to get a ValueView or ObjectView.
Definition ArrayView.hpp:102
ArrayIterator operator++(int)
Postfix increment operator.
Definition ArrayView.hpp:88