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/config/ValueView.hpp"
23
24#include <cstddef>
25#include <functional>
26#include <optional>
27#include <string>
28#include <string_view>
29
30namespace util::config {
31
33class ArrayView;
34
41public:
48 ObjectView(std::string_view prefix, ClioConfigDefinition const& clioConfig);
49
58 std::string_view prefix,
59 std::size_t arrayIndex,
60 ClioConfigDefinition const& clioConfig
61 );
62
69 [[nodiscard]] bool
70 containsKey(std::string_view key) const;
71
78 [[nodiscard]] ValueView
79 getValueView(std::string_view key) const;
80
88 template <typename T>
89 T
90 get(std::string_view key) const
91 {
92 return getValueView(key).getValueImpl<T>();
93 }
94
102 template <typename T>
103 std::optional<T>
104 maybeValue(std::string_view key) const
105 {
106 return getValueView(key).asOptional<T>();
107 }
108
116 [[nodiscard]] ObjectView
117 getObject(std::string_view key) const;
118
126 [[nodiscard]] ArrayView
127 getArray(std::string_view key) const;
128
129private:
136 [[nodiscard]] std::string
137 getFullKey(std::string_view key) const;
138
145 [[nodiscard]] bool
146 startsWithKey(std::string_view key) const;
147
148 std::string prefix_;
149 std::optional<size_t> arrayIndex_;
150 std::reference_wrapper<ClioConfigDefinition const> clioConfig_;
151};
152
153} // 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:50
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:68
ValueView getValueView(std::string_view key) const
Retrieves the value associated with the specified prefix._key in ClioConfigDefinition.
Definition ObjectView.cpp:58
ArrayView getArray(std::string_view key) const
Retrieves an ArrayView in ClioConfigDefinition with key that starts with prefix_.key....
Definition ObjectView.cpp:82
bool containsKey(std::string_view key) const
Checks if prefix_.key (fullkey) exists in ClioConfigDefinition.
Definition ObjectView.cpp:52
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:104
T get(std::string_view key) const
Returns the specified value of given string if value exists.
Definition ObjectView.hpp:90
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:196