Assignments Assignments

factory.h
Go to the documentation of this file.
1#ifndef FACTORY_INCLUDED
2#define FACTORY_INCLUDED
3
4#include <type_traits>
5
6 namespace Util
7{
10 template< typename BaseType >
11 class BaseFactory
12 {
14 std::vector< BaseType * > _baseTypes;
15
17 virtual BaseType *_create( void ) = 0;
18
19 public:
21 virtual ~BaseFactory( void ){ for( int i=0 ; i<_baseTypes.size() ; i++ ) delete _baseTypes[i]; }
22
24 BaseType *create( void )
25 {
26 BaseType *baseType = _create();
27 _baseTypes.push_back( baseType );
28 return baseType;
29 }
30
32 template< typename DerivedType >
33 BaseType *create( void )
34 {
35 static_assert( std::is_base_of< BaseType , DerivedType >::value , "[ERROR] BaseType must be base of DerivedType" );
36 BaseType *baseType = new DerivedType();
37 _baseTypes.push_back( baseType );
38 return baseType;
39 }
40 };
41
43 template< typename BaseType , typename DerivedType >
44 class DerivedFactory : public BaseFactory< BaseType >
45 {
46 static_assert( std::is_base_of< BaseType , DerivedType >::value , "[ERROR] BaseType must be base of DerivedType" );
47
49 // BaseFactory< BaseType > methods //
51 BaseType *_create( void ){ return new DerivedType(); }
52 };
53}
54#endif // FACTORY_INCLUDED
Util::BaseFactory
Definition factory.h:12
Util::BaseFactory::create
BaseType * create(void)
Definition factory.h:33
Util::BaseFactory::_create
virtual BaseType * _create(void)=0
Util::BaseFactory::_baseTypes
std::vector< BaseType * > _baseTypes
Definition factory.h:14
Util::BaseFactory::~BaseFactory
virtual ~BaseFactory(void)
Definition factory.h:21
Util::BaseFactory::create
BaseType * create(void)
Definition factory.h:24
Util::DerivedFactory
Definition factory.h:45
Util::DerivedFactory::_create
BaseType * _create(void)
Definition factory.h:51
Util
Definition algebra.h:7