diff --git a/include/etl/generators/variant_pool_generator.h b/include/etl/generators/variant_pool_generator.h index 8a53244f..22f7dc3e 100644 --- a/include/etl/generators/variant_pool_generator.h +++ b/include/etl/generators/variant_pool_generator.h @@ -66,69 +66,52 @@ cog.outl("//******************************************************************** #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" - -#include "utility.h" +#include "type_traits.h" +#include "static_assert.h" +#include "largest.h" namespace etl { - //*************************************************************************** - class variant_pool_exception : public etl::exception - { - public: - - variant_pool_exception(string_type reason_, string_type file_name_, numeric_type line_number_) - : exception(reason_, file_name_, line_number_) - { - } - }; - - //*************************************************************************** - class variant_pool_cannot_create : public etl::variant_pool_exception - { - public: - - variant_pool_cannot_create(string_type file_name_, numeric_type line_number_) - : variant_pool_exception(ETL_ERROR_TEXT("variant_pool:cannot create", ETL_VARIANT_POOL_FILE_ID"A"), file_name_, line_number_) - { - } - }; - - //*************************************************************************** - class variant_pool_did_not_create : public etl::variant_pool_exception - { - public: - - variant_pool_did_not_create(string_type file_name_, numeric_type line_number_) - : variant_pool_exception(ETL_ERROR_TEXT("variant_pool:did not create", ETL_VARIANT_POOL_FILE_ID"B"), file_name_, line_number_) - { - } - }; - //*************************************************************************** /*[[[cog import cog cog.outl("template " % int(NTypes)) + cog.outl("class variant_pool") + cog.out(" : public etl::generic_pool<") + cog.out("etl::largest<") + for n in range(1, int(NTypes)): + cog.out("T%s, " % n) + cog.outl("T%s>::size," % int(NTypes)) + cog.out(" etl::largest<") + for n in range(1, int(NTypes)): + cog.out("T%s, " % n) + cog.outl("T%s>::alignment," % int(NTypes)) + cog.outl(" MAX_SIZE_>") ]]]*/ /*[[[end]]]*/ - class variant_pool { public: + /*[[[cog + import cog + cog.out("typedef etl::generic_pool<") + cog.out("etl::largest<") + for n in range(1, int(NTypes)): + cog.out("T%s, " % n) + cog.outl("T%s>::size," % int(NTypes)) + cog.out(" etl::largest<") + for n in range(1, int(NTypes)): + cog.out("T%s, " % n) + cog.outl("T%s>::alignment," % int(NTypes)) + cog.outl(" MAX_SIZE_> base_t;") + ]]]*/ + /*[[[end]]]*/ + static const size_t MAX_SIZE = MAX_SIZE_; //************************************************************************* @@ -157,23 +140,7 @@ namespace etl ]]]*/ /*[[[end]]]*/ - T* p = ETL_NULLPTR; - - if (pool.full()) - { - ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); - } - else - { - p = pool.template allocate(); - - if (p != ETL_NULLPTR) - { - new (p) T(); - } - } - - return p; + return base_t::template create(); } //************************************************************************* @@ -194,23 +161,7 @@ namespace etl ]]]*/ /*[[[end]]]*/ - T* p = ETL_NULLPTR; - - if (pool.full()) - { - ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); - } - else - { - p = pool.template allocate(); - - if (p != ETL_NULLPTR) - { - new (p) T(p1); - } - } - - return p; + return base_t::template create(p1); } //************************************************************************* @@ -231,23 +182,7 @@ namespace etl ]]]*/ /*[[[end]]]*/ - T* p = ETL_NULLPTR; - - if (pool.full()) - { - ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); - } - else - { - p = pool.template allocate(); - - if (p != ETL_NULLPTR) - { - new (p) T(p1, p2); - } - } - - return p; + return base_t::template create(p1, p2); } //************************************************************************* @@ -268,23 +203,7 @@ namespace etl ]]]*/ /*[[[end]]]*/ - T* p = ETL_NULLPTR; - - if (pool.full()) - { - ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); - } - else - { - p = pool.template allocate(); - - if (p != ETL_NULLPTR) - { - new (p) T(p1, p2, p3); - } - } - - return p; + return base_t::template create(p1, p2, p3); } //************************************************************************* @@ -305,23 +224,7 @@ namespace etl ]]]*/ /*[[[end]]]*/ - T* p = ETL_NULLPTR; - - if (pool.full()) - { - ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); - } - else - { - p = pool.template allocate(); - - if (p != ETL_NULLPTR) - { - new (p) T(p1, p2, p3, p4); - } - } - - return p; + return base_t::template create(p1, p2, p3, p4); } #else //************************************************************************* @@ -342,23 +245,7 @@ namespace etl ]]]*/ /*[[[end]]]*/ - T* p = ETL_NULLPTR; - - if (pool.full()) - { - ETL_ASSERT(false, ETL_ERROR(etl::variant_pool_cannot_create)); - } - else - { - p = pool.template allocate(); - - if (p != ETL_NULLPTR) - { - new (p) T(ETL_OR_STD::forward(args)...); - } - } - - return p; + return base_t::template create(args...); } #endif @@ -379,24 +266,13 @@ namespace etl cog.outl("T%s>::value ||" % int(NTypes)) for n in range(1, int(NTypes)): - cog.outl(" etl::is_base_of::value ||" % n) - cog.outl(" etl::is_base_of::value), \"Invalid type\");" % int(NTypes)) + cog.outl(" etl::is_base_of::value ||" % n) + cog.outl(" etl::is_base_of::value), \"Invalid type\");" % int(NTypes)) ]]]*/ /*[[[end]]]*/ - p->~T(); - - void* vp = reinterpret_cast(const_cast(p)); - - if (pool.is_in_pool(vp)) - { - pool.release(vp); - } - else - { - ETL_ASSERT(false, ETL_ERROR(variant_pool_did_not_create)); - } + base_t::destroy(p); } //************************************************************************* @@ -407,66 +283,222 @@ namespace etl return MAX_SIZE; } + private: + + variant_pool(const variant_pool&) ETL_DELETE; + variant_pool& operator =(const variant_pool&) ETL_DELETE; + }; + + //*************************************************************************** + /*[[[cog + import cog + cog.outl("template " % int(NTypes)) + cog.outl("class variant_pool_ext") + cog.out(" : public etl::generic_pool_ext<") + cog.out("etl::largest<") + for n in range(1, int(NTypes)): + cog.out("T%s, " % n) + cog.outl("T%s>::size," % int(NTypes)) + cog.out(" etl::largest<") + for n in range(1, int(NTypes)): + cog.out("T%s, " % n) + cog.outl("T%s>::alignment>" % int(NTypes)) + ]]]*/ + /*[[[end]]]*/ + { + public: + + /*[[[cog + import cog + cog.out("typedef etl::generic_pool_ext<") + cog.out("etl::largest<") + for n in range(1, int(NTypes)): + cog.out("T%s, " % n) + cog.outl("T%s>::size," % int(NTypes)) + cog.out(" etl::largest<") + for n in range(1, int(NTypes)): + cog.out("T%s, " % n) + cog.outl("T%s>::alignment> base_t;" % int(NTypes)) + ]]]*/ + /*[[[end]]]*/ + //************************************************************************* - /// Returns the number of free items in the variant_pool. + /// Default constructor. //************************************************************************* - size_t available() const + variant_pool_ext(typename base_t::element* buffer, size_t size) + : base_t(buffer, size) { - return pool.available(); + } + +#if ETL_CPP11_NOT_SUPPORTED || ETL_USING_STLPORT + //************************************************************************* + /// Creates the object. Default constructor. + //************************************************************************* + template + T* create() + { + /*[[[cog + import cog + cog.out("ETL_STATIC_ASSERT((etl::is_one_of::value), \"Unsupported type\");" % int(NTypes)) + ]]]*/ + /*[[[end]]]*/ + + return base_t::template create(); } //************************************************************************* - /// Returns the number of allocated items in the variant_pool. + /// Creates the object. One parameter constructor. //************************************************************************* - size_t size() const + template + T* create(const TP1& p1) { - return pool.size(); + /*[[[cog + import cog + cog.out("ETL_STATIC_ASSERT((etl::is_one_of::value), \"Unsupported type\");" % int(NTypes)) + ]]]*/ + /*[[[end]]]*/ + + return base_t::template create(p1); } //************************************************************************* - /// Checks to see if there are no allocated items in the variant_pool. - /// \return true if there are none allocated. + /// Creates the object. Two parameter constructor. //************************************************************************* - bool empty() const + template + T* create(const TP1& p1, const TP2& p2) { - return pool.empty(); + /*[[[cog + import cog + cog.out("ETL_STATIC_ASSERT((etl::is_one_of::value), \"Unsupported type\");" % int(NTypes)) + ]]]*/ + /*[[[end]]]*/ + + return base_t::template create(p1, p2); } //************************************************************************* - /// Checks to see if there are no free items in the variant_pool. - /// \return true if there are none free. + /// Creates the object. Three parameter constructor. //************************************************************************* - bool full() const + template + T* create(const TP1& p1, const TP2& p2, const TP3& p3) { - return pool.full(); + /*[[[cog + import cog + cog.out("ETL_STATIC_ASSERT((etl::is_one_of::value), \"Unsupported type\");" % int(NTypes)) + ]]]*/ + /*[[[end]]]*/ + + return base_t::template create(p1, p2, p3); + } + + //************************************************************************* + /// Creates the object. Four parameter constructor. + //************************************************************************* + template + T* create(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4) + { + /*[[[cog + import cog + cog.out("ETL_STATIC_ASSERT((etl::is_one_of::value), \"Unsupported type\");" % int(NTypes)) + ]]]*/ + /*[[[end]]]*/ + + return base_t::template create(p1, p2, p3, p4); + } +#else + //************************************************************************* + /// Creates the object from a type. Variadic parameter constructor. + //************************************************************************* + template + T* create(Args&&... args) + { + /*[[[cog + import cog + cog.out("ETL_STATIC_ASSERT((etl::is_one_of::value), \"Unsupported type\");" % int(NTypes)) + ]]]*/ + /*[[[end]]]*/ + + return base_t::template create(args...); + } +#endif + + //************************************************************************* + /// Destroys the object. + //************************************************************************* + template + void destroy(const T* const p) + { + /*[[[cog + import cog + cog.out("ETL_STATIC_ASSERT((etl::is_one_of::value ||" % int(NTypes)) + + for n in range(1, int(NTypes)): + cog.outl(" etl::is_base_of::value ||" % n) + cog.outl(" etl::is_base_of::value), \"Invalid type\");" % int(NTypes)) + + ]]]*/ + /*[[[end]]]*/ + + base_t::destroy(p); + } + + //************************************************************************* + /// Returns the maximum number of items in the variant_pool. + //************************************************************************* + size_t max_size() const + { + return base_t::max_size(); } private: - variant_pool(const variant_pool&); - variant_pool& operator =(const variant_pool&); - - // The pool. - /*[[[cog - import cog - cog.out("etl::generic_pool::size," % int(NTypes)) - - cog.out(" etl::largest<") - for n in range(1, int(NTypes)): - cog.out("T%s, " % n) - if n % 16 == 0: - cog.outl("") - cog.out(" ") - cog.outl("T%s>::alignment," % int(NTypes)) - cog.outl(" MAX_SIZE> pool;") - ]]]*/ - /*[[[end]]]*/ + variant_pool_ext(const variant_pool_ext&) ETL_DELETE; + variant_pool_ext& operator =(const variant_pool_ext&) ETL_DELETE; }; } diff --git a/include/etl/variant_pool.h b/include/etl/variant_pool.h index 493bda30..42592cb7 100644 --- a/include/etl/variant_pool.h +++ b/include/etl/variant_pool.h @@ -79,9 +79,10 @@ namespace etl typename T14 = void, typename T15 = void, typename T16 = void> - class variant_pool : public etl::generic_pool::size, - etl::largest::alignment, - MAX_SIZE_> + class variant_pool + : public etl::generic_pool::size, + etl::largest::alignment, + MAX_SIZE_> { public: @@ -226,16 +227,20 @@ namespace etl typename T16 = void> class variant_pool_ext : public etl::generic_pool_ext::size, - etl::largest::alignment> { + etl::largest::alignment> + { public: + typedef etl::generic_pool_ext::size, - etl::largest::alignment> - base_t; + etl::largest::alignment> base_t; //************************************************************************* /// Default constructor. //************************************************************************* - variant_pool_ext(typename base_t::element* buffer, size_t size) : base_t(buffer, size) {} + variant_pool_ext(typename base_t::element* buffer, size_t size) + : base_t(buffer, size) + { + } #if ETL_CPP11_NOT_SUPPORTED || ETL_USING_STLPORT //************************************************************************* @@ -311,13 +316,23 @@ namespace etl template void destroy(const T* const p) { - ETL_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"); + ETL_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"); base_t::destroy(p); } @@ -325,12 +340,15 @@ namespace etl //************************************************************************* /// Returns the maximum number of items in the variant_pool. //************************************************************************* - // redundant also declared in ipool::max_size() - size_t max_size() const { return base_t::max_size(); } + size_t max_size() const + { + return base_t::max_size(); + } private: + variant_pool_ext(const variant_pool_ext&) ETL_DELETE; - variant_pool_ext& operator=(const variant_pool_ext&) ETL_DELETE; + variant_pool_ext& operator =(const variant_pool_ext&) ETL_DELETE; }; }