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 "nullptr.h"
38#include "static_assert.h"
39
40namespace etl
41{
42 //***************************************************************************
48 //***************************************************************************
49#if ETL_USING_CPP11 && !defined(ETL_DELEGATE_FORCE_CPP03_IMPLEMENTATION)
50 template <size_t Range, size_t Offset = 0U, const etl::delegate<void(size_t)>* Delegates = nullptr>
52 {
53 public:
54
55 typedef etl::delegate<void(size_t)> delegate_type;
56
57 //*************************************************************************
61 //*************************************************************************
62 template <size_t Id>
63 void call() const
64 {
65 ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
66 ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
67
68 Delegates[Id - Offset](Id);
69 }
70
71 //*************************************************************************
74 //*************************************************************************
75 void call(size_t id) const
76 {
77 if ((id >= Offset) && (id < (Offset + Range)))
78 {
79 // Call the delegate with the specified Id.
80 Delegates[id - Offset](id);
81 }
82 else
83 {
84 // Call the 'unhandled' delegate.
85 Delegates[Range](id);
86 }
87 }
88 };
89#endif
90
91 //***************************************************************************
96 //***************************************************************************
97 template <size_t Range, size_t Offset>
98#if ETL_USING_CPP11 && !defined(ETL_DELEGATE_FORCE_CPP03_IMPLEMENTATION)
99 class delegate_service<Range, Offset, nullptr>
100#else
102#endif
103 {
104 public:
105
106 typedef etl::delegate<void(size_t)> delegate_type;
107
108 //*************************************************************************
111 //*************************************************************************
113 {
114 delegate_type default_delegate = delegate_type::create<delegate_service<Range, Offset>, &delegate_service<Range, Offset>::unhandled>(*this);
115
116 lookup.fill(default_delegate);
117 }
118
119 //*************************************************************************
124 //*************************************************************************
125 template <size_t Id>
126 void register_delegate(delegate_type callback)
127 {
128 ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
129 ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
130
131 lookup[Id - Offset] = callback;
132 }
133
134 //*************************************************************************
139 //*************************************************************************
140 void register_delegate(size_t id, delegate_type callback)
141 {
142 if ((id >= Offset) && (id < (Offset + Range)))
143 {
144 lookup[id - Offset] = callback;
145 }
146 }
147
148 //*************************************************************************
151 //*************************************************************************
153 {
154 unhandled_delegate = callback;
155 }
156
157 //*************************************************************************
161 //*************************************************************************
162 template <size_t Id>
163 void call() const
164 {
165 ETL_STATIC_ASSERT(Id < (Offset + Range), "Callback Id out of range");
166 ETL_STATIC_ASSERT(Id >= Offset, "Callback Id out of range");
167
168 lookup[Id - Offset](Id);
169 }
170
171 //*************************************************************************
174 //*************************************************************************
175 void call(const size_t id) const
176 {
177 if ((id >= Offset) && (id < (Offset + Range)))
178 {
179 // Call the delegate with the specified Id.
180 lookup[id - Offset](id);
181 }
182 else
183 {
184 // Call the 'unhandled' delegate.
185 unhandled(id);
186 }
187 }
188
189 private:
190
191 //*************************************************************************
194 //*************************************************************************
195 void unhandled(size_t id) const
196 {
197 if (unhandled_delegate.is_valid())
198 {
199 unhandled_delegate(id);
200 }
201 }
202
204 delegate_type unhandled_delegate;
205
207 etl::array<delegate_type, Range> lookup;
208 };
209} // namespace etl
210
211#endif
Definition callback.h:46
Definition delegate_service.h:103
delegate_service()
Definition delegate_service.h:112
void call() const
Definition delegate_service.h:163
void register_delegate(size_t id, delegate_type callback)
Definition delegate_service.h:140
void register_delegate(delegate_type callback)
Definition delegate_service.h:126
void register_unhandled_delegate(delegate_type callback)
Definition delegate_service.h:152
void call(const size_t id) const
Definition delegate_service.h:175
Declaration.
Definition delegate_cpp03.h:191
bitset_ext
Definition absolute.h:40