Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
packet.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2017 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
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
46
namespace
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
//***************************************************************************
95
~packet
()
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
//***************************************************************************
163
typename
etl::aligned_storage<SIZE, ALIGNMENT>::type
data
;
164
};
165
}
// namespace etl
166
167
#endif
alignment.h
etl::packet::get
TBase & get()
Get access to the contained object.
Definition
packet.h:141
etl::packet::~packet
~packet()
Destructor.
Definition
packet.h:95
etl::packet::operator=
packet & operator=(const T &value)
Definition
packet.h:125
etl::packet::packet
packet(const T &value)
Definition
packet.h:82
etl::packet::get
const TBase & get() const
Get access to the contained object.
Definition
packet.h:149
etl::packet
Definition
packet.h:55
etl
Definition
absolute.h:40
etl::data
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition
iterator.h:1470
placement_new.h
platform.h
etl::aligned_storage::type
Definition
alignment.h:253
etl::type
utility.h
include
etl
packet.h
Generated on
for Embedded Template Library by
1.17.0