Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
callback_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_CALLBACK_SERVICE_INCLUDED
32
#define ETL_CALLBACK_SERVICE_INCLUDED
33
34
#include "
platform.h
"
35
#include "
array.h
"
36
#include "
function.h
"
37
#include "
nullptr.h
"
38
#include "static_assert.h"
39
40
namespace
etl
41
{
42
//***************************************************************************
47
//***************************************************************************
48
template
<
size_t
Range,
size_t
Offset = 0U>
49
class
callback_service
50
{
51
public
:
52
53
//*************************************************************************
56
//*************************************************************************
57
callback_service
()
58
: unhandled_callback(*this)
59
, p_unhandled(ETL_NULLPTR)
60
{
61
lookup.fill(&unhandled_callback);
62
}
63
64
callback_service
(
const
callback_service
&) ETL_DELETE;
65
callback_service
& operator=(
const
callback_service
&) ETL_DELETE;
66
67
#if ETL_USING_CPP11
68
callback_service
(
callback_service
&&) ETL_DELETE;
69
callback_service
& operator=(
callback_service
&&) ETL_DELETE;
70
#endif
71
72
//*************************************************************************
77
//*************************************************************************
78
template
<
size_t
Id>
79
void
register_callback
(
etl::ifunction<size_t>
&
callback
)
80
{
81
ETL_STATIC_ASSERT(Id < (Offset + Range),
"Callback Id out of range"
);
82
ETL_STATIC_ASSERT(Id >= Offset,
"Callback Id out of range"
);
83
84
lookup[Id - Offset] = &
callback
;
85
}
86
87
//*************************************************************************
92
//*************************************************************************
93
void
register_callback
(
size_t
id
,
etl::ifunction<size_t>
&
callback
)
94
{
95
if
((
id
>= Offset) && (
id
< (Offset + Range)))
96
{
97
lookup[
id
- Offset] = &
callback
;
98
}
99
}
100
101
//*************************************************************************
104
//*************************************************************************
105
void
register_unhandled_callback
(
etl::ifunction<size_t>
&
callback
)
106
{
107
p_unhandled = &
callback
;
108
}
109
110
//*************************************************************************
114
//*************************************************************************
115
template
<
size_t
Id>
116
void
callback
()
117
{
118
ETL_STATIC_ASSERT(Id < (Offset + Range),
"Callback Id out of range"
);
119
ETL_STATIC_ASSERT(Id >= Offset,
"Callback Id out of range"
);
120
121
(*lookup[Id - Offset])(Id);
122
}
123
124
//*************************************************************************
127
//*************************************************************************
128
void
callback
(
size_t
id
)
129
{
130
if
((
id
>= Offset) && (
id
< (Offset + Range)))
131
{
132
(*lookup[
id
- Offset])(
id
);
133
}
134
else
135
{
136
unhandled(
id
);
137
}
138
}
139
140
private
:
141
142
//*************************************************************************
145
//*************************************************************************
146
void
unhandled(
size_t
id
)
147
{
148
if
(p_unhandled != ETL_NULLPTR)
149
{
150
(*p_unhandled)(id);
151
}
152
}
153
155
etl::function_mp<callback_service<Range, Offset>, size_t, &
callback_service<Range, Offset>::unhandled
> unhandled_callback;
156
158
etl::ifunction<size_t>* p_unhandled;
159
161
etl::array<etl::ifunction<size_t>*, Range> lookup;
162
};
163
}
// namespace etl
164
165
#endif
array.h
etl::callback_service
Definition
callback_service.h:50
etl::callback_service::callback_service
callback_service()
Definition
callback_service.h:57
etl::callback_service::register_callback
void register_callback(size_t id, etl::ifunction< size_t > &callback)
Definition
callback_service.h:93
etl::callback_service::register_callback
void register_callback(etl::ifunction< size_t > &callback)
Definition
callback_service.h:79
etl::callback_service::register_unhandled_callback
void register_unhandled_callback(etl::ifunction< size_t > &callback)
Definition
callback_service.h:105
etl::callback_service::callback
void callback()
Definition
callback_service.h:116
etl::callback_service::callback
void callback(size_t id)
Definition
callback_service.h:128
function.h
etl::ifunction
Definition
function.h:54
etl
Definition
absolute.h:40
nullptr.h
platform.h
include
etl
callback_service.h
Generated on
for Embedded Template Library by
1.17.0