cppfig 0.1.0
Modern C++20 compile-time type-safe configuration library
Loading...
Searching...
No Matches
traits.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <optional>
5#include <string>
6#include <string_view>
7#include <type_traits>
8
9#include "cppfig/value.h"
10
11namespace cppfig {
12
20template <typename T>
25 static auto Serialize(const T& value) -> Value = delete;
26
30 static auto Deserialize(const Value& value) -> std::optional<T> = delete;
31
35 static auto ToString(const T& value) -> std::string = delete;
36
40 static auto FromString(std::string_view str) -> std::optional<T> = delete;
41};
42
47template <typename T>
48concept Configurable = requires(const T& value, const Value& val, std::string_view str) {
49 { ConfigTraits<T>::Serialize(value) } -> std::convertible_to<Value>;
50 { ConfigTraits<T>::Deserialize(val) } -> std::same_as<std::optional<T>>;
51 { ConfigTraits<T>::ToString(value) } -> std::convertible_to<std::string>;
52 { ConfigTraits<T>::FromString(str) } -> std::same_as<std::optional<T>>;
53};
54
55template <>
56struct ConfigTraits<bool> {
57 static auto Serialize(bool value) -> Value { return value; }
58
59 static auto Deserialize(const Value& value) -> std::optional<bool>
60 {
61 if (!value.IsBoolean()) {
62 return std::nullopt;
63 }
64 return value.Get<bool>();
65 }
66
67 static auto ToString(bool value) -> std::string { return value ? "true" : "false"; }
68
69 static auto FromString(std::string_view str) -> std::optional<bool>
70 {
71 if (str == "true" || str == "1" || str == "yes" || str == "on") {
72 return true;
73 }
74 if (str == "false" || str == "0" || str == "no" || str == "off") {
75 return false;
76 }
77 return std::nullopt;
78 }
79};
80
81template <>
82struct ConfigTraits<int> {
83 static auto Serialize(int value) -> Value { return value; }
84
85 static auto Deserialize(const Value& value) -> std::optional<int>
86 {
87 if (!value.IsInteger()) {
88 return std::nullopt;
89 }
90 return value.Get<int>();
91 }
92
93 static auto ToString(int value) -> std::string { return std::to_string(value); }
94
95 static auto FromString(std::string_view str) -> std::optional<int>
96 {
97 try {
98 std::size_t pos = 0;
99 int result = std::stoi(std::string(str), &pos);
100 if (pos != str.size()) {
101 return std::nullopt;
102 }
103 return result;
104 }
105 catch (...) {
106 return std::nullopt;
107 }
108 }
109};
110
111template <>
112struct ConfigTraits<std::int64_t> {
113 static auto Serialize(std::int64_t value) -> Value { return value; }
114
115 static auto Deserialize(const Value& value) -> std::optional<std::int64_t>
116 {
117 if (!value.IsInteger()) {
118 return std::nullopt;
119 }
120 return value.Get<std::int64_t>();
121 }
122
123 static auto ToString(std::int64_t value) -> std::string { return std::to_string(value); }
124
125 static auto FromString(std::string_view str) -> std::optional<std::int64_t>
126 {
127 try {
128 std::size_t pos = 0;
129 std::int64_t result = std::stoll(std::string(str), &pos);
130 if (pos != str.size()) {
131 return std::nullopt;
132 }
133 return result;
134 }
135 catch (...) {
136 return std::nullopt;
137 }
138 }
139};
140
141template <>
142struct ConfigTraits<double> {
143 static auto Serialize(double value) -> Value { return value; }
144
145 static auto Deserialize(const Value& value) -> std::optional<double>
146 {
147 if (!value.IsNumber()) {
148 return std::nullopt;
149 }
150 return value.Get<double>();
151 }
152
153 static auto ToString(double value) -> std::string { return std::to_string(value); }
154
155 static auto FromString(std::string_view str) -> std::optional<double>
156 {
157 try {
158 std::size_t pos = 0;
159 double result = std::stod(std::string(str), &pos);
160 if (pos != str.size()) {
161 return std::nullopt;
162 }
163 return result;
164 }
165 catch (...) {
166 return std::nullopt;
167 }
168 }
169};
170
171template <>
172struct ConfigTraits<float> {
173 static auto Serialize(float value) -> Value { return value; }
174
175 static auto Deserialize(const Value& value) -> std::optional<float>
176 {
177 if (!value.IsNumber()) {
178 return std::nullopt;
179 }
180 return value.Get<float>();
181 }
182
183 static auto ToString(float value) -> std::string { return std::to_string(value); }
184
185 static auto FromString(std::string_view str) -> std::optional<float>
186 {
187 try {
188 std::size_t pos = 0;
189 float result = std::stof(std::string(str), &pos);
190 if (pos != str.size()) {
191 return std::nullopt;
192 }
193 return result;
194 }
195 catch (...) {
196 return std::nullopt;
197 }
198 }
199};
200
201template <>
202struct ConfigTraits<std::string> {
203 static auto Serialize(const std::string& value) -> Value { return value; }
204
205 static auto Deserialize(const Value& value) -> std::optional<std::string>
206 {
207 if (!value.IsString()) {
208 return std::nullopt;
209 }
210 return value.Get<std::string>();
211 }
212
213 static auto ToString(const std::string& value) -> std::string { return value; }
214
215 static auto FromString(std::string_view str) -> std::optional<std::string> { return std::string(str); }
216};
217
218} // namespace cppfig
A self-contained, recursive value type for configuration data.
Definition value.h:26
Concept constraining types that can be used as configuration values.
Definition traits.h:48
C++20 compile-time type-safe configuration library.
Definition conf.h:13
static auto Serialize(bool value) -> Value
Definition traits.h:57
static auto Deserialize(const Value &value) -> std::optional< bool >
Definition traits.h:59
static auto FromString(std::string_view str) -> std::optional< bool >
Definition traits.h:69
static auto ToString(bool value) -> std::string
Definition traits.h:67
static auto Deserialize(const Value &value) -> std::optional< double >
Definition traits.h:145
static auto ToString(double value) -> std::string
Definition traits.h:153
static auto FromString(std::string_view str) -> std::optional< double >
Definition traits.h:155
static auto Serialize(double value) -> Value
Definition traits.h:143
static auto ToString(float value) -> std::string
Definition traits.h:183
static auto FromString(std::string_view str) -> std::optional< float >
Definition traits.h:185
static auto Deserialize(const Value &value) -> std::optional< float >
Definition traits.h:175
static auto Serialize(float value) -> Value
Definition traits.h:173
static auto Serialize(int value) -> Value
Definition traits.h:83
static auto FromString(std::string_view str) -> std::optional< int >
Definition traits.h:95
static auto Deserialize(const Value &value) -> std::optional< int >
Definition traits.h:85
static auto ToString(int value) -> std::string
Definition traits.h:93
static auto Deserialize(const Value &value) -> std::optional< std::int64_t >
Definition traits.h:115
static auto ToString(std::int64_t value) -> std::string
Definition traits.h:123
static auto FromString(std::string_view str) -> std::optional< std::int64_t >
Definition traits.h:125
static auto Serialize(std::int64_t value) -> Value
Definition traits.h:113
static auto FromString(std::string_view str) -> std::optional< std::string >
Definition traits.h:215
static auto Deserialize(const Value &value) -> std::optional< std::string >
Definition traits.h:205
static auto ToString(const std::string &value) -> std::string
Definition traits.h:213
static auto Serialize(const std::string &value) -> Value
Definition traits.h:203
Primary template for configuration type traits.
Definition traits.h:21
static auto Deserialize(const Value &value) -> std::optional< T >=delete
Deserializes a value from a Value node.
static auto ToString(const T &value) -> std::string=delete
Converts a value to a human-readable string.
static auto FromString(std::string_view str) -> std::optional< T >=delete
Parses a value from a string (e.g., from environment variables).
static auto Serialize(const T &value) -> Value=delete
Serializes a value to a Value node.