WebCFace 3.1.1
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 }
54
55 protected:
60 const Variant &set(const ValAdaptor &v) const;
61
62 public:
67 const Variant &request() const;
72 std::optional<ValAdaptor> tryGet() const;
80 ValAdaptor get() const;
81
82 template <typename T,
83 typename std::enable_if_t<std::is_convertible_v<ValAdaptor, T>,
84 std::nullptr_t> = nullptr>
85 operator T() const {
86 return static_cast<T>(get());
87 }
92 bool empty() const { return get().empty(); }
102 [[deprecated("(ver3.0〜) use asStringView() or asString() instead")]]
103 std::string asStringRef() const {
104 return asString();
105 }
113 [[deprecated("(ver3.0〜) use asWStringView() or asWString() instead")]]
114 std::wstring asWStringRef() const {
115 return asWString();
116 }
121 StringView asStringView() const { return get().asStringView(); }
126 WStringView asWStringView() const { return get().asWStringView(); }
131 std::string asString() const { return get().asString(); }
136 std::wstring asWString() const { return get().asWString(); }
141 double asDouble() const { return get().asDouble(); }
146 int asInt() const { return get().asInt(); }
151 long long asLLong() const { return get().asLLong(); }
156 bool asBool() const { return get().asBool(); }
157
158 template <typename T,
159 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
160 std::nullptr_t> = nullptr>
161 bool operator==(const T &other) const {
162 return get() == other;
163 }
164 template <typename T,
165 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
166 std::nullptr_t> = nullptr>
167 bool operator!=(const T &other) const {
168 return get() != other;
169 }
170
171 template <typename T, typename std::enable_if_t<std::is_same_v<T, Variant>,
172 std::nullptr_t> = nullptr>
173 bool operator==(const T &other) const {
174 return static_cast<Field>(*this) == static_cast<Field>(other);
175 }
176 template <typename T, typename std::enable_if_t<std::is_same_v<T, Variant>,
177 std::nullptr_t> = nullptr>
178 bool operator!=(const T &other) const {
179 return static_cast<Field>(*this) != static_cast<Field>(other);
180 }
181 bool operator<(const Variant &) const = delete;
182 bool operator<=(const Variant &) const = delete;
183 bool operator>(const Variant &) const = delete;
184 bool operator>=(const Variant &) const = delete;
185};
186
187
194class WEBCFACE_DLL Text : protected Variant {
195 public:
196 Text() = default;
197 Text(const Field &base) : Variant(base) {}
198 Text(const Field &base, const SharedString &field)
199 : Text(Field{base, field}) {}
200
201 using Field::lastName;
202 using Field::lastNameW;
203 using Field::member;
204 using Field::name;
205 using Field::nameW;
213 return this->Field::child(static_cast<SharedString &>(field));
214 }
219 [[deprecated]]
220 Text child(int index) const {
221 return this->Field::child(std::to_string(index));
222 }
231 return child(std::move(field));
232 }
240 Text operator[](const char *field) const { return child(field); }
244 Text operator[](const wchar_t *field) const { return child(field); }
249 template <typename CharT, std::size_t N,
250 typename std::enable_if_t<std::is_same_v<CharT, char> ||
251 std::is_same_v<CharT, wchar_t>,
252 std::nullptr_t> = nullptr>
253 Text operator[](const CharT (&static_str)[N]) {
254 return child(StringInitializer(static_str));
255 }
261 [[deprecated]]
262 Text
263 operator[](int index) const {
264 return child(std::to_string(index));
265 }
270 Text parent() const { return this->Field::parent(); }
271
278 template <typename F,
279 typename std::enable_if_t<std::is_invocable_v<F, Text>,
280 std::nullptr_t> = nullptr>
281 const Text &onChange(F callback) const {
282 this->Variant::onChange(
283 [callback = std::move(callback)](const Variant &base) {
284 callback(Text(base));
285 });
286 return *this;
287 }
294 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
295 std::nullptr_t> = nullptr>
296 const Text &onChange(F callback) const {
297 this->Variant::onChange(
298 [callback = std::move(callback)](const auto &) { callback(); });
299 return *this;
300 }
301
308 const Text &set(StringInitializer v) const {
309 this->Variant::set(ValAdaptor{std::move(v)});
310 return *this;
311 }
312
320 this->set(std::move(v));
321 return *this;
322 }
323
329 const Text &request() const {
330 this->Variant::request();
331 return *this;
332 }
342 std::optional<StringView> tryGet() const;
351 std::optional<WStringView> tryGetW() const;
362 StringView get() const;
371 WStringView getW() const;
372
378 operator std::string_view() const { return get(); }
384 operator std::wstring_view() const { return getW(); }
385
394 bool exists() const;
395
400 const Text &free() const;
401
402 bool operator==(std::string_view rhs) const { return this->get() == rhs; }
403 bool operator!=(std::string_view rhs) const { return this->get() != rhs; }
404 bool operator==(std::wstring_view rhs) const { return this->getW() == rhs; }
405 bool operator!=(std::wstring_view rhs) const { return this->getW() != rhs; }
406
415 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
416 std::nullptr_t> = nullptr>
417 bool operator==(const T &other) const {
418 return static_cast<Field>(*this) == static_cast<Field>(other);
419 }
420 template <typename T, typename std::enable_if_t<std::is_same_v<T, Text>,
421 std::nullptr_t> = nullptr>
422 bool operator!=(const T &other) const {
423 return static_cast<Field>(*this) != static_cast<Field>(other);
424 }
425 bool operator<(const Text &) const = delete;
426 bool operator<=(const Text &) const = delete;
427 bool operator>(const Text &) const = delete;
428 bool operator>=(const Text &) const = delete;
429};
430
435WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
436 const Text &data);
437
438namespace internal {
439struct InputRefState;
440}
441
457 std::shared_ptr<internal::InputRefState> state;
458
459 void lockTo(const Variant &target);
460 Variant &lockedField() const;
461
462 public:
464
465 InputRef();
466 InputRef(const InputRef &) = default;
467 InputRef &operator=(const InputRef &) = default;
472 InputRef(InputRef &&) = delete;
474 ~InputRef() = default;
475
485 ValAdaptor get() const;
486
491 template <typename T,
492 typename std::enable_if_t<std::is_convertible_v<ValAdaptor, T>,
493 std::nullptr_t> = nullptr>
494 operator T() const {
495 return static_cast<T>(get());
496 }
497
502 bool empty() const { return get().empty(); }
503
517 [[deprecated("(ver3.0〜) use asStringView() or asString() instead")]]
518 std::string asStringRef() const {
519 return asString();
520 }
530 [[deprecated("(ver3.0〜) use asWStringView() or asWString() instead")]]
531 std::wstring asWStringRef() const {
532 return asWString();
533 }
538 StringView asStringView() const { return get().asStringView(); }
543 WStringView asWStringView() const { return get().asWStringView(); }
548 std::string asString() const { return get().asString(); }
553 std::wstring asWString() const { return get().asWString(); }
558 double asDouble() const { return get().asDouble(); }
563 int asInt() const { return get().asInt(); }
568 long long asLLong() const { return get().asLLong(); }
579 template <typename T>
580 [[deprecated("use asDouble(), asInt() or asLLong() instead")]]
581 double as() const {
582 return get().as<T>();
583 }
584
589 bool asBool() const { return get().asBool(); }
590
591 template <typename T,
592 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
593 std::nullptr_t> = nullptr>
594 bool operator==(const T &other) const {
595 return get() == other;
596 }
597 template <typename T,
598 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
599 std::nullptr_t> = nullptr>
600 bool operator!=(const T &other) const {
601 return get() != other;
602 }
603};
604
605template <typename T,
606 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
607 std::nullptr_t> = nullptr>
608bool operator==(const T &other, const InputRef &ref) {
609 return ref.get() == other;
610}
611template <typename T,
612 typename std::enable_if_t<std::is_constructible_v<ValAdaptor, T>,
613 std::nullptr_t> = nullptr>
614bool operator!=(const T &other, const InputRef &ref) {
615 return ref.get() != other;
616}
617inline std::ostream &operator<<(std::ostream &os, const InputRef &ref) {
618 return os << ref.get();
619}
名前を指定しないText
Definition text.h:456
InputRef & operator=(const InputRef &)=default
InputRef & operator=(InputRef &&)=delete
InputRef(InputRef &&)=delete
bool operator==(const T &other) const
Definition text.h:594
std::string asString() const
文字列として返す(コピー)
Definition text.h:548
std::string asStringRef() const
文字列として返す
Definition text.h:518
ValAdaptor get() const
値を返す
Definition text.cc:21
StringView asStringView() const
null終端の文字列として返す
Definition text.h:538
bool empty() const
値が空かどうか調べる
Definition text.h:502
std::wstring asWStringRef() const
文字列として返す (wstring)
Definition text.h:531
double as() const
数値として返す
Definition text.h:581
long long asLLong() const
long long型の整数として返す
Definition text.h:568
bool operator!=(const T &other) const
Definition text.h:600
bool asBool() const
bool値を返す
Definition text.h:589
int asInt() const
int型の整数として返す
Definition text.h:563
InputRef(const InputRef &)=default
double asDouble() const
実数として返す
Definition text.h:558
std::wstring asWString() const
文字列として返す(コピー) (wstring)
Definition text.h:553
WStringView asWStringView() const
null終端の文字列として返す
Definition text.h:543
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:170
SharedString のpublicなコンストラクタインタフェース (入力専用)
Definition encoding.h:235
webcfaceで管理されている文字列を参照するstring_view
Definition encoding.h:71
Viewを構築するときに使う一時的なViewComponent.
Definition component_view.h:292
文字列の送受信データを表すクラス
Definition text.h:194
bool operator==(const T &other) const
Textの参照先を比較
Definition text.h:417
Text operator[](const char *field) const
Definition text.h:240
bool operator!=(std::string_view rhs) const
Definition text.h:403
Text child(StringInitializer field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition text.h:212
Text operator[](const CharT(&static_str)[N])
Definition text.h:253
bool operator>(const Text &) const =delete
const Text & request() const
文字列をリクエストする
Definition text.h:329
const Text & operator=(StringInitializer v) const
文字列をセットする
Definition text.h:319
bool operator>=(const Text &) const =delete
Text child(int index) const
Definition text.h:220
Text parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition text.h:270
bool operator==(std::wstring_view rhs) const
Definition text.h:404
bool operator!=(const T &other) const
Definition text.h:422
bool operator==(std::string_view rhs) const
Definition text.h:402
Text(const Field &base, const SharedString &field)
Definition text.h:198
Text operator[](int index) const
Definition text.h:263
bool operator<=(const Text &) const =delete
const Text & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition text.h:281
Text(const Field &base)
Definition text.h:197
bool operator<(const Text &) const =delete
Text operator[](const wchar_t *field) const
Definition text.h:244
const Text & set(StringInitializer v) const
文字列をセットする
Definition text.h:308
Text operator[](StringInitializer field) const
Definition text.h:230
Text()=default
bool operator!=(std::wstring_view rhs) const
Definition text.h:405
数値、文字列などの値を相互変換するクラス
Definition val_adaptor.h:81
文字列、数値などの型を送受信するクラス
Definition text.h:25
bool operator<=(const Variant &) const =delete
bool operator==(const T &other) const
Definition text.h:161
std::string asString() const
文字列として返す(コピー)
Definition text.h:131
const Variant & request() const
値をリクエストする
Definition text.cc:40
std::string asStringRef() const
文字列として返す
Definition text.h:103
StringView asStringView() const
null終端の文字列の参照として返す
Definition text.h:121
bool empty() const
値が空かどうか調べる
Definition text.h:92
std::wstring asWStringRef() const
文字列として返す (wstring)
Definition text.h:114
long long asLLong() const
long long型の整数として返す
Definition text.h:151
const Variant & onChange(std::function< void(Variant)> callback) const
値が変化したときに呼び出されるコールバックを設定
Definition text.cc:61
bool operator!=(const T &other) const
Definition text.h:167
bool asBool() const
bool値を返す
Definition text.h:156
int asInt() const
int型の整数として返す
Definition text.h:146
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:141
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:136
bool operator<(const Variant &) const =delete
WStringView asWStringView() const
null終端の文字列の参照として返す (wstring)
Definition text.h:126
std::ostream & operator<<(std::ostream &os, const Arg &arg)
Definition func_info.cc:99
bool operator==(const T &other, const InputRef &ref)
Definition text.h:608
const auto & get(const AxisAngle &aa)
Definition transform.h:227
bool operator!=(const T &other, const InputRef &ref)
Definition text.h:614
ClientDataの参照とメンバ名とデータ名を持つクラス
Definition field.h:68
Field parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition field.cc:39
Field child(const SharedString &field) const
Definition field.cc:48
#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