Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
mutable_cell.hpp
1/*
2 * live_cells_cpp
3 * Copyright (C) 2024 Alexander Gutev <alex.gutev@gmail.com>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you
6 * may not use this file except in compliance with the License. You
7 * may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 */
17
18#ifndef LIVE_CELLS_MUTABLE_CELL_HPP
19#define LIVE_CELLS_MUTABLE_CELL_HPP
20
21#include <memory>
22
23#include "cell_state.hpp"
24#include "stateful_cell.hpp"
25#include "tracker.hpp"
26
27namespace live_cells {
28
45 public:
48
49 batch_update(const batch_update &) = delete;
50 batch_update& operator=(const batch_update &) = delete;
51
52 private:
54 bool is_batching;
55
59 static bool is_batch_update();
60
68 static void add_to_batch(std::shared_ptr<cell_state> state);
69
70 template <typename T>
71 friend class mutable_cell_state;
72 };
73
77 template<typename T>
79 public:
81
91 value_(value) {
92 }
93
99 T value() {
100 return value_;
101 }
102
108 void value(T value) {
109 if (value_ != value) {
111 value_ = std::move(value);
112
113 if (!batch_update::is_batch_update()) {
115 }
116 else {
117 batch_update::add_to_batch(this->shared_from_this());
118 }
119 }
120 }
121
122 protected:
129 value_ = std::move(value);
130 }
131
137 static bool is_batch_update() {
138 return batch_update::is_batch_update();
139 }
140
146 batch_update::add_to_batch(this->shared_from_this());
147 }
148
149 private:
151 T value_;
152 };
153
157 template<typename T>
158 class mutable_cell : public stateful_cell<mutable_cell_state<T>> {
163
164 public:
168 typedef T value_type;
169
175
183
200
201
211 T operator=(const T &value) {
212 this->value(value);
213 return value;
214 }
215
230 T operator=(const T &value) const {
231 this->value(value);
232 return value;
233 }
234
240 T value() const {
241 return this->state->value();
242 }
243
252 void value(T value) const {
253 this->state->value(value);
254 }
255
262 T operator()() const {
263 argument_tracker::global().track_argument(*this);
264 return value();
265 }
266 };
267
275 template<typename T>
279
292 template<typename T>
296
306 template<typename F>
307 void batch(F fn) {
309 fn();
310 }
311
322 template <typename T>
323 T operator+=(const MutableCell auto &c, const T &value) {
324 return c = c.value() + value;
325 }
326
337 template <typename T>
338 T operator-=(const MutableCell auto &c, const T &value) {
339 return c = c.value() - value;
340 }
341
352 template <typename T>
353 T operator*=(const MutableCell auto &c, const T &value) {
354 return c = c.value() * value;
355 }
356
367 template <typename T>
368 T operator/=(const MutableCell auto &c, const T &value) {
369 return c = c.value() / value;
370 }
371
383 template <typename T>
384 T operator%=(const MutableCell auto &c, const T &value) {
385 return c = c.value() % value;
386 }
387
398 template <typename T>
399 T operator<<=(const MutableCell auto &c, const T &value) {
400 return c = c.value() << value;
401 }
402
413 template <typename T>
414 T operator>>=(const MutableCell auto &c, const T &value) {
415 return c = c.value() >> value;
416 }
417
429 template <typename T>
430 T operator&=(const MutableCell auto &c, const T &value) {
431 return c = c.value() & value;
432 }
433
445 template <typename T>
446 T operator^=(const MutableCell auto &c, const T &value) {
447 return c = c.value() ^ value;
448 }
449
461 template <typename T>
462 T operator|=(const MutableCell auto &c, const T &value) {
463 return c = c.value() | value;
464 }
465
476 auto operator++(const MutableCell auto &c) {
477 auto value = c.value();
478 c = ++value;
479
480 return value;
481 }
482
493 auto operator++(const MutableCell auto &c, int) {
494 auto old = c.value();
495
496 ++c;
497 return old;
498 }
499
510 auto operator--(const MutableCell auto &c) {
511 auto value = c.value();
512 c = --value;
513
514 return value;
515 }
516
527 auto operator--(const MutableCell auto &c, int) {
528 auto old = c.value();
529
530 --c;
531 return old;
532 }
533
534} // live_cells
535
536#endif /* LIVE_CELLS_MUTABLE_CELL_HPP */
static argument_tracker & global()
Definition tracker.hpp:99
Defer changes to the values of mutable cells.
Definition mutable_cell.hpp:44
Maintains the state of a stateful cell.
Definition cell_state.hpp:43
virtual void notify_will_update()
Notify the observers that the cell's value will change.
Definition live_cells.cpp:76
virtual void notify_update(bool did_change=true)
Notify the observers that the cell's value has changed.
Definition live_cells.cpp:92
cell_state(key_ref k)
Create a cell state with a given key.
Definition cell_state.hpp:53
A computed cell which determines its argument cells at runtime.
Definition dynamic_compute_cell.hpp:153
Dynamically type key container.
Definition keys.hpp:76
Defines the interface for a key which uniquely identifies a cell.
Definition keys.hpp:33
Maintains the state of a mutable_cell.
Definition mutable_cell.hpp:78
void add_to_batch()
Add this state to the list of mutable cells that were updating during the current batch update.
Definition mutable_cell.hpp:145
T value()
Get the value of the cell.
Definition mutable_cell.hpp:99
void value(T value)
Set the value of the cell and notify its observers.
Definition mutable_cell.hpp:108
mutable_cell_state(key_ref key, T value)
Create a mutable cell state with the value initialized to value.
Definition mutable_cell.hpp:89
void silent_set(T value)
Set the cell's value without notifying observers.
Definition mutable_cell.hpp:128
static bool is_batch_update()
Is a batch update of mutable cells currently in effect?
Definition mutable_cell.hpp:137
A stateful cell which can have its value set directly.
Definition mutable_cell.hpp:158
void value(T value) const
Set the value of the cell.
Definition mutable_cell.hpp:252
T value() const
Get the value of the cell.
Definition mutable_cell.hpp:240
mutable_cell(key_ref key, T value)
Create a mutable cell with an initial value.
Definition mutable_cell.hpp:197
T operator=(const T &value)
Set the value of the cell.
Definition mutable_cell.hpp:211
mutable_cell()
Create a mutable cell with a default initialized value.
Definition mutable_cell.hpp:174
T value_type
The type of value held by this cell.
Definition mutable_cell.hpp:168
T operator()() const
Get the value of the cell and track it as a dependency.
Definition mutable_cell.hpp:262
mutable_cell(T value)
Create a mutable cell with an initial value.
Definition mutable_cell.hpp:181
T operator=(const T &value) const
Set the value of the cell.
Definition mutable_cell.hpp:230
Base class for a cell with a state.
Definition stateful_cell.hpp:42
std::shared_ptr< mutable_cell_state< T > > state
Reference to the cell's state.
Definition stateful_cell.hpp:105
A key of uniquely identified by a single instance.
Definition keys.hpp:128
Concept defining a Cell that can have its value set.
Definition types.hpp:155
Definition boolean.hpp:26
constant_cell< T > value(const T &value)
Definition constant_cell.hpp:132
T operator&=(const MutableCell auto &c, const T &value)
Compute the bitwise and of the value of Cell c and value, and assign it to c.
Definition mutable_cell.hpp:430
T operator>>=(const MutableCell auto &c, const T &value)
Right shift the value of Cell c by value.
Definition mutable_cell.hpp:414
T operator<<=(const MutableCell auto &c, const T &value)
Left shift the value of Cell c by value.
Definition mutable_cell.hpp:399
T operator|=(const MutableCell auto &c, const T &value)
Compute the bitwise or of the value of Cell c and value, and assign it to c.
Definition mutable_cell.hpp:462
T operator+=(const MutableCell auto &c, const T &value)
Add value to the value of Cell c.
Definition mutable_cell.hpp:323
T operator%=(const MutableCell auto &c, const T &value)
Compute the remainder of the value of Cell c divided by value, and assign it to c.
Definition mutable_cell.hpp:384
void batch(F fn)
Batch changes to the values of mutable cells.
Definition mutable_cell.hpp:307
T operator/=(const MutableCell auto &c, const T &value)
Divide the value of Cell c by value.
Definition mutable_cell.hpp:368
mutable_cell< T > variable(T value)
Create a new mutable cell with an initial value.
Definition mutable_cell.hpp:276
T operator-=(const MutableCell auto &c, const T &value)
Subtract value from the value of Cell c.
Definition mutable_cell.hpp:338
T operator^=(const MutableCell auto &c, const T &value)
Compute the bitwise exclusive or of the value of Cell c and value, and assign it to c.
Definition mutable_cell.hpp:446
auto operator++(const MutableCell auto &c)
Increment the value of Cell c.
Definition mutable_cell.hpp:476
T operator*=(const MutableCell auto &c, const T &value)
Multiply the value of Cell c by value.
Definition mutable_cell.hpp:353
auto operator--(const MutableCell auto &c)
Decrement the value of Cell c.
Definition mutable_cell.hpp:510