Compare commits

...

3 Commits

Author SHA1 Message Date
DH
7905dc02ae Remove _T postfix. No reference 2026-02-27 19:13:42 -05:00
DH
bd4cdd53a4 Unordered map instead regular 2026-02-27 19:09:18 -05:00
DH
b6348e16f5 Spell checking. No reference. 2026-02-27 19:06:36 -05:00

View File

@@ -1,24 +1,24 @@
#ifndef TYPEFACTORY_H
#define TYPEFACTORY_H
#include <map>
#include <unordered_map>
#include <memory>
/**
* @brief The TypeFsactory class - base typefactory (abstract factory) template
* @param ClassId_T - unique class identification class. For example string, or integer id.
* @param BaseClass_T - all objects should have one base class. For example vehicle or parameter
* @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
*
* Usage example:
* @code
* class Base_T {
* class Base {
* public:
* virtual int get() = 0;
* }
*
* class Derived1_T : public Base_T {
* class Derived1 : public Base {
* public:
* Derived1_T (int start) : m_start(start){}
* Derived1 (int start) : m_start(start){}
* int get() override {
* return m_start + 1;
* }
@@ -26,9 +26,9 @@
* int m_start = 0;
* }
*
* class Derived2_T : public Base_T {
* class Derived2 : public Base {
* public:
* Derived1_T (int start) : m_start(start){}
* Derived1 (int start) : m_start(start){}
* int get() override {
* return m_start + 2;
* }
@@ -38,7 +38,7 @@
* .
* .
* .
* TypeFsactory <std::string, Base_T, int> typefactory;
* TypeFsactory <std::string, Base, int> typefactory;
* typefactory.registerType<Derived1>("one");
* typefactory.registerType<Derived2>("2");
*
@@ -49,13 +49,13 @@
* d2->get();
* @endcode
*/
template<class ClassId_T, class BaseClass_T, class ... Args>
template<class ClassId, class BaseClass, class ... Args>
class TypeFsactory {
public:
/**
* @brief BaseClass_SP - base class shared pointer type
*/
typedef std::shared_ptr<BaseClass_T> BaseClass_SP;
typedef std::shared_ptr<BaseClass> BaseClass_SP;
/**
* @brief typefactoryFunction - typefactory function pointer type. Generally speaking it is pointer to constructor.
*/
@@ -75,7 +75,7 @@ class TypeFsactory {
* @endcode
*/
template<class Derived>
void registerType (const ClassId_T& id) {
void registerType (const ClassId& id) {
/// store pointer to constructor of Derived class
classes[id] = &typefactory<Derived>;
}
@@ -84,9 +84,9 @@ class TypeFsactory {
* @brief create - create class by unique id. Class should be registered before
* @param id - unique class identification
* @return shared pointer to new class.
* @throws std::our_of_range when id not found in map
* @throws std::out_of_range when id not found in map
*/
typefactoryFunction create (const ClassId_T& id) const {
typefactoryFunction create (const ClassId& id) const {
/// constructor of registered type will call here
return classes.at (id);
}
@@ -94,7 +94,7 @@ class TypeFsactory {
/**
* @brief classes - main storage for pointers to constructors of registered types
*/
std::map<ClassId_T, typefactoryFunction> classes;
std::unordered_map<ClassId, typefactoryFunction> classes;
private:
/**