Live Cells C++
Reactive Programming for C++
Loading...
Searching...
No Matches
tracker.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_TRACKER_HPP
19#define LIVE_CELLS_TRACKER_HPP
20
21#include <functional>
22
23#include "observable.hpp"
24
25namespace live_cells {
26
32 public:
38 typedef std::function<void(const cell &arg)> track_fn;
39
45 template <typename T>
46 class tracker {
47 public:
59 tracker_(tracker),
60 previous(tracker.track_callback) {
61 tracker_.track_callback = track_fn(fn);
62 }
63
64 tracker(const tracker &) = delete;
66 tracker_(other.tracker_),
67 previous(other.previous) {
68 other.moved = true;
69 }
70
71 tracker &operator=(const tracker &) = delete;
72 tracker &operator=(tracker &&other) {
73 tracker_ = std::move(other.tracker_);
74 previous = std::move(other.previous);
75 }
76
81 if (!moved)
82 tracker_.track_callback = previous;
83 }
84
85 private:
87 argument_tracker &tracker_;
88
90 track_fn previous;
91
93 bool moved = false;
94 };
95
100 return instance_;
101 }
102
103 argument_tracker(const argument_tracker &other) = delete;
104 argument_tracker& operator=(const argument_tracker &other) = delete;
105
112 void track_argument(const cell &arg);
113
123 template <typename F>
125 return tracker<F>(track, *this);
126 }
127
128 private:
130 static argument_tracker instance_;
131
133 track_fn track_callback;
134
135 argument_tracker() = default;
136 };
137
138} // live_cells
139
140#endif /* LIVE_CELLS_TRACKER_HPP */
Definition tracker.hpp:46
~tracker()
Definition tracker.hpp:80
tracker(T fn, argument_tracker &tracker)
Definition tracker.hpp:58
Definition tracker.hpp:31
static argument_tracker & global()
Definition tracker.hpp:99
void track_argument(const cell &arg)
Definition live_cells.cpp:151
std::function< void(const cell &arg) track_fn)
Definition tracker.hpp:38
tracker< F > with_tracker(F track)
Definition tracker.hpp:124
Dynamically typed Cell container.
Definition observable.hpp:133
A computed cell which determines its argument cells at runtime.
Definition dynamic_compute_cell.hpp:153
Definition boolean.hpp:26