Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ValueView.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/ConfigConstraints.hpp"
24#include "util/newconfig/ConfigValue.hpp"
25#include "util/newconfig/Types.hpp"
26
27#include <fmt/core.h>
28
29#include <cassert>
30#include <cstddef>
31#include <cstdint>
32#include <functional>
33#include <optional>
34#include <ostream>
35#include <string>
36#include <type_traits>
37#include <utility>
38
39namespace util::config {
40
41class ClioConfigDefinition;
42
46class ValueView {
47public:
53 ValueView(ConfigValue const& configVal);
54
61 [[nodiscard]] std::string
62 asString() const;
63
70 [[nodiscard]] bool
71 asBool() const;
72
80 template <typename T>
81 [[nodiscard]] T
82 asIntType() const
83 {
84 if ((type() == ConfigType::Integer) && configVal_.get().hasValue()) {
85 auto const val = std::get<int64_t>(configVal_.get().getValue());
86 if (std::is_unsigned_v<T> && val < 0)
87 ASSERT(false, "Int {} cannot be converted to the specified unsigned type", val);
88
89 if (std::is_convertible_v<decltype(val), T>) {
90 return static_cast<T>(val);
91 }
92 }
93 ASSERT(false, "Value view is not of Int type");
94 return 0;
95 }
96
103 [[nodiscard]] double
104 asDouble() const;
105
112 [[nodiscard]] float
113 asFloat() const;
114
120 [[nodiscard]] constexpr ConfigType
121 type() const
122 {
123 return configVal_.get().type();
124 }
125
131 [[nodiscard]] bool constexpr hasValue() const
132 {
133 return configVal_.get().hasValue();
134 }
135
141 [[nodiscard]] bool constexpr isOptional() const
142 {
143 return configVal_.get().isOptional();
144 }
145
151 [[nodiscard]] constexpr std::optional<std::reference_wrapper<Constraint const>>
153 {
154 return configVal_.get().getConstraint();
155 }
156
163 template <typename T>
164 T
166 {
167 ASSERT(configVal_.get().hasValue(), "ConfigValue does not have a value");
168 if constexpr (std::is_same_v<T, bool>) {
169 ASSERT(type() == ConfigType::Boolean, "Value type is not a bool");
170 return asBool();
171 } else if constexpr (std::is_integral_v<T>) {
172 ASSERT(type() == ConfigType::Integer, "Value type is not an int");
173 return asIntType<T>();
174 } else if constexpr (std::is_same_v<T, std::string>) {
175 ASSERT(type() == ConfigType::String, "Value type is not a string");
176 return asString();
177 } else if constexpr (std::is_floating_point_v<T>) {
178 ASSERT(type() == ConfigType::Double || type() == ConfigType::Integer, "Value type is not a floating point");
179 return asDouble();
180 }
181
182 std::unreachable();
183 }
184
191 template <typename T>
192 std::optional<T>
194 {
195 ASSERT(isOptional(), "A Config Value is not an optional value");
196 if (!hasValue())
197 return std::nullopt;
198
199 return std::make_optional(getValueImpl<T>());
200 }
201
209 friend std::ostream&
210 operator<<(std::ostream& stream, ValueView value)
211 {
212 stream << value.configVal_;
213 return stream;
214 }
215
216private:
217 std::reference_wrapper<ConfigValue const> configVal_;
218};
219
220} // namespace util::config
Represents the config values for Json/Yaml config.
Definition ConfigValue.hpp:48
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:46
double asDouble() const
Retrieves the value as a double.
Definition ValueView.cpp:56
bool constexpr isOptional() const
Check if config value is optional.
Definition ValueView.hpp:141
friend std::ostream & operator<<(std::ostream &stream, ValueView value)
Custom output stream for ValueView.
Definition ValueView.hpp:210
T getValueImpl() const
Retrieves the stored value as the specified type T.
Definition ValueView.hpp:165
ValueView(ConfigValue const &configVal)
Constructs a ValueView object.
Definition ValueView.cpp:33
constexpr std::optional< std::reference_wrapper< Constraint const > > getConstraint() const
Retrieves the constraint associated with the ConfigValue in this ValueView, if any.
Definition ValueView.hpp:152
bool constexpr hasValue() const
Check if Config Value exists.
Definition ValueView.hpp:131
bool asBool() const
Retrieves the value as a boolean.
Definition ValueView.cpp:47
float asFloat() const
Retrieves the value as a float.
Definition ValueView.cpp:75
T asIntType() const
Retrieves any type of "int" value (uint_32, int64_t etc)
Definition ValueView.hpp:82
constexpr ConfigType type() const
Gets the config type.
Definition ValueView.hpp:121
std::string asString() const
Retrieves the value as a string.
Definition ValueView.cpp:38
std::optional< T > asOptional() const
Returns an optional value of the specified type T if valid.
Definition ValueView.hpp:193