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 }
241 Text operator[](StringInitializer field) const { return child(std::move(field)); }
249 Text operator[](const char *field) const { return child(field); }
253 Text operator[](const wchar_t *field) const { return child(field); }
258 template <std::size_t N>
259 Text operator[](const char (&static_str)[N]) {
260 return child(StringInitializer(static_str));
261 }
266 template <std::size_t N>
267 Text operator[](const wchar_t (&static_str)[N]) {
268 return child(StringInitializer(static_str));
269 }
275 [[deprecated]]
276 Text
277 operator[](int index) const {
278 return child(std::to_string(index));
279 }
284 Text parent() const { return this->Field::parent(); }
285
292 template <typename F,
293 typename std::enable_if_t<std::is_invocable_v<F, Text>,
294 std::nullptr_t> = nullptr>
295 const Text &onChange(F callback) const {
296 this->Variant::onChange(
297 [callback = std::move(callback)](const Variant &base) {
298 callback(Text(base));
299 });
300 return *this;
301 }
308 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
309 std::nullptr_t> = nullptr>
310 const Text &onChange(F callback) const {
311 this->Variant::onChange(
312 [callback = std::move(callback)](const auto &) { callback(); });
313 return *this;
314 }
322 template <typename T>
323 [[deprecated]]
324 void appendListener(T &&callback) const {
325 onChange(std::forward<T>(callback));
326 }
327
334 const Text &set(StringInitializer v) const {
335 this->Variant::set(ValAdaptor{std::move(v)});
336 return *this;
337 }
338
346 this->set(std::move(v));
347 return *this;
348 }
349
355 const Text &request() const {
356 this->Variant::request();
357 return *this;
358 }
368 std::optional<StringView> tryGet() const;
377 std::optional<WStringView> tryGetW() const;
388 StringView get() const;
397 WStringView getW() const;
398
404 operator std::string_view() const { return get(); }
410 operator std::wstring_view() const { return getW(); }
411
420 bool exists() const;
421
426 [[deprecated]]
427 std::chrono::system_clock::time_point time() const;
428
433 const Text &free() const;
434
435 bool operator==(std::string_view rhs) const { return this->get() == rhs; }
436 bool operator!=(std::string_view rhs) const { return this->get() != rhs; }
437 bool operator==(std::wstring_view rhs) const { return this->getW() == rhs; }
438 bool operator!=(std::wstring_view rhs) const { return this->getW() != rhs; }
439
448 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
449 std::nullptr_t> = nullptr>
450 bool operator==(const T &other) const {
451 return static_cast<Field>(*this) == static_cast<Field>(other);
452 }
453 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
454 std::nullptr_t> = nullptr>
455 bool operator!=(const T &other) const {
456 return static_cast<Field>(*this) != static_cast<Field>(other);
457 }
458 bool operator<(const Text &) const = delete;
459 bool operator<=(const Text &) const = delete;
460 bool operator>(const Text &) const = delete;
461 bool operator>=(const Text &) const = delete;
462};
463
468WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
469 const Text &data);
470
471namespace internal {
472struct InputRefState;
473}
474
490 std::shared_ptr<internal::InputRefState> state;
491
492 void lockTo(const Variant &target);
493 Variant &lockedField() const;
494
495 public:
497
498 InputRef();
499 InputRef(const InputRef &) = default;
500 InputRef &operator=(const InputRef &) = default;
505 InputRef(InputRef &&) = delete;
507 ~InputRef() = default;
508
518 ValAdaptor get() const;
519
524 template <typename T,
525 typename std::enable_if_t<std::is_convertible_v<ValAdaptor, T>,
526 std::nullptr_t> = nullptr>
527 operator T() const {
528 return static_cast<T>(get());
529 }
530
535 bool empty() const { return get().empty(); }
536
550 [[deprecated("(ver2.10〜) use asStringView() or asString() instead")]]
551 std::string asStringRef() const {
552 return asString();
553 }
563 [[deprecated("(ver2.10〜) use asWStringView() or asWString() instead")]]
564 std::wstring asWStringRef() const {
565 return asWString();
566 }
571 StringView asStringView() const { return get().asStringView(); }
576 WStringView asWStringView() const { return get().asWStringView(); }
581 std::string asString() const { return get().asString(); }
586 std::wstring asWString() const { return get().asWString(); }
591 double asDouble() const { return get().asDouble(); }
596 int asInt() const { return get().asInt(); }
601 long long asLLong() const { return get().asLLong(); }
612 template <typename T>
613 [[deprecated("use asDouble(), asInt() or asLLong() instead")]]
614 double as() const {
615 return get().as<T>();
616 }
617
622 bool asBool() const { return get().asBool(); }
623
624 template <typename T,
625 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
626 std::nullptr_t> = nullptr>
627 bool operator==(const T &other) const {
628 return get() == other;
629 }
630 template <typename T,
631 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
632 std::nullptr_t> = nullptr>
633 bool operator!=(const T &other) const {
634 return get() != other;
635 }
636};
637
638template <typename T,
639 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
640 std::nullptr_t> = nullptr>
641bool operator==(const T &other, const InputRef &ref) {
642 return ref.get() == other;
643}
644template <typename T,
645 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
646 std::nullptr_t> = nullptr>
647bool operator!=(const T &other, const InputRef &ref) {
648 return ref.get() != other;
649}
650inline std::ostream &operator<<(std::ostream &os, const InputRef &ref) {
651 return os << ref.get();
652}
名前を指定しないText
Definition text.h:489
InputRef & operator=(const InputRef &)=default
InputRef & operator=(InputRef &&)=delete
InputRef(InputRef &&)=delete
bool operator==(const T &other) const
Definition text.h:627
std::string asString() const
文字列として返す(コピー)
Definition text.h:581
std::string asStringRef() const
文字列として返す
Definition text.h:551
ValAdaptor get() const
値を返す
Definition text.cc:21
StringView asStringView() const
null終端の文字列として返す
Definition text.h:571
bool empty() const
値が空かどうか調べる
Definition text.h:535
std::wstring asWStringRef() const
文字列として返す (wstring)
Definition text.h:564
double as() const
数値として返す
Definition text.h:614
long long asLLong() const
long long型の整数として返す
Definition text.h:601
bool operator!=(const T &other) const
Definition text.h:633
bool asBool() const
bool値を返す
Definition text.h:622
int asInt() const
int型の整数として返す
Definition text.h:596
InputRef(const InputRef &)=default
double asDouble() const
実数として返す
Definition text.h:591
std::wstring asWString() const
文字列として返す(コピー) (wstring)
Definition text.h:586
WStringView asWStringView() const
null終端の文字列として返す
Definition text.h:576
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
Text operator[](const wchar_t(&static_str)[N])
Definition text.h:267
bool operator==(const T &other) const
Textの参照先を比較
Definition text.h:450
Text operator[](const char *field) const
Definition text.h:249
bool operator!=(std::string_view rhs) const
Definition text.h:436
Text child(StringInitializer field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition text.h:223
bool operator>(const Text &) const =delete
const Text & request() const
文字列をリクエストする
Definition text.h:355
const Text & operator=(StringInitializer v) const
文字列をセットする
Definition text.h:345
bool operator>=(const Text &) const =delete
Text child(int index) const
Definition text.h:231
Text parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition text.h:284
bool operator==(std::wstring_view rhs) const
Definition text.h:437
Text operator[](const char(&static_str)[N])
Definition text.h:259
bool operator!=(const T &other) const
Definition text.h:455
bool operator==(std::string_view rhs) const
Definition text.h:435
Text(const Field &base, const SharedString &field)
Definition text.h:210
Text operator[](int index) const
Definition text.h:277
void appendListener(T &&callback) const
Definition text.h:324
bool operator<=(const Text &) const =delete
const Text & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition text.h:295
Text(const Field &base)
Definition text.h:209
bool operator<(const Text &) const =delete
Text operator[](const wchar_t *field) const
Definition text.h:253
const Text & set(StringInitializer v) const
文字列をセットする
Definition text.h:334
Text operator[](StringInitializer field) const
Definition text.h:241
Text()=default
bool operator!=(std::wstring_view rhs) const
Definition text.h:438
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:87
文字列、数値などの型を送受信するクラス
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:641
const auto & get(const AxisAngle &aa)
Definition transform.h:231
bool operator!=(const T &other, const InputRef &ref)
Definition text.h:647
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