diff --git a/src/factory.h b/src/factory.h index c31e3e39..7b4615a2 100644 --- a/src/factory.h +++ b/src/factory.h @@ -79,7 +79,6 @@ namespace etl //*************************************************************************** template , typename T3 = etl::type_id_pair<>, @@ -124,8 +123,6 @@ namespace etl static const size_t MAX_SIZE = MAX_SIZE_; - typedef TBase base_t; - //************************************************************************* /// Constructor //************************************************************************* @@ -278,17 +275,35 @@ namespace etl { typedef typename lookup_t::template type_from_id::type type; STATIC_ASSERT((!etl::is_same::value), "Invalid index"); - return reinterpret_cast(create_from_type(p1, p2, p3, p4)); + return create_from_type(p1, p2, p3, p4); } //************************************************************************* /// Destroys the object. //************************************************************************* - bool destroy(const TBase* const p) + template + bool destroy(const T* const p) { - p->~TBase(); + STATIC_ASSERT((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(); - return release_item(reinterpret_cast(const_cast(p))); + return release_item(reinterpret_cast(const_cast(p))); } //************************************************************************* diff --git a/test/test_factory.cpp b/test/test_factory.cpp index 3f5b5f1b..2aa41c77 100644 --- a/test/test_factory.cpp +++ b/test/test_factory.cpp @@ -146,18 +146,42 @@ namespace } }; + //*********************************** + struct NonDerived + { + NonDerived() + : s("constructed") + { + } + + ~NonDerived() + { + destructor = true; + } + + void Set() + { + s = "set"; + } + + std::string s; + }; + enum { DERIVED1, DERIVED2, - DERIVED3 + DERIVED3, + NONDERIVED }; - typedef etl::type_id_pair D1_Type; - typedef etl::type_id_pair D2_Type; - typedef etl::type_id_pair D3_Type; + typedef etl::type_id_pair D1_Type; + typedef etl::type_id_pair D2_Type; + typedef etl::type_id_pair D3_Type; + typedef etl::type_id_pair ND_Type; - typedef etl::factory<4, Base, D1_Type, D2_Type, D3_Type> Factory; + // Notice that the type declaration order is not important. + typedef etl::factory<4, D1_Type, ND_Type, D3_Type, D2_Type> Factory; SUITE(test_factory) { @@ -198,6 +222,7 @@ namespace Base* p; + // Derived 1 p = factory.create_from_type(); Derived1* pd1 = static_cast(p); CHECK_EQUAL(0, pd1->i); @@ -206,6 +231,7 @@ namespace factory.destroy(p); CHECK(destructor); + // Derived 2 destructor = false; p = factory.create_from_type(); Derived2* pd2 = static_cast(p); @@ -215,6 +241,7 @@ namespace factory.destroy(p); CHECK(destructor); + // Derived 3 destructor = false; p = factory.create_from_type(); Derived3* pd3 = static_cast(p); @@ -223,6 +250,15 @@ namespace CHECK_EQUAL("set", pd3->s); factory.destroy(p); CHECK(destructor); + + // Non Derived + destructor = false; + NonDerived* pnd = factory.create_from_type(); + CHECK_EQUAL("constructed", pnd->s); + pnd->Set(); + CHECK_EQUAL("set", pnd->s); + factory.destroy(pnd); + CHECK(destructor); } //*************************************************************************