Clapkit
Loading...
Searching...
No Matches
ckProperty.h
Go to the documentation of this file.
1/*
2 *
3 * Clapkit
4 * ----------------------------------------------------------------------
5 * A wrapper for creating a 'generalized' app for Classic MacOS
6 * that (hopefully) can be ported easily to other platforms.
7 *
8 * CKProperty
9 * ----------------------------------------------------------------------
10 * A simple reactivity wrapper for properties.
11 *
12 */
13
14#pragma once
15#include <functional>
16
17template <typename, typename = void>
18struct has_subscribe : std::false_type {};
19
20template <typename U>
21struct has_subscribe<U, std::void_t<decltype(std::declval<U>().Subscribe(std::declval<std::function<void()>>()))>> : std::true_type {};
22
59template <typename T>
61 public:
62 CKProperty() = default;
64 : value_(v) {}
65
66 // read access
67 operator const T&() const { return value_; }
68 const T& get() const { return value_; }
69 T& get() { return value_; }
70
71 // For regular types
72 template <typename U = T>
73 auto operator->() -> typename std::enable_if<!std::is_pointer<U>::value, U*>::type {
74 return &value_;
75 }
76
77 template <typename U = T>
78 auto operator->() const -> typename std::enable_if<!std::is_pointer<U>::value, const U*>::type {
79 return &value_;
80 }
81
82 // For pointer types — unwrap the pointer
83 template <typename U = T>
84 auto operator->() -> typename std::enable_if<std::is_pointer<U>::value, typename std::remove_pointer<U>::type*>::type {
85 return value_;
86 }
87
88 template <typename U = T>
89 auto operator->() const -> typename std::enable_if<std::is_pointer<U>::value, typename std::remove_pointer<U>::type*>::type {
90 return value_;
91 }
92
93 // write access
94 CKProperty& operator=(const T& v) {
95 if (value_ != v) {
96 value_ = v;
97 bindSubscribable();
98 if (onChange) {
99 onChange();
100 }
101 }
102 return *this;
103 }
104
105 CKProperty& operator+=(const T& v) {
106 value_ += v;
107 if (onChange)
108 onChange();
109 return *this;
110 }
111
112 CKProperty& operator-=(const T& v) {
113 value_ -= v;
114 if (onChange)
115 onChange();
116 return *this;
117 }
118
119 CKProperty& operator*=(const T& v) {
120 value_ *= v;
121 if (onChange)
122 onChange();
123 return *this;
124 }
125
126 CKProperty& operator/=(const T& v) {
127 value_ /= v;
128 if (onChange)
129 onChange();
130 return *this;
131 }
132
133 CKProperty& operator%=(const T& v) {
134 value_ %= v;
135 if (onChange)
136 onChange();
137 return *this;
138 }
139
141 ++value_;
142 if (onChange)
143 onChange();
144 return *this;
145 }
146
148 CKProperty tmp = *this;
149 ++(*this);
150 return tmp;
151 }
152
154 --value_;
155 if (onChange)
156 onChange();
157 return *this;
158 }
159
161 CKProperty tmp = *this;
162 --(*this);
163 return tmp;
164 }
165
169 std::function<void()> onChange;
170
171 private:
172 void bindSubscribable() {
173 if constexpr (has_subscribe<T>::value) {
174 value_.Subscribe([this] { if(onChange) onChange(); });
175 }
176 }
177
178 private:
179 T value_;
180};
Defines an observable value.
Definition ckProperty.h:60
const T & get() const
Definition ckProperty.h:68
CKProperty & operator-=(const T &v)
Definition ckProperty.h:112
auto operator->() -> typename std::enable_if< std::is_pointer< U >::value, typename std::remove_pointer< U >::type * >::type
Definition ckProperty.h:84
CKProperty & operator--()
Definition ckProperty.h:153
auto operator->() -> typename std::enable_if<!std::is_pointer< U >::value, U * >::type
Definition ckProperty.h:73
CKProperty & operator++()
Definition ckProperty.h:140
auto operator->() const -> typename std::enable_if<!std::is_pointer< U >::value, const U * >::type
Definition ckProperty.h:78
CKProperty & operator*=(const T &v)
Definition ckProperty.h:119
CKProperty & operator/=(const T &v)
Definition ckProperty.h:126
std::function< void()> onChange
The function to be called when the value changes.
Definition ckProperty.h:169
CKProperty(T v)
Definition ckProperty.h:63
CKProperty & operator+=(const T &v)
Definition ckProperty.h:105
CKProperty operator++(int)
Definition ckProperty.h:147
CKProperty()=default
CKProperty & operator%=(const T &v)
Definition ckProperty.h:133
auto operator->() const -> typename std::enable_if< std::is_pointer< U >::value, typename std::remove_pointer< U >::type * >::type
Definition ckProperty.h:89
CKProperty & operator=(const T &v)
Definition ckProperty.h:94
CKProperty operator--(int)
Definition ckProperty.h:160
T & get()
Definition ckProperty.h:69
Definition ckProperty.h:18