Embedded Template Library 1.0
Loading...
Searching...
No Matches
callback_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_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
40namespace etl
41{
42 //***************************************************************************
47 //***************************************************************************
48 template <size_t Range, size_t Offset = 0U>
50 {
51 public:
52
53 //*************************************************************************
56 //*************************************************************************
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
69 callback_service& operator=(callback_service&&) ETL_DELETE;
70#endif
71
72 //*************************************************************************
77 //*************************************************************************
78 template <size_t Id>
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 //*************************************************************************
94 {
95 if ((id >= Offset) && (id < (Offset + Range)))
96 {
97 lookup[id - Offset] = &callback;
98 }
99 }
100
101 //*************************************************************************
104 //*************************************************************************
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
Definition callback_service.h:50
callback_service()
Definition callback_service.h:57
void register_callback(size_t id, etl::ifunction< size_t > &callback)
Definition callback_service.h:93
void register_callback(etl::ifunction< size_t > &callback)
Definition callback_service.h:79
void register_unhandled_callback(etl::ifunction< size_t > &callback)
Definition callback_service.h:105
void callback()
Definition callback_service.h:116
void callback(size_t id)
Definition callback_service.h:128
Definition function.h:54
Definition absolute.h:40