template<class AbstractObject, typename ObjectIDType, typename ObjectCreator = ShPtr<AbstractObject> (*)(), template< typename, class > class FactoryErrorPolicy = DefaultFactoryError>
class retdec::llvmir2hll::Factory< AbstractObject, ObjectIDType, ObjectCreator, FactoryErrorPolicy >
Implementation of a generic object factory.
Implements the Object Factory design pattern - creates instances of objects according to the given object identifier.
For details, see the following book:
- A. Alexandrescu: "Modern C++ Design: Generic Programming and Design
Patterns Applied", Addison-Wesley, 2001
- Template Parameters
-
AbstractObject | Base class of all classes whose instances are to be created by a factory. |
ObjectIDType | Type of an object's identifier. |
ObjectCreator | Type of a function used to create instances of AbstractObject. |
FactoryErrorPolicy | Policy to be used when trying to instantiate an object with unknown identifier. |
FactoryErrorPolicy requirements:
- A class template with two template parameters
ObjectIDType
and AbstractObject
.
- Defines a public function
static ShPtr< AbstractObject > onUnknownType(ObjectIDType id)
Reaction to the "Unknown Type" error.
Definition: factory.h:72
which handles the "Unknown Type" error.
It supports ObjectCreators with up to five parameters.
template<class AbstractObject , typename ObjectIDType , typename ObjectCreator = ShPtr<AbstractObject> (*)(), template< typename, class > class FactoryErrorPolicy = DefaultFactoryError>
template<typename... Args>
ShPtr<AbstractObject> retdec::llvmir2hll::Factory< AbstractObject, ObjectIDType, ObjectCreator, FactoryErrorPolicy >::createObject |
( |
const ObjectIDType & |
id, |
|
|
Args &&... |
args |
|
) |
| const |
|
inline |
Creates an instance of the given object with the given arguments.
- Parameters
-
[in] | id | Object's ID. |
| args | Arguments to be passed to the instance's constructor. |
- Returns
- Instance of the given object.
template<class AbstractObject , typename ObjectIDType , typename ObjectCreator = ShPtr<AbstractObject> (*)(), template< typename, class > class FactoryErrorPolicy = DefaultFactoryError>
bool retdec::llvmir2hll::Factory< AbstractObject, ObjectIDType, ObjectCreator, FactoryErrorPolicy >::isRegistered |
( |
const ObjectIDType & |
id | ) |
const |
|
inline |
Returns true if there is an object registered with the selected ID, false
otherwise.
template<class AbstractObject , typename ObjectIDType , typename ObjectCreator = ShPtr<AbstractObject> (*)(), template< typename, class > class FactoryErrorPolicy = DefaultFactoryError>
bool retdec::llvmir2hll::Factory< AbstractObject, ObjectIDType, ObjectCreator, FactoryErrorPolicy >::registerObject |
( |
ObjectIDType |
id, |
|
|
ObjectCreator |
creator |
|
) |
| |
|
inline |
Registers the given object.
- Parameters
-
[in] | id | Object's ID. |
[in] | creator | Creator to be used to create instances. |
- Returns
true
if the registration was successful, false
otherwise.
Every ID can be registered only once. If someone tries to register the same object twice, this function will return false.