Iridescence 1.00
Embedded Graphic Framework
Loading...
Searching...
No Matches
ds_live_data.h
1/* Copyright (C) 2017 - 2024, Thornwave Labs Inc - All Rights Reserved.
2 * Unauthorized copying of this file, via any medium is strictly prohibited.
3 * Proprietary and confidential.
4 * Written by Razvan Turiac <razvan.turiac@thornwave.com>
5*/
6
7#ifndef _DS_LIVE_DATA_H
8#define _DS_LIVE_DATA_H
9
10
11#include <ds_view.h>
12
23template<typename T, size_t S = 8>
25{
26public:
30 DsLiveData(): mIsValid(false), mScreen(nullptr)
31 {
32 }
33
34 DsLiveData(const DsLiveData &rhs) = delete;
35 DsLiveData& operator=(const DsLiveData &rhs) = delete;
36
45 void observe(DsView* view, const lambda<bool(T&)> &callback)
46 {
47 mScreen = &view->getScreen();
48
49 const int32_t index = mObservers.findIndex([view](const Observer& item)
50 {
51 return view == item.view;
52 });
53
54 if (index < 0)
55 {
56 Observer obs;
57 obs.view = view;
58 obs.callback = callback;
59
60 mObservers.append(obs);
61
62 view->addStateObserver(this);
63
64 if (mIsValid)
65 {
66 if (callback(mValue))
67 mIsValid = false;
68 }
69 }
70 }
71
76 void remove(DsView* view)
77 {
78 view->removeStateObserver(this);
79
80 const int32_t index = mObservers.find([view](const Observer &item)
81 {
82 return view == item.view;
83 });
84
85 if (index >= 0)
86 mObservers.removeAtPos(index);
87
88 if (mObservers.size() == 0)
89 mScreen = nullptr;
90 }
91
92
97 void invalidate(void)
98 {
99 mIsValid = false;
100 }
101
102
106 void post(void)
107 {
108 mIsValid = true;
109
110 if (mScreen)
111 {
112 if (mScreen->isUiThread())
113 {
114 update();
115 }
116 else
117 {
118 mScreen->queueLambda([this]()
119 {
120 update();
121 });
122 }
123 }
124 }
125
130 void post(const T &value)
131 {
132 if (&value != &mValue)
133 {
134 mMutex.take();
135 mValue = value;
136 mMutex.give();
137 }
138
139 post();
140 }
141
142
147 T& value(void)
148 {
149 return mValue;
150 }
151
152private:
153 struct Observer
154 {
155 DsView* view;
156 lambda<bool(T&)> callback;
157 };
158
159 bool mIsValid;
160 T mValue;
161 static_list<S, Observer> mObservers;
162 DsScreen* mScreen;
163 DsMutex mMutex;
164
165 void onViewStateChange(DsView* view, bool isVisible) override final
166 {
167 if (mIsValid && isVisible)
168 {
169 //notify the observers
170 for(mObservers.startFirst(); mObservers.valid(); mObservers.goNext())
171 {
172 Observer &obs = mObservers.current();
173
174 if (obs.view == view)
175 {
176 mMutex.take();
177 T value = mValue;
178 mMutex.give();
179
180 if (obs.callback(value))
181 mIsValid = false;
182 break;
183 }
184 }
185 }
186 }
187
188 void update(void)
189 {
190 //notify the observers
191 for(mObservers.startFirst(); mObservers.valid(); mObservers.goNext())
192 {
193 Observer &obs = mObservers.current();
194 if (obs.view->isVisible())
195 {
196 mMutex.take();
197 T value = mValue;
198 mMutex.give();
199
200 if (obs.callback(value))
201 {
202 mIsValid = false;
203 break;
204 }
205 }
206 }
207 }
208};
209
210#endif
LiveData is an observable data holder class.
Definition ds_live_data.h:25
void post(void)
Posts the same value again.
Definition ds_live_data.h:106
void post(const T &value)
Posts a new value.
Definition ds_live_data.h:130
void invalidate(void)
Marks the contained data as invalid. The observer callbacks will not be called for updates until the ...
Definition ds_live_data.h:97
void observe(DsView *view, const lambda< bool(T &)> &callback)
Registers a new observer.
Definition ds_live_data.h:45
T & value(void)
Returns a reference to the contained value.
Definition ds_live_data.h:147
void remove(DsView *view)
Removes the registered observer.
Definition ds_live_data.h:76
DsLiveData()
Default constructor.
Definition ds_live_data.h:30
Definition ds_os.h:53
DsScreen is class of type DsView (inherits from DsView) that implements a physical screen.
Definition ds_screen.h:29
bool isUiThread(void) const
Returns true if the caller is running in the UI thread.
void queueLambda(const lambda< void(void), 64 > &lambda)
Queues a lambda to get executed in the UI thread event loop.
StateObserver is a pure virtual class implementing the interface for notifying state observers of cha...
Definition ds_view.h:47
DsView is a base class that represents any view on the screen.
Definition ds_view.h:40
DsScreen & getScreen(void)
Returns a reference to the screen to which this view belongs.
void removeStateObserver(StateObserver *observer)
Removes a state observer object.
void addStateObserver(StateObserver *observer)
Adds a state observer object.
Definition ds_types_lambda.h:24
void startFirst(void)
Initializes the internal element pointer to the first one in the list.
Definition ds_types_list.h:444
bool valid(void) const
Is the internal element pointer valid?
Definition ds_types_list.h:479
T & current(void)
Returns the element pointed by the internal pointer. If the internal pointer is not valid it will lik...
Definition ds_types_list.h:490
void goNext(void)
Advances the internal element pointer to the next one (going forward).
Definition ds_types_list.h:460
static_list is a specialization of the list class. It pre-allocates all elements in the list....
Definition ds_types_list.h:524