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/config/ConfigConstraints.hpp"
24#include "util/config/ConfigValue.hpp"
25#include "util/config/Types.hpp"
26
27#include <fmt/format.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
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(
179 type() == ConfigType::Double || type() == ConfigType::Integer,
180 "Value type is not a floating point"
181 );
182 return asDouble();
183 }
184
185 std::unreachable();
186 }
187
194 template <typename T>
195 std::optional<T>
197 {
198 ASSERT(isOptional(), "A Config Value is not an optional value");
199 if (!hasValue())
200 return std::nullopt;
201
202 return std::make_optional(getValueImpl<T>());
203 }
204
212 friend std::ostream&
213 operator<<(std::ostream& stream, ValueView value)
214 {
215 stream << value.configVal_;
216 return stream;
217 }
218
219private:
220 std::reference_wrapper<ConfigValue const> configVal_;
221};
222
223} // namespace util::config
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
Represents the config values for Json/Yaml config.
Definition ConfigValue.hpp:47
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:213
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:196