diff --git a/ClassFactory/CMakeLists.txt b/ClassFactory/CMakeLists.txt new file mode 100644 index 0000000..adfa336 --- /dev/null +++ b/ClassFactory/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.10) + +# Set the project name +project(ClassFactory) + +# Create a library target for ClassFactory module +add_library(ClassFactory STATIC + ClassFactory.cpp + ClassFactory.h + ClassFactory.tpp +) + +# Set the include directory +target_include_directories(ClassFactory PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + diff --git a/ClassFactory/ClassFactory.cpp b/ClassFactory/ClassFactory.cpp new file mode 100644 index 0000000..29642f6 --- /dev/null +++ b/ClassFactory/ClassFactory.cpp @@ -0,0 +1,17 @@ +#include "ClassFactory.h" + +using namespace std; + +bool UnorderPointersMap::isExisting(const string& key) const { + return myList.find(key) != myList.end(); +} + + +void ClassFactory::setNotCorrected() { + setVar(isCorrect, false); +} + +bool ClassFactory::checkIfCorrect() { + return isCorrect; +} + diff --git a/ClassFactory/ClassFactory.h b/ClassFactory/ClassFactory.h new file mode 100644 index 0000000..e696b11 --- /dev/null +++ b/ClassFactory/ClassFactory.h @@ -0,0 +1,51 @@ +#ifndef CLASS_FACTORY_H_ +#define CLASS_FACTORY_H_ + +#include +#include +#include +#include +#include +#include +#include "Logs.h" + +class UnorderPointersMap { + private: + using VarType = std::variant, std::shared_ptr, std::shared_ptr, std::shared_ptr, std::shared_ptr>; + std::unordered_map myList; + + public: + template + void addItem(const std::string& key, T& item); + + template + T& getItem(const std::string& key) const; + + bool isExisting(const std::string& key) const; +}; + +class ClassFactory { + private: + bool isCorrect = true; + UnorderPointersMap unorderedPointersMap; + + protected: + void setNotCorrected(); + + template + bool setVar(T& VarPtr, std::any input, const std::string& name = ""); + + public: + template + bool setVar(const std::string& varName, T input); + + template + T getVar(const std::string& varName); + + bool checkIfCorrect(); +}; + +// Template function implementations +#include "ClassFactory.tpp" + +#endif /* CLASS_FACTORY_H_ */ diff --git a/ClassFactory/ClassFactory.tpp b/ClassFactory/ClassFactory.tpp new file mode 100644 index 0000000..0532df4 --- /dev/null +++ b/ClassFactory/ClassFactory.tpp @@ -0,0 +1,67 @@ +// Template function implementations for UnorderPointersMap + +template +void UnorderPointersMap::addItem(const std::string& key, T& item) { + std::shared_ptr ptr = std::make_shared(item); + myList[key] = ptr; +} + +template +T& UnorderPointersMap::getItem(const std::string& key) const { + auto it = myList.find(key); + if (it != myList.end()) { + if (auto ptr = std::get_if>(&(it->second))) { + return **ptr; + } + } + throw std::invalid_argument("Key not found or type mismatch"); +} + +// Template function implementations for ClassFactory + +template +bool ClassFactory::setVar(T& Var, std::any input, const std::string& name) { + try { + Var = std::any_cast(input); + if (!name.empty()) { + unorderedPointersMap.addItem(name, Var); + } + return true; + } catch (const std::exception& e) { + std::cout << "Invalid input" << std::endl; + writeToLogs(string("Error in private setVar: ") + string(e.what())); + return false; + } +} + +template +bool ClassFactory::setVar(const std::string& varName, T input) { + try { + if (unorderedPointersMap.isExisting(varName)) { + unorderedPointersMap.getItem(varName) = input; + } else { + std::cout << "Variable not found" << std::endl; + writeToLogs("Error in public setVar: variable not found"); + return false; + } + return true; + } catch (const std::exception& e) { + writeToLogs(string("Error in public setVar: ") + string(e.what())); + return false; + } +} + +template +T ClassFactory::getVar(const std::string& varName) { + if (!checkIfCorrect()) { + std::cout << "This class contains errors; one of the arguments in the constructor was of an incorrect type" << std::endl; + throw std::runtime_error("ClassFactory contains errors"); + } else { + if (unorderedPointersMap.isExisting(varName)) { + return unorderedPointersMap.getItem(varName); + } else { + writeToLogs("Error in public getVar: variable not found"); + throw std::runtime_error("Variable not found: " + varName); + } + } +} diff --git a/README.md b/README.md index 8d9d7a0..cb66dc4 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# LabiryntGenerator \ No newline at end of file +#LabiryntGenerator