Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ValueView.hpp
1#pragma once
2
3#include "util/Assert.hpp"
4#include "util/config/ConfigConstraints.hpp"
5#include "util/config/ConfigValue.hpp"
6#include "util/config/Types.hpp"
7
8#include <fmt/format.h>
9
10#include <cassert>
11#include <cstddef>
12#include <cstdint>
13#include <functional>
14#include <optional>
15#include <ostream>
16#include <string>
17#include <type_traits>
18#include <utility>
19
20namespace util::config {
21
23
27class ValueView {
28public:
34 ValueView(ConfigValue const& configVal);
35
42 [[nodiscard]] std::string
43 asString() const;
44
51 [[nodiscard]] bool
52 asBool() const;
53
61 template <typename T>
62 [[nodiscard]] T
63 asIntType() const
64 {
65 if ((type() == ConfigType::Integer) && configVal_.get().hasValue()) {
66 auto const val = std::get<int64_t>(configVal_.get().getValue());
67 if (std::is_unsigned_v<T> && val < 0)
68 ASSERT(false, "Int {} cannot be converted to the specified unsigned type", val);
69
70 if (std::is_convertible_v<decltype(val), T>) {
71 return static_cast<T>(val);
72 }
73 }
74 ASSERT(false, "Value view is not of Int type");
75 return 0;
76 }
77
84 [[nodiscard]] double
85 asDouble() const;
86
93 [[nodiscard]] float
94 asFloat() const;
95
101 [[nodiscard]] constexpr ConfigType
102 type() const
103 {
104 return configVal_.get().type();
105 }
106
112 [[nodiscard]] bool constexpr hasValue() const
113 {
114 return configVal_.get().hasValue();
115 }
116
122 [[nodiscard]] bool constexpr isOptional() const
123 {
124 return configVal_.get().isOptional();
125 }
126
132 [[nodiscard]] constexpr std::optional<std::reference_wrapper<Constraint const>>
134 {
135 return configVal_.get().getConstraint();
136 }
137
144 template <typename T>
145 T
147 {
148 ASSERT(configVal_.get().hasValue(), "ConfigValue does not have a value");
149 if constexpr (std::is_same_v<T, bool>) {
150 ASSERT(type() == ConfigType::Boolean, "Value type is not a bool");
151 return asBool();
152 } else if constexpr (std::is_integral_v<T>) {
153 ASSERT(type() == ConfigType::Integer, "Value type is not an int");
154 return asIntType<T>();
155 } else if constexpr (std::is_same_v<T, std::string>) {
156 ASSERT(type() == ConfigType::String, "Value type is not a string");
157 return asString();
158 } else if constexpr (std::is_floating_point_v<T>) {
159 ASSERT(
160 type() == ConfigType::Double || type() == ConfigType::Integer,
161 "Value type is not a floating point"
162 );
163 return asDouble();
164 }
165
166 std::unreachable();
167 }
168
175 template <typename T>
176 std::optional<T>
178 {
179 ASSERT(isOptional(), "A Config Value is not an optional value");
180 if (!hasValue())
181 return std::nullopt;
182
183 return std::make_optional(getValueImpl<T>());
184 }
185
193 friend std::ostream&
194 operator<<(std::ostream& stream, ValueView value)
195 {
196 stream << value.configVal_;
197 return stream;
198 }
199
200private:
201 std::reference_wrapper<ConfigValue const> configVal_;
202};
203
204} // namespace util::config
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
Represents the config values for Json/Yaml config.
Definition ConfigValue.hpp:28
double asDouble() const
Retrieves the value as a double.
Definition ValueView.cpp:37
bool constexpr isOptional() const
Check if config value is optional.
Definition ValueView.hpp:122
friend std::ostream & operator<<(std::ostream &stream, ValueView value)
Custom output stream for ValueView.
Definition ValueView.hpp:194
T getValueImpl() const
Retrieves the stored value as the specified type T.
Definition ValueView.hpp:146
ValueView(ConfigValue const &configVal)
Constructs a ValueView object.
Definition ValueView.cpp:14
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:133
bool constexpr hasValue() const
Check if Config Value exists.
Definition ValueView.hpp:112
bool asBool() const
Retrieves the value as a boolean.
Definition ValueView.cpp:28
float asFloat() const
Retrieves the value as a float.
Definition ValueView.cpp:56
T asIntType() const
Retrieves any type of "int" value (uint_32, int64_t etc).
Definition ValueView.hpp:63
constexpr ConfigType type() const
Gets the config type.
Definition ValueView.hpp:102
std::string asString() const
Retrieves the value as a string.
Definition ValueView.cpp:19
std::optional< T > asOptional() const
Returns an optional value of the specified type T if valid.
Definition ValueView.hpp:177