WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
text.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <ostream>
4#include <optional>
5#include <chrono>
6#include <memory>
7#include "field.h"
8#ifdef WEBCFACE_MESON
9#include "webcface-config.h"
10#else
11#include "webcface/common/webcface-config.h"
12#endif
14
16
25class WEBCFACE_DLL Variant : protected Field {
26 public:
27 Variant() = default;
28 Variant(const Field &base);
29 Variant(const Field &base, const SharedString &field)
30 : Variant(Field{base, field}) {}
31
32 friend class InputRef;
33 friend struct InputRefState;
35
41 const Variant &
42 onChange(std::function<void WEBCFACE_CALL_FP(Variant)> callback) const;
48 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
49 std::nullptr_t> = nullptr>
50 const Variant &onChange(F callback) const {
51 return onChange(
52 [callback = std::move(callback)](const auto &) { callback(); });
53 }
61 template <typename T>
62 [[deprecated]] void appendListener(T &&callback) const {
63 onChange(std::forward<T>(callback));
64 }
65
66 protected:
71 const Variant &set(const ValAdaptor &v) const;
72
73 public:
78 const Variant &request() const;
83 std::optional<ValAdaptor> tryGet() const;
90 const ValAdaptor &get() const;
91
92 template <typename T,
93 typename std::enable_if_t<std::is_convertible_v<ValAdaptor, T>,
94 std::nullptr_t> = nullptr>
95 operator T() const {
96 return static_cast<T>(get());
97 }
102 bool empty() const { return get().empty(); }
110 const std::string &asStringRef() const { return get().asStringRef(); }
115 const std::wstring &asWStringRef() const { return get().asWStringRef(); }
120 std::string asString() const { return get().asString(); }
125 std::wstring asWString() const { return get().asWString(); }
130 double asDouble() const { return get().asDouble(); }
135 int asInt() const { return get().asInt(); }
140 long long asLLong() const { return get().asLLong(); }
145 bool asBool() const { return get().asBool(); }
146
147 template <typename T,
148 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
149 std::nullptr_t> = nullptr>
150 bool operator==(const T &other) const {
151 return get() == other;
152 }
153 template <typename T,
154 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
155 std::nullptr_t> = nullptr>
156 bool operator!=(const T &other) const {
157 return get() != other;
158 }
159
160 template <typename T, typename std::enable_if_t<std::is_same_v<T, Variant>,
161 std::nullptr_t> = nullptr>
162 bool operator==(const T &other) const {
163 return static_cast<Field>(*this) == static_cast<Field>(other);
164 }
165 template <typename T, typename std::enable_if_t<std::is_same_v<T, Variant>,
166 std::nullptr_t> = nullptr>
167 bool operator!=(const T &other) const {
168 return static_cast<Field>(*this) != static_cast<Field>(other);
169 }
170 bool operator<(const Variant &) const = delete;
171 bool operator<=(const Variant &) const = delete;
172 bool operator>(const Variant &) const = delete;
173 bool operator>=(const Variant &) const = delete;
174};
175
176
183class WEBCFACE_DLL Text : protected Variant {
184 public:
185 Text() = default;
186 Text(const Field &base) : Variant(base) {}
187 Text(const Field &base, const SharedString &field)
188 : Text(Field{base, field}) {}
189
190 using Field::lastName;
191 using Field::member;
192 using Field::name;
193 using Field::nameW;
198 Text child(std::string_view field) const {
199 return this->Field::child(field);
200 }
205 Text child(std::wstring_view field) const {
206 return this->Field::child(field);
207 }
211 Text child(int index) const { return this->Field::child(index); }
216 Text operator[](std::string_view field) const { return child(field); }
221 Text operator[](std::wstring_view field) const { return child(field); }
226 Text operator[](const char *field) const { return child(field); }
230 Text operator[](const wchar_t *field) const { return child(field); }
235 Text operator[](int index) const { return child(index); }
240 Text parent() const { return this->Field::parent(); }
241
248 template <typename F,
249 typename std::enable_if_t<std::is_invocable_v<F, Text>,
250 std::nullptr_t> = nullptr>
251 const Text &onChange(F callback) const {
252 this->Variant::onChange(
253 [callback = std::move(callback)](const Variant &base) {
254 callback(Text(base));
255 });
256 return *this;
257 }
264 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
265 std::nullptr_t> = nullptr>
266 const Text &onChange(F callback) const {
267 this->Variant::onChange(
268 [callback = std::move(callback)](const auto &) { callback(); });
269 return *this;
270 }
278 template <typename T>
279 [[deprecated]] void appendListener(T &&callback) const {
280 onChange(std::forward<T>(callback));
281 }
282
289 const Text &set(std::string_view v) const {
290 this->Variant::set(ValAdaptor{v});
291 return *this;
292 }
297 const Text &set(std::wstring_view v) const {
298 this->Variant::set(ValAdaptor{v});
299 return *this;
300 }
301
306 const Text &operator=(std::string_view v) const {
307 this->set(v);
308 return *this;
309 }
314 const Text &operator=(std::wstring_view v) const {
315 this->set(v);
316 return *this;
317 }
318
324 const Text &request() const {
325 this->Variant::request();
326 return *this;
327 }
335 std::optional<std::string> tryGet() const;
340 std::optional<std::wstring> tryGetW() const;
350 const std::string &get() const;
358 const std::wstring &getW() const;
359
360 operator const std::string &() const { return get(); }
361 operator const std::wstring &() const { return getW(); }
362
371 bool exists() const;
372
377 [[deprecated]] std::chrono::system_clock::time_point time() const;
378
383 const Text &free() const;
384
385 bool operator==(std::string_view rhs) const { return this->get() == rhs; }
386 bool operator!=(std::string_view rhs) const { return this->get() != rhs; }
387 bool operator==(std::wstring_view rhs) const { return this->getW() == rhs; }
388 bool operator!=(std::wstring_view rhs) const { return this->getW() != rhs; }
389
398 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
399 std::nullptr_t> = nullptr>
400 bool operator==(const T &other) const {
401 return static_cast<Field>(*this) == static_cast<Field>(other);
402 }
403 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
404 std::nullptr_t> = nullptr>
405 bool operator!=(const T &other) const {
406 return static_cast<Field>(*this) != static_cast<Field>(other);
407 }
408 bool operator<(const Text &) const = delete;
409 bool operator<=(const Text &) const = delete;
410 bool operator>(const Text &) const = delete;
411 bool operator>=(const Text &) const = delete;
412};
413
418WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
419 const Text &data);
420
421namespace internal {
422struct InputRefState;
423}
424
440 std::shared_ptr<internal::InputRefState> state;
441
442 void lockTo(const Variant &target);
443 Variant &lockedField() const;
444
445 public:
447
448 InputRef();
449 InputRef(const InputRef &) = default;
450 InputRef &operator=(const InputRef &) = default;
455 InputRef(InputRef &&) = delete;
457 ~InputRef() = default;
458
467 const ValAdaptor &get() const;
468
473 template <typename T,
474 typename std::enable_if_t<std::is_convertible_v<ValAdaptor, T>,
475 std::nullptr_t> = nullptr>
476 operator T() const {
477 return static_cast<T>(get());
478 }
479
484 bool empty() const { return get().empty(); }
485
495 const std::string &asStringRef() const { return get().asStringRef(); }
501 const std::wstring &asWStringRef() const { return get().asWStringRef(); }
506 std::string asString() const { return get().asString(); }
511 std::wstring asWString() const { return get().asWString(); }
516 double asDouble() const { return get().asDouble(); }
521 int asInt() const { return get().asInt(); }
526 long long asLLong() const { return get().asLLong(); }
537 template <typename T>
538 [[deprecated("use asDouble(), asInt() or asLLong() instead")]]
539 double as() const {
540 return get().as<T>();
541 }
542
547 bool asBool() const { return get().asBool(); }
548
549 template <typename T,
550 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
551 std::nullptr_t> = nullptr>
552 bool operator==(const T &other) const {
553 return get() == other;
554 }
555 template <typename T,
556 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
557 std::nullptr_t> = nullptr>
558 bool operator!=(const T &other) const {
559 return get() != other;
560 }
561};
562
563template <typename T,
564 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
565 std::nullptr_t> = nullptr>
566bool operator==(const T &other, const InputRef &ref) {
567 return ref.get() == other;
568}
569template <typename T,
570 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
571 std::nullptr_t> = nullptr>
572bool operator!=(const T &other, const InputRef &ref) {
573 return ref.get() != other;
574}
575inline std::ostream &operator<<(std::ostream &os, const InputRef &ref) {
576 return os << ref.get();
577}
名前を指定しないText
Definition text.h:439
InputRef & operator=(const InputRef &)=default
InputRef & operator=(InputRef &&)=delete
InputRef(InputRef &&)=delete
bool operator==(const T &other) const
Definition text.h:552
std::string asString() const
文字列として返す(コピー)
Definition text.h:506
const std::wstring & asWStringRef() const
文字列として返す (wstring)
Definition text.h:501
bool empty() const
値が空かどうか調べる
Definition text.h:484
const std::string & asStringRef() const
文字列として返す
Definition text.h:495
double as() const
数値として返す
Definition text.h:539
long long asLLong() const
long long型の整数として返す
Definition text.h:526
bool operator!=(const T &other) const
Definition text.h:558
bool asBool() const
bool値を返す
Definition text.h:547
int asInt() const
int型の整数として返す
Definition text.h:521
const ValAdaptor & get() const
値を返す
Definition text.cc:21
InputRef(const InputRef &)=default
double asDouble() const
実数として返す
Definition text.h:516
std::wstring asWString() const
文字列として返す(コピー) (wstring)
Definition text.h:511
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:69
Viewを構築するときに使う一時的なViewComponent.
Definition component_view.h:267
文字列の送受信データを表すクラス
Definition text.h:183
const Text & operator=(std::wstring_view v) const
文字列をセットする (wstring)
Definition text.h:314
bool operator==(const T &other) const
Textの参照先を比較
Definition text.h:400
Text operator[](const char *field) const
Definition text.h:226
bool operator!=(std::string_view rhs) const
Definition text.h:386
const Text & set(std::wstring_view v) const
文字列をセットする (wstring)
Definition text.h:297
bool operator>(const Text &) const =delete
const Text & request() const
文字列をリクエストする
Definition text.h:324
Text child(std::string_view field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition text.h:198
Text operator[](std::wstring_view field) const
Definition text.h:221
bool operator>=(const Text &) const =delete
const Text & operator=(std::string_view v) const
文字列をセットする
Definition text.h:306
Text child(std::wstring_view field) const
「(thisの名前).(追加の名前)」を新しい名前とするField (wstring)
Definition text.h:205
Text child(int index) const
Definition text.h:211
Text parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition text.h:240
bool operator==(std::wstring_view rhs) const
Definition text.h:387
const Text & set(std::string_view v) const
文字列をセットする
Definition text.h:289
bool operator!=(const T &other) const
Definition text.h:405
bool operator==(std::string_view rhs) const
Definition text.h:385
Text(const Field &base, const SharedString &field)
Definition text.h:187
Text operator[](int index) const
Definition text.h:235
void appendListener(T &&callback) const
Definition text.h:279
bool operator<=(const Text &) const =delete
const Text & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition text.h:251
Text(const Field &base)
Definition text.h:186
bool operator<(const Text &) const =delete
Text operator[](const wchar_t *field) const
Definition text.h:230
Text()=default
Text operator[](std::string_view field) const
Definition text.h:216
bool operator!=(std::wstring_view rhs) const
Definition text.h:388
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:87
文字列、数値などの型を送受信するクラス
Definition text.h:25
bool operator<=(const Variant &) const =delete
bool operator==(const T &other) const
Definition text.h:150
std::string asString() const
文字列として返す(コピー)
Definition text.h:120
const Variant & request() const
値をリクエストする
Definition text.cc:40
const std::wstring & asWStringRef() const
文字列として返す (wstring)
Definition text.h:115
bool empty() const
値が空かどうか調べる
Definition text.h:102
const std::string & asStringRef() const
文字列として返す
Definition text.h:110
long long asLLong() const
long long型の整数として返す
Definition text.h:140
const Variant & onChange(std::function< void(Variant)> callback) const
値が変化したときに呼び出されるコールバックを設定
Definition text.cc:63
bool operator!=(const T &other) const
Definition text.h:156
void appendListener(T &&callback) const
Definition text.h:62
bool asBool() const
bool値を返す
Definition text.h:145
int asInt() const
int型の整数として返す
Definition text.h:135
const Variant & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition text.h:50
const Variant & set(const ValAdaptor &v) const
値をセットする
Definition text.cc:50
double asDouble() const
実数として返す
Definition text.h:130
bool operator>(const Variant &) const =delete
Variant(const Field &base, const SharedString &field)
Definition text.h:29
bool operator>=(const Variant &) const =delete
std::wstring asWString() const
文字列として返す(コピー) (wstring)
Definition text.h:125
bool operator<(const Variant &) const =delete
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
bool operator!=(const T &other, const InputRef &ref)
Definition text.h:572
ClientDataの参照とメンバ名とデータ名を持つクラス
Definition field.h:70
Field parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition field.cc:33
Field child(const SharedString &field) const
Definition field.cc:42
#define WEBCFACE_DLL
Definition webcface-config.h:60
#define WEBCFACE_CALL
Definition webcface-config.h:97
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103
#define WEBCFACE_CALL_FP
Definition webcface-config.h:98