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 "type_traits.h"
37#include "static_assert.h"
38
39#include <stdint.h>
40
41namespace etl
42{
43 //***************************************************************************
53
54 //***************************************************************************
56 {
57 public:
58
60 : message_exception(ETL_ERROR_TEXT("message:unknown", ETL_MESSAGE_FILE_ID"A"), file_name_, line_number_)
61 {
62 }
63 };
64
65 class message_tag {};
66
67#if ETL_HAS_VIRTUAL_MESSAGES
68 //***************************************************************************
71 //***************************************************************************
73 {
74 public:
75
76 //***********************************
77 virtual ~imessage() ETL_NOEXCEPT
78 {
79 }
80
81 //***********************************
82 ETL_NODISCARD virtual etl::message_id_t get_message_id() const ETL_NOEXCEPT = 0;
83 };
84
85 //***************************************************************************
88 //***************************************************************************
89 template <etl::message_id_t ID_, typename TBase = etl::imessage>
90 class message : public TBase, public etl::message_tag
91 {
92 public:
93
94 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TBase>::value), "TBase is not derived from etl::imessage");
95
96 typedef TBase base_type;
97
98 //***********************************
99 ETL_NODISCARD virtual etl::message_id_t get_message_id() const ETL_NOEXCEPT ETL_OVERRIDE
100 {
101 return ID;
102 }
103
104 //***********************************
105 static ETL_CONSTANT etl::message_id_t ID = ID_;
106 };
107
108#else
109
110 //***************************************************************************
113 //***************************************************************************
114 class imessage
115 {
116 public:
117
118 //***********************************
119 ETL_NODISCARD etl::message_id_t get_message_id() const ETL_NOEXCEPT
120 {
121 return id;
122 }
123
124 protected:
125
126 //***********************************
127 imessage(etl::message_id_t id_) ETL_NOEXCEPT
128 : id(id_)
129 {
130 }
131
132 //***********************************
133 imessage(const imessage& other) ETL_NOEXCEPT
134 : id(other.id)
135 {
136 }
137
138 //***********************************
139 imessage& operator =(const imessage& rhs) ETL_NOEXCEPT
140 {
141 id = rhs.id;
142 return *this;
143 }
144
145 //***********************************
147
148 private:
149
150 imessage() ETL_DELETE;
151 };
152
153 //***************************************************************************
156 //***************************************************************************
157 template <etl::message_id_t ID_, typename TBase = etl::imessage>
158 class message : public TBase, public etl::message_tag
159 {
160 public:
161
162 ETL_STATIC_ASSERT((etl::is_base_of<etl::imessage, TBase>::value), "TBase is not derived from etl::imessage");
163
164 typedef TBase base_type;
165
166 //***********************************
167 message() ETL_NOEXCEPT
168 : TBase(ID)
169 {
170 }
171
172 //***********************************
173 message(const message&) ETL_NOEXCEPT
174 : TBase(ID)
175 {
176 }
177
178 //***********************************
179 message& operator =(const message&) ETL_NOEXCEPT
180 {
181 return *this;
182 }
183
184 //***********************************
185 static ETL_CONSTANT etl::message_id_t ID = ID_;
186 };
187#endif
188
189 //***************************************************************************
191 //***************************************************************************
192 template <etl::message_id_t ID_, typename TBase>
194
195 //***************************************************************************
197 //***************************************************************************
198 template <typename T>
199 struct is_imessage : public etl::bool_constant<etl::is_same<etl::imessage, typename etl::remove_cvref<T>::type>::value>
200 {
201 };
202
203 //***************************************************************************
205 //***************************************************************************
206 template <typename T>
207 struct is_message : public etl::bool_constant<etl::is_base_of<etl::imessage, typename etl::remove_cvref<T>::type>::value>
208 {
209 };
210
211 //***************************************************************************
213 //***************************************************************************
214 template <typename T>
215 struct is_message_type : public etl::bool_constant<etl::is_base_of<etl::message_tag, typename etl::remove_cvref<T>::type>::value>
216 {
217 };
218
219 //***************************************************************************
221 //***************************************************************************
222 template <typename T>
223 struct is_message_base : public etl::bool_constant<etl::is_message<T>::value && !etl::is_message_type<T>::value>
224 {
225 };
226
227 //***************************************************************************
229 //***************************************************************************
230 template <typename T>
231 struct is_user_message_base : public etl::bool_constant<etl::is_message_base<T>::value && !etl::is_imessage<T>::value>
232 {
233 };
234
235#if ETL_USING_CPP17
236 //***************************************************************************
238 //***************************************************************************
239 template <typename T>
240 inline constexpr bool is_imessage_v = is_imessage<T>::value;
241
242 //***************************************************************************
244 //***************************************************************************
245 template <typename T>
246 inline constexpr bool is_message_v = is_message<T>::value;
247
248 //***************************************************************************
250 //***************************************************************************
251 template <typename T>
252 inline constexpr bool is_message_type_v = is_message_type<T>::value;
253
254 //***************************************************************************
256 //***************************************************************************
257 template <typename T>
258 inline constexpr bool is_message_base_v = is_message_base<T>::value;
259
260 //***************************************************************************
262 //***************************************************************************
263 template <typename T>
265#endif
266}
267
268#endif
Definition message.h:73
Definition message.h:45
Definition message.h:65
Definition message.h:91
static ETL_CONSTANT etl::message_id_t ID
The message's static ID.
Definition message.h:105
Definition message.h:56
Definition exception.h:47
is_base_of
Definition type_traits_generator.h:1247
bitset_ext
Definition absolute.h:38
uint_least8_t message_id_t
Allow alternative type for message id.
Definition message_types.h:40
Definition type_traits_generator.h:844
Is T an etl::imessage?
Definition message.h:200
Is T a base of etl::message<T>
Definition message.h:224
Is T an etl::message<> or derived from etl::message<>
Definition message.h:216
Is T ultimately derived from etl::imessage?
Definition message.h:208
Is T a user defined base of etl::message<T> and not an etl::imessage.
Definition message.h:232
pair holds two objects of arbitrary type
Definition utility.h:164