diff --git a/src/factory.h b/src/factory.h index e63bf964..c31e3e39 100644 --- a/src/factory.h +++ b/src/factory.h @@ -230,51 +230,51 @@ namespace etl /// Creates the object from an index. Default constructor. //************************************************************************* template - TBase* create_from_id() + 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 reinterpret_cast(create_from_type()); + return create_from_type(); } //************************************************************************* /// Creates the object from an index. One parameter constructor. //************************************************************************* template - TBase* create_from_id(const TP1& p1) + 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 reinterpret_cast(create_from_type(p1)); + return create_from_type(p1); } //************************************************************************* /// Creates the object from an index. Two parameter constructor. //************************************************************************* template - TBase* create_from_id(const TP1& p1, const TP2& p2) + 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 reinterpret_cast(create_from_type(p1, p2)); + return create_from_type(p1, p2); } //************************************************************************* /// Creates the object from an index. Three parameter constructor. //************************************************************************* template - TBase* create_from_id(const TP1& p1, const TP2& p2, const TP3& p3) + 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 reinterpret_cast(create_from_type(p1, p2, p3)); + return create_from_type(p1, p2, p3); } //************************************************************************* /// Creates the object from an index. Three parameter constructor. //************************************************************************* template - TBase* create_from_id(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4) + 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"); diff --git a/test/test_factory.cpp b/test/test_factory.cpp index c5d7ac54..3f5b5f1b 100644 --- a/test/test_factory.cpp +++ b/test/test_factory.cpp @@ -286,25 +286,25 @@ namespace TEST(test_create_from_index) { Factory factory; - Base* p; + Derived1* p1; + Derived2* p2; Derived3* p3; - CHECK_NO_THROW(p = factory.create_from_id()); - CHECK(dynamic_cast(p) != 0); - factory.destroy(p); + CHECK_NO_THROW(p1 = factory.create_from_id()); + CHECK_EQUAL(0, p1->i); + factory.destroy(p1); - CHECK_NO_THROW(p = factory.create_from_id()); - CHECK(dynamic_cast(p) != 0); - factory.destroy(p); + CHECK_NO_THROW(p2 = factory.create_from_id()); + CHECK_EQUAL(0.0, p2->d); + factory.destroy(p2); - CHECK_NO_THROW(p = factory.create_from_id()); - CHECK(dynamic_cast(p) != 0); - factory.destroy(p); + CHECK_NO_THROW(p3 = factory.create_from_id()); + CHECK_EQUAL("constructed", p3->s); + factory.destroy(p3); - CHECK_NO_THROW(p = factory.create_from_id("1")); - p3 = static_cast(p); + CHECK_NO_THROW(p3 = factory.create_from_id("1")); CHECK_EQUAL("constructed1", p3->s); - factory.destroy(p); + factory.destroy(p3); } }; }