Embedded Template Library  1.0
callback_service.h
Go to the documentation of this file.
1 
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 jwellbelove
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 "nullptr.h"
36 #include "static_assert.h"
37 #include "function.h"
38 #include "array.h"
39 
40 namespace etl
41 {
42  //***************************************************************************
47  //***************************************************************************
48  template <const size_t RANGE, const 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 <const 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 <const 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(const 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 callback(const size_t id)
Definition: callback_service.h:120
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 register_callback(const size_t id, etl::ifunction< size_t > &callback)
Definition: callback_service.h:85
Definition: callback.h:45
void fill(parameter_t value)
Definition: array.h:341
Definition: array.h:87
Definition: function.h:236
Definition: absolute.h:37