diff --git a/typefactory/.gitignore b/typefactory/.gitignore new file mode 100644 index 0000000..4a0b530 --- /dev/null +++ b/typefactory/.gitignore @@ -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 + diff --git a/typefactory/CMakeLists.txt b/typefactory/CMakeLists.txt new file mode 100644 index 0000000..69fd07f --- /dev/null +++ b/typefactory/CMakeLists.txt @@ -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}) diff --git a/typefactory/main.cpp b/typefactory/main.cpp new file mode 100644 index 0000000..ccf6566 --- /dev/null +++ b/typefactory/main.cpp @@ -0,0 +1,8 @@ +#include + +using namespace std; + +int main() { + cout << "Hello World!" << endl; + return 0; +} diff --git a/typefactory/typefactory.h b/typefactory/typefactory.h new file mode 100644 index 0000000..1b38f2f --- /dev/null +++ b/typefactory/typefactory.h @@ -0,0 +1,114 @@ +#ifndef TYPEFACTORY_H +#define TYPEFACTORY_H +#include +#include +#include +#include + +/** + * @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 typefactory; + * typefactory.registerType("one"); + * typefactory.registerType("2"); + * + * auto d1 = typefactory.create ("one")(10); + * auto d2 = typefactory.create ("2")(10); + * + * d1->get(); + * d2->get(); + * @endcode + */ +template +class TypeFsactory { + public: + /** + * @brief BaseClass_SP - base class shared pointer type + */ + typedef std::shared_ptr 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("one"); + * typefactory.registerType("2"); + * @endcode + */ + template + void registerType (ClassId_T id) { + /// store pointer to constructor of Derived class + classes[id] = &typefactory; + } + + /** + * @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 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 + static BaseClass_SP typefactory (Args ... args) { + return std::make_shared (args ...); + } +}; + +#endif // TYPEFACTORY_H