/****************************************************************************** The MIT License(MIT) Embedded Template Library. https://github.com/ETLCPP/etl https://www.etlcpp.com Copyright(c) 2017 jwellbelove Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #ifndef __ETL_FACTORY__ #define __ETL_FACTORY__ #include #include #include "platform.h" #include "error_handler.h" #include "exception.h" #include "largest.h" #include "type_traits.h" #include "alignment.h" #include "static_assert.h" #include "type_lookup.h" #include "pool.h" #if defined(ETL_COMPILER_GCC) #warning THIS CLASS IS DEPRECATED!USE VARIANT_POOL INSTEAD. #elif defined(ETL_COMPILER_MICROSOFT) #pragma message ("THIS CLASS IS DEPRECATED! USE VARIANT_POOL INSTEAD.") #endif #undef ETL_FILE #define ETL_FILE "40" namespace etl { //*************************************************************************** class factory_exception : public etl::exception { public: factory_exception(string_type reason_, string_type file_name_, numeric_type line_number_) : exception(reason_, file_name_, line_number_) { } }; //*************************************************************************** class factory_cannot_create : public etl::factory_exception { public: factory_cannot_create(string_type file_name_, numeric_type line_number_) : factory_exception(ETL_ERROR_TEXT("factory:cannot create", ETL_FILE"A"), file_name_, line_number_) { } }; //*************************************************************************** class factory_did_not_create : public etl::factory_exception { public: factory_did_not_create(string_type file_name_, numeric_type line_number_) : factory_exception(ETL_ERROR_TEXT("factory:did not create", ETL_FILE"B"), file_name_, line_number_) { } }; //*************************************************************************** template , typename T3 = etl::type_id_pair, typename T4 = etl::type_id_pair, typename T5 = etl::type_id_pair, typename T6 = etl::type_id_pair, typename T7 = etl::type_id_pair, typename T8 = etl::type_id_pair, typename T9 = etl::type_id_pair, typename T10 = etl::type_id_pair, typename T11 = etl::type_id_pair, typename T12 = etl::type_id_pair, typename T13 = etl::type_id_pair, typename T14 = etl::type_id_pair, typename T15 = etl::type_id_pair, typename T16 = etl::type_id_pair > class factory { private: typedef typename T1::type TT1; typedef typename T2::type TT2; typedef typename T3::type TT3; typedef typename T4::type TT4; typedef typename T5::type TT5; typedef typename T6::type TT6; typedef typename T7::type TT7; typedef typename T8::type TT8; typedef typename T9::type TT9; typedef typename T10::type TT10; typedef typename T11::type TT11; typedef typename T12::type TT12; typedef typename T13::type TT13; typedef typename T14::type TT14; typedef typename T15::type TT15; typedef typename T16::type TT16; typedef etl::type_id_lookup lookup_t; public: static const size_t MAX_SIZE = MAX_SIZE_; //************************************************************************* /// Default constructor. //************************************************************************* factory() { } #if !ETL_CPP11_SUPPORTED //************************************************************************* /// Creates the object. Default constructor. //************************************************************************* template T* create_from_type() { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); T* p = nullptr; if (pool.full()) { ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); } else { p = pool.template allocate(); if (p != nullptr) { new (p) T(); } } return p; } //************************************************************************* /// Creates the object. One parameter constructor. //************************************************************************* template T* create_from_type(const TP1& p1) { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); T* p = nullptr; if (pool.full()) { ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); } else { p = pool.template allocate(); if (p != nullptr) { new (p) T(p1); } } return p; } //************************************************************************* /// Creates the object. Two parameter constructor. //************************************************************************* template T* create_from_type(const TP1& p1, const TP2& p2) { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); T* p = nullptr; if (pool.full()) { ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); } else { p = pool.template allocate(); if (p != nullptr) { new (p) T(p1, p2); } } return p; } //************************************************************************* /// Creates the object. Three parameter constructor. //************************************************************************* template T* create_from_type(const TP1& p1, const TP2& p2, const TP3& p3) { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); T* p = nullptr; if (pool.full()) { ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); } else { p = pool.template allocate(); if (p != nullptr) { new (p) T(p1, p2, p3); } } return p; } //************************************************************************* /// Creates the object. Four parameter constructor. //************************************************************************* template T* create_from_type(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4) { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); T* p = nullptr; if (pool.full()) { ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); } else { p = pool.template allocate(); if (p != nullptr) { new (p) T(p1, p2, p3, p4); } } return p; } //************************************************************************* /// Creates the object from an index. Default constructor. //************************************************************************* template typename lookup_t::template type_from_id::type* create_from_id() { typedef typename lookup_t::template type_from_id::type type; STATIC_ASSERT((!etl::is_same::value), "Invalid index"); return create_from_type(); } //************************************************************************* /// Creates the object from an index. One parameter constructor. //************************************************************************* template typename lookup_t::template type_from_id::type* create_from_id(const TP1& p1) { typedef typename lookup_t::template type_from_id::type type; STATIC_ASSERT((!etl::is_same::value), "Invalid index"); return create_from_type(p1); } //************************************************************************* /// Creates the object from an index. Two parameter constructor. //************************************************************************* template typename lookup_t::template type_from_id::type* create_from_id(const TP1& p1, const TP2& p2) { typedef typename lookup_t::template type_from_id::type type; STATIC_ASSERT((!etl::is_same::value), "Invalid index"); return create_from_type(p1, p2); } //************************************************************************* /// Creates the object from an index. Three parameter constructor. //************************************************************************* template typename lookup_t::template type_from_id::type* create_from_id(const TP1& p1, const TP2& p2, const TP3& p3) { typedef typename lookup_t::template type_from_id::type type; STATIC_ASSERT((!etl::is_same::value), "Invalid index"); return create_from_type(p1, p2, p3); } //************************************************************************* /// Creates the object from an index. Three parameter constructor. //************************************************************************* template typename lookup_t::template type_from_id::type* create_from_id(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4) { typedef typename lookup_t::template type_from_id::type type; STATIC_ASSERT((!etl::is_same::value), "Invalid index"); return create_from_type(p1, p2, p3, p4); } #else //************************************************************************* /// Creates the object from a type. Variadic parameter constructor. //************************************************************************* template T* create_from_type(Args&&... args) { STATIC_ASSERT((etl::is_one_of::value), "Unsupported type"); T* p = nullptr; if (pool.full()) { ETL_ASSERT(false, ETL_ERROR(etl::factory_cannot_create)); } else { p = pool.template allocate(); if (p != nullptr) { new (p) T(std::forward(args)...); } } return p; } //************************************************************************* /// Creates the object from an index. Variadic parameter constructor. //************************************************************************* template typename lookup_t::template type_from_id::type* create_from_id(Args&&... args) { typedef typename lookup_t::template type_from_id::type type; STATIC_ASSERT((!etl::is_same::value), "Invalid index"); return create_from_type(std::forward(args)...); } #endif //************************************************************************* /// Destroys the object. //************************************************************************* template bool destroy(const T* const p) { STATIC_ASSERT((etl::is_one_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value || etl::is_base_of::value), "Invalid type"); p->~T(); void* vp = reinterpret_cast(const_cast(p)); if (pool.is_in_pool(vp)) { pool.release(vp); return true; } else { ETL_ASSERT(false, ETL_ERROR(factory_did_not_create)); return false; } } //************************************************************************* /// Returns the maximum number of items in the factory. //************************************************************************* size_t max_size() const { return MAX_SIZE; } //************************************************************************* /// Returns the number of free items in the factory. //************************************************************************* size_t available() const { return pool.available(); } //************************************************************************* /// Returns the number of allocated items in the factory. //************************************************************************* size_t size() const { return pool.size(); } //************************************************************************* /// Checks to see if there are no allocated items in the factory. /// \return true if there are none allocated. //************************************************************************* bool empty() const { return pool.empty(); } //************************************************************************* /// Checks to see if there are no free items in the factory. /// \return true if there are none free. //************************************************************************* bool full() const { return pool.full(); } private: factory(const factory&); factory& operator =(const factory&); // The pool. etl::generic_pool::size, etl::largest::alignment, MAX_SIZE> pool; }; } #undef ETL_FILE #endif