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 "delegate.h"
36#include "array.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 //*****************************************************************
105 //*****************************************************************
106 ETL_CONSTEXPR14 bool add_observer(delegate_type observer)
107 {
108 for (size_t i = 0; i < Max_Observers; ++i)
109 {
110 if (delegate_list[i] == observer)
111 {
112 // Already there, so just return.
113 return true;
114 }
115 }
116
117 // If we get here, then we need to add it.
118 for (size_t i = 0; i < Max_Observers; ++i)
119 {
120 if (!delegate_list[i].is_valid())
121 {
122 // Found an empty slot, so add it.
123 delegate_list[i] = observer;
124 ++delegate_count;
125 return true;
126 }
127 }
128
129 return false;
130 }
131
132 //*****************************************************************
136 //*****************************************************************
137 ETL_CONSTEXPR14 bool remove_observer(const delegate_type& observer)
138 {
139 for (size_t i = 0; i < Max_Observers; ++i)
140 {
141 if (delegate_list[i] == observer)
142 {
143 // Clear it.
144 delegate_list[i].clear();
145 --delegate_count;
146 return true;
147 }
148 }
149
150 return false;
151 }
152
153 //*****************************************************************
155 //*****************************************************************
156 ETL_CONSTEXPR14 void clear_observers()
157 {
158 for (size_t i = 0; i < Max_Observers; ++i)
159 {
160 delegate_list[i].clear();
161 }
162
163 delegate_count = 0;
164 }
165
166 //*****************************************************************
168 //*****************************************************************
169 ETL_CONSTEXPR14 size_type number_of_observers() const
170 {
171 return delegate_count;
172 }
173
174 //*****************************************************************
178 //*****************************************************************
179 ETL_CONSTEXPR14 void notify_observers(notification_type n) const
180 {
181 if (delegate_count != 0)
182 {
183 for (size_t i = 0; i < Max_Observers; ++i)
184 {
185 delegate_list[i].call_if(n);
186 }
187 }
188 }
189
190 private:
191
193 DelegateList delegate_list;
194
196 size_t delegate_count;
197 };
198
199 //*************************************************************************
201 //*************************************************************************
202#if ETL_USING_CPP17
203 template <typename TNotification, typename... TDelegates>
204 delegate_observable(TNotification, TDelegates...) -> delegate_observable<TNotification, sizeof...(TDelegates)>;
205#endif
206}
207
208#endif
Declaration.
Definition delegate_cpp03.h:191
Definition array.h:88
ETL_CONSTEXPR14 void clear_observers()
Clear all observers.
Definition delegate_observable.h:156
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:137
ETL_CONSTEXPR14 void notify_observers(notification_type n) const
Definition delegate_observable.h:179
ETL_CONSTEXPR14 bool add_observer(delegate_type observer)
Definition delegate_observable.h:106
ETL_CONSTEXPR14 size_type number_of_observers() const
Returns the number of observers.
Definition delegate_observable.h:169
size_t size_type
The type for sizes.
Definition delegate_observable.h:61
Definition observer.h:363
bitset_ext
Definition absolute.h:39