Compare commits
2 Commits
7905dc02ae
...
063292c7ed
| Author | SHA1 | Date | |
|---|---|---|---|
| 063292c7ed | |||
| 4ef52a954b |
@@ -4,7 +4,11 @@
|
||||
#include <memory>
|
||||
|
||||
/**
|
||||
* @brief The TypeFsactory class - base typefactory (abstract factory) template
|
||||
* @brief Registry-based factory for creating objects by runtime identifier.
|
||||
*
|
||||
* Allows registering derived types and instantiating them
|
||||
* using a ClassId key.
|
||||
*
|
||||
* @param ClassId - unique class identification class. For example string, or integer id.
|
||||
* @param BaseClass - all objects should have one base class. For example vehicle or parameter
|
||||
* @param Args - constructor arguments
|
||||
@@ -42,8 +46,8 @@
|
||||
* typefactory.registerType<Derived1>("one");
|
||||
* typefactory.registerType<Derived2>("2");
|
||||
*
|
||||
* auto d1 = typefactory.create ("one")(10);
|
||||
* auto d2 = typefactory.create ("2")(10);
|
||||
* auto d1 = typefactory.createInstance ("one")(10);
|
||||
* auto d2 = typefactory.createInstance ("2")(10);
|
||||
*
|
||||
* d1->get();
|
||||
* d2->get();
|
||||
@@ -52,9 +56,6 @@
|
||||
template<class ClassId, class BaseClass, class ... Args>
|
||||
class TypeFsactory {
|
||||
public:
|
||||
/**
|
||||
* @brief BaseClass_SP - base class shared pointer type
|
||||
*/
|
||||
typedef std::shared_ptr<BaseClass> BaseClass_SP;
|
||||
/**
|
||||
* @brief typefactoryFunction - typefactory function pointer type. Generally speaking it is pointer to constructor.
|
||||
@@ -76,29 +77,28 @@ class TypeFsactory {
|
||||
*/
|
||||
template<class Derived>
|
||||
void registerType (const ClassId& id) {
|
||||
/// store pointer to constructor of Derived class
|
||||
/// Register Derived type by storing its constructor wrapper
|
||||
classes[id] = &typefactory<Derived>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief create - create class by unique id. Class should be registered before
|
||||
* @brief createInstance - createInstance class by unique id. Class should be registered before
|
||||
* @param id - unique class identification
|
||||
* @return shared pointer to new class.
|
||||
* @throws std::out_of_range when id not found in map
|
||||
*/
|
||||
typefactoryFunction create (const ClassId& id) const {
|
||||
/// constructor of registered type will call here
|
||||
typefactoryFunction createInstance (const ClassId& id) const {
|
||||
return classes.at (id);
|
||||
}
|
||||
protected:
|
||||
/**
|
||||
* @brief classes - main storage for pointers to constructors of registered types
|
||||
* @brief classes - Not thread-safe. Register types during initialization phase only.
|
||||
*/
|
||||
std::unordered_map<ClassId, typefactoryFunction> classes;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief typefactory - main functionality, create registered object
|
||||
* @brief typefactory - main functionality, createInstance registered object
|
||||
* @param Derived - class type
|
||||
* @param args - constructor arguments
|
||||
* @return shared pointer to register Derived class
|
||||
|
||||
Reference in New Issue
Block a user