Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ObjectView.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/newconfig/ValueView.hpp"
23
24#include <cstddef>
25#include <functional>
26#include <optional>
27#include <string>
28#include <string_view>
29
30namespace util::config {
31
32class ClioConfigDefinition;
33class ArrayView;
34
41public:
48 ObjectView(std::string_view prefix, ClioConfigDefinition const& clioConfig);
49
57 ObjectView(std::string_view prefix, std::size_t arrayIndex, ClioConfigDefinition const& clioConfig);
58
65 [[nodiscard]] bool
66 containsKey(std::string_view key) const;
67
74 [[nodiscard]] ValueView
75 getValueView(std::string_view key) const;
76
84 template <typename T>
85 T
86 get(std::string_view key) const
87 {
88 return getValueView(key).getValueImpl<T>();
89 }
90
98 template <typename T>
99 std::optional<T>
100 maybeValue(std::string_view key) const
101 {
102 return getValueView(key).asOptional<T>();
103 }
104
112 [[nodiscard]] ObjectView
113 getObject(std::string_view key) const;
114
122 [[nodiscard]] ArrayView
123 getArray(std::string_view key) const;
124
125private:
132 [[nodiscard]] std::string
133 getFullKey(std::string_view key) const;
134
141 [[nodiscard]] bool
142 startsWithKey(std::string_view key) const;
143
144 std::string prefix_;
145 std::optional<size_t> arrayIndex_;
146 std::reference_wrapper<ClioConfigDefinition const> clioConfig_;
147};
148
149} // namespace util::config
View for array structure for config.
Definition ArrayView.hpp:42
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
ObjectView getObject(std::string_view key) const
Retrieves an ObjectView in ClioConfigDefinition with key that starts with prefix_....
Definition ObjectView.cpp:64
ValueView getValueView(std::string_view key) const
Retrieves the value associated with the specified prefix._key in ClioConfigDefinition.
Definition ObjectView.cpp:54
ArrayView getArray(std::string_view key) const
Retrieves an ArrayView in ClioConfigDefinition with key that starts with prefix_.key....
Definition ObjectView.cpp:78
bool containsKey(std::string_view key) const
Checks if prefix_.key (fullkey) exists in ClioConfigDefinition.
Definition ObjectView.cpp:48
std::optional< T > maybeValue(std::string_view key) const
Returns the specified value of given string of type T if type and value exists.
Definition ObjectView.hpp:100
T get(std::string_view key) const
Returns the specified value of given string if value exists.
Definition ObjectView.hpp:86
ObjectView(std::string_view prefix, ClioConfigDefinition const &clioConfig)
Constructs an ObjectView for the specified prefix. The view must be of type object.
Definition ObjectView.cpp:37
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:46
T getValueImpl() const
Retrieves the stored value as the specified type T.
Definition ValueView.hpp:165
std::optional< T > asOptional() const
Returns an optional value of the specified type T if valid.
Definition ValueView.hpp:193