Compare commits
2 Commits
063292c7ed
...
c9c2bc11ae
| Author | SHA1 | Date | |
|---|---|---|---|
| c9c2bc11ae | |||
| fb7c83d04d |
@@ -1,6 +1,7 @@
|
|||||||
#ifndef TYPEFACTORY_H
|
#ifndef TYPEFACTORY_H
|
||||||
#define TYPEFACTORY_H
|
#define TYPEFACTORY_H
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <type_traits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,7 +33,7 @@
|
|||||||
*
|
*
|
||||||
* class Derived2 : public Base {
|
* class Derived2 : public Base {
|
||||||
* public:
|
* public:
|
||||||
* Derived1 (int start) : m_start(start){}
|
* Derived2 (int start) : m_start(start){}
|
||||||
* int get() override {
|
* int get() override {
|
||||||
* return m_start + 2;
|
* return m_start + 2;
|
||||||
* }
|
* }
|
||||||
@@ -42,19 +43,19 @@
|
|||||||
* .
|
* .
|
||||||
* .
|
* .
|
||||||
* .
|
* .
|
||||||
* TypeFsactory <std::string, Base, int> typefactory;
|
* TypeFactory <std::string, Base, int> typefactory;
|
||||||
* typefactory.registerType<Derived1>("one");
|
* typefactory.registerType<Derived1>("one");
|
||||||
* typefactory.registerType<Derived2>("2");
|
* typefactory.registerType<Derived2>("2");
|
||||||
*
|
*
|
||||||
* auto d1 = typefactory.createInstance ("one")(10);
|
* auto d1 = typefactory.create ("one")(10);
|
||||||
* auto d2 = typefactory.createInstance ("2")(10);
|
* auto d2 = typefactory.create ("2")(10);
|
||||||
*
|
*
|
||||||
* d1->get();
|
* d1->get();
|
||||||
* d2->get();
|
* d2->get();
|
||||||
* @endcode
|
* @endcode
|
||||||
*/
|
*/
|
||||||
template<class ClassId, class BaseClass, class ... Args>
|
template<class ClassId, class BaseClass, class ... Args>
|
||||||
class TypeFsactory {
|
class TypeFactory {
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<BaseClass> BaseClass_SP;
|
typedef std::shared_ptr<BaseClass> BaseClass_SP;
|
||||||
/**
|
/**
|
||||||
@@ -62,7 +63,7 @@ class TypeFsactory {
|
|||||||
*/
|
*/
|
||||||
typedef BaseClass_SP (*typefactoryFunction) (Args ...);
|
typedef BaseClass_SP (*typefactoryFunction) (Args ...);
|
||||||
|
|
||||||
TypeFsactory() = default;
|
TypeFactory() = default;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief registerType - template function to register class in factory
|
* @brief registerType - template function to register class in factory
|
||||||
@@ -78,16 +79,22 @@ class TypeFsactory {
|
|||||||
template<class Derived>
|
template<class Derived>
|
||||||
void registerType (const ClassId& id) {
|
void registerType (const ClassId& id) {
|
||||||
/// Register Derived type by storing its constructor wrapper
|
/// Register Derived type by storing its constructor wrapper
|
||||||
|
|
||||||
|
|
||||||
|
static_assert(std::is_base_of_v<BaseClass, Derived>,
|
||||||
|
"Derived must inherit from BaseClass");
|
||||||
|
static_assert(std::is_constructible_v<Derived, Args...>,
|
||||||
|
"Derived must be constructible from Args...");
|
||||||
classes[id] = &typefactory<Derived>;
|
classes[id] = &typefactory<Derived>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief createInstance - createInstance class by unique id. Class should be registered before
|
* @brief create - Returns creator function for id.
|
||||||
* @param id - unique class identification
|
* @param id - unique class identification
|
||||||
* @return shared pointer to new class.
|
* @return shared pointer to new class.
|
||||||
* @throws std::out_of_range when id not found in map
|
* @throws std::out_of_range when id not found in map
|
||||||
*/
|
*/
|
||||||
typefactoryFunction createInstance (const ClassId& id) const {
|
typefactoryFunction create (const ClassId& id) const {
|
||||||
return classes.at (id);
|
return classes.at (id);
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
@@ -98,7 +105,7 @@ class TypeFsactory {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* @brief typefactory - main functionality, createInstance registered object
|
* @brief typefactory - main functionality, create registered object
|
||||||
* @param Derived - class type
|
* @param Derived - class type
|
||||||
* @param args - constructor arguments
|
* @param args - constructor arguments
|
||||||
* @return shared pointer to register Derived class
|
* @return shared pointer to register Derived class
|
||||||
|
|||||||
Reference in New Issue
Block a user