Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
delegate_service.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2019 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
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
41
namespace
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
>
52
class
delegate_service
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
102
class
delegate_service
103
#endif
104
{
105
public
:
106
107
typedef
etl::delegate
<void(
size_t
)> delegate_type;
108
109
//*************************************************************************
112
//*************************************************************************
113
delegate_service
()
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
124
delegate_service
(
delegate_service
&&) ETL_DELETE;
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
//*************************************************************************
161
void
register_unhandled_delegate
(delegate_type
callback
)
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
array.h
etl::callback
Definition
callback.h:46
etl::delegate_service
Definition
delegate_service.h:104
etl::delegate_service::delegate_service
delegate_service()
Definition
delegate_service.h:113
etl::delegate_service::call
void call() const
Definition
delegate_service.h:172
etl::delegate_service::register_delegate
void register_delegate(size_t id, delegate_type callback)
Definition
delegate_service.h:149
etl::delegate_service::register_delegate
void register_delegate(delegate_type callback)
Definition
delegate_service.h:135
etl::delegate_service::register_unhandled_delegate
void register_unhandled_delegate(delegate_type callback)
Definition
delegate_service.h:161
etl::delegate_service::call
void call(const size_t id) const
Definition
delegate_service.h:184
etl::delegate
Declaration.
Definition
delegate_cpp03.h:191
delegate.h
etl
Definition
absolute.h:40
platform.h
include
etl
delegate_service.h
Generated on
for Embedded Template Library by
1.17.0