Embedded Template Library 1.0
Loading...
Searching...
No Matches
factory.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2017 jwellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29#ifndef __ETL_FACTORY__
30#define __ETL_FACTORY__
31
32#include <stdint.h>
33#include <utility>
34
35#include "platform.h"
36#include "alignment.h"
37#include "error_handler.h"
38#include "exception.h"
39#include "largest.h"
40#include "pool.h"
41#include "static_assert.h"
42#include "type_lookup.h"
43#include "type_traits.h"
44
45#if defined(ETL_COMPILER_GCC)
46 #warning THIS CLASS IS DEPRECATED!USE VARIANT_POOL INSTEAD.
47#elif defined(ETL_COMPILER_MICROSOFT)
48 #pragma message("THIS CLASS IS DEPRECATED! USE VARIANT_POOL INSTEAD.")
49#endif
50
51#undef ETL_FILE
52#define ETL_FILE "40"
53
54namespace etl
55{
56 //***************************************************************************
57 class factory_exception : public etl::exception
58 {
59 public:
60
61 factory_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
62 : exception(reason_, file_name_, line_number_)
63 {
64 }
65 };
66
67 //***************************************************************************
68 class factory_cannot_create : public etl::factory_exception
69 {
70 public:
71
72 factory_cannot_create(string_type file_name_, numeric_type line_number_)
73 : factory_exception(ETL_ERROR_TEXT("factory:cannot create", ETL_FILE"A"), file_name_, line_number_)
74 {
75 }
76 };
77
78 //***************************************************************************
79 class factory_did_not_create : public etl::factory_exception
80 {
81 public:
82
83 factory_did_not_create(string_type file_name_, numeric_type line_number_)
84 : factory_exception(ETL_ERROR_TEXT("factory:did not create", ETL_FILE"B"), file_name_, line_number_)
85 {
86 }
87 };
88
89 //***************************************************************************
90 template < const size_t MAX_SIZE_, typename T1, typename T2 = etl::type_id_pair<etl::null_type, -2>,
91 typename T3 = etl::type_id_pair<etl::null_type, -3>, typename T4 = etl::type_id_pair<etl::null_type, -4>,
92 typename T5 = etl::type_id_pair<etl::null_type, -5>, typename T6 = etl::type_id_pair<etl::null_type, -6>,
93 typename T7 = etl::type_id_pair<etl::null_type, -7>, typename T8 = etl::type_id_pair<etl::null_type, -8>,
94 typename T9 = etl::type_id_pair<etl::null_type, -9>, typename T10 = etl::type_id_pair<etl::null_type, -10>,
95 typename T11 = etl::type_id_pair<etl::null_type, -11>, typename T12 = etl::type_id_pair<etl::null_type, -12>,
96 typename T13 = etl::type_id_pair<etl::null_type, -13>, typename T14 = etl::type_id_pair<etl::null_type, -14>,
97 typename T15 = etl::type_id_pair<etl::null_type, -15>, typename T16 = etl::type_id_pair<etl::null_type, -16> >
98 class factory
99 {
100 private:
101
102 typedef typename T1::type TT1;
103 typedef typename T2::type TT2;
104 typedef typename T3::type TT3;
105 typedef typename T4::type TT4;
106 typedef typename T5::type TT5;
107 typedef typename T6::type TT6;
108 typedef typename T7::type TT7;
109 typedef typename T8::type TT8;
110 typedef typename T9::type TT9;
111 typedef typename T10::type TT10;
112 typedef typename T11::type TT11;
113 typedef typename T12::type TT12;
114 typedef typename T13::type TT13;
115 typedef typename T14::type TT14;
116 typedef typename T15::type TT15;
117 typedef typename T16::type TT16;
118
120
121 public:
122
123 static const size_t MAX_SIZE = MAX_SIZE_;
124
125 //*************************************************************************
127 //*************************************************************************
129
130#if !ETL_CPP11_SUPPORTED
131 //*************************************************************************
133 //*************************************************************************
134 template <typename T>
136 {
137 STATIC_ASSERT((etl::is_one_of<T, TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16>::value),
138 "Unsupported type");
139
140 T* p = nullptr;
141
142 if (pool.full())
143 {
144 ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create));
145 }
146 else
147 {
148 p = pool.template allocate<T>();
149
150 if (p != nullptr)
151 {
152 new (p) T();
153 }
154 }
155
156 return p;
157 }
158
159 //*************************************************************************
161 //*************************************************************************
162 template <typename T, typename TP1>
163 T* create_from_type(const TP1& p1)
164 {
165 STATIC_ASSERT((etl::is_one_of<T, TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16>::value),
166 "Unsupported type");
167
168 T* p = nullptr;
169
170 if (pool.full())
171 {
172 ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create));
173 }
174 else
175 {
176 p = pool.template allocate<T>();
177
178 if (p != nullptr)
179 {
180 new (p) T(p1);
181 }
182 }
183
184 return p;
185 }
186
187 //*************************************************************************
189 //*************************************************************************
190 template <typename T, typename TP1, typename TP2>
191 T* create_from_type(const TP1& p1, const TP2& p2)
192 {
193 STATIC_ASSERT((etl::is_one_of<T, TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16>::value),
194 "Unsupported type");
195
196 T* p = nullptr;
197
198 if (pool.full())
199 {
200 ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create));
201 }
202 else
203 {
204 p = pool.template allocate<T>();
205
206 if (p != nullptr)
207 {
208 new (p) T(p1, p2);
209 }
210 }
211
212 return p;
213 }
214
215 //*************************************************************************
217 //*************************************************************************
218 template <typename T, typename TP1, typename TP2, typename TP3>
219 T* create_from_type(const TP1& p1, const TP2& p2, const TP3& p3)
220 {
221 STATIC_ASSERT((etl::is_one_of<T, TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16>::value),
222 "Unsupported type");
223
224 T* p = nullptr;
225
226 if (pool.full())
227 {
228 ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create));
229 }
230 else
231 {
232 p = pool.template allocate<T>();
233
234 if (p != nullptr)
235 {
236 new (p) T(p1, p2, p3);
237 }
238 }
239
240 return p;
241 }
242
243 //*************************************************************************
245 //*************************************************************************
246 template <typename T, typename TP1, typename TP2, typename TP3, typename TP4>
247 T* create_from_type(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
248 {
249 STATIC_ASSERT((etl::is_one_of<T, TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16>::value),
250 "Unsupported type");
251
252 T* p = nullptr;
253
254 if (pool.full())
255 {
256 ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create));
257 }
258 else
259 {
260 p = pool.template allocate<T>();
261
262 if (p != nullptr)
263 {
264 new (p) T(p1, p2, p3, p4);
265 }
266 }
267
268 return p;
269 }
270
271 //*************************************************************************
273 //*************************************************************************
274 template <size_t ID>
275 typename lookup_t::template type_from_id<ID>::type* create_from_id()
276 {
277 typedef typename lookup_t::template type_from_id<ID>::type type;
278 STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
279 return create_from_type<type>();
280 }
281
282 //*************************************************************************
284 //*************************************************************************
285 template <size_t ID, typename TP1>
286 typename lookup_t::template type_from_id<ID>::type* create_from_id(const TP1& p1)
287 {
288 typedef typename lookup_t::template type_from_id<ID>::type type;
289 STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
290 return create_from_type<type>(p1);
291 }
292
293 //*************************************************************************
295 //*************************************************************************
296 template <size_t ID, typename TP1, typename TP2>
297 typename lookup_t::template type_from_id<ID>::type* create_from_id(const TP1& p1, const TP2& p2)
298 {
299 typedef typename lookup_t::template type_from_id<ID>::type type;
300 STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
301 return create_from_type<type>(p1, p2);
302 }
303
304 //*************************************************************************
306 //*************************************************************************
307 template <size_t ID, typename TP1, typename TP2, typename TP3>
308 typename lookup_t::template type_from_id<ID>::type* create_from_id(const TP1& p1, const TP2& p2, const TP3& p3)
309 {
310 typedef typename lookup_t::template type_from_id<ID>::type type;
311 STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
312 return create_from_type<type>(p1, p2, p3);
313 }
314
315 //*************************************************************************
317 //*************************************************************************
318 template <size_t ID, typename TP1, typename TP2, typename TP3, typename TP4>
319 typename lookup_t::template type_from_id<ID>::type* create_from_id(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
320 {
321 typedef typename lookup_t::template type_from_id<ID>::type type;
322 STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
323 return create_from_type<type>(p1, p2, p3, p4);
324 }
325#else
326 //*************************************************************************
328 //*************************************************************************
329 template <typename T, typename... Args>
330 T* create_from_type(Args&&... args)
331 {
332 STATIC_ASSERT((etl::is_one_of<T, TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16>::value),
333 "Unsupported type");
334
335 T* p = nullptr;
336
337 if (pool.full())
338 {
339 ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create));
340 }
341 else
342 {
343 p = pool.template allocate<T>();
344
345 if (p != nullptr)
346 {
347 new (p) T(std::forward<Args>(args)...);
348 }
349 }
350
351 return p;
352 }
353
354 //*************************************************************************
356 //*************************************************************************
357 template <size_t ID, typename... Args>
358 typename lookup_t::template type_from_id<ID>::type* create_from_id(Args&&... args)
359 {
360 typedef typename lookup_t::template type_from_id<ID>::type type;
361 STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
362 return create_from_type<type>(std::forward<Args>(args)...);
363 }
364#endif
365
366 //*************************************************************************
368 //*************************************************************************
369 template <typename T>
370 bool destroy(const T* const p)
371 {
372 STATIC_ASSERT(
373 (etl::is_one_of<T, TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16>::value
374 || etl::is_base_of<T, TT1>::value || etl::is_base_of<T, TT2>::value || etl::is_base_of<T, TT3>::value || etl::is_base_of<T, TT4>::value
375 || etl::is_base_of<T, TT5>::value || etl::is_base_of<T, TT6>::value || etl::is_base_of<T, TT7>::value || etl::is_base_of<T, TT8>::value
376 || etl::is_base_of<T, TT9>::value || etl::is_base_of<T, TT10>::value || etl::is_base_of<T, TT11>::value || etl::is_base_of<T, TT12>::value
377 || etl::is_base_of<T, TT13>::value || etl::is_base_of<T, TT14>::value || etl::is_base_of<T, TT15>::value || etl::is_base_of<T, TT16>::value),
378 "Invalid type");
379
380 p->~T();
381
382 void* vp = reinterpret_cast<char*>(const_cast<T*>(p));
383
384 if (pool.is_in_pool(vp))
385 {
386 pool.release(vp);
387 return true;
388 }
389 else
390 {
391 ETL_ASSERT(false, ETL_ERROR(factory_did_not_create));
392 return false;
393 }
394 }
395
396 //*************************************************************************
398 //*************************************************************************
399 size_t max_size() const
400 {
401 return MAX_SIZE;
402 }
403
404 //*************************************************************************
406 //*************************************************************************
407 size_t available() const
408 {
409 return pool.available();
410 }
411
412 //*************************************************************************
414 //*************************************************************************
415 size_t size() const
416 {
417 return pool.size();
418 }
419
420 //*************************************************************************
423 //*************************************************************************
424 bool empty() const
425 {
426 return pool.empty();
427 }
428
429 //*************************************************************************
432 //*************************************************************************
433 bool full() const
434 {
435 return pool.full();
436 }
437
438 private:
439
440 factory(const factory&);
441 factory& operator=(const factory&);
442
443 // The pool.
445 etl::largest<TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16>::alignment, MAX_SIZE>
446 pool;
447 };
448} // namespace etl
449
450#undef ETL_FILE
451
452#endif
Definition factory.h:69
Definition factory.h:80
Definition factory.h:58
Definition factory.h:99
T * create_from_type(const TP1 &p1, const TP2 &p2, const TP3 &p3, const TP4 &p4)
Creates the object. Four parameter constructor.
Definition factory.h:247
lookup_t::template type_from_id< ID >::type * create_from_id(const TP1 &p1, const TP2 &p2)
Creates the object from an index. Two parameter constructor.
Definition factory.h:297
factory()
Default constructor.
Definition factory.h:128
lookup_t::template type_from_id< ID >::type * create_from_id(const TP1 &p1)
Creates the object from an index. One parameter constructor.
Definition factory.h:286
bool empty() const
Definition factory.h:424
bool full() const
Definition factory.h:433
T * create_from_type(const TP1 &p1)
Creates the object. One parameter constructor.
Definition factory.h:163
T * create_from_type(const TP1 &p1, const TP2 &p2)
Creates the object. Two parameter constructor.
Definition factory.h:191
lookup_t::template type_from_id< ID >::type * create_from_id(const TP1 &p1, const TP2 &p2, const TP3 &p3, const TP4 &p4)
Creates the object from an index. Three parameter constructor.
Definition factory.h:319
size_t available() const
Returns the number of free items in the factory.
Definition factory.h:407
lookup_t::template type_from_id< ID >::type * create_from_id()
Creates the object from an index. Default constructor.
Definition factory.h:275
bool destroy(const T *const p)
Destroys the object.
Definition factory.h:370
T * create_from_type()
Creates the object. Default constructor.
Definition factory.h:135
size_t size() const
Returns the number of allocated items in the factory.
Definition factory.h:415
lookup_t::template type_from_id< ID >::type * create_from_id(const TP1 &p1, const TP2 &p2, const TP3 &p3)
Creates the object from an index. Three parameter constructor.
Definition factory.h:308
T * create_from_type(const TP1 &p1, const TP2 &p2, const TP3 &p3)
Creates the object. Three parameter constructor.
Definition factory.h:219
size_t max_size() const
Returns the maximum number of items in the factory.
Definition factory.h:399
Definition null_type.h:40
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition exception.h:59
bool full() const
Definition ipool.h:540
Definition generic_pool.h:56
Definition pool.h:54
bitset_ext
Definition absolute.h:40
Definition type_lookup.h:54
The type/id pair type to use for type/id lookup template parameters.
Definition type_lookup.h:46