Embedded Template Library 1.0
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 "nullptr.h"
36#include "static_assert.h"
37#include "function.h"
38#include "array.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 //*************************************************************************
69 //*************************************************************************
70 template <size_t ID>
72 {
73 ETL_STATIC_ASSERT(ID < (OFFSET + RANGE), "Callback Id out of range");
74 ETL_STATIC_ASSERT(ID >= OFFSET, "Callback Id out of range");
75
76 lookup[ID - OFFSET] = &callback;
77 }
78
79 //*************************************************************************
84 //*************************************************************************
86 {
87 if ((id >= OFFSET) && (id < (OFFSET + RANGE)))
88 {
89 lookup[id - OFFSET] = &callback;
90 }
91 }
92
93 //*************************************************************************
96 //*************************************************************************
98 {
99 p_unhandled = &callback;
100 }
101
102 //*************************************************************************
106 //*************************************************************************
107 template <size_t ID>
108 void callback()
109 {
110 ETL_STATIC_ASSERT(ID < (OFFSET + RANGE), "Callback Id out of range");
111 ETL_STATIC_ASSERT(ID >= OFFSET, "Callback Id out of range");
112
113 (*lookup[ID - OFFSET])(ID);
114 }
115
116 //*************************************************************************
119 //*************************************************************************
120 void callback(size_t id)
121 {
122 if ((id >= OFFSET) && (id < (OFFSET + RANGE)))
123 {
124 (*lookup[id - OFFSET])(id);
125 }
126 else
127 {
128 unhandled(id);
129 }
130 }
131
132 private:
133
134 //*************************************************************************
137 //*************************************************************************
138 void unhandled(size_t id)
139 {
140 if (p_unhandled != ETL_NULLPTR)
141 {
142 (*p_unhandled)(id);
143 }
144 }
145
148 size_t,
149 &callback_service<RANGE, OFFSET>::unhandled> unhandled_callback;
150
152 etl::ifunction<size_t>* p_unhandled;
153
155 etl::array<etl::ifunction<size_t>*, RANGE> lookup;
156 };
157}
158
159#endif
Definition: callback_service.h:50
void register_callback(size_t id, etl::ifunction< size_t > &callback)
Definition: callback_service.h:85
void register_unhandled_callback(etl::ifunction< size_t > &callback)
Definition: callback_service.h:97
void callback()
Definition: callback_service.h:108
void register_callback(etl::ifunction< size_t > &callback)
Definition: callback_service.h:71
callback_service()
Definition: callback_service.h:57
void callback(size_t id)
Definition: callback_service.h:120
Definition: callback.h:45
ETL_CONSTEXPR14 void fill(parameter_t value)
Definition: array.h:369
Definition: array.h:88
Definition: function.h:236
bitset_ext
Definition: absolute.h:38