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/config/ConfigDefinition.hpp"
24#include "util/config/ObjectView.hpp"
25#include "util/config/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
118 bool
119 operator==(ArrayIterator const& other) const
120 {
121 return &arr_ == &(other.arr_) && index_ == other.index_;
122 }
123
130 bool
131 operator!=(ArrayIterator const& other) const
132 {
133 return &arr_ != &(other.arr_) || index_ != other.index_;
134 }
135
136 private:
137 ArrayView const& arr_;
138 std::size_t index_ = 0;
139 };
140
146 template <typename T>
147 [[nodiscard]] auto
148 begin() const
149 {
150 return ArrayIterator<T>(*this, 0);
151 }
152
158 template <typename T>
159 [[nodiscard]] auto
160 end() const
161 {
162 return ArrayIterator<T>(*this, this->size());
163 }
164
171 ArrayView(std::string_view prefix, ClioConfigDefinition const& configDef);
172
179 [[nodiscard]] ObjectView
180 objectAt(std::size_t idx) const;
181
188 [[nodiscard]] ValueView
189 valueAt(std::size_t idx) const;
190
196 [[nodiscard]] size_t
197 size() const;
198
199private:
200 std::string prefix_;
201 std::reference_wrapper<ClioConfigDefinition const> clioConfig_;
202};
203
204} // namespace util::config
ObjectView objectAt(std::size_t idx) const
Returns an ObjectView at the specified index.
Definition ArrayView.cpp:58
auto end() const
Returns an iterator to the end of the Array.
Definition ArrayView.hpp:160
auto begin() const
Returns an iterator to the beginning of the Array.
Definition ArrayView.hpp:148
size_t size() const
Returns the number of elements in the array.
Definition ArrayView.cpp:52
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:50
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:131
bool operator==(ArrayIterator const &other) const
Equality operator.
Definition ArrayView.hpp:119
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