Initial commit. Base Type factory implementation

This commit is contained in:
DH
2026-02-27 18:29:26 -05:00
parent 074006a3e4
commit 684eb891f7
4 changed files with 207 additions and 0 deletions

74
typefactory/.gitignore vendored Normal file
View File

@@ -0,0 +1,74 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
CMakeLists.txt.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

View File

@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.5)
project(typefactory LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(typefactory main.cpp)
install(TARGETS typefactory
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

8
typefactory/main.cpp Normal file
View File

@@ -0,0 +1,8 @@
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}

114
typefactory/typefactory.h Normal file
View File

@@ -0,0 +1,114 @@
#ifndef TYPEFACTORY_H
#define TYPEFACTORY_H
#include <map>
#include <list>
#include <set>
#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 Args - constructor arguments
*
* Usage example:
* @code
* class Base_T {
* public:
* virtual int get() = 0;
* }
*
* class Derived1_T : public Base_T {
* public:
* Derived1_T (int start) : m_start(start){}
* int get() override {
* return m_start + 1;
* }
* private:
* int m_start = 0;
* }
*
* class Derived2_T : public Base_T {
* public:
* Derived1_T (int start) : m_start(start){}
* int get() override {
* return m_start + 2;
* }
* private:
* int m_start = 0;
* }
* .
* .
* .
* TypeFsactory <std::string, Base_T, int> typefactory;
* typefactory.registerType<Derived1>("one");
* typefactory.registerType<Derived2>("2");
*
* auto d1 = typefactory.create ("one")(10);
* auto d2 = typefactory.create ("2")(10);
*
* d1->get();
* d2->get();
* @endcode
*/
template<class ClassId_T, class BaseClass_T, class ... Args>
class TypeFsactory {
public:
/**
* @brief BaseClass_SP - base class shared pointer type
*/
typedef std::shared_ptr<BaseClass_T> BaseClass_SP;
/**
* @brief typefactoryFunction - typefactory function pointer type. Generally speaking it is pointer to constructor.
*/
typedef BaseClass_SP (*typefactoryFunction) (Args ...);
TypeFsactory() = default;
/**
* @brief registerType - template function to register class in factory
* @param Derived - registering type
* @param id - unique class identification
*
* Usage example:
* @code
* typefactory.registerType<Derived1>("one");
* typefactory.registerType<Derived2>("2");
* @endcode
*/
template<class Derived>
void registerType (ClassId_T id) {
/// store pointer to constructor of Derived class
classes[id] = &typefactory<Derived>;
}
/**
* @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
*/
typefactoryFunction create (ClassId_T id) const {
/// constructor of registered type will call here
return classes.at (id);
}
protected:
/**
* @brief classes - main storage for pointers to constructors of registered types
*/
std::map<ClassId_T, typefactoryFunction> classes;
private:
/**
* @brief typefactory - main functionality, create registered object
* @param Derived - class type
* @param args - constructor arguments
* @return shared pointer to register Derived class
*/
template<class Derived>
static BaseClass_SP typefactory (Args ... args) {
return std::make_shared<Derived> (args ...);
}
};
#endif // TYPEFACTORY_H