So sort of small refactoring.
This commit is contained in:
@@ -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;
|
||||||
* }
|
* }
|
||||||
@@ -46,8 +47,8 @@
|
|||||||
* 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();
|
||||||
@@ -78,16 +79,22 @@ class TypeFactory {
|
|||||||
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 TypeFactory {
|
|||||||
|
|
||||||
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