mirror of
https://github.com/ETLCPP/etl.git
synced 2026-11-09 14:49:53 +08:00
Removed constraint for types to derived from base class.
This commit is contained in:
parent
a985a72840
commit
24e661c63a
@ -79,7 +79,6 @@ namespace etl
|
||||
|
||||
//***************************************************************************
|
||||
template <const size_t MAX_SIZE_,
|
||||
typename TBase,
|
||||
typename T1,
|
||||
typename T2 = etl::type_id_pair<>,
|
||||
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<ID>::type type;
|
||||
STATIC_ASSERT((!etl::is_same<void, type>::value), "Invalid index");
|
||||
return reinterpret_cast<TBase*>(create_from_type<type>(p1, p2, p3, p4));
|
||||
return create_from_type<type>(p1, p2, p3, p4);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Destroys the object.
|
||||
//*************************************************************************
|
||||
bool destroy(const TBase* const p)
|
||||
template <typename T>
|
||||
bool destroy(const T* const p)
|
||||
{
|
||||
p->~TBase();
|
||||
STATIC_ASSERT((etl::is_base_of<T, TT1>::value ||
|
||||
etl::is_base_of<T, TT2>::value ||
|
||||
etl::is_base_of<T, TT3>::value ||
|
||||
etl::is_base_of<T, TT4>::value ||
|
||||
etl::is_base_of<T, TT5>::value ||
|
||||
etl::is_base_of<T, TT6>::value ||
|
||||
etl::is_base_of<T, TT7>::value ||
|
||||
etl::is_base_of<T, TT8>::value ||
|
||||
etl::is_base_of<T, TT9>::value ||
|
||||
etl::is_base_of<T, TT10>::value ||
|
||||
etl::is_base_of<T, TT11>::value ||
|
||||
etl::is_base_of<T, TT12>::value ||
|
||||
etl::is_base_of<T, TT13>::value ||
|
||||
etl::is_base_of<T, TT14>::value ||
|
||||
etl::is_base_of<T, TT15>::value ||
|
||||
etl::is_base_of<T, TT16>::value), "Invalid type");
|
||||
|
||||
p->~T();
|
||||
|
||||
return release_item(reinterpret_cast<char*>(const_cast<TBase*>(p)));
|
||||
return release_item(reinterpret_cast<char*>(const_cast<T*>(p)));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
|
||||
@ -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<Derived1, DERIVED1> D1_Type;
|
||||
typedef etl::type_id_pair<Derived2, DERIVED2> D2_Type;
|
||||
typedef etl::type_id_pair<Derived3, DERIVED3> D3_Type;
|
||||
typedef etl::type_id_pair<Derived1, DERIVED1> D1_Type;
|
||||
typedef etl::type_id_pair<Derived2, DERIVED2> D2_Type;
|
||||
typedef etl::type_id_pair<Derived3, DERIVED3> D3_Type;
|
||||
typedef etl::type_id_pair<NonDerived, NONDERIVED> 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>();
|
||||
Derived1* pd1 = static_cast<Derived1*>(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>();
|
||||
Derived2* pd2 = static_cast<Derived2*>(p);
|
||||
@ -215,6 +241,7 @@ namespace
|
||||
factory.destroy(p);
|
||||
CHECK(destructor);
|
||||
|
||||
// Derived 3
|
||||
destructor = false;
|
||||
p = factory.create_from_type<Derived3>();
|
||||
Derived3* pd3 = static_cast<Derived3*>(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<NonDerived>();
|
||||
CHECK_EQUAL("constructed", pnd->s);
|
||||
pnd->Set();
|
||||
CHECK_EQUAL("set", pnd->s);
|
||||
factory.destroy(pnd);
|
||||
CHECK(destructor);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user