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]]
63 void appendListener(T &&callback) const {
64 onChange(std::forward<T>(callback));
65 }
66
67 protected:
72 const Variant &set(const ValAdaptor &v) const;
73
74 public:
79 const Variant &request() const;
84 std::optional<ValAdaptor> tryGet() const;
92 ValAdaptor get() const;
93
94 template <typename T,
95 typename std::enable_if_t<std::is_convertible_v<ValAdaptor, T>,
96 std::nullptr_t> = nullptr>
97 operator T() const {
98 return static_cast<T>(get());
99 }
104 bool empty() const { return get().empty(); }
114 [[deprecated("(ver2.10〜) use asStringView() or asString() instead")]]
115 std::string asStringRef() const {
116 return asString();
117 }
125 [[deprecated("(ver2.10〜) use asWStringView() or asWString() instead")]]
126 std::wstring asWStringRef() const {
127 return asWString();
128 }
133 StringView asStringView() const { return get().asStringView(); }
138 WStringView asWStringView() const { return get().asWStringView(); }
143 std::string asString() const { return get().asString(); }
148 std::wstring asWString() const { return get().asWString(); }
153 double asDouble() const { return get().asDouble(); }
158 int asInt() const { return get().asInt(); }
163 long long asLLong() const { return get().asLLong(); }
168 bool asBool() const { return get().asBool(); }
169
170 template <typename T,
171 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
172 std::nullptr_t> = nullptr>
173 bool operator==(const T &other) const {
174 return get() == other;
175 }
176 template <typename T,
177 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
178 std::nullptr_t> = nullptr>
179 bool operator!=(const T &other) const {
180 return get() != other;
181 }
182
183 template <typename T, typename std::enable_if_t<std::is_same_v<T, Variant>,
184 std::nullptr_t> = nullptr>
185 bool operator==(const T &other) const {
186 return static_cast<Field>(*this) == static_cast<Field>(other);
187 }
188 template <typename T, typename std::enable_if_t<std::is_same_v<T, Variant>,
189 std::nullptr_t> = nullptr>
190 bool operator!=(const T &other) const {
191 return static_cast<Field>(*this) != static_cast<Field>(other);
192 }
193 bool operator<(const Variant &) const = delete;
194 bool operator<=(const Variant &) const = delete;
195 bool operator>(const Variant &) const = delete;
196 bool operator>=(const Variant &) const = delete;
197};
198
199
206class WEBCFACE_DLL Text : protected Variant {
207 public:
208 Text() = default;
209 Text(const Field &base) : Variant(base) {}
210 Text(const Field &base, const SharedString &field)
211 : Text(Field{base, field}) {}
212
213 using Field::lastName;
214 using Field::member;
215 using Field::name;
216 using Field::nameW;
224 return this->Field::child(static_cast<SharedString &>(field));
225 }
230 [[deprecated]]
231 Text child(int index) const {
232 return this->Field::child(std::to_string(index));
233 }
242 return child(std::move(field));
243 }
251 Text operator[](const char *field) const { return child(field); }
255 Text operator[](const wchar_t *field) const { return child(field); }
260 template <typename CharT, std::size_t N,
261 typename std::enable_if_t<std::is_same_v<CharT, char> ||
262 std::is_same_v<CharT, wchar_t>,
263 std::nullptr_t> = nullptr>
264 Text operator[](const CharT (&static_str)[N]) {
265 return child(StringInitializer(static_str));
266 }
272 [[deprecated]]
273 Text
274 operator[](int index) const {
275 return child(std::to_string(index));
276 }
281 Text parent() const { return this->Field::parent(); }
282
289 template <typename F,
290 typename std::enable_if_t<std::is_invocable_v<F, Text>,
291 std::nullptr_t> = nullptr>
292 const Text &onChange(F callback) const {
293 this->Variant::onChange(
294 [callback = std::move(callback)](const Variant &base) {
295 callback(Text(base));
296 });
297 return *this;
298 }
305 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
306 std::nullptr_t> = nullptr>
307 const Text &onChange(F callback) const {
308 this->Variant::onChange(
309 [callback = std::move(callback)](const auto &) { callback(); });
310 return *this;
311 }
319 template <typename T>
320 [[deprecated]]
321 void appendListener(T &&callback) const {
322 onChange(std::forward<T>(callback));
323 }
324
331 const Text &set(StringInitializer v) const {
332 this->Variant::set(ValAdaptor{std::move(v)});
333 return *this;
334 }
335
343 this->set(std::move(v));
344 return *this;
345 }
346
352 const Text &request() const {
353 this->Variant::request();
354 return *this;
355 }
365 std::optional<StringView> tryGet() const;
374 std::optional<WStringView> tryGetW() const;
385 StringView get() const;
394 WStringView getW() const;
395
401 operator std::string_view() const { return get(); }
407 operator std::wstring_view() const { return getW(); }
408
417 bool exists() const;
418
423 [[deprecated]]
424 std::chrono::system_clock::time_point time() const;
425
430 const Text &free() const;
431
432 bool operator==(std::string_view rhs) const { return this->get() == rhs; }
433 bool operator!=(std::string_view rhs) const { return this->get() != rhs; }
434 bool operator==(std::wstring_view rhs) const { return this->getW() == rhs; }
435 bool operator!=(std::wstring_view rhs) const { return this->getW() != rhs; }
436
445 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
446 std::nullptr_t> = nullptr>
447 bool operator==(const T &other) const {
448 return static_cast<Field>(*this) == static_cast<Field>(other);
449 }
450 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
451 std::nullptr_t> = nullptr>
452 bool operator!=(const T &other) const {
453 return static_cast<Field>(*this) != static_cast<Field>(other);
454 }
455 bool operator<(const Text &) const = delete;
456 bool operator<=(const Text &) const = delete;
457 bool operator>(const Text &) const = delete;
458 bool operator>=(const Text &) const = delete;
459};
460
465WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
466 const Text &data);
467
468namespace internal {
469struct InputRefState;
470}
471
487 std::shared_ptr<internal::InputRefState> state;
488
489 void lockTo(const Variant &target);
490 Variant &lockedField() const;
491
492 public:
494
495 InputRef();
496 InputRef(const InputRef &) = default;
497 InputRef &operator=(const InputRef &) = default;
502 InputRef(InputRef &&) = delete;
504 ~InputRef() = default;
505
515 ValAdaptor get() const;
516
521 template <typename T,
522 typename std::enable_if_t<std::is_convertible_v<ValAdaptor, T>,
523 std::nullptr_t> = nullptr>
524 operator T() const {
525 return static_cast<T>(get());
526 }
527
532 bool empty() const { return get().empty(); }
533
547 [[deprecated("(ver2.10〜) use asStringView() or asString() instead")]]
548 std::string asStringRef() const {
549 return asString();
550 }
560 [[deprecated("(ver2.10〜) use asWStringView() or asWString() instead")]]
561 std::wstring asWStringRef() const {
562 return asWString();
563 }
568 StringView asStringView() const { return get().asStringView(); }
573 WStringView asWStringView() const { return get().asWStringView(); }
578 std::string asString() const { return get().asString(); }
583 std::wstring asWString() const { return get().asWString(); }
588 double asDouble() const { return get().asDouble(); }
593 int asInt() const { return get().asInt(); }
598 long long asLLong() const { return get().asLLong(); }
609 template <typename T>
610 [[deprecated("use asDouble(), asInt() or asLLong() instead")]]
611 double as() const {
612 return get().as<T>();
613 }
614
619 bool asBool() const { return get().asBool(); }
620
621 template <typename T,
622 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
623 std::nullptr_t> = nullptr>
624 bool operator==(const T &other) const {
625 return get() == other;
626 }
627 template <typename T,
628 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
629 std::nullptr_t> = nullptr>
630 bool operator!=(const T &other) const {
631 return get() != other;
632 }
633};
634
635template <typename T,
636 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
637 std::nullptr_t> = nullptr>
638bool operator==(const T &other, const InputRef &ref) {
639 return ref.get() == other;
640}
641template <typename T,
642 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
643 std::nullptr_t> = nullptr>
644bool operator!=(const T &other, const InputRef &ref) {
645 return ref.get() != other;
646}
647inline std::ostream &operator<<(std::ostream &os, const InputRef &ref) {
648 return os << ref.get();
649}
名前を指定しないText
Definition text.h:486
InputRef & operator=(const InputRef &)=default
InputRef & operator=(InputRef &&)=delete
InputRef(InputRef &&)=delete
bool operator==(const T &other) const
Definition text.h:624
std::string asString() const
文字列として返す(コピー)
Definition text.h:578
std::string asStringRef() const
文字列として返す
Definition text.h:548
ValAdaptor get() const
値を返す
Definition text.cc:21
StringView asStringView() const
null終端の文字列として返す
Definition text.h:568
bool empty() const
値が空かどうか調べる
Definition text.h:532
std::wstring asWStringRef() const
文字列として返す (wstring)
Definition text.h:561
double as() const
数値として返す
Definition text.h:611
long long asLLong() const
long long型の整数として返す
Definition text.h:598
bool operator!=(const T &other) const
Definition text.h:630
bool asBool() const
bool値を返す
Definition text.h:619
int asInt() const
int型の整数として返す
Definition text.h:593
InputRef(const InputRef &)=default
double asDouble() const
実数として返す
Definition text.h:588
std::wstring asWString() const
文字列として返す(コピー) (wstring)
Definition text.h:583
WStringView asWStringView() const
null終端の文字列として返す
Definition text.h:573
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:159
SharedString のpublicなコンストラクタインタフェース (入力専用)
Definition encoding.h:215
webcfaceで管理されている文字列を参照するstring_view
Definition encoding.h:60
Viewを構築するときに使う一時的なViewComponent.
Definition component_view.h:292
文字列の送受信データを表すクラス
Definition text.h:206
bool operator==(const T &other) const
Textの参照先を比較
Definition text.h:447
Text operator[](const char *field) const
Definition text.h:251
bool operator!=(std::string_view rhs) const
Definition text.h:433
Text child(StringInitializer field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition text.h:223
Text operator[](const CharT(&static_str)[N])
Definition text.h:264
bool operator>(const Text &) const =delete
const Text & request() const
文字列をリクエストする
Definition text.h:352
const Text & operator=(StringInitializer v) const
文字列をセットする
Definition text.h:342
bool operator>=(const Text &) const =delete
Text child(int index) const
Definition text.h:231
Text parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition text.h:281
bool operator==(std::wstring_view rhs) const
Definition text.h:434
bool operator!=(const T &other) const
Definition text.h:452
bool operator==(std::string_view rhs) const
Definition text.h:432
Text(const Field &base, const SharedString &field)
Definition text.h:210
Text operator[](int index) const
Definition text.h:274
void appendListener(T &&callback) const
Definition text.h:321
bool operator<=(const Text &) const =delete
const Text & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition text.h:292
Text(const Field &base)
Definition text.h:209
bool operator<(const Text &) const =delete
Text operator[](const wchar_t *field) const
Definition text.h:255
const Text & set(StringInitializer v) const
文字列をセットする
Definition text.h:331
Text operator[](StringInitializer field) const
Definition text.h:241
Text()=default
bool operator!=(std::wstring_view rhs) const
Definition text.h:435
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:81
文字列、数値などの型を送受信するクラス
Definition text.h:25
bool operator<=(const Variant &) const =delete
bool operator==(const T &other) const
Definition text.h:173
std::string asString() const
文字列として返す(コピー)
Definition text.h:143
const Variant & request() const
値をリクエストする
Definition text.cc:40
std::string asStringRef() const
文字列として返す
Definition text.h:115
StringView asStringView() const
null終端の文字列の参照として返す
Definition text.h:133
bool empty() const
値が空かどうか調べる
Definition text.h:104
std::wstring asWStringRef() const
文字列として返す (wstring)
Definition text.h:126
long long asLLong() const
long long型の整数として返す
Definition text.h:163
const Variant & onChange(std::function< void(Variant)> callback) const
値が変化したときに呼び出されるコールバックを設定
Definition text.cc:61
bool operator!=(const T &other) const
Definition text.h:179
void appendListener(T &&callback) const
Definition text.h:63
bool asBool() const
bool値を返す
Definition text.h:168
int asInt() const
int型の整数として返す
Definition text.h:158
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:153
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:148
bool operator<(const Variant &) const =delete
WStringView asWStringView() const
null終端の文字列の参照として返す (wstring)
Definition text.h:138
std::ostream & operator<<(std::ostream &os, const Arg &arg)
Definition func_info.cc:98
bool operator==(const T &other, const InputRef &ref)
Definition text.h:638
const auto & get(const AxisAngle &aa)
Definition transform.h:231
bool operator!=(const T &other, const InputRef &ref)
Definition text.h:644
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:113
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:112
#define WEBCFACE_CALL_FP
Definition webcface-config.h:107