WebCFace 2.9.0
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
value.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 "field.h"
9#ifdef WEBCFACE_MESON
10#include "webcface-config.h"
11#else
12#include "webcface/common/webcface-config.h"
13#endif
14
16
22 std::size_t index;
23
24 public:
25 ValueElementRef(const Field &base, std::size_t index)
26 : Field(base), index(index) {}
27
36 const ValueElementRef &set(double v) const;
37 const ValueElementRef &operator=(double v) const { return set(v); }
38
44 std::optional<double> tryGet() const;
51 double get() const { return tryGet().value_or(0); }
52};
53
61class WEBCFACE_DLL Value : protected Field {
62 public:
63 Value() = default;
64 Value(const Field &base);
65 Value(const Field &base, const SharedString &field)
66 : Value(Field{base, field}) {}
67
68 friend class ValueElementRef;
69
70 using Field::lastName;
71 using Field::member;
72 using Field::name;
73 using Field::nameW;
81 return this->Field::child(static_cast<SharedString &>(field));
82 }
87 [[deprecated]]
88 Value child(int index) const {
89 return this->Field::child(std::to_string(index));
90 }
99 return child(std::move(field));
100 }
105 Value operator[](const char *field) const { return child(field); }
109 Value operator[](const wchar_t *field) const { return child(field); }
114 template <typename CharT, std::size_t N,
115 typename std::enable_if_t<std::is_same_v<CharT, char> ||
116 std::is_same_v<CharT, wchar_t>,
117 std::nullptr_t> = nullptr>
118 Value operator[](const CharT (&static_str)[N]) {
119 return child(StringInitializer(static_str));
120 }
125 template <std::size_t N>
126 Value operator[](const wchar_t (&static_str)[N]) {
127 return child(StringInitializer(static_str));
128 }
140 template <typename T,
141 std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
143 return ValueElementRef(*this, index);
144 }
149 ValueElementRef at(std::size_t index) const {
150 return ValueElementRef(*this, index);
151 }
156 Value parent() const { return this->Field::parent(); }
157
164 const Value &
165 onChange(std::function<void WEBCFACE_CALL_FP(Value)> callback) const;
172 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
173 std::nullptr_t> = nullptr>
174 const Value &onChange(F callback) const {
175 return onChange(
176 [callback = std::move(callback)](const auto &) { callback(); });
177 }
185 template <typename T>
186 [[deprecated]]
187 void appendListener(T &&callback) const {
188 onChange(std::forward<T>(callback));
189 }
190
198 const Value &set(double v) const;
204 const Value &set(std::vector<double> v) const;
214 template <typename R,
215 typename traits::ArrayLikeTrait<R>::ArrayLike = traits::TraitOk>
216 const Value &set(const R &range) const {
217 return set(traits::arrayLikeToVector(range));
218 }
223 const Value &resize(std::size_t size) const;
227 const Value &push_back(double v) const;
238 std::size_t size() const;
239
244 template <typename T>
245 const Value &operator=(T &&v) const {
246 this->set(std::forward<T>(v));
247 return *this;
248 }
257 const Value &operator=(std::vector<double> v) const {
258 this->set(std::move(v));
259 return *this;
260 }
261
267 const Value &request() const;
274 std::optional<double> tryGet() const;
282 std::optional<NumVector> tryGetVec() const;
287 double get() const { return tryGet().value_or(0); }
296 return tryGetVec().value_or(std::vector<double>{});
297 }
298 operator double() const { return get(); }
299 operator std::vector<double>() const { return getVec(); }
303 operator NumVector() const { return getVec(); }
304
313 bool exists() const;
318 [[deprecated]]
319 std::chrono::system_clock::time_point time() const;
320
325 const Value &free() const;
326
327 const Value &operator+=(double rhs) const {
328 this->set(this->get() + rhs);
329 return *this;
330 }
331 const Value &operator-=(double rhs) const {
332 this->set(this->get() - rhs);
333 return *this;
334 }
335 const Value &operator*=(double rhs) const {
336 this->set(this->get() * rhs);
337 return *this;
338 }
339 const Value &operator/=(double rhs) const {
340 this->set(this->get() / rhs);
341 return *this;
342 }
343 const Value &operator%=(std::int32_t rhs) const {
344 this->set(static_cast<std::int32_t>(this->get()) % rhs);
345 return *this;
346 }
347 const Value &operator<<=(std::int32_t rhs) const {
348 // todo: int64_tかuint64_tにしたほうがいいかもしれない
349 this->set(static_cast<std::int32_t>(this->get()) << rhs);
350 return *this;
351 }
352 const Value &operator>>=(std::int32_t rhs) const {
353 this->set(static_cast<std::int32_t>(this->get()) >> rhs);
354 return *this;
355 }
356 const Value &operator&=(std::int32_t rhs) const {
357 this->set(static_cast<std::int32_t>(this->get()) & rhs);
358 return *this;
359 }
360 const Value &operator|=(std::int32_t rhs) const {
361 this->set(static_cast<std::int32_t>(this->get()) | rhs);
362 return *this;
363 }
364 const Value &operator^=(std::int32_t rhs) const {
365 this->set(static_cast<std::int32_t>(this->get()) ^ rhs);
366 return *this;
367 }
372 const Value &operator++() const { // ++s
373 this->set(this->get() + 1);
374 return *this;
375 }
380 double operator++(int) const { // s++
381 auto v = this->get();
382 this->set(v + 1);
383 return v;
384 }
389 const Value &operator--() const { // --const s
390 this->set(this->get() - 1);
391 return *this;
392 }
397 double operator--(int) const { // s--
398 auto v = this->get();
399 this->set(v - 1);
400 return v;
401 }
402
411 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
412 std::nullptr_t> = nullptr>
413 bool operator==(const T &other) const {
414 return static_cast<Field>(*this) == static_cast<Field>(other);
415 }
416 template <typename T, typename std::enable_if_t<std::is_same_v<T, Value>,
417 std::nullptr_t> = nullptr>
418 bool operator!=(const T &other) const {
419 return static_cast<Field>(*this) == static_cast<Field>(other);
420 }
421 bool operator<(const Value &) const = delete;
422 bool operator<=(const Value &) const = delete;
423 bool operator>(const Value &) const = delete;
424 bool operator>=(const Value &) const = delete;
425};
426
433WEBCFACE_DLL std::ostream &WEBCFACE_CALL operator<<(std::ostream &os,
434 const Value &data);
435
shared_ptrで管理されているdoubleのvector
Definition num_vector.h:20
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:159
SharedString のpublicなコンストラクタインタフェース (入力専用)
Definition encoding.h:215
配列型のValueデータの一部の要素を指定するクラス
Definition value.h:21
ValueElementRef(const Field &base, std::size_t index)
Definition value.h:25
const ValueElementRef & operator=(double v) const
Definition value.h:37
double get() const
値があればその要素を返す
Definition value.h:51
実数値またはその配列の送受信データを表すクラス
Definition value.h:61
Value operator[](StringInitializer field) const
Definition value.h:98
Value parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition value.h:156
const Value & operator/=(double rhs) const
Definition value.h:339
const Value & operator+=(double rhs) const
Definition value.h:327
const Value & operator|=(std::int32_t rhs) const
Definition value.h:360
bool operator==(const T &other) const
Valueの参照先を比較
Definition value.h:413
ValueElementRef operator[](T index) const
1次元配列型データの要素を参照する
Definition value.h:142
const Value & onChange(F callback) const
値が変化したときに呼び出されるコールバックを設定
Definition value.h:174
const Value & set(const R &range) const
配列型の値をセットする
Definition value.h:216
const Value & operator%=(std::int32_t rhs) const
Definition value.h:343
const Value & operator>>=(std::int32_t rhs) const
Definition value.h:352
double operator--(int) const
1引いたものをsetし、足す前の値を返す
Definition value.h:397
bool operator<=(const Value &) const =delete
NumVector getVec() const
値をvectorで返す
Definition value.h:295
const Value & operator*=(double rhs) const
Definition value.h:335
Value child(int index) const
Definition value.h:88
bool operator!=(const T &other) const
Definition value.h:418
double operator++(int) const
1足したものをsetし、足す前の値を返す
Definition value.h:380
bool operator>=(const Value &) const =delete
void appendListener(T &&callback) const
Definition value.h:187
const Value & operator&=(std::int32_t rhs) const
Definition value.h:356
const Value & operator--() const
1引いたものをsetした後自身を返す
Definition value.h:389
const Value & operator-=(double rhs) const
Definition value.h:331
Value child(StringInitializer field) const
「(thisの名前).(追加の名前)」を新しい名前とするField
Definition value.h:80
Value()=default
bool operator>(const Value &) const =delete
Value operator[](const wchar_t(&static_str)[N])
Definition value.h:126
const Value & operator=(T &&v) const
数値または配列をセットする
Definition value.h:245
const Value & operator^=(std::int32_t rhs) const
Definition value.h:364
const Value & operator=(std::vector< double > v) const
vector型配列をセットする
Definition value.h:257
ValueElementRef at(std::size_t index) const
1次元配列型データの要素を参照する
Definition value.h:149
bool operator<(const Value &) const =delete
Value operator[](const char *field) const
Definition value.h:105
Value operator[](const wchar_t *field) const
Definition value.h:109
Value operator[](const CharT(&static_str)[N])
Definition value.h:118
double get() const
値を返す
Definition value.h:287
Value(const Field &base, const SharedString &field)
Definition value.h:65
const Value & operator<<=(std::int32_t rhs) const
Definition value.h:347
const Value & operator++() const
1足したものをsetした後自身を返す
Definition value.h:372
std::ostream & operator<<(std::ostream &os, const Arg &arg)
Definition func_info.cc:98
const auto & get(const AxisAngle &aa)
Definition transform.h:231
ClientDataの参照とメンバ名とデータ名を持つクラス
Definition field.h:71
Field parent() const
nameの最後のピリオドの前までを新しい名前とするField
Definition field.cc:33
Field child(const SharedString &field) const
Definition field.cc:42
Definition array_like.h:58
#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