Embedded Template Library  1.0
message_packet_generator.h
1 /******************************************************************************
2 The MIT License(MIT)
3 
4 Embedded Template Library.
5 https://github.com/ETLCPP/etl
6 https://www.etlcpp.com
7 
8 Copyright(c) 2020 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 /*[[[cog
30 import cog
31 cog.outl("#if 0")
32 ]]]*/
33 /*[[[end]]]*/
34 #error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
35 /*[[[cog
36 import cog
37 cog.outl("#endif")
38 ]]]*/
39 /*[[[end]]]*/
40 
41 /*[[[cog
42 import cog
43 cog.outl("//***************************************************************************")
44 cog.outl("// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.")
45 cog.outl("//***************************************************************************")
46 ]]]*/
47 /*[[[end]]]*/
48 
49 //***************************************************************************
50 // To generate to header file, run this at the command line.
51 // Note: You will need Python and COG installed.
52 //
53 // python -m cogapp -d -e -omessage_packet.h -DHandlers=<n> message_packet_generator.h
54 // Where <n> is the number of messages to support.
55 //
56 // e.g.
57 // To generate handlers for up to 16 messages...
58 // python -m cogapp -d -e -omessage_packet.h -DHandlers=16 message_packet_generator.h
59 //
60 // See generate.bat
61 //***************************************************************************
62 
63 #ifndef ETL_MESSAGE_PACKET_INCLUDED
64 #define ETL_MESSAGE_PACKET_INCLUDED
65 
66 #include "message.h"
67 #include "error_handler.h"
68 #include "static_assert.h"
69 #include "largest.h"
70 #include "alignment.h"
71 #include "utility.h"
72 
73 #include <stdint.h>
74 
75 #undef ETL_FILE
76 #define ETL_FILE "55"
77 
78 namespace etl
79 {
80  /*[[[cog
81  import cog
82  ################################################
83  # The first definition for all of the messages.
84  ################################################
85  cog.outl("//***************************************************************************")
86  cog.outl("// The definition for all %s message types." % Handlers)
87  cog.outl("//***************************************************************************")
88  cog.out("template <")
89  cog.out("typename T1, ")
90  for n in range(2, int(Handlers)):
91  cog.out("typename T%s = void, " % n)
92  if n % 4 == 0:
93  cog.outl("")
94  cog.out(" ")
95  cog.outl("typename T%s = void>" % int(Handlers))
96  cog.outl("class message_packet")
97  cog.outl("{")
98  cog.outl("public:")
99  cog.outl("")
100  cog.outl(" //********************************************")
101  cog.outl(" message_packet()")
102  cog.outl(" : valid(false)")
103  cog.outl(" {")
104  cog.outl(" }")
105  cog.outl("")
106  cog.outl(" //********************************************")
107  cog.outl(" explicit message_packet(const etl::imessage& msg)")
108  cog.outl(" : valid(true)")
109  cog.outl(" {")
110  cog.outl(" add_new_message(msg);")
111  cog.outl(" }")
112  cog.outl("")
113  cog.outl("#if ETL_CPP11_SUPPORTED")
114  cog.outl(" //********************************************")
115  cog.outl(" explicit message_packet(etl::imessage&& msg)")
116  cog.outl(" : valid(true)")
117  cog.outl(" {")
118  cog.outl(" add_new_message(etl::move(msg));")
119  cog.outl(" }")
120  cog.outl("#endif")
121  cog.outl("")
122  cog.outl(" //**********************************************")
123  cog.outl(" message_packet(const message_packet& other)")
124  cog.outl(" : valid(other.is_valid())")
125  cog.outl(" {")
126  cog.outl(" if (valid)")
127  cog.outl(" {")
128  cog.outl(" add_new_message(other.get());")
129  cog.outl(" }")
130  cog.outl(" }")
131  cog.outl("")
132  cog.outl("#if ETL_CPP11_SUPPORTED")
133  cog.outl(" //**********************************************")
134  cog.outl(" message_packet(message_packet&& other)")
135  cog.outl(" : valid(other.is_valid())")
136  cog.outl(" {")
137  cog.outl(" if (valid)")
138  cog.outl(" {")
139  cog.outl(" add_new_message(etl::move(other.get()));")
140  cog.outl(" }")
141  cog.outl(" }")
142  cog.outl("#endif")
143  cog.outl("")
144  cog.outl(" //**********************************************")
145  cog.outl(" message_packet& operator =(const message_packet& rhs)")
146  cog.outl(" {")
147  cog.outl(" delete_current_message();")
148  cog.outl(" valid = rhs.is_valid();")
149  cog.outl(" if (valid)")
150  cog.outl(" {")
151  cog.outl(" add_new_message(rhs.get());")
152  cog.outl(" }")
153  cog.outl("")
154  cog.outl(" return *this;")
155  cog.outl(" }")
156  cog.outl("")
157  cog.outl("#if ETL_CPP11_SUPPORTED")
158  cog.outl(" //**********************************************")
159  cog.outl(" message_packet& operator =(message_packet&& rhs)")
160  cog.outl(" {")
161  cog.outl(" delete_current_message();")
162  cog.outl(" valid = rhs.is_valid();")
163  cog.outl(" if (valid)")
164  cog.outl(" {")
165  cog.outl(" add_new_message(etl::move(rhs.get()));")
166  cog.outl(" }")
167  cog.outl("")
168  cog.outl(" return *this;")
169  cog.outl(" }")
170  cog.outl("#endif")
171  cog.outl("")
172  cog.outl(" //********************************************")
173  cog.outl(" ~message_packet()")
174  cog.outl(" {")
175  cog.outl(" delete_current_message();")
176  cog.outl(" }")
177  cog.outl("")
178  cog.outl(" //********************************************")
179  cog.outl(" etl::imessage& get() ETL_NOEXCEPT")
180  cog.outl(" {")
181  cog.outl(" return *static_cast<etl::imessage*>(data);")
182  cog.outl(" }")
183  cog.outl("")
184  cog.outl(" //********************************************")
185  cog.outl(" const etl::imessage& get() const ETL_NOEXCEPT")
186  cog.outl(" {")
187  cog.outl(" return *static_cast<const etl::imessage*>(data);")
188  cog.outl(" }")
189  cog.outl("")
190  cog.outl(" //********************************************")
191  cog.outl(" bool is_valid() const")
192  cog.outl(" {")
193  cog.outl(" return valid;")
194  cog.outl(" }")
195  cog.outl("")
196  cog.outl(" enum")
197  cog.outl(" {")
198  cog.out(" SIZE = etl::largest<")
199  for n in range(1, int(Handlers)):
200  cog.out("T%d, " % n)
201  cog.outl("T%s>::size," % int(Handlers))
202  cog.out(" ALIGNMENT = etl::largest<")
203  for n in range(1, int(Handlers)):
204  cog.out("T%d, " % n)
205  cog.outl("T%s>::alignment" % int(Handlers))
206  cog.outl(" };")
207  cog.outl("")
208  cog.outl("private:")
209  cog.outl("")
210  cog.outl(" //********************************************")
211  cog.outl(" void delete_current_message()")
212  cog.outl(" {")
213  cog.outl(" if (valid)")
214  cog.outl(" {")
215  cog.outl(" etl::imessage* pmsg = static_cast<etl::imessage*>(data);")
216  cog.outl("")
217  cog.outl("#if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)")
218  cog.outl(" pmsg->~imessage();")
219  cog.outl("#else")
220  cog.outl(" size_t id = pmsg->message_id;")
221  cog.outl("")
222  cog.outl(" switch (id)")
223  cog.outl(" {")
224  for n in range(1, int(Handlers) + 1):
225  cog.outl(" case T%s::ID: static_cast<T%s*>(pmsg)->~T%s(); break;" % (n, n, n))
226  cog.outl(" default: assert(false); break;")
227  cog.outl(" }")
228  cog.outl(" #endif")
229  cog.outl(" }")
230  cog.outl(" }")
231  cog.outl("")
232  cog.outl(" //********************************************")
233  cog.outl(" void add_new_message(const etl::imessage& msg)")
234  cog.outl(" {")
235  cog.outl(" const size_t id = msg.message_id;")
236  cog.outl(" void* p = data;")
237  cog.outl("")
238  cog.outl(" switch (id)")
239  cog.outl(" {")
240  for n in range(1, int(Handlers) + 1):
241  cog.outl(" case T%d::ID: ::new (p) T%d(static_cast<const T%d&>(msg)); break;" %(n, n, n))
242  cog.outl(" default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;")
243  cog.outl(" }")
244  cog.outl(" }")
245  cog.outl("")
246  cog.outl("#if ETL_CPP11_SUPPORTED")
247  cog.outl(" //********************************************")
248  cog.outl(" void add_new_message(etl::imessage&& msg)")
249  cog.outl(" {")
250  cog.outl(" const size_t id = msg.message_id;")
251  cog.outl(" void* p = data;")
252  cog.outl("")
253  cog.outl(" switch (id)")
254  cog.outl(" {")
255  for n in range(1, int(Handlers) + 1):
256  cog.outl(" case T%d::ID: ::new (p) T%d(static_cast<T%d&&>(msg)); break;" %(n, n, n))
257  cog.outl(" default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;")
258  cog.outl(" }")
259  cog.outl(" }")
260  cog.outl("#endif")
261  cog.outl("")
262  cog.outl(" typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;")
263  cog.outl(" bool valid;")
264  cog.outl("};")
265 
266  ####################################
267  # All of the other specialisations.
268  ####################################
269  for n in range(int(Handlers) - 1, 0, -1):
270  cog.outl("")
271  cog.outl("//***************************************************************************")
272  if n == 1:
273  cog.outl("// Specialisation for %d message type." % n)
274  else:
275  cog.outl("// Specialisation for %d message types." % n)
276  cog.outl("//***************************************************************************")
277  cog.out("template <")
278  for t in range(1, n):
279  cog.out("typename T%s, " % t)
280  if t % 4 == 0:
281  cog.outl("")
282  cog.out(" ")
283  cog.outl("typename T%s>" % n)
284  cog.out("class message_packet<")
285  for t in range(1, n + 1):
286  cog.out("T%d, " % t)
287  if t % 16 == 0:
288  cog.outl("")
289  cog.out(" ")
290  for t in range(n + 1, int(Handlers)):
291  cog.out("void, ")
292  if t % 16 == 0:
293  cog.outl("")
294  cog.out(" ")
295  cog.outl("void>")
296  cog.outl("{")
297  cog.outl("public:")
298  cog.outl("")
299  cog.outl(" //********************************************")
300  cog.outl(" message_packet()")
301  cog.outl(" : valid(false)")
302  cog.outl(" {")
303  cog.outl(" }")
304  cog.outl("")
305  cog.outl(" //********************************************")
306  cog.outl(" explicit message_packet(const etl::imessage& msg)")
307  cog.outl(" : valid(true)")
308  cog.outl(" {")
309  cog.outl(" add_new_message(msg);")
310  cog.outl(" }")
311  cog.outl("")
312  cog.outl("#if ETL_CPP11_SUPPORTED")
313  cog.outl(" //********************************************")
314  cog.outl(" explicit message_packet(etl::imessage&& msg)")
315  cog.outl(" : valid(true)")
316  cog.outl(" {")
317  cog.outl(" add_new_message(etl::move(msg));")
318  cog.outl(" }")
319  cog.outl("#endif")
320  cog.outl("")
321  cog.outl(" //**********************************************")
322  cog.outl(" message_packet(const message_packet& other)")
323  cog.outl(" : valid(other.is_valid())")
324  cog.outl(" {")
325  cog.outl(" if (valid)")
326  cog.outl(" {")
327  cog.outl(" add_new_message(other.get());")
328  cog.outl(" }")
329  cog.outl(" }")
330  cog.outl("")
331  cog.outl("#if ETL_CPP11_SUPPORTED")
332  cog.outl(" //**********************************************")
333  cog.outl(" message_packet(message_packet&& other)")
334  cog.outl(" : valid(other.is_valid())")
335  cog.outl(" {")
336  cog.outl(" if (valid)")
337  cog.outl(" {")
338  cog.outl(" add_new_message(etl::move(other.get()));")
339  cog.outl(" }")
340  cog.outl(" }")
341  cog.outl("#endif")
342  cog.outl("")
343  cog.outl(" //**********************************************")
344  cog.outl(" message_packet& operator =(const message_packet& rhs)")
345  cog.outl(" {")
346  cog.outl(" delete_current_message();")
347  cog.outl(" valid = rhs.is_valid();")
348  cog.outl(" if (valid)")
349  cog.outl(" {")
350  cog.outl(" add_new_message(rhs.get());")
351  cog.outl(" }")
352  cog.outl("")
353  cog.outl(" return *this;")
354  cog.outl(" }")
355  cog.outl("")
356  cog.outl("#if ETL_CPP11_SUPPORTED")
357  cog.outl(" //**********************************************")
358  cog.outl(" message_packet& operator =(message_packet&& rhs)")
359  cog.outl(" {")
360  cog.outl(" delete_current_message();")
361  cog.outl(" valid = rhs.is_valid();")
362  cog.outl(" if (valid)")
363  cog.outl(" {")
364  cog.outl(" add_new_message(etl::move(rhs.get()));")
365  cog.outl(" }")
366  cog.outl("")
367  cog.outl(" return *this;")
368  cog.outl(" }")
369  cog.outl("#endif")
370  cog.outl("")
371  cog.outl(" //********************************************")
372  cog.outl(" ~message_packet()")
373  cog.outl(" {")
374  cog.outl(" delete_current_message();")
375  cog.outl(" }")
376  cog.outl("")
377  cog.outl(" //********************************************")
378  cog.outl(" etl::imessage& get() ETL_NOEXCEPT")
379  cog.outl(" {")
380  cog.outl(" return *static_cast<etl::imessage*>(data);")
381  cog.outl(" }")
382  cog.outl("")
383  cog.outl(" //********************************************")
384  cog.outl(" const etl::imessage& get() const ETL_NOEXCEPT")
385  cog.outl(" {")
386  cog.outl(" return *static_cast<const etl::imessage*>(data);")
387  cog.outl(" }")
388  cog.outl("")
389  cog.outl(" //********************************************")
390  cog.outl(" bool is_valid() const")
391  cog.outl(" {")
392  cog.outl(" return valid;")
393  cog.outl(" }")
394  cog.outl("")
395  cog.outl(" enum")
396  cog.outl(" {")
397  cog.out(" SIZE = etl::largest<")
398  for t in range(1, n):
399  cog.out("T%d, " % t)
400  cog.outl("T%s>::size," % n)
401  cog.out(" ALIGNMENT = etl::largest<")
402  for t in range(1, n):
403  cog.out("T%d, " % t)
404  cog.outl("T%s>::alignment" % n)
405  cog.outl(" };")
406  cog.outl("")
407  cog.outl("private:")
408  cog.outl("")
409  cog.outl(" //********************************************")
410  cog.outl(" void delete_current_message()")
411  cog.outl(" {")
412  cog.outl(" if (valid)")
413  cog.outl(" {")
414  cog.outl(" etl::imessage* pmsg = static_cast<etl::imessage*>(data);")
415  cog.outl("")
416  cog.outl("#if defined(ETL_MESSAGES_ARE_VIRTUAL) || defined(ETL_POLYMORPHIC_MESSAGES)")
417  cog.outl(" pmsg->~imessage();")
418  cog.outl("#else")
419  cog.outl(" size_t id = pmsg->message_id;")
420  cog.outl("")
421  cog.outl(" switch (id)")
422  cog.outl(" {")
423  for t in range(1, n + 1):
424  cog.outl(" case T%s::ID: static_cast<T%s*>(pmsg)->~T%s(); break;" % (t, t, t))
425  cog.outl(" default: assert(false); break;")
426  cog.outl(" }")
427  cog.outl(" #endif")
428  cog.outl(" }")
429  cog.outl(" }")
430  cog.outl("")
431  cog.outl(" //********************************************")
432  cog.outl(" void add_new_message(const etl::imessage& msg)")
433  cog.outl(" {")
434  cog.outl(" const size_t id = msg.message_id;")
435  cog.outl(" void* p = data;")
436  cog.outl("")
437  cog.outl(" switch (id)")
438  cog.outl(" {")
439  for t in range(1, n + 1):
440  cog.outl(" case T%d::ID: ::new (p) T%d(static_cast<const T%d&>(msg)); break;" %(t, t, t))
441  cog.outl(" default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;")
442  cog.outl(" }")
443  cog.outl(" }")
444  cog.outl("")
445  cog.outl("#if ETL_CPP11_SUPPORTED")
446  cog.outl(" //********************************************")
447  cog.outl(" void add_new_message(etl::imessage&& msg)")
448  cog.outl(" {")
449  cog.outl(" const size_t id = msg.message_id;")
450  cog.outl(" void* p = data;")
451  cog.outl("")
452  cog.outl(" switch (id)")
453  cog.outl(" {")
454  for t in range(1, n + 1):
455  cog.outl(" case T%d::ID: ::new (p) T%d(static_cast<T%d&&>(msg)); break;" %(t, t, t))
456  cog.outl(" default: ETL_ASSERT(false, ETL_ERROR(unhandled_message_exception)); break;")
457  cog.outl(" }")
458  cog.outl(" }")
459  cog.outl("#endif")
460  cog.outl("")
461  cog.outl(" typename etl::aligned_storage<SIZE, ALIGNMENT>::type data;")
462  cog.outl(" bool valid;")
463  cog.outl("};")
464  ]]]*/
465  /*[[[end]]]*/
466 }
467 
468 #undef ETL_FILE
469 
470 #endif
Definition: absolute.h:37