Embedded Template Library  1.0
delegate_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_DELEGATE_SERVICE_INCLUDED
32 #define ETL_DELEGATE_SERVICE_INCLUDED
33 
34 #include "platform.h"
35 #include "nullptr.h"
36 #include "static_assert.h"
37 #include "delegate.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  {
59  etl::delegate<void(size_t)> default_delegate = etl::delegate<void(size_t)>::create<delegate_service<RANGE, OFFSET>, &delegate_service<RANGE, OFFSET>::unhandled>(*this);
60 
61  lookup.fill(default_delegate);
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  //*************************************************************************
85  void register_delegate(const size_t id, etl::delegate<void(size_t)> callback)
86  {
87  if ((id >= OFFSET) && (id < (OFFSET + RANGE)))
88  {
89  lookup[id - OFFSET] = callback;
90  }
91  }
92 
93  //*************************************************************************
96  //*************************************************************************
98  {
99  unhandled_delegate = callback;
100  }
101 
102  //*************************************************************************
106  //*************************************************************************
107  template <const size_t ID>
108  void call()
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 call(const size_t id)
121  {
122  if ((id >= OFFSET) && (id < (OFFSET + RANGE)))
123  {
124  lookup[id - OFFSET](id);
125  }
126  else
127  {
128  if (unhandled_delegate.is_valid())
129  {
130  unhandled_delegate(id);
131  }
132  }
133  }
134 
135  private:
136 
137  //*************************************************************************
140  //*************************************************************************
141  void unhandled(size_t id)
142  {
143  if (unhandled_delegate.is_valid())
144  {
145  unhandled_delegate(id);
146  }
147  }
148 
150  etl::delegate<void(size_t)> unhandled_delegate;
151 
153  etl::array<etl::delegate<void(size_t)>, RANGE> lookup;
154  };
155 }
156 
157 #endif
Definition: callback.h:45
Definition: delegate_service.h:50
void register_unhandled_delegate(etl::delegate< void(size_t)> callback)
Definition: delegate_service.h:97
void call()
Definition: delegate_service.h:108
delegate_service()
Definition: delegate_service.h:57
void register_delegate(const size_t id, etl::delegate< void(size_t)> callback)
Definition: delegate_service.h:85
void register_delegate(etl::delegate< void(size_t)> callback)
Definition: delegate_service.h:71
void call(const size_t id)
Definition: delegate_service.h:120
Definition: delegate.h:91
void fill(parameter_t value)
Definition: array.h:341
Definition: array.h:87
Definition: absolute.h:37