WebCFace 2.9.0
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 }
212 [[deprecated]]
213 Text child(int index) const {
214 return this->Field::child(std::to_string(index));
215 }
220 Text operator[](std::string_view field) const { return child(field); }
225 Text operator[](std::wstring_view field) const { return child(field); }
230 Text operator[](const char *field) const { return child(field); }
234 Text operator[](const wchar_t *field) const { return child(field); }
240 [[deprecated]]
241 Text operator[](int index) const {
242 return child(std::to_string(index));
243 }
248 Text parent() const { return this->Field::parent(); }
249
256 template <typename F,
257 typename std::enable_if_t<std::is_invocable_v<F, Text>,
258 std::nullptr_t> = nullptr>
259 const Text &onChange(F callback) const {
260 this->Variant::onChange(
261 [callback = std::move(callback)](const Variant &base) {
262 callback(Text(base));
263 });
264 return *this;
265 }
272 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
273 std::nullptr_t> = nullptr>
274 const Text &onChange(F callback) const {
275 this->Variant::onChange(
276 [callback = std::move(callback)](const auto &) { callback(); });
277 return *this;
278 }
286 template <typename T>
287 [[deprecated]] void appendListener(T &&callback) const {
288 onChange(std::forward<T>(callback));
289 }
290
297 const Text &set(std::string_view v) const {
298 this->Variant::set(ValAdaptor{v});
299 return *this;
300 }
305 const Text &set(std::wstring_view v) const {
306 this->Variant::set(ValAdaptor{v});
307 return *this;
308 }
309
314 const Text &operator=(std::string_view v) const {
315 this->set(v);
316 return *this;
317 }
322 const Text &operator=(std::wstring_view v) const {
323 this->set(v);
324 return *this;
325 }
326
332 const Text &request() const {
333 this->Variant::request();
334 return *this;
335 }
343 std::optional<std::string> tryGet() const;
348 std::optional<std::wstring> tryGetW() const;
358 const std::string &get() const;
366 const std::wstring &getW() const;
367
368 operator const std::string &() const { return get(); }
369 operator const std::wstring &() const { return getW(); }
370
379 bool exists() const;
380
385 [[deprecated]] std::chrono::system_clock::time_point time() const;
386
391 const Text &free() const;
392
393 bool operator==(std::string_view rhs) const { return this->get() == rhs; }
394 bool operator!=(std::string_view rhs) const { return this->get() != rhs; }
395 bool operator==(std::wstring_view rhs) const { return this->getW() == rhs; }
396 bool operator!=(std::wstring_view rhs) const { return this->getW() != rhs; }
397
406 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
407 std::nullptr_t> = nullptr>
408 bool operator==(const T &other) const {
409 return static_cast<Field>(*this) == static_cast<Field>(other);
410 }
411 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
412 std::nullptr_t> = nullptr>
413 bool operator!=(const T &other) const {
414 return static_cast<Field>(*this) != static_cast<Field>(other);
415 }
416 bool operator<(const Text &) const = delete;
417 bool operator<=(const Text &) const = delete;
418 bool operator>(const Text &) const = delete;
419 bool operator>=(const Text &) const = delete;
420};
421
426WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
427 const Text &data);
428
429namespace internal {
430struct InputRefState;
431}
432
448 std::shared_ptr<internal::InputRefState> state;
449
450 void lockTo(const Variant &target);
451 Variant &lockedField() const;
452
453 public:
455
456 InputRef();
457 InputRef(const InputRef &) = default;
458 InputRef &operator=(const InputRef &) = default;
463 InputRef(InputRef &&) = delete;
465 ~InputRef() = default;
466
475 const ValAdaptor &get() const;
476
481 template <typename T,
482 typename std::enable_if_t<std::is_convertible_v<ValAdaptor, T>,
483 std::nullptr_t> = nullptr>
484 operator T() const {
485 return static_cast<T>(get());
486 }
487
492 bool empty() const { return get().empty(); }
493
503 const std::string &asStringRef() const { return get().asStringRef(); }
509 const std::wstring &asWStringRef() const { return get().asWStringRef(); }
514 std::string asString() const { return get().asString(); }
519 std::wstring asWString() const { return get().asWString(); }
524 double asDouble() const { return get().asDouble(); }
529 int asInt() const { return get().asInt(); }
534 long long asLLong() const { return get().asLLong(); }
545 template <typename T>
546 [[deprecated("use asDouble(), asInt() or asLLong() instead")]]
547 double as() const {
548 return get().as<T>();
549 }
550
555 bool asBool() const { return get().asBool(); }
556
557 template <typename T,
558 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
559 std::nullptr_t> = nullptr>
560 bool operator==(const T &other) const {
561 return get() == other;
562 }
563 template <typename T,
564 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
565 std::nullptr_t> = nullptr>
566 bool operator!=(const T &other) const {
567 return get() != other;
568 }
569};
570
571template <typename T,
572 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
573 std::nullptr_t> = nullptr>
574bool operator==(const T &other, const InputRef &ref) {
575 return ref.get() == other;
576}
577template <typename T,
578 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
579 std::nullptr_t> = nullptr>
580bool operator!=(const T &other, const InputRef &ref) {
581 return ref.get() != other;
582}
583inline std::ostream &operator<<(std::ostream &os, const InputRef &ref) {
584 return os << ref.get();
585}
名前を指定しないText
Definition text.h:447
InputRef & operator=(const InputRef &)=default
InputRef & operator=(InputRef &&)=delete
InputRef(InputRef &&)=delete
bool operator==(const T &other) const
Definition text.h:560
std::string asString() const
文字列として返す(コピー)
Definition text.h:514
const std::wstring & asWStringRef() const
文字列として返す (wstring)
Definition text.h:509
bool empty() const
値が空かどうか調べる
Definition text.h:492
const std::string & asStringRef() const
文字列として返す
Definition text.h:503
double as() const
数値として返す
Definition text.h:547
long long asLLong() const
long long型の整数として返す
Definition text.h:534
bool operator!=(const T &other) const
Definition text.h:566
bool asBool() const
bool値を返す
Definition text.h:555
int asInt() const
int型の整数として返す
Definition text.h:529
const ValAdaptor & get() const
値を返す
Definition text.cc:21
InputRef(const InputRef &)=default
double asDouble() const
実数として返す
Definition text.h:524
std::wstring asWString() const
文字列として返す(コピー) (wstring)
Definition text.h:519
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:67
Viewを構築するときに使う一時的なViewComponent.
Definition component_view.h:286
文字列の送受信データを表すクラス
Definition text.h:183
const Text & operator=(std::wstring_view v) const
文字列をセットする (wstring)
Definition text.h:322
bool operator==(const T &other) const
Textの参照先を比較
Definition text.h:408
Text operator[](const char *field) const
Definition text.h:230
bool operator!=(std::string_view rhs) const
Definition text.h:394
const Text & set(std::wstring_view v) const
文字列をセットする (wstring)
Definition text.h:305
bool operator>(const Text &) const =delete
const Text & request() const
文字列をリクエストする
Definition text.h:332
Text child(std::string_view field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition text.h:198
Text operator[](std::wstring_view field) const
Definition text.h:225
bool operator>=(const Text &) const =delete
const Text & operator=(std::string_view v) const
文字列をセットする
Definition text.h:314
Text child(std::wstring_view field) const
「(thisの名前).(追加の名前)」を新しい名前とするField (wstring)
Definition text.h:205
Text child(int index) const
Definition text.h:213
Text parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition text.h:248
bool operator==(std::wstring_view rhs) const
Definition text.h:395
const Text & set(std::string_view v) const
文字列をセットする
Definition text.h:297
bool operator!=(const T &other) const
Definition text.h:413
bool operator==(std::string_view rhs) const
Definition text.h:393
Text(const Field &base, const SharedString &field)
Definition text.h:187
Text operator[](int index) const
Definition text.h:241
void appendListener(T &&callback) const
Definition text.h:287
bool operator<=(const Text &) const =delete
const Text & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition text.h:259
Text(const Field &base)
Definition text.h:186
bool operator<(const Text &) const =delete
Text operator[](const wchar_t *field) const
Definition text.h:234
Text()=default
Text operator[](std::string_view field) const
Definition text.h:220
bool operator!=(std::wstring_view rhs) const
Definition text.h:396
数値、文字列などの値を相互変換するクラス
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:61
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:100
bool operator==(const T &other, const InputRef &ref)
Definition text.h:574
const auto & get(const AxisAngle &aa)
Definition transform.h:231
bool operator!=(const T &other, const InputRef &ref)
Definition text.h:580
ClientDataの参照とメンバ名とデータ名を持つクラス
Definition field.h:71
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:69
#define WEBCFACE_CALL
Definition webcface-config.h:106
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117
#define WEBCFACE_CALL_FP
Definition webcface-config.h:107