Embedded Template Library 1.0
delegate_service.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) 2019 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_SERVICE_INCLUDED
32#define ETL_DELEGATE_SERVICE_INCLUDED
33
34#include "platform.h"
35#include "nullptr.h"
36#include "static_assert.h"
37#include "delegate.h"
38#include "array.h"
39
40namespace etl
41{
42 //***************************************************************************
48 //***************************************************************************
49#if ETL_USING_CPP11 && !defined(ETL_DELEGATE_FORCE_CPP03_IMPLEMENTATION)
50 template <size_t Range,
51 size_t Offset = 0U,
52 const etl::delegate<void(size_t)>* Delegates = nullptr>
53 class delegate_service
54 {
55 public:
56
57 typedef etl::delegate<void(size_t)> delegate_type;
58
59 //*************************************************************************
63 //*************************************************************************
64 template <size_t Id>
65 void call() const
66 {
67 ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
68 ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
69
70 Delegates[Id - Offset](Id);
71 }
72
73 //*************************************************************************
76 //*************************************************************************
77 void call(size_t id) const
78 {
79 if ((id >= Offset) && (id < (Offset + Range)))
80 {
81 // Call the delegate with the specified Id.
82 Delegates[id - Offset](id);
83 }
84 else
85 {
86 // Call the 'unhandled' delegate.
87 Delegates[Range](id);
88 }
89 }
90 };
91#endif
92
93 //***************************************************************************
98 //***************************************************************************
99 template <size_t Range,
100 size_t Offset>
101#if ETL_USING_CPP11 && !defined(ETL_DELEGATE_FORCE_CPP03_IMPLEMENTATION)
102 class delegate_service<Range, Offset, nullptr>
103#else
105#endif
106 {
107 public:
108
109 typedef etl::delegate<void(size_t)> delegate_type;
110
111 //*************************************************************************
114 //*************************************************************************
116 {
117 delegate_type default_delegate = delegate_type::create<delegate_service<Range, Offset>, &delegate_service<Range, Offset>::unhandled>(*this);
118
119 lookup.fill(default_delegate);
120 }
121
122 //*************************************************************************
127 //*************************************************************************
128 template <size_t Id>
130 {
131 ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
132 ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
133
134 lookup[Id - Offset] = callback;
135 }
136
137 //*************************************************************************
142 //*************************************************************************
144 {
145 if ((id >= Offset) && (id < (Offset + Range)))
146 {
147 lookup[id - Offset] = callback;
148 }
149 }
150
151 //*************************************************************************
154 //*************************************************************************
156 {
157 unhandled_delegate = callback;
158 }
159
160 //*************************************************************************
164 //*************************************************************************
165 template <size_t Id>
166 void call() const
167 {
168 ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
169 ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
170
171 lookup[Id - Offset](Id);
172 }
173
174 //*************************************************************************
177 //*************************************************************************
178 void call(const size_t id) const
179 {
180 if ((id >= Offset) && (id < (Offset + Range)))
181 {
182 // Call the delegate with the specified Id.
183 lookup[id - Offset](id);
184 }
185 else
186 {
187 // Call the 'unhandled' delegate.
188 unhandled(id);
189 }
190 }
191
192 private:
193
194 //*************************************************************************
197 //*************************************************************************
198 void unhandled(size_t id) const
199 {
200 if (unhandled_delegate.is_valid())
201 {
202 unhandled_delegate(id);
203 }
204 }
205
207 delegate_type unhandled_delegate;
208
211 };
212}
213
214#endif
215
Definition: callback.h:45
Definition: delegate_service.h:106
delegate_service()
Definition: delegate_service.h:115
void call() const
Definition: delegate_service.h:166
void register_delegate(size_t id, delegate_type callback)
Definition: delegate_service.h:143
void register_delegate(delegate_type callback)
Definition: delegate_service.h:129
void register_unhandled_delegate(delegate_type callback)
Definition: delegate_service.h:155
void call(const size_t id) const
Definition: delegate_service.h:178
Declaration.
Definition: delegate_cpp03.h:175
Definition: array.h:88
bitset_ext
Definition: absolute.h:38