Compare commits

...

2 Commits

Author SHA1 Message Date
DH
c9c2bc11ae So sort of small refactoring. 2026-02-27 19:53:55 -05:00
DH
fb7c83d04d Spell checking 2026-02-27 19:46:33 -05:00

View File

@@ -1,6 +1,7 @@
#ifndef TYPEFACTORY_H
#define TYPEFACTORY_H
#include <unordered_map>
#include <type_traits>
#include <memory>
/**
@@ -32,7 +33,7 @@
*
* class Derived2 : public Base {
* public:
* Derived1 (int start) : m_start(start){}
* Derived2 (int start) : m_start(start){}
* int get() override {
* 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<Derived2>("2");
*
* auto d1 = typefactory.createInstance ("one")(10);
* auto d2 = typefactory.createInstance ("2")(10);
* auto d1 = typefactory.create ("one")(10);
* auto d2 = typefactory.create ("2")(10);
*
* d1->get();
* d2->get();
* @endcode
*/
template<class ClassId, class BaseClass, class ... Args>
class TypeFsactory {
class TypeFactory {
public:
typedef std::shared_ptr<BaseClass> BaseClass_SP;
/**
@@ -62,7 +63,7 @@ class TypeFsactory {
*/
typedef BaseClass_SP (*typefactoryFunction) (Args ...);
TypeFsactory() = default;
TypeFactory() = default;
/**
* @brief registerType - template function to register class in factory
@@ -78,16 +79,22 @@ class TypeFsactory {
template<class Derived>
void registerType (const ClassId& id) {
/// 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>;
}
/**
* @brief createInstance - createInstance class by unique id. Class should be registered before
* @brief create - Returns creator function for id.
* @param id - unique class identification
* @return shared pointer to new class.
* @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);
}
protected:
@@ -98,7 +105,7 @@ class TypeFsactory {
private:
/**
* @brief typefactory - main functionality, createInstance registered object
* @brief typefactory - main functionality, create registered object
* @param Derived - class type
* @param args - constructor arguments
* @return shared pointer to register Derived class