WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
val_adaptor.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <vector>
4#include <tuple>
5#include <cstdint>
6#include <ostream>
7#include <variant>
8#include "encoding.h"
9#ifdef WEBCFACE_MESON
10#include "webcface-config.h"
11#else
12#include "webcface/common/webcface-config.h"
13#endif
14
16
21enum class ValType {
22 none_ = 0,
23 string_ = 1,
24 bool_ = 2,
25 int_ = 3,
26 float_ = 4,
27 double_ = 4,
28};
33template <typename T>
35 if constexpr (std::is_void_v<T>) {
36 return ValType::none_;
37 } else if constexpr (std::is_same_v<bool, T>) {
38 return ValType::bool_;
39 } else if constexpr (std::is_integral_v<T>) {
40 return ValType::int_;
41 } else if constexpr (std::is_floating_point_v<T>) {
42 return ValType::float_;
43 } else {
44 return ValType::string_;
45 }
46}
47
52inline std::string valTypeStr(ValType a) {
53 switch (a) {
54 case ValType::none_:
55 return "none";
56 case ValType::string_:
57 return "string";
58 case ValType::bool_:
59 return "bool";
60 case ValType::int_:
61 return "int";
62 case ValType::float_:
63 return "float";
64 default:
65 return "unknown";
66 }
67}
72inline std::ostream &operator<<(std::ostream &os, ValType a) {
73 return os << valTypeStr(a);
74}
75
92 mutable SharedString as_str;
93
94 std::variant<double, std::int64_t> as_val;
95 ValType type;
96
97 enum ValVariant { DOUBLEV = 0, INT64V = 1 };
98
99 public:
100 ValAdaptor();
101
105 explicit ValAdaptor(const SharedString &str);
109 ValAdaptor &operator=(const SharedString &str);
110
111 explicit ValAdaptor(std::string_view str);
112 ValAdaptor &operator=(std::string_view str);
113 explicit ValAdaptor(const char *str) : ValAdaptor(std::string_view(str)) {}
114 ValAdaptor &operator=(const char *str) {
115 return *this = std::string_view(str);
116 }
117
121 explicit ValAdaptor(std::wstring_view str);
125 ValAdaptor &operator=(std::wstring_view str);
129 explicit ValAdaptor(const wchar_t *str)
130 : ValAdaptor(std::wstring_view(str)) {}
134 ValAdaptor &operator=(const wchar_t *str) {
135 return *this = std::wstring_view(str);
136 }
137
138 explicit ValAdaptor(bool value);
139 ValAdaptor &operator=(bool v);
140
141 explicit ValAdaptor(std::int64_t value);
142 ValAdaptor &operator=(std::int64_t v);
143
144 explicit ValAdaptor(double value);
145 ValAdaptor &operator=(double v);
146
147 template <typename T, typename std::enable_if_t<std::is_integral_v<T>,
148 std::nullptr_t> = nullptr>
149 explicit ValAdaptor(T value)
150 : ValAdaptor(static_cast<std::int64_t>(value)) {}
151 template <typename T, typename std::enable_if_t<std::is_integral_v<T>,
152 std::nullptr_t> = nullptr>
154 return *this = static_cast<std::int64_t>(v);
155 }
156
157 template <typename T, typename std::enable_if_t<std::is_floating_point_v<T>,
158 std::nullptr_t> = nullptr>
159 explicit ValAdaptor(T value) : ValAdaptor(static_cast<double>(value)) {}
160 template <typename T, typename std::enable_if_t<std::is_floating_point_v<T>,
161 std::nullptr_t> = nullptr>
163 return *this = static_cast<double>(v);
164 }
165
166 ValType valType() const { return type; }
167
168 static const ValAdaptor &emptyVal();
169
174 bool empty() const;
175
188 const std::string &asStringRef() const;
194 const std::wstring &asWStringRef() const;
198 const std::string &asU8StringRef() const;
203 std::string asString() const { return asStringRef(); }
208 std::wstring asWString() const { return asWStringRef(); }
209
213 operator const std::string &() const { return asStringRef(); }
217 operator const std::wstring &() const { return asWStringRef(); }
221 operator const char *() const { return asStringRef().c_str(); }
225 operator const wchar_t *() const { return asWStringRef().c_str(); }
226
231 double asDouble() const;
236 int asInt() const;
241 long long asLLong() const;
252 template <typename T>
253 [[deprecated("use asDouble(), asInt() or asLLong() instead")]] double
254 as() const {
255 return static_cast<T>(asDouble());
256 }
260 template <typename T,
261 typename std::enable_if_t<std::is_convertible_v<double, T> &&
262 !std::is_same_v<T, bool>,
263 std::nullptr_t> = nullptr>
264 operator T() const {
265 if constexpr (std::is_floating_point_v<T>) {
266 return static_cast<T>(asDouble());
267 } else if constexpr (sizeof(T) > sizeof(int)) {
268 return static_cast<T>(asLLong());
269 } else {
270 return static_cast<T>(asInt());
271 }
272 }
273
282 bool asBool() const;
286 operator bool() const { return asBool(); }
287
288 bool operator==(const ValAdaptor &other) const;
289 bool operator!=(const ValAdaptor &other) const { return !(*this == other); }
290
291 template <typename T, typename std::enable_if_t<
292 std::is_constructible_v<ValAdaptor, T> &&
293 !std::is_same_v<ValAdaptor, T>,
294 std::nullptr_t> = nullptr>
295 bool operator==(const T &other) const {
296 return *this == ValAdaptor(other);
297 }
298 template <typename T, typename std::enable_if_t<
299 std::is_constructible_v<ValAdaptor, T> &&
300 !std::is_same_v<ValAdaptor, T>,
301 std::nullptr_t> = nullptr>
302 bool operator!=(const T &other) const {
303 return *this != ValAdaptor(other);
304 }
305};
306
307template <typename T,
308 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T> &&
309 !std::is_same_v<ValAdaptor, T>,
310 std::nullptr_t> = nullptr>
311bool operator==(const T &other, const ValAdaptor &val) {
312 return val == ValAdaptor(other);
313}
314template <typename T,
315 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T> &&
316 !std::is_same_v<ValAdaptor, T>,
317 std::nullptr_t> = nullptr>
318bool operator!=(const T &other, const ValAdaptor &val) {
319 return val != ValAdaptor(other);
320}
321
322inline std::ostream &operator<<(std::ostream &os, const ValAdaptor &a) {
323 return os << static_cast<std::string>(a);
324}
325
330template <int n = 0, typename T>
331void argToTuple(const std::vector<ValAdaptor> &args, T &tuple) {
332 constexpr int tuple_size = std::tuple_size<T>::value;
333 if constexpr (n < tuple_size) {
334 using Type = typename std::tuple_element<n, T>::type;
335 std::get<n>(tuple) = static_cast<Type>(args[n]);
336 argToTuple<n + 1>(args, tuple);
337 }
338}
339
340namespace [[deprecated("symbols in webcface::encoding namespace are "
341 "now directly in webcface namespace")]] encoding {
343template <typename T>
345 return webcface::valTypeOf<T>();
346}
347inline std::string valTypeStr(webcface::ValType a) {
348 return webcface::valTypeStr(a);
349}
351
352template <int n = 0, typename T>
353void argToTuple(const std::vector<webcface::ValAdaptor> &args, T &tuple) {
354 webcface::argToTuple<n>(args, tuple);
355}
356
357} // namespace encoding
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:69
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:87
bool operator==(const T &other) const
Definition val_adaptor.h:295
std::string asString() const
文字列として返す(コピー)
Definition val_adaptor.h:203
double as() const
数値として返す
Definition val_adaptor.h:254
bool operator!=(const ValAdaptor &other) const
Definition val_adaptor.h:289
ValAdaptor & operator=(const wchar_t *str)
Definition val_adaptor.h:134
bool operator!=(const T &other) const
Definition val_adaptor.h:302
ValAdaptor(T value)
Definition val_adaptor.h:149
ValType valType() const
Definition val_adaptor.h:166
ValAdaptor & operator=(T v)
Definition val_adaptor.h:153
std::wstring asWString() const
文字列として返す(コピー) (wstring)
Definition val_adaptor.h:208
ValAdaptor(const char *str)
Definition val_adaptor.h:113
ValAdaptor & operator=(const char *str)
Definition val_adaptor.h:114
ValAdaptor(const wchar_t *str)
Definition val_adaptor.h:129
std::ostream & operator<<(std::ostream &os, const Arg &arg)
Definition func_info.cc:96
bool operator==(const T &other, const InputRef &ref)
Definition text.h:566
void argToTuple(const std::vector< ValAdaptor > &args, T &tuple)
ValAdaptorのリストから任意の型のタプルに変換する
Definition val_adaptor.h:331
std::string valTypeStr(ValType a)
型名を文字列で取得
Definition val_adaptor.h:52
ValType
引数や戻り値の型を表すenum
Definition val_adaptor.h:21
ValType valTypeOf()
TのValTypeを得る
Definition val_adaptor.h:34
bool operator!=(const T &other, const InputRef &ref)
Definition text.h:572
#define WEBCFACE_DLL
Definition webcface-config.h:60
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103