Embedded Template Library 1.0
Loading...
Searching...
No Matches
delegate_observable.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2023 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_DELEGATE_OBSERVABLE_INCLUDED
32#define ETL_DELEGATE_OBSERVABLE_INCLUDED
33
34#include "platform.h"
35#include "array.h"
36#include "delegate.h"
37
38namespace etl
39{
40 //*********************************************************************
45 //*********************************************************************
46 template <typename TNotification, size_t Max_Observers>
48 {
49 public:
50
52 typedef etl::delegate<void(TNotification)> delegate_type;
53
54 private:
55
57
58 public:
59
61 typedef size_t size_type;
62
64 typedef TNotification notification_type;
65
66 //*****************************************************************
68 //*****************************************************************
69 ETL_CONSTEXPR14 delegate_observable()
70 : delegate_list()
71 , delegate_count(0)
72 {
73 }
74
75#if ETL_USING_CPP11
76 //*****************************************************************
78 //*****************************************************************
79 template <typename... TDelegate>
80 ETL_CONSTEXPR14 delegate_observable(TDelegate&&... delegates)
81 : delegate_list{etl::forward<TDelegate>(delegates)...}
82 , delegate_count(sizeof...(delegates))
83 {
84 ETL_STATIC_ASSERT(Max_Observers >= sizeof...(delegates), "Number of delegates exceeds maximum observers");
85 ETL_STATIC_ASSERT((etl::are_all_same<delegate_type, etl::decay_t<TDelegate>...>::value), "All delegates must be delegate_type");
86 }
87
88 //*****************************************************************
93 //*****************************************************************
94 template <typename... TDelegate>
95 ETL_CONSTEXPR14 delegate_observable(notification_type, TDelegate&&... delegates)
96 : delegate_observable(etl::forward<TDelegate>(delegates)...)
97 {
98 }
99#endif
100
101 //*****************************************************************
106 //*****************************************************************
107 ETL_CONSTEXPR14 bool add_observer(delegate_type observer)
108 {
109 for (size_t i = 0; i < Max_Observers; ++i)
110 {
111 if (delegate_list[i] == observer)
112 {
113 // Already there, so just return.
114 return true;
115 }
116 }
117
118 // If we get here, then we need to add it.
119 for (size_t i = 0; i < Max_Observers; ++i)
120 {
121 if (!delegate_list[i].is_valid())
122 {
123 // Found an empty slot, so add it.
124 delegate_list[i] = observer;
125 ++delegate_count;
126 return true;
127 }
128 }
129
130 return false;
131 }
132
133 //*****************************************************************
137 //*****************************************************************
138 ETL_CONSTEXPR14 bool remove_observer(const delegate_type& observer)
139 {
140 for (size_t i = 0; i < Max_Observers; ++i)
141 {
142 if (delegate_list[i] == observer)
143 {
144 // Clear it.
145 delegate_list[i].clear();
146 --delegate_count;
147 return true;
148 }
149 }
150
151 return false;
152 }
153
154 //*****************************************************************
156 //*****************************************************************
157 ETL_CONSTEXPR14 void clear_observers()
158 {
159 for (size_t i = 0; i < Max_Observers; ++i)
160 {
161 delegate_list[i].clear();
162 }
163
164 delegate_count = 0;
165 }
166
167 //*****************************************************************
169 //*****************************************************************
170 ETL_CONSTEXPR14 size_type number_of_observers() const
171 {
172 return delegate_count;
173 }
174
175 //*****************************************************************
179 //*****************************************************************
180 ETL_CONSTEXPR14 void notify_observers(notification_type n) const
181 {
182 if (delegate_count != 0)
183 {
184 for (size_t i = 0; i < Max_Observers; ++i)
185 {
186 delegate_list[i].call_if(n);
187 }
188 }
189 }
190
191 private:
192
194 DelegateList delegate_list;
195
197 size_t delegate_count;
198 };
199
200 //*************************************************************************
202 //*************************************************************************
203#if ETL_USING_CPP17
204 template <typename TNotification, typename... TDelegates>
205 delegate_observable(TNotification, TDelegates...) -> delegate_observable<TNotification, sizeof...(TDelegates)>;
206#endif
207} // namespace etl
208
209#endif
Declaration.
Definition delegate_cpp03.h:191
Definition array.h:88
ETL_CONSTEXPR14 void clear_observers()
Clear all observers.
Definition delegate_observable.h:157
TNotification notification_type
The type of the notification.
Definition delegate_observable.h:64
etl::delegate< void(TNotification)> delegate_type
The type of the observers.
Definition delegate_observable.h:52
ETL_CONSTEXPR14 delegate_observable()
Default constructor.
Definition delegate_observable.h:69
ETL_CONSTEXPR14 bool remove_observer(const delegate_type &observer)
Definition delegate_observable.h:138
ETL_CONSTEXPR14 void notify_observers(notification_type n) const
Definition delegate_observable.h:180
ETL_CONSTEXPR14 bool add_observer(delegate_type observer)
Definition delegate_observable.h:107
ETL_CONSTEXPR14 size_type number_of_observers() const
Returns the number of observers.
Definition delegate_observable.h:170
size_t size_type
The type for sizes.
Definition delegate_observable.h:61
Definition observer.h:379
bitset_ext
Definition absolute.h:40