Embedded Template Library 1.0
Loading...
Searching...
No Matches
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 "array.h"
36#include "delegate.h"
37#include "static_assert.h"
38
39#include <stddef.h>
40
41namespace etl
42{
43 //***************************************************************************
49 //***************************************************************************
50#if ETL_USING_CPP11 && !defined(ETL_DELEGATE_FORCE_CPP03_IMPLEMENTATION)
51 template <size_t Range, size_t Offset = 0U, const etl::delegate<void(size_t)>* Delegates = nullptr>
53 {
54 public:
55
56 typedef etl::delegate<void(size_t)> delegate_type;
57
58 //*************************************************************************
62 //*************************************************************************
63 template <size_t Id>
64 void call() const
65 {
66 ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
67 ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
68
69 Delegates[Id - Offset](Id);
70 }
71
72 //*************************************************************************
75 //*************************************************************************
76 void call(size_t id) const
77 {
78 if ((id >= Offset) && (id < (Offset + Range)))
79 {
80 // Call the delegate with the specified Id.
81 Delegates[id - Offset](id);
82 }
83 else
84 {
85 // Call the 'unhandled' delegate.
86 Delegates[Range](id);
87 }
88 }
89 };
90#endif
91
92 //***************************************************************************
97 //***************************************************************************
98 template <size_t Range, size_t Offset>
99#if ETL_USING_CPP11 && !defined(ETL_DELEGATE_FORCE_CPP03_IMPLEMENTATION)
100 class delegate_service<Range, Offset, nullptr>
101#else
103#endif
104 {
105 public:
106
107 typedef etl::delegate<void(size_t)> delegate_type;
108
109 //*************************************************************************
112 //*************************************************************************
114 {
115 delegate_type default_delegate = delegate_type::create<delegate_service<Range, Offset>, &delegate_service<Range, Offset>::unhandled>(*this);
116
117 lookup.fill(default_delegate);
118 }
119
120 delegate_service(const delegate_service&) ETL_DELETE;
121 delegate_service& operator=(const delegate_service&) ETL_DELETE;
122
123#if ETL_USING_CPP11
125 delegate_service& operator=(delegate_service&&) ETL_DELETE;
126#endif
127
128 //*************************************************************************
133 //*************************************************************************
134 template <size_t Id>
135 void register_delegate(delegate_type callback)
136 {
137 ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
138 ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
139
140 lookup[Id - Offset] = callback;
141 }
142
143 //*************************************************************************
148 //*************************************************************************
149 void register_delegate(size_t id, delegate_type callback)
150 {
151 if ((id >= Offset) && (id < (Offset + Range)))
152 {
153 lookup[id - Offset] = callback;
154 }
155 }
156
157 //*************************************************************************
160 //*************************************************************************
162 {
163 unhandled_delegate = callback;
164 }
165
166 //*************************************************************************
170 //*************************************************************************
171 template <size_t Id>
172 void call() const
173 {
174 ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
175 ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
176
177 lookup[Id - Offset](Id);
178 }
179
180 //*************************************************************************
183 //*************************************************************************
184 void call(const size_t id) const
185 {
186 if ((id >= Offset) && (id < (Offset + Range)))
187 {
188 // Call the delegate with the specified Id.
189 lookup[id - Offset](id);
190 }
191 else
192 {
193 // Call the 'unhandled' delegate.
194 unhandled(id);
195 }
196 }
197
198 private:
199
200 //*************************************************************************
203 //*************************************************************************
204 void unhandled(size_t id) const
205 {
206 if (unhandled_delegate.is_valid())
207 {
208 unhandled_delegate(id);
209 }
210 }
211
213 delegate_type unhandled_delegate;
214
216 etl::array<delegate_type, Range> lookup;
217 };
218} // namespace etl
219
220#endif
Definition callback.h:46
Definition delegate_service.h:104
delegate_service()
Definition delegate_service.h:113
void call() const
Definition delegate_service.h:172
void register_delegate(size_t id, delegate_type callback)
Definition delegate_service.h:149
void register_delegate(delegate_type callback)
Definition delegate_service.h:135
void register_unhandled_delegate(delegate_type callback)
Definition delegate_service.h:161
void call(const size_t id) const
Definition delegate_service.h:184
Declaration.
Definition delegate_cpp03.h:191
Definition absolute.h:40