Embedded Template Library 1.0
Loading...
Searching...
No Matches
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 "alignment.h"
36#include "placement_new.h"
37#include "static_assert.h"
38#include "utility.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 //***************************************************************************
64 //***************************************************************************
65 template <typename T>
66 explicit packet(T&& value)
67 {
68 typedef typename etl::types<T>::type type;
69
70 ETL_STATIC_ASSERT((etl::is_base_of<TBase, type>::value), "Unsupported type");
71 ETL_STATIC_ASSERT(sizeof(type) <= SIZE, "Unsupported size");
72 ETL_STATIC_ASSERT(etl::alignment_of<type>::value <= ALIGNMENT, "Unsupported alignment");
73
74 ::new (static_cast<type*>(data)) type(etl::forward<T>(value));
75 }
76#else
77 //***************************************************************************
80 //***************************************************************************
81 template <typename T>
82 explicit packet(const T& value)
83 {
84 ETL_STATIC_ASSERT((etl::is_base_of<TBase, T>::value), "Unsupported type");
85 ETL_STATIC_ASSERT(sizeof(T) <= SIZE, "Unsupported size");
86 ETL_STATIC_ASSERT(etl::alignment_of<T>::value <= ALIGNMENT, "Unsupported alignment");
87
88 ::new (static_cast<T*>(data)) T(value);
89 }
90#endif
91
92 //***************************************************************************
94 //***************************************************************************
96 {
97 static_cast<TBase*>(data)->~TBase();
98 }
99
100#if ETL_USING_CPP11
101 //***************************************************************************
104 //***************************************************************************
105 template <typename T>
106 packet& operator=(T&& value)
107 {
108 typedef typename etl::types<T>::type type;
109
110 ETL_STATIC_ASSERT((etl::is_base_of<TBase, type>::value), "Unsupported type");
111 ETL_STATIC_ASSERT(sizeof(type) <= SIZE, "Unsupported size");
112 ETL_STATIC_ASSERT(etl::alignment_of<type>::value <= ALIGNMENT, "Unsupported alignment");
113
114 static_cast<TBase*>(data)->~TBase();
115 ::new (static_cast<type*>(data)) type(etl::forward<T>(value));
116
117 return *this;
118 }
119#else
120 //***************************************************************************
123 //***************************************************************************
124 template <typename T>
125 packet& operator=(const T& value)
126 {
127 ETL_STATIC_ASSERT((etl::is_base_of<TBase, T>::value), "Unsupported type");
128 ETL_STATIC_ASSERT(sizeof(T) <= SIZE, "Unsupported size");
129 ETL_STATIC_ASSERT(etl::alignment_of<T>::value <= ALIGNMENT, "Unsupported alignment");
130
131 static_cast<TBase*>(data)->~TBase();
132 ::new (static_cast<T*>(data)) T(value);
133
134 return *this;
135 }
136#endif
137
138 //***************************************************************************
140 //***************************************************************************
141 TBase& get()
142 {
143 return *static_cast<TBase*>(data);
144 }
145
146 //***************************************************************************
148 //***************************************************************************
149 const TBase& get() const
150 {
151 return *static_cast<const TBase*>(data);
152 }
153
154 private:
155
156 packet(const packet& other);
157 packet& operator=(const packet& other);
158
159 //***************************************************************************
162 //***************************************************************************
164 };
165} // namespace etl
166
167#endif
TBase & get()
Get access to the contained object.
Definition packet.h:141
~packet()
Destructor.
Definition packet.h:95
packet & operator=(const T &value)
Definition packet.h:125
packet(const T &value)
Definition packet.h:82
const TBase & get() const
Get access to the contained object.
Definition packet.h:149
Definition packet.h:55
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1228
Definition alignment.h:253