Embedded Template Library 1.0
packet.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2017 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_PACKET_INCLUDED
32#define ETL_PACKET_INCLUDED
33
34#include "platform.h"
35#include "static_assert.h"
36#include "alignment.h"
37#include "utility.h"
38#include "placement_new.h"
39
40//*****************************************************************************
44//*****************************************************************************
45
46namespace etl
47{
48 //***************************************************************************
52 //***************************************************************************
53 template <typename TBase, size_t SIZE, size_t ALIGNMENT>
54 class packet
55 {
56 public:
57
58 typedef TBase base_t;
59
60#if ETL_USING_CPP11
61 //***************************************************************************
63 //***************************************************************************
64 template <typename T>
65 explicit packet(T&& value)
66 {
67 typedef typename etl::types<T>::type type;
68
69 ETL_STATIC_ASSERT((etl::is_base_of<TBase, type>::value), "Unsupported type");
70 ETL_STATIC_ASSERT(sizeof(type) <= SIZE, "Unsupported size");
71 ETL_STATIC_ASSERT(etl::alignment_of<type>::value <= ALIGNMENT, "Unsupported alignment");
72
73 ::new (static_cast<type*>(data)) type(etl::forward<T>(value));
74 }
75#else
76 //***************************************************************************
78 //***************************************************************************
79 template <typename T>
80 explicit packet(const T& value)
81 {
82 ETL_STATIC_ASSERT((etl::is_base_of<TBase, T>::value), "Unsupported type");
83 ETL_STATIC_ASSERT(sizeof(T) <= SIZE, "Unsupported size");
84 ETL_STATIC_ASSERT(etl::alignment_of<T>::value <= ALIGNMENT, "Unsupported alignment");
85
86 ::new (static_cast<T*>(data)) T(value);
87 }
88#endif
89
90 //***************************************************************************
92 //***************************************************************************
94 {
95 static_cast<TBase*>(data)->~TBase();
96 }
97
98#if ETL_USING_CPP11
99 //***************************************************************************
102 //***************************************************************************
103 template <typename T>
104 packet& operator =(T&& value)
105 {
106 typedef typename etl::types<T>::type type;
107
108 ETL_STATIC_ASSERT((etl::is_base_of<TBase, type>::value), "Unsupported type");
109 ETL_STATIC_ASSERT(sizeof(type) <= SIZE, "Unsupported size");
110 ETL_STATIC_ASSERT(etl::alignment_of<type>::value <= ALIGNMENT, "Unsupported alignment");
111
112 static_cast<TBase*>(data)->~TBase();
113 ::new (static_cast<type*>(data)) type(etl::forward<T>(value));
114
115 return *this;
116 }
117#else
118 //***************************************************************************
121 //***************************************************************************
122 template <typename T>
123 packet& operator =(const T& value)
124 {
125 ETL_STATIC_ASSERT((etl::is_base_of<TBase, T>::value), "Unsupported type");
126 ETL_STATIC_ASSERT(sizeof(T) <= SIZE, "Unsupported size");
127 ETL_STATIC_ASSERT(etl::alignment_of<T>::value <= ALIGNMENT, "Unsupported alignment");
128
129 static_cast<TBase*>(data)->~TBase();
130 ::new (static_cast<T*>(data)) T(value);
131
132 return *this;
133 }
134#endif
135
136 //***************************************************************************
138 //***************************************************************************
139 TBase& get()
140 {
141 return *static_cast<TBase*>(data);
142 }
143
144 //***************************************************************************
146 //***************************************************************************
147 const TBase& get() const
148 {
149 return *static_cast<const TBase*>(data);
150 }
151
152 private:
153
154 packet(const packet& other);
155 packet& operator =(const packet& other);
156
157 //***************************************************************************
160 //***************************************************************************
162 };
163}
164
165#endif
TBase & get()
Get access to the contained object.
Definition: packet.h:139
~packet()
Destructor.
Definition: packet.h:93
packet & operator=(const T &value)
Definition: packet.h:123
packet(const T &value)
Constructor that static asserts any types that do not conform to the max size and alignment.
Definition: packet.h:80
const TBase & get() const
Get access to the contained object.
Definition: packet.h:147
Definition: packet.h:55
add_rvalue_reference
Definition: type_traits_generator.h:1327
is_base_of
Definition: type_traits_generator.h:1252
bitset_ext
Definition: absolute.h:38
Definition: alignment.h:223