Embedded Template Library  1.0
packet.h
Go to the documentation of this file.
1 
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 jwellbelove
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 "static_assert.h"
36 #include "alignment.h"
37 #include "utility.h"
38 #include "placement_new.h"
39 
40 #undef ETL_FILE
41 #define ETL_FILE "38"
42 
43 //*****************************************************************************
47 //*****************************************************************************
48 
49 namespace etl
50 {
51  //***************************************************************************
55  //***************************************************************************
56  template <typename TBase, size_t SIZE, size_t ALIGNMENT>
57  class packet
58  {
59  public:
60 
61  typedef TBase base_t;
62 
63 #if ETL_CPP11_SUPPORTED
64  //***************************************************************************
66  //***************************************************************************
67  template <typename T>
68  explicit packet(T&& value)
69  {
70  typedef typename etl::types<T>::type type;
71 
72  ETL_STATIC_ASSERT((etl::is_base_of<TBase, type>::value), "Unsupported type");
73  ETL_STATIC_ASSERT(sizeof(type) <= SIZE, "Unsupported size");
74  ETL_STATIC_ASSERT(etl::alignment_of<type>::value <= ALIGNMENT, "Unsupported alignment");
75 
76  ::new (static_cast<type*>(data)) type(etl::forward<T>(value));
77  }
78 #else
79  //***************************************************************************
81  //***************************************************************************
82  template <typename T>
83  explicit packet(const T& value)
84  {
85  ETL_STATIC_ASSERT((etl::is_base_of<TBase, T>::value), "Unsupported type");
86  ETL_STATIC_ASSERT(sizeof(T) <= SIZE, "Unsupported size");
87  ETL_STATIC_ASSERT(etl::alignment_of<T>::value <= ALIGNMENT, "Unsupported alignment");
88 
89  ::new (static_cast<T*>(data)) T(value);
90  }
91 #endif
92 
93  //***************************************************************************
95  //***************************************************************************
97  {
98  static_cast<TBase*>(data)->~TBase();
99  }
100 
101 #if ETL_CPP11_SUPPORTED
102  //***************************************************************************
105  //***************************************************************************
106  template <typename T>
107  packet& operator =(T&& value)
108  {
109  typedef typename etl::types<T>::type type;
110 
111  ETL_STATIC_ASSERT((etl::is_base_of<TBase, type>::value), "Unsupported type");
112  ETL_STATIC_ASSERT(sizeof(type) <= SIZE, "Unsupported size");
113  ETL_STATIC_ASSERT(etl::alignment_of<type>::value <= ALIGNMENT, "Unsupported alignment");
114 
115  static_cast<TBase*>(data)->~TBase();
116  ::new (static_cast<type*>(data)) type(etl::forward<T>(value));
117 
118  return *this;
119  }
120 #else
121  //***************************************************************************
124  //***************************************************************************
125  template <typename T>
126  packet& operator =(const T& value)
127  {
128  ETL_STATIC_ASSERT((etl::is_base_of<TBase, T>::value), "Unsupported type");
129  ETL_STATIC_ASSERT(sizeof(T) <= SIZE, "Unsupported size");
130  ETL_STATIC_ASSERT(etl::alignment_of<T>::value <= ALIGNMENT, "Unsupported alignment");
131 
132  static_cast<TBase*>(data)->~TBase();
133  ::new (static_cast<T*>(data)) T(value);
134 
135  return *this;
136  }
137 #endif
138 
139  //***************************************************************************
141  //***************************************************************************
142  TBase& get()
143  {
144  return *static_cast<TBase*>(data);
145  }
146 
147  //***************************************************************************
149  //***************************************************************************
150  const TBase& get() const
151  {
152  return *static_cast<const TBase*>(data);
153  }
154 
155  private:
156 
157  packet(const packet& other);
158  packet& operator =(const packet& other);
159 
160  //***************************************************************************
163  //***************************************************************************
165  };
166 }
167 
168 #undef ETL_FILE
169 
170 #endif
TBase & get()
Get access to the contained object.
Definition: packet.h:142
~packet()
Destructor.
Definition: packet.h:96
packet & operator=(const T &value)
Definition: packet.h:126
packet(const T &value)
Constructor that static asserts any types that do not conform to the max size and alignment.
Definition: packet.h:83
const TBase & get() const
Get access to the contained object.
Definition: packet.h:150
Definition: packet.h:58
add_rvalue_reference
Definition: type_traits_generator.h:1348
is_base_of
Definition: type_traits_generator.h:1289
Definition: absolute.h:37
Definition: alignment.h:118