Embedded Template Library 1.0
Loading...
Searching...
No Matches
message.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2017 John Wellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29#ifndef ETL_MESSAGE_INCLUDED
30#define ETL_MESSAGE_INCLUDED
31
32#include "platform.h"
33#include "error_handler.h"
34#include "exception.h"
35#include "message_types.h"
36#include "static_assert.h"
37#include "type_traits.h"
38
39#include <stdint.h>
40
41namespace etl
42{
43 //***************************************************************************
44 class message_exception : public etl::exception
45 {
46 public:
47
48 message_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
49 : exception(reason_, file_name_, line_number_)
50 {
51 }
52 };
53
54 //***************************************************************************
55 class unhandled_message_exception : public etl::message_exception
56 {
57 public:
58
59 unhandled_message_exception(string_type file_name_, numeric_type line_number_)
60 : message_exception(ETL_ERROR_TEXT("message:unknown", ETL_MESSAGE_FILE_ID"A"), file_name_, line_number_)
61 {
62 }
63 };
64
66 {
67 };
68
69#if ETL_HAS_VIRTUAL_MESSAGES
70 //***************************************************************************
73 //***************************************************************************
75 {
76 public:
77
78 //***********************************
79 virtual ~imessage() ETL_NOEXCEPT {}
80
81 //***********************************
82 ETL_NODISCARD
83 virtual etl::message_id_t get_message_id() const ETL_NOEXCEPT = 0;
84 };
85
86 //***************************************************************************
89 //***************************************************************************
90 template <etl::message_id_t ID_, typename TBase = etl::imessage>
91 class message
92 : public TBase
93 , public etl::message_tag
94 {
95 public:
96
97 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TBase>::value), "TBase is not derived from etl::imessage");
98
99 typedef TBase base_type;
100
101 //***********************************
102 ETL_NODISCARD
103 virtual etl::message_id_t get_message_id() const ETL_NOEXCEPT ETL_OVERRIDE
104 {
105 return ID;
106 }
107
108 //***********************************
109 static ETL_CONSTANT etl::message_id_t ID = ID_;
110 };
111
112#else
113
114 //***************************************************************************
117 //***************************************************************************
118 class imessage
119 {
120 public:
121
122 //***********************************
123 ETL_NODISCARD
124 etl::message_id_t get_message_id() const ETL_NOEXCEPT
125 {
126 return id;
127 }
128
129 protected:
130
131 //***********************************
132 imessage(etl::message_id_t id_) ETL_NOEXCEPT
133 : id(id_)
134 {
135 }
136
137 //***********************************
138 imessage(const imessage& other) ETL_NOEXCEPT
139 : id(other.id)
140 {
141 }
142
143 //***********************************
144 imessage& operator=(const imessage& rhs) ETL_NOEXCEPT
145 {
146 id = rhs.id;
147 return *this;
148 }
149
150 //***********************************
152
153 private:
154
155 imessage() ETL_DELETE;
156 };
157
158 //***************************************************************************
161 //***************************************************************************
162 template <etl::message_id_t ID_, typename TBase = etl::imessage>
163 class message
164 : public TBase
165 , public etl::message_tag
166 {
167 public:
168
169 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TBase>::value), "TBase is not derived from etl::imessage");
170
171 typedef TBase base_type;
172
173 //***********************************
174 message() ETL_NOEXCEPT
175 : TBase(ID)
176 {
177 }
178
179 //***********************************
180 message(const message&) ETL_NOEXCEPT
181 : TBase(ID)
182 {
183 }
184
185 //***********************************
186 message& operator=(const message&) ETL_NOEXCEPT
187 {
188 return *this;
189 }
190
191 //***********************************
192 static ETL_CONSTANT etl::message_id_t ID = ID_;
193 };
194#endif
195
196 //***************************************************************************
198 //***************************************************************************
199 template <etl::message_id_t ID_, typename TBase>
201
202 //***************************************************************************
204 //***************************************************************************
205 template <typename T>
206 struct is_imessage : public etl::bool_constant< etl::is_same<etl::imessage, typename etl::remove_cvref<T>::type>::value>
207 {
208 };
209
210 //***************************************************************************
212 //***************************************************************************
213 template <typename T>
214 struct is_message : public etl::bool_constant< etl::is_base_of< etl::imessage, typename etl::remove_cvref<T>::type>::value>
215 {
216 };
217
218 //***************************************************************************
220 //***************************************************************************
221 template <typename T>
222 struct is_message_type : public etl::bool_constant< etl::is_base_of< etl::message_tag, typename etl::remove_cvref<T>::type>::value>
223 {
224 };
225
226 //***************************************************************************
228 //***************************************************************************
229 template <typename T>
230 struct is_message_base : public etl::bool_constant<etl::is_message<T>::value && !etl::is_message_type<T>::value>
231 {
232 };
233
234 //***************************************************************************
236 //***************************************************************************
237 template <typename T>
238 struct is_user_message_base : public etl::bool_constant<etl::is_message_base<T>::value && !etl::is_imessage<T>::value>
239 {
240 };
241
242#if ETL_USING_CPP17
243 //***************************************************************************
245 //***************************************************************************
246 template <typename T>
247 inline constexpr bool is_imessage_v = is_imessage<T>::value;
248
249 //***************************************************************************
251 //***************************************************************************
252 template <typename T>
253 inline constexpr bool is_message_v = is_message<T>::value;
254
255 //***************************************************************************
257 //***************************************************************************
258 template <typename T>
259 inline constexpr bool is_message_type_v = is_message_type<T>::value;
260
261 //***************************************************************************
263 //***************************************************************************
264 template <typename T>
265 inline constexpr bool is_message_base_v = is_message_base<T>::value;
266
267 //***************************************************************************
269 //***************************************************************************
270 template <typename T>
271 inline constexpr bool is_user_message_base_v = is_user_message_base<T>::value;
272#endif
273
274 //***************************************************************************
276 //***************************************************************************
277 template <typename T>
279 {
280 private:
281
282 ETL_STATIC_ASSERT(etl::is_message<T>::value, "T is not an ETL message");
283
284 typedef char yes;
285 struct no
286 {
287 char value[2];
288 };
289
290 template <typename U>
291 static yes test(char (*)[sizeof(&U::ID)]);
292
293 template <typename>
294 static no test(...);
295
296 public:
297
298 static const bool value = sizeof(test<typename etl::remove_cv<T>::type>(0)) == sizeof(yes);
299 };
300
301 template <typename T>
302 const bool has_message_id<T>::value;
303
304#if ETL_USING_CPP17
305 template <typename T>
306 inline constexpr bool has_message_id_v = has_message_id<T>::value;
307#endif
308
309#if ETL_USING_CPP11
310 namespace private_message
311 {
312 template <typename TMsg1, typename TMsg2, bool HasIDs>
313 struct compare_message_id_less_impl;
314
315 //**********************************************
316 // Compare the message ID of two messages.
317 // \tparam TMsg1 The first message type.
318 // \tparam TMsg2 The second message type.
319 // Only selected if both TMsg1 or TMsg2 don't have an ID.
320 //**********************************************
321 template <typename TMsg1, typename TMsg2>
322 struct compare_message_id_less_impl<TMsg1, TMsg2, false>
323 {
324 ETL_STATIC_ASSERT(etl::has_message_id<TMsg1>::value, "TMsg1 does not have an ID");
325 ETL_STATIC_ASSERT(etl::has_message_id<TMsg2>::value, "TMsg2 does not have an ID");
326 ETL_STATIC_ASSERT(etl::is_message_type<TMsg1>::value, "TMsg1 is not derived from etl::message<>");
327 ETL_STATIC_ASSERT(etl::is_message_type<TMsg2>::value, "TMsg2 is not derived from etl::message<>");
328 };
329
330 //**********************************************
331 // Compare the message ID of two messages.
332 // \tparam TMsg1 The first message type.
333 // \tparam TMsg2 The second message type.
334 // value is true if TMsg1::ID < TMsg2::ID.
335 // Only selected if both TMsg1 and TMsg2 have an ID.
336 //***********************************************
337 template <typename TMsg1, typename TMsg2>
338 struct compare_message_id_less_impl<TMsg1, TMsg2, true> : etl::bool_constant < TMsg1::ID< TMsg2::ID>
339 {
340 ETL_STATIC_ASSERT(etl::is_message_type<TMsg1>::value, "TMsg1 is not derived from etl::message<>");
341 ETL_STATIC_ASSERT(etl::is_message_type<TMsg2>::value, "TMsg2 is not derived from etl::message<>");
342 };
343 } // namespace private_message
344
345 //**********************************************
350 //**********************************************
351 template <typename TMsg1, typename TMsg2>
352 struct compare_message_id_less
353 : public private_message::compare_message_id_less_impl< TMsg1, TMsg2, has_message_id<TMsg1>::value && has_message_id<TMsg2>::value>
354 {
355 };
356#else
357 //**********************************************
362 //**********************************************
363 template <typename TMsg1, typename TMsg2>
364 struct compare_message_id_less : etl::bool_constant < TMsg1::ID< TMsg2::ID>
365 {
366 ETL_STATIC_ASSERT(etl::is_message_type<TMsg1>::value, "TMsg1 is not derived from etl::message<>");
367 ETL_STATIC_ASSERT(etl::is_message_type<TMsg2>::value, "TMsg2 is not derived from etl::message<>");
368 };
369#endif
370
371#if ETL_USING_CPP17
372 template <typename TMsg1, typename TMsg2>
373 inline constexpr bool compare_message_id_less_v = compare_message_id_less<TMsg1, TMsg2>::value;
374#endif
375} // namespace etl
376
377#endif
Definition message.h:75
Definition message.h:45
Definition message.h:66
Definition message.h:94
static ETL_CONSTANT etl::message_id_t ID
The message's static ID.
Definition message.h:109
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition exception.h:59
bitset_ext
Definition absolute.h:40
uint_least8_t message_id_t
Allow alternative type for message id.
Definition message_types.h:40
Definition type_traits.h:97
Detects presence of a static ID member.
Definition message.h:279
Is T an etl::imessage?
Definition message.h:207
Is T a base of etl::message<T>.
Definition message.h:231
Is T an etl::message<> or derived from etl::message<>.
Definition message.h:223
Is T ultimately derived from etl::imessage?
Definition message.h:215
Is T a user defined base of etl::message<T> and not an etl::imessage.
Definition message.h:239