Changed create_from_id to return actual derived type.

This commit is contained in:
John Wellbelove 2017-09-17 16:08:49 +01:00
parent 84f5fd946e
commit a9e61444a9
2 changed files with 22 additions and 22 deletions

View File

@ -230,51 +230,51 @@ namespace etl
/// Creates the object from an index. Default constructor.
//*************************************************************************
template <size_t ID>
TBase* create_from_id()
typename lookup_t::template type_from_id<ID>::type* create_from_id()
{
typedef typename lookup_t::template type_from_id<ID>::type type;
STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
return reinterpret_cast<TBase*>(create_from_type<type>());
return create_from_type<type>();
}
//*************************************************************************
/// Creates the object from an index. One parameter constructor.
//*************************************************************************
template <size_t ID, typename TP1>
TBase* create_from_id(const TP1& p1)
typename lookup_t::template type_from_id<ID>::type* create_from_id(const TP1& p1)
{
typedef typename lookup_t::template type_from_id<ID>::type type;
STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
return reinterpret_cast<TBase*>(create_from_type<type>(p1));
return create_from_type<type>(p1);
}
//*************************************************************************
/// Creates the object from an index. Two parameter constructor.
//*************************************************************************
template <size_t ID, typename TP1, typename TP2>
TBase* create_from_id(const TP1& p1, const TP2& p2)
typename lookup_t::template type_from_id<ID>::type* create_from_id(const TP1& p1, const TP2& p2)
{
typedef typename lookup_t::template type_from_id<ID>::type type;
STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
return reinterpret_cast<TBase*>(create_from_type<type>(p1, p2));
return create_from_type<type>(p1, p2);
}
//*************************************************************************
/// Creates the object from an index. Three parameter constructor.
//*************************************************************************
template <size_t ID, typename TP1, typename TP2, typename TP3>
TBase* create_from_id(const TP1& p1, const TP2& p2, const TP3& p3)
typename lookup_t::template type_from_id<ID>::type* create_from_id(const TP1& p1, const TP2& p2, const TP3& p3)
{
typedef typename lookup_t::template type_from_id<ID>::type type;
STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
return reinterpret_cast<TBase*>(create_from_type<type>(p1, p2, p3));
return create_from_type<type>(p1, p2, p3);
}
//*************************************************************************
/// Creates the object from an index. Three parameter constructor.
//*************************************************************************
template <size_t ID, typename TP1, typename TP2, typename TP3, typename TP4>
TBase* create_from_id(const TP1& p1, const TP2& p2, const TP3& p3, const TP4& p4)
typename lookup_t::template type_from_id<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<ID>::type type;
STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");

View File

@ -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<DERIVED1>());
CHECK(dynamic_cast<Derived1*>(p) != 0);
factory.destroy(p);
CHECK_NO_THROW(p1 = factory.create_from_id<DERIVED1>());
CHECK_EQUAL(0, p1->i);
factory.destroy(p1);
CHECK_NO_THROW(p = factory.create_from_id<DERIVED2>());
CHECK(dynamic_cast<Derived2*>(p) != 0);
factory.destroy(p);
CHECK_NO_THROW(p2 = factory.create_from_id<DERIVED2>());
CHECK_EQUAL(0.0, p2->d);
factory.destroy(p2);
CHECK_NO_THROW(p = factory.create_from_id<DERIVED3>());
CHECK(dynamic_cast<Derived3*>(p) != 0);
factory.destroy(p);
CHECK_NO_THROW(p3 = factory.create_from_id<DERIVED3>());
CHECK_EQUAL("constructed", p3->s);
factory.destroy(p3);
CHECK_NO_THROW(p = factory.create_from_id<DERIVED3>("1"));
p3 = static_cast<Derived3*>(p);
CHECK_NO_THROW(p3 = factory.create_from_id<DERIVED3>("1"));
CHECK_EQUAL("constructed1", p3->s);
factory.destroy(p);
factory.destroy(p3);
}
};
}