diff --git a/CMakeLists.txt b/CMakeLists.txt index 1689e185fed18cf53738e535a3a6a53df729da24..c23b9edc641111b5f43c9390e61d5a63044902e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,18 +63,36 @@ endif() add_executable("Test_Command_List" src/testCommandList.cpp +src/CommandListOWN.cpp +src/Gear.cpp +src/Pause.cpp +src/Direction.cpp + ) # Add include directory target_include_directories("Test_Command_List" PUBLIC include/) add_executable("Test_Control_Model" src/testControlModel.cpp +src/ControlModel.cpp +src/ControlModelRegistry.cpp +src/CommandListOWN.cpp +src/CommandType.cpp +src/Gear.cpp +src/Pause.cpp +src/Direction.cpp +include/ComHandler.cpp +include/ObjectFileHandler.cpp ) # Add include directory target_include_directories("Test_Control_Model" PUBLIC include/) add_executable("Test_Object_File_Handler" src/testObjectFileHandler.cpp +include/ObjectFileHandler.cpp +src/Gear.cpp +src/Pause.cpp +src/Direction.cpp ) # Add include directory target_include_directories("Test_Object_File_Handler" PUBLIC include/) diff --git a/include/ComHandler.cpp b/include/ComHandler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcc4210593d0aaaec345864e072faaee2a7f60e9 --- /dev/null +++ b/include/ComHandler.cpp @@ -0,0 +1,63 @@ +#include "ComHandler.h" +#include <iostream> + +/** + * @brief Manages the static Singleton instance of ComHandler + * + * @return ComHandler& the Instance of ComHandler + */ +ComHandler& ComHandler::getInstance() +{ + static ComHandler instance = ComHandler(); + return instance; +} + +/** + * @brief Registers an IComListener to ComHandler + * for use in an observer pattern. + * @param cl the raw pointer to be registered. + */ +void ComHandler::registerComListener(IComListener *cl) +{ + comListener = cl; +} + +/** + * @brief Unregister an IComListener from ComHandler. + * Unregisters the Listener only if the pointer matches + * the one already registered to ComHandler. + * @param cl the raw pointer to the Listener + */ +void ComHandler::unregisterComListener(IComListener *cl) +{ + if (comListener == cl) + comListener.reset(); +} + +/** + * @brief Will currently always simulate the execution as the rover communitaction is not implemented. + * + * @param commands A vector of ICommands to be executed + * @param rover The rover to be used (currently ignored!) + * @return true if a Rover was given -> real execution + * @return false if no Rover was given -> simulation + */ +bool ComHandler::start(std::vector<std::shared_ptr<ICommand>> commands, std::optional<Rover> rover) +{ + if (rover) + { + //* Funtionality unknown + std::cout << "Many robot, such wow!\nSimuliere stattdessen:\n"; + start(commands, std::nullopt); + } + else + { + std::cout << "Simuliere Ausführung durch Listeneraufrufe.\n"; + for (auto &&i : commands) + { + if (comListener) + comListener.value()->commandPerformed(i, ComState::connection_error); + } + } + return rover ? true : false; +} \ No newline at end of file diff --git a/include/ComHandler.h b/include/ComHandler.h index cc1ac23048819caea5b7e5c666cb16eca7157b3b..5b0e54c7af64f9d47193b86886de0caacadca6c3 100644 --- a/include/ComHandler.h +++ b/include/ComHandler.h @@ -29,64 +29,3 @@ public: */ bool stop() { return true; } }; - -/** - * @brief Manages the static Singleton instance of ComHandler - * - * @return ComHandler& the Instance of ComHandler - */ -ComHandler& ComHandler::getInstance() -{ - static ComHandler instance = ComHandler(); - return instance; -} - -/** - * @brief Registers an IComListener to ComHandler - * for use in an observer pattern. - * @param cl the raw pointer to be registered. - */ -void ComHandler::registerComListener(IComListener *cl) -{ - comListener = cl; -} - -/** - * @brief Unregister an IComListener from ComHandler. - * Unregisters the Listener only if the pointer matches - * the one already registered to ComHandler. - * @param cl the raw pointer to the Listener - */ -void ComHandler::unregisterComListener(IComListener *cl) -{ - if (comListener == cl) - comListener.reset(); -} - -/** - * @brief Will currently always simulate the execution as the rover communitaction is not implemented. - * - * @param commands A vector of ICommands to be executed - * @param rover The rover to be used (currently ignored!) - * @return true if a Rover was given -> real execution - * @return false if no Rover was given -> simulation - */ -bool ComHandler::start(std::vector<std::shared_ptr<ICommand>> commands, std::optional<Rover> rover) -{ - if (rover) - { - //* Funtionality unknown - std::cout << "Many robot, such wow!\nSimuliere stattdessen:\n"; - start(commands, std::nullopt); - } - else - { - std::cout << "Simuliere Ausführung durch Listeneraufrufe.\n"; - for (auto &&i : commands) - { - if (comListener) - comListener.value()->commandPerformed(i, ComState::connection_error); - } - } - return rover ? true : false; -} \ No newline at end of file diff --git a/include/ObjectFileHandler.cpp b/include/ObjectFileHandler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ed174316eba1d47446d5b9f0194990572c030a7 --- /dev/null +++ b/include/ObjectFileHandler.cpp @@ -0,0 +1,111 @@ +#include "ObjectFileHandler.h" + +/** + * @brief Construct a new ObjectFileHandler, no file manipulation done here. + * + * @param _file The relative filepath to the used file. + */ +ObjectFileHandler::ObjectFileHandler(std::string _file) +{ + fileName = _file; +} + +/** + * @brief Write out the ICommands to the file specified in the constructor. + * + * @param source Reference of a vector filled with ICommands to be saved. + * @returns false if something went wrong, true on success. + */ +bool ObjectFileHandler::write(const std::vector <std::shared_ptr<ICommand>> &source) +{ + std::fstream file; + file.open(fileName, file.out); + if (!file.is_open()) + return false; + + for (std::shared_ptr<ICommand> i : source) + { + bool typeWasFound = false; + std::stringstream s; + + std::shared_ptr<IGear> pG = std::dynamic_pointer_cast<IGear>(i); + if (pG) + { + typeWasFound = true; + s << IGear::gear << ' '; + s << pG->getSpeed() << ' '; + s << pG->getDuration(); + } + + std::shared_ptr<IDirection> pD = std::dynamic_pointer_cast<IDirection>(i); + if (pD) + { + typeWasFound = true; + s << IDirection::direction << ' '; + s << pD->getDegree(); + } + + std::shared_ptr<IPause> pP = std::dynamic_pointer_cast<IPause>(i); + if (pP) + { + typeWasFound = true; + s << IPause::pause << ' '; + s << pP->getDuration(); + } + + if (!typeWasFound) + return false; + s << '\n'; + file << s.rdbuf(); + } + file << std::flush; + file.close(); + return true; +} + +/** + * @brief Reads the file specified in the constructor, interpreting the file + * and appending matching ICommands to the given vector. + * @param destination The vector to which to append the ICommands from the file. + * @return false if something went wrong, true on success. + */ +bool ObjectFileHandler::read(std::vector <std::shared_ptr<ICommand>> &destination) +{ + std::fstream file; + file.open(fileName, file.in); + if (!file.is_open()) + return false; + + std::string name; + while (file >> name) + { + if (name == IGear::gear) + { + int speed; + file >> speed; + double duration; + file >> duration; + destination.emplace_back(std::make_shared<Gear>(speed, duration)); + } + else if (name == IDirection::direction) + { + int degree; + file >> degree; + destination.emplace_back(std::make_shared<Direction>(degree)); + } + else if (name == IPause::pause) + { + double duration; + file >> duration; + destination.emplace_back(std::make_shared<Pause>(duration)); + } + else + { + //This shouldn't happen. + return false; + } + } + + file.close(); + return true; +} \ No newline at end of file diff --git a/include/ObjectFileHandler.h b/include/ObjectFileHandler.h index cca9a1af062dbf529778146f9d8136c3f1d72685..41772b106f045372f6550f007bb681b946a172ff 100644 --- a/include/ObjectFileHandler.h +++ b/include/ObjectFileHandler.h @@ -23,113 +23,3 @@ public: bool read(std::vector <std::shared_ptr<ICommand>> &destination); bool write(const std::vector<std::shared_ptr<ICommand>> &source); }; - -/** - * @brief Construct a new ObjectFileHandler, no file manipulation done here. - * - * @param _file The relative filepath to the used file. - */ -ObjectFileHandler::ObjectFileHandler(std::string _file) -{ - fileName = _file; -} - -/** - * @brief Write out the ICommands to the file specified in the constructor. - * - * @param source Reference of a vector filled with ICommands to be saved. - * @returns false if something went wrong, true on success. - */ -bool ObjectFileHandler::write(const std::vector <std::shared_ptr<ICommand>> &source) -{ - std::fstream file; - file.open(fileName, file.out); - if (!file.is_open()) - return false; - - for (std::shared_ptr<ICommand> i : source) - { - bool typeWasFound = false; - std::stringstream s; - - std::shared_ptr<IGear> pG = std::dynamic_pointer_cast<IGear>(i); - if (pG) - { - typeWasFound = true; - s << IGear::gear << ' '; - s << pG->getSpeed() << ' '; - s << pG->getDuration(); - } - - std::shared_ptr<IDirection> pD = std::dynamic_pointer_cast<IDirection>(i); - if (pD) - { - typeWasFound = true; - s << IDirection::direction << ' '; - s << pD->getDegree(); - } - - std::shared_ptr<IPause> pP = std::dynamic_pointer_cast<IPause>(i); - if (pP) - { - typeWasFound = true; - s << IPause::pause << ' '; - s << pP->getDuration(); - } - - if (!typeWasFound) - return false; - s << '\n'; - file << s.rdbuf(); - } - file << std::flush; - file.close(); - return true; -} - -/** - * @brief Reads the file specified in the constructor, interpreting the file - * and appending matching ICommands to the given vector. - * @param destination The vector to which to append the ICommands from the file. - * @return false if something went wrong, true on success. - */ -bool ObjectFileHandler::read(std::vector <std::shared_ptr<ICommand>> &destination) -{ - std::fstream file; - file.open(fileName, file.in); - if (!file.is_open()) - return false; - - std::string name; - while (file >> name) - { - if (name == IGear::gear) - { - int speed; - file >> speed; - double duration; - file >> duration; - destination.emplace_back(std::make_shared<Gear>(speed, duration)); - } - else if (name == IDirection::direction) - { - int degree; - file >> degree; - destination.emplace_back(std::make_shared<Direction>(degree)); - } - else if (name == IPause::pause) - { - double duration; - file >> duration; - destination.emplace_back(std::make_shared<Pause>(duration)); - } - else - { - //This shouldn't happen. - return false; - } - } - - file.close(); - return true; -} \ No newline at end of file diff --git a/src/CommandListOWN.cpp b/src/CommandListOWN.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce538a890d23079d3c2c15d9bcaf57719ffce832 --- /dev/null +++ b/src/CommandListOWN.cpp @@ -0,0 +1,231 @@ +#include "CommandListOWN.h" + +/** + * @brief Returns the number of Elements in the List + * + * @return int Elements in the List + */ +int CommandList::getSize() +{ + std::shared_ptr<Element> ptr = root; + int i = 0; + for (; ptr->getNext(); ++i) + { + ptr = ptr->getNext(); + } + return i; +} + +/** + * @brief Removes every Element from the List. + * + */ +void CommandList::clear() +{ + std::shared_ptr<Element> current = root; + std::shared_ptr<Element> next = current->getNext(); + + while (next) + { + current->setPrev(nullptr); + current->setNext(nullptr); + current = next; + next = next->getNext(); + } +} + +/** + * @brief Adds a Command of Type to the end of the list. + * This template does not check if the given Type inherits from Command. + * @tparam Type implicit + * @param _cmd any Command that inherits from ICommand + * @return std::shared_ptr<ICommand> to the newly instanciated Command + */ +template <typename Type> +std::shared_ptr<ICommand> CommandList::add(const Type _cmd) +{ + std::shared_ptr<Element> newElement = std::make_shared<Element>(_cmd); + std::shared_ptr<Element> end = getElement(getSize()); + newElement->setPrev(end); + end->setNext(newElement); + return newElement->getCommand(); +} + +/** + * @brief Clears list before adding 5 Commands to the list. + * + */ +void CommandList::createCommands() +{ + clear(); + + add(Gear(10, 2.71)); + add(Direction(90)); + add(Pause(2.5)); + add(Direction(-90)); + add(Gear(-10, 2.71)); +} + +/** + * @brief Prints out every Command in the list to std::cout using the getConfig() method. + * + */ +void CommandList::printCommands() +{ + std::shared_ptr<Element> current = root; + while (current->getNext()) + { + //In this order to not print out the root element. + current = current->getNext(); + std::shared_ptr<Command> command = std::dynamic_pointer_cast<Command>(current->getCommand()); + if (command != nullptr) + std::cout << command->getConfig() << '\n'; + } +} + +/** + * @brief Removes the Element at _pos from the list. + * + * @param _pos of the Element to be deleted + * @return std::shared_ptr<ICommand> to the Command the Element was holding + * or nullptr, if unsuccessful + */ +std::shared_ptr<ICommand> CommandList::remove(unsigned int _pos) +{ + std::shared_ptr<Element> toRemove = getElement(_pos); + if (toRemove == nullptr | toRemove == root) + { + return nullptr; + } + std::cout << "removing: " << toRemove->getCommand()->getName() << '\n'; + std::shared_ptr<Element> prev = toRemove->getPrev(); + std::shared_ptr<Element> next = toRemove->getNext(); + prev->setNext(next); + if (next) + next->setPrev(prev); + + return toRemove->getCommand(); +} + +/** + * @brief Get the Element at _pos of the list. + * + * @param _pos Position of the wanted Element. + * @return std::shared_ptr<Element> to the Element at _pos or nullptr if unsuccessful. + */ +std::shared_ptr<Element> CommandList::getElement(unsigned int _pos) +{ + if (_pos > getSize()) + { + return nullptr; + } + std::shared_ptr<Element> elem = root; + for (int i = 0; elem->getNext() && i < _pos; ++i) + { + elem = elem->getNext(); + } + return elem; +} + +/** + * @brief Get the Command at _pos of the list utilizing CommandList::getElement. + * + * @param _pos of the wanted Command + * @return std::shared_ptr<ICommand> to the wanted Command or nullptr if unsuccessful. + */ +std::shared_ptr<ICommand> CommandList::getCommand(unsigned int _pos) +{ + std::shared_ptr<Element> elem = getElement(_pos); + if (elem == nullptr) + { + return nullptr; + } + else + { + return elem->getCommand(); + } +} + +/** + * @brief Searches the list for a std::shared_ptr<ICommand>. + * + * @param _cmd to look up. + * @return int position of the std::shared_ptr<ICommand> or -1 if unsuccessful. + */ +int CommandList::getPos(std::shared_ptr<ICommand> _cmd) +{ + std::shared_ptr<Element> elem = root; + int i = 0; + int size = getSize(); + while (elem->getNext() && elem->getCommand() != _cmd) + { + elem = elem->getNext(); + ++i; + if (i > size) + { + return -1; + } + } + return i; +} + +/** + * @brief Moves up an Element in the list. + * Switching the Element at _pos with the Element at _pos-1. + * @param _pos of the Element to move up + * @return std::shared_ptr<ICommand> held by the moved Element or nullptr if unsuccessful. + */ +std::shared_ptr<ICommand> CommandList::moveUp(unsigned int _pos) +{ + if (_pos <= 1) + { + return nullptr; + } + std::shared_ptr<Element> ptr = getElement(_pos); + std::shared_ptr<Element> prevPtr = getElement(_pos -1); + if (ptr == nullptr | prevPtr == nullptr) + { + return nullptr; + } + std::shared_ptr<ICommand> toReturn = ptr->getCommand(); + + prevPtr->getPrev()->setNext(ptr); + ptr->setPrev(prevPtr->getPrev()); + + ptr->getNext()->setPrev(prevPtr); + prevPtr->setNext(ptr->getNext()); + + prevPtr->setPrev(ptr); + ptr->setNext(prevPtr); + + return toReturn; +} + +/** + * @brief Moves down an Element in the list. + * Switching the Element at _pos with the Element at _pos+1. + * Probably one of these functions could use the other one with an offset, but whatever. + * @param _pos of the Element to move down + * @return std::shared_ptr<ICommand> held by the moved Element or nullptr if unsuccessful. + */ +std::shared_ptr<ICommand> CommandList::moveDown(unsigned int _pos) +{ + std::shared_ptr<Element> ptr = getElement(_pos); + std::shared_ptr<Element> nextPtr = getElement(_pos +1); + if (ptr == nullptr | nextPtr == nullptr | ptr == root) + { + return nullptr; + } + std::shared_ptr<ICommand> toReturn = ptr->getCommand(); + + ptr->getPrev()->setNext(nextPtr); + nextPtr->setPrev(ptr->getPrev()); + + nextPtr->getNext()->setPrev(ptr); + ptr->setNext(nextPtr->getNext()); + + ptr->setPrev(nextPtr); + nextPtr->setNext(ptr); + + return toReturn; +} \ No newline at end of file diff --git a/src/CommandListOWN.h b/src/CommandListOWN.h index b9e0cabcef4a1e957f4706dd4a99a84ae457bfd3..43bf8f0d1a6353100290a0833f9f3349e3e28222 100644 --- a/src/CommandListOWN.h +++ b/src/CommandListOWN.h @@ -45,233 +45,3 @@ public: void createCommands(); void printCommands(); }; - -/** - * @brief Returns the number of Elements in the List - * - * @return int Elements in the List - */ -int CommandList::getSize() -{ - std::shared_ptr<Element> ptr = root; - int i = 0; - for (; ptr->getNext(); ++i) - { - ptr = ptr->getNext(); - } - return i; -} - -/** - * @brief Removes every Element from the List. - * - */ -void CommandList::clear() -{ - std::shared_ptr<Element> current = root; - std::shared_ptr<Element> next = current->getNext(); - - while (next) - { - current->setPrev(nullptr); - current->setNext(nullptr); - current = next; - next = next->getNext(); - } -} - -/** - * @brief Adds a Command of Type to the end of the list. - * This template does not check if the given Type inherits from Command. - * @tparam Type implicit - * @param _cmd any Command that inherits from ICommand - * @return std::shared_ptr<ICommand> to the newly instanciated Command - */ -template <typename Type> -std::shared_ptr<ICommand> CommandList::add(const Type _cmd) -{ - std::shared_ptr<Element> newElement = std::make_shared<Element>(_cmd); - std::shared_ptr<Element> end = getElement(getSize()); - newElement->setPrev(end); - end->setNext(newElement); - return newElement->getCommand(); -} - -/** - * @brief Clears list before adding 5 Commands to the list. - * - */ -void CommandList::createCommands() -{ - clear(); - - add(Gear(10, 2.71)); - add(Direction(90)); - add(Pause(2.5)); - add(Direction(-90)); - add(Gear(-10, 2.71)); -} - -/** - * @brief Prints out every Command in the list to std::cout using the getConfig() method. - * - */ -void CommandList::printCommands() -{ - std::shared_ptr<Element> current = root; - while (current->getNext()) - { - //In this order to not print out the root element. - current = current->getNext(); - std::shared_ptr<Command> command = std::dynamic_pointer_cast<Command>(current->getCommand()); - if (command != nullptr) - std::cout << command->getConfig() << '\n'; - } -} - -/** - * @brief Removes the Element at _pos from the list. - * - * @param _pos of the Element to be deleted - * @return std::shared_ptr<ICommand> to the Command the Element was holding - * or nullptr, if unsuccessful - */ -std::shared_ptr<ICommand> CommandList::remove(unsigned int _pos) -{ - std::shared_ptr<Element> toRemove = getElement(_pos); - if (toRemove == nullptr | toRemove == root) - { - return nullptr; - } - std::cout << "removing: " << toRemove->getCommand()->getName() << '\n'; - std::shared_ptr<Element> prev = toRemove->getPrev(); - std::shared_ptr<Element> next = toRemove->getNext(); - prev->setNext(next); - if (next) - next->setPrev(prev); - - return toRemove->getCommand(); -} - -/** - * @brief Get the Element at _pos of the list. - * - * @param _pos Position of the wanted Element. - * @return std::shared_ptr<Element> to the Element at _pos or nullptr if unsuccessful. - */ -std::shared_ptr<Element> CommandList::getElement(unsigned int _pos) -{ - if (_pos > getSize()) - { - return nullptr; - } - std::shared_ptr<Element> elem = root; - for (int i = 0; elem->getNext() && i < _pos; ++i) - { - elem = elem->getNext(); - } - return elem; -} - -/** - * @brief Get the Command at _pos of the list utilizing CommandList::getElement. - * - * @param _pos of the wanted Command - * @return std::shared_ptr<ICommand> to the wanted Command or nullptr if unsuccessful. - */ -std::shared_ptr<ICommand> CommandList::getCommand(unsigned int _pos) -{ - std::shared_ptr<Element> elem = getElement(_pos); - if (elem == nullptr) - { - return nullptr; - } - else - { - return elem->getCommand(); - } -} - -/** - * @brief Searches the list for a std::shared_ptr<ICommand>. - * - * @param _cmd to look up. - * @return int position of the std::shared_ptr<ICommand> or -1 if unsuccessful. - */ -int CommandList::getPos(std::shared_ptr<ICommand> _cmd) -{ - std::shared_ptr<Element> elem = root; - int i = 0; - int size = getSize(); - while (elem->getNext() && elem->getCommand() != _cmd) - { - elem = elem->getNext(); - ++i; - if (i > size) - { - return -1; - } - } - return i; -} - -/** - * @brief Moves up an Element in the list. - * Switching the Element at _pos with the Element at _pos-1. - * @param _pos of the Element to move up - * @return std::shared_ptr<ICommand> held by the moved Element or nullptr if unsuccessful. - */ -std::shared_ptr<ICommand> CommandList::moveUp(unsigned int _pos) -{ - if (_pos <= 1) - { - return nullptr; - } - std::shared_ptr<Element> ptr = getElement(_pos); - std::shared_ptr<Element> prevPtr = getElement(_pos -1); - if (ptr == nullptr | prevPtr == nullptr) - { - return nullptr; - } - std::shared_ptr<ICommand> toReturn = ptr->getCommand(); - - prevPtr->getPrev()->setNext(ptr); - ptr->setPrev(prevPtr->getPrev()); - - ptr->getNext()->setPrev(prevPtr); - prevPtr->setNext(ptr->getNext()); - - prevPtr->setPrev(ptr); - ptr->setNext(prevPtr); - - return toReturn; -} - -/** - * @brief Moves down an Element in the list. - * Switching the Element at _pos with the Element at _pos+1. - * Probably one of these functions could use the other one with an offset, but whatever. - * @param _pos of the Element to move down - * @return std::shared_ptr<ICommand> held by the moved Element or nullptr if unsuccessful. - */ -std::shared_ptr<ICommand> CommandList::moveDown(unsigned int _pos) -{ - std::shared_ptr<Element> ptr = getElement(_pos); - std::shared_ptr<Element> nextPtr = getElement(_pos +1); - if (ptr == nullptr | nextPtr == nullptr | ptr == root) - { - return nullptr; - } - std::shared_ptr<ICommand> toReturn = ptr->getCommand(); - - ptr->getPrev()->setNext(nextPtr); - nextPtr->setPrev(ptr->getPrev()); - - nextPtr->getNext()->setPrev(ptr); - ptr->setNext(nextPtr->getNext()); - - ptr->setPrev(nextPtr); - nextPtr->setNext(ptr); - - return toReturn; -} \ No newline at end of file diff --git a/src/CommandType.cpp b/src/CommandType.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fe86ef181060cd4933e6ac8894eb3e67aadd658 --- /dev/null +++ b/src/CommandType.cpp @@ -0,0 +1,28 @@ +#include "CommandType.h" + +/** + * @brief Construct a new CommandType:: Command Type object + * + * @param _name of the Command to be instanciated later. + */ +CommandType::CommandType(std::string _name) +{ + name = _name; +} + +/** + * @brief Creates an instance of its Command wrapped in a shared_ptr. + * + * @return std::shared_ptr<ICommand> to the new Object. + */ +std::shared_ptr<ICommand> CommandType::createInstance() +{ + if (name == IGear::gear) + return std::make_shared<Gear>(0, 0); + else if (name == IDirection::direction) + return std::make_shared<Direction>(0); + else if (name == IPause::pause) + return std::make_shared<Pause>(0); + else + return nullptr; +} diff --git a/src/CommandType.h b/src/CommandType.h index e21e9c457c3324d61b23de01309fda5974016d42..2551983ce9e8823f28c03bb4dc8eae69b4e91f55 100644 --- a/src/CommandType.h +++ b/src/CommandType.h @@ -23,30 +23,3 @@ public: std::string toString() { return getName(); } std::shared_ptr<ICommand> createInstance(); }; - -/** - * @brief Construct a new CommandType:: Command Type object - * - * @param _name of the Command to be instanciated later. - */ -CommandType::CommandType(std::string _name) -{ - name = _name; -} - -/** - * @brief Creates an instance of its Command wrapped in a shared_ptr. - * - * @return std::shared_ptr<ICommand> to the new Object. - */ -std::shared_ptr<ICommand> CommandType::createInstance() -{ - if (name == IGear::gear) - return std::make_shared<Gear>(0, 0); - else if (name == IDirection::direction) - return std::make_shared<Direction>(0); - else if (name == IPause::pause) - return std::make_shared<Pause>(0); - else - return nullptr; -} diff --git a/src/ControlModel.cpp b/src/ControlModel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ed9e6b050bbeb28eea4321803fabc8e92f31b9f --- /dev/null +++ b/src/ControlModel.cpp @@ -0,0 +1,172 @@ +#include "ControlModel.h" + + +/** + * @brief Construct a new Control Model:: Control Model object + * Registers itself to ComHandler + */ +ControlModel::ControlModel() +{ + ComHandler::getInstance().registerComListener(this); +} + +/** + * @brief Destroy the Control Model:: Control Model object + * Unregisters itself from ComHandler + */ +ControlModel::~ControlModel() +{ + ComHandler::getInstance().unregisterComListener(this); +} + +/** + * @brief As ControlModel is Singleton, this function manages + * the static instance and returns a reference to it. + * @return ControlModel& the Instance of ControlModel + */ +ControlModel& ControlModel::getInstance() +{ + static ControlModel instance = ControlModel(); + return instance; +} + +/** + * @brief Will notify all IControlModelListeners subscribed to ControlModel + * with a string corresponding to the given parameters. + * @param _command an optional ICommand. The Message will be getConfig() or "no Command" + * @param _state The ComState the ComHandler is in. + */ +void ControlModel::commandPerformed(std::optional<std::shared_ptr<ICommand>> _command, ComState _state) +{ + std::string commandPart = ""; + std::shared_ptr<Command> command = std::dynamic_pointer_cast<Command>(_command.value_or(nullptr)); + if (command) + commandPart = command->getConfig(); + else + commandPart = "no Command"; + + std::string statePart = ""; + switch (_state) + { + case ComState::connection_error: + statePart = "Connection Error"; + break; + case ComState::rover_finish_ICommand: + statePart = "Rover finish ICommand"; + break; + case ComState::rover_in_use: + statePart = "Rover in Use"; + break; + case ComState::rover_running_ICommand: + statePart = "Rover running ICommand"; + break; + case ComState::send_ICommand: + statePart = "Send ICommand"; + break; + default: + statePart = ""; + break; + } + notifyMessageChanged("Command: " + commandPart + " - State: " + statePart); +} + +/** + * @brief Will gather every ICommand from the CommandList into a vector and + * hand it over to the ComHandler together with the selected Rover to start the + * execution. + * @return the result of ComHandler.start() + */ +bool ControlModel::start() +{ + std::vector<std::shared_ptr<ICommand>> commands; + uint size = list.getSize(); + //0 is root element + // iterator would help greatly with of by one errors + for (int i = 1; i <= size; ++i) + { + commands.push_back(list.getCommand(i)); + } + + return ComHandler::getInstance().start(commands, selectedRover); +} + +/** + * @brief Will tell the ComHandler to stop the execution of commands + * + * @return the result of ComHandler.stop() + */ +bool ControlModel::stop() +{ + return ComHandler::getInstance().stop(); +} + +/** + * @brief Uses ObjectFileHandler to parse a file for Commands + * and adds them to the CommandList + * @param _fileName relative Path to the file to be read. + * @see ObjectFileHandler + */ +void ControlModel::readCommands(std::string _fileName) +{ + ObjectFileHandler fH = ObjectFileHandler(_fileName); + std::vector<std::shared_ptr<ICommand>> v; + fH.read(v); + for (auto &&i : v) + { + list.add(i); + } +} + +/** + * @brief Gathers all Commands from the CommandList, then uses + * ObjectFileHandler to write them to a file + * @param _fileName relative Path to the file to be written to. (See ObjectFileHandler) + * @see ObjectFileHandler + */ +void ControlModel::writeCommands(std::string _fileName) +{ + std::vector<std::shared_ptr<ICommand>> v; + uint size = list.getSize(); + // 0 is root element + // iterator would help greatly with of by one errors + for (int i = 1; i <= size; ++i) + { + v.push_back(list.getCommand(i)); + } + ObjectFileHandler fH = ObjectFileHandler(_fileName); + fH.write(v); +} + +/** + * @brief Gets free Rovers from RoverHandler and sets the first + * one as the selectedRover. + * If the List is empty, e.g. during Simulation, an empty Rover is created. + * All subsrcribers to ControlModel are notified of the rover change. + */ +void ControlModel::setSelectedRover() +{ + std::vector<Rover> rovers = RoverHandler::getFreeRover(); + if (!rovers.empty()) + { + selectedRover = rovers.front(); + notifyRoverChanged(); + } + else + { + selectedRover = Rover(); //some example Rover + notifyRoverChanged(); + } +} + +/** + * @brief Since it is possible no rover is selected the value is optional. + * + * @return std::optional<std::string> the Id of the Rover or nullopt if no Rover was selected. + */ +std::optional<std::string> ControlModel::getSelectedRoverId() +{ +if (selectedRover) + return selectedRover.value().getId(); +else + return std::nullopt; +} diff --git a/src/ControlModel.h b/src/ControlModel.h index 0a7d49880a09080584952c5e7ec2869870c15430..2320c7596000285a26773e8b46219b78414e4c60 100644 --- a/src/ControlModel.h +++ b/src/ControlModel.h @@ -47,173 +47,3 @@ public: std::optional<std::string> getSelectedRoverId(); }; - -/** - * @brief Construct a new Control Model:: Control Model object - * Registers itself to ComHandler - */ -ControlModel::ControlModel() -{ - ComHandler::getInstance().registerComListener(this); -} - -/** - * @brief Destroy the Control Model:: Control Model object - * Unregisters itself from ComHandler - */ -ControlModel::~ControlModel() -{ - ComHandler::getInstance().unregisterComListener(this); -} - -/** - * @brief As ControlModel is Singleton, this function manages - * the static instance and returns a reference to it. - * @return ControlModel& the Instance of ControlModel - */ -ControlModel& ControlModel::getInstance() -{ - static ControlModel instance = ControlModel(); - return instance; -} - -/** - * @brief Will notify all IControlModelListeners subscribed to ControlModel - * with a string corresponding to the given parameters. - * @param _command an optional ICommand. The Message will be getConfig() or "no Command" - * @param _state The ComState the ComHandler is in. - */ -void ControlModel::commandPerformed(std::optional<std::shared_ptr<ICommand>> _command, ComState _state) -{ - std::string commandPart = ""; - std::shared_ptr<Command> command = std::dynamic_pointer_cast<Command>(_command.value_or(nullptr)); - if (command) - commandPart = command->getConfig(); - else - commandPart = "no Command"; - - std::string statePart = ""; - switch (_state) - { - case ComState::connection_error: - statePart = "Connection Error"; - break; - case ComState::rover_finish_ICommand: - statePart = "Rover finish ICommand"; - break; - case ComState::rover_in_use: - statePart = "Rover in Use"; - break; - case ComState::rover_running_ICommand: - statePart = "Rover running ICommand"; - break; - case ComState::send_ICommand: - statePart = "Send ICommand"; - break; - default: - statePart = ""; - break; - } - notifyMessageChanged("Command: " + commandPart + " - State: " + statePart); -} - -/** - * @brief Will gather every ICommand from the CommandList into a vector and - * hand it over to the ComHandler together with the selected Rover to start the - * execution. - * @return the result of ComHandler.start() - */ -bool ControlModel::start() -{ - std::vector<std::shared_ptr<ICommand>> commands; - uint size = list.getSize(); - //0 is root element - // iterator would help greatly with of by one errors - for (int i = 1; i <= size; ++i) - { - commands.push_back(list.getCommand(i)); - } - - return ComHandler::getInstance().start(commands, selectedRover); -} - -/** - * @brief Will tell the ComHandler to stop the execution of commands - * - * @return the result of ComHandler.stop() - */ -bool ControlModel::stop() -{ - return ComHandler::getInstance().stop(); -} - -/** - * @brief Uses ObjectFileHandler to parse a file for Commands - * and adds them to the CommandList - * @param _fileName relative Path to the file to be read. - * @see ObjectFileHandler - */ -void ControlModel::readCommands(std::string _fileName) -{ - ObjectFileHandler fH = ObjectFileHandler(_fileName); - std::vector<std::shared_ptr<ICommand>> v; - fH.read(v); - for (auto &&i : v) - { - list.add(i); - } -} - -/** - * @brief Gathers all Commands from the CommandList, then uses - * ObjectFileHandler to write them to a file - * @param _fileName relative Path to the file to be written to. (See ObjectFileHandler) - * @see ObjectFileHandler - */ -void ControlModel::writeCommands(std::string _fileName) -{ - std::vector<std::shared_ptr<ICommand>> v; - uint size = list.getSize(); - // 0 is root element - // iterator would help greatly with of by one errors - for (int i = 1; i <= size; ++i) - { - v.push_back(list.getCommand(i)); - } - ObjectFileHandler fH = ObjectFileHandler(_fileName); - fH.write(v); -} - -/** - * @brief Gets free Rovers from RoverHandler and sets the first - * one as the selectedRover. - * If the List is empty, e.g. during Simulation, an empty Rover is created. - * All subsrcribers to ControlModel are notified of the rover change. - */ -void ControlModel::setSelectedRover() -{ - std::vector<Rover> rovers = RoverHandler::getFreeRover(); - if (!rovers.empty()) - { - selectedRover = rovers.front(); - notifyRoverChanged(); - } - else - { - selectedRover = Rover(); //some example Rover - notifyRoverChanged(); - } -} - -/** - * @brief Since it is possible no rover is selected the value is optional. - * - * @return std::optional<std::string> the Id of the Rover or nullopt if no Rover was selected. - */ -std::optional<std::string> ControlModel::getSelectedRoverId() -{ -if (selectedRover) - return selectedRover.value().getId(); -else - return std::nullopt; -} \ No newline at end of file diff --git a/src/ControlModelRegistry.cpp b/src/ControlModelRegistry.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8971df3c19a9e8040109b8dad2f325bd74c7615d --- /dev/null +++ b/src/ControlModelRegistry.cpp @@ -0,0 +1,57 @@ +#include "ControlModelRegistry.h" + +/** + * @brief Add a Listener / Subscriber / Observer to the registry. + * + * @param _listener A shared_ptr to the listener to be added. + */ +void ControlModelRegistry::addControlModelListener(std::shared_ptr<IControlModelListener> _listener) +{ + registry.push_back(_listener); +} + +/** + * @brief Remove a Listener / Subscriber / Observer from the registry. + * Removal happens if the same address is referenced. + * @param _listener A shared_ptr to the listener to be removed. + */ +void ControlModelRegistry::removeControlModelListener(std::shared_ptr<IControlModelListener> _listener) +{ + for (auto iter = registry.begin(); iter != registry.end(); ++iter) + { + if (iter->lock().get() == _listener.get()) + { + registry.erase(iter); + } + } + +} + +/** + * @brief Calls messageUpdated() of every object in the registry. + * + * @param _message The message to be communicated to the Listeners. + */ +void ControlModelRegistry::notifyMessageChanged(std::string _message) +{ + for (auto &&i : registry) + { + std::shared_ptr<IControlModelListener> l = i.lock(); + if (l) + l->messageUpdated(_message); + } +} + +/** + * @brief Call roverUpdated() of every object in the registry, + * not communicating any additional data. + */ +void ControlModelRegistry::notifyRoverChanged() +{ + for (auto &&i : registry) + { + std::shared_ptr<IControlModelListener> l = i.lock(); + if (l) + l->roverUpdated(); + } +} \ No newline at end of file diff --git a/src/ControlModelRegistry.h b/src/ControlModelRegistry.h index cfbbcaae5219ad577e32d34cd6701a337aa6ac7e..e34532fddacad8285caf8c57908ce90d63b728c2 100644 --- a/src/ControlModelRegistry.h +++ b/src/ControlModelRegistry.h @@ -19,59 +19,3 @@ public: void notifyMessageChanged(std::string _message); void notifyRoverChanged(); }; - -/** - * @brief Add a Listener / Subscriber / Observer to the registry. - * - * @param _listener A shared_ptr to the listener to be added. - */ -void ControlModelRegistry::addControlModelListener(std::shared_ptr<IControlModelListener> _listener) -{ - registry.push_back(_listener); -} - -/** - * @brief Remove a Listener / Subscriber / Observer from the registry. - * Removal happens if the same address is referenced. - * @param _listener A shared_ptr to the listener to be removed. - */ -void ControlModelRegistry::removeControlModelListener(std::shared_ptr<IControlModelListener> _listener) -{ - for (auto iter = registry.begin(); iter != registry.end(); ++iter) - { - if (iter->lock().get() == _listener.get()) - { - registry.erase(iter); - } - } - -} - -/** - * @brief Calls messageUpdated() of every object in the registry. - * - * @param _message The message to be communicated to the Listeners. - */ -void ControlModelRegistry::notifyMessageChanged(std::string _message) -{ - for (auto &&i : registry) - { - std::shared_ptr<IControlModelListener> l = i.lock(); - if (l) - l->messageUpdated(_message); - } -} - -/** - * @brief Call roverUpdated() of every object in the registry, - * not communicating any additional data. - */ -void ControlModelRegistry::notifyRoverChanged() -{ - for (auto &&i : registry) - { - std::shared_ptr<IControlModelListener> l = i.lock(); - if (l) - l->roverUpdated(); - } -} \ No newline at end of file diff --git a/src/Direction.cpp b/src/Direction.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e3832f2165e73b774fe5ead399661ae2a4f431d --- /dev/null +++ b/src/Direction.cpp @@ -0,0 +1,40 @@ +#include "Direction.h" + +/** + * @brief Construct a new Direction:: Direction Command setting its + * degree variable. + * @param newDegree, the angle to be set + */ +Direction::Direction(int newDegree) : Command(IDirection::direction) +{ + setDegree(newDegree); +} + +/** + * @brief Change the angle variable, + * clipping at -90 and +90 degrees. + * @param newDegree, the angle to be set + */ +void Direction::setDegree(int newDegree) +{ + if (newDegree <= -90) + degree = -90; + else if (newDegree >= 90) + degree = 90; + else + degree = newDegree; +} + +/** + * @brief Returns a string representation of the Command + * and it's value. + * @return std::string "Direction - Degree: ...." + */ +std::string Direction::getConfig() +{ + std::string str = IDirection::direction + + " - Degree: " + + std::to_string(degree) + + '.'; + return str; +}; \ No newline at end of file diff --git a/src/Direction.h b/src/Direction.h index 739f694933ba04f1020a13a8eb96fc19c55b3d33..0b89840ab9a928d936828d0626e4a5a628c8df39 100644 --- a/src/Direction.h +++ b/src/Direction.h @@ -18,42 +18,3 @@ public: int getDegree() { return degree; } std::string getConfig(); }; - -/** - * @brief Construct a new Direction:: Direction Command setting its - * degree variable. - * @param newDegree, the angle to be set - */ -Direction::Direction(int newDegree) : Command(IDirection::direction) -{ - setDegree(newDegree); -} - -/** - * @brief Change the angle variable, - * clipping at -90 and +90 degrees. - * @param newDegree, the angle to be set - */ -void Direction::setDegree(int newDegree) -{ - if (newDegree <= -90) - degree = -90; - else if (newDegree >= 90) - degree = 90; - else - degree = newDegree; -} - -/** - * @brief Returns a string representation of the Command - * and it's value. - * @return std::string "Direction - Degree: ...." - */ -std::string Direction::getConfig() -{ - std::string str = IDirection::direction - + " - Degree: " - + std::to_string(degree) - + '.'; - return str; -}; \ No newline at end of file diff --git a/src/Gear.cpp b/src/Gear.cpp new file mode 100644 index 0000000000000000000000000000000000000000..811a3e5720b346c701d47382d5bb35fc460b9a00 --- /dev/null +++ b/src/Gear.cpp @@ -0,0 +1,57 @@ +#include "Gear.h" + +/** + * @brief Construct a new Gear:: Gear Command setting its variables. + * + * @param newSpeed is the speed to be set + * @param newDuration the time for which to drive + */ +Gear::Gear(int newSpeed, double newDuration) : Command(IGear::gear) +{ + setSpeed(newSpeed); + setDuration(newDuration); +} + +/** + * @brief Change the speed variable, + * clipping at -100 and +100. + * @param newSpeed the speed to be set + */ +void Gear::setSpeed(int newSpeed) +{ + if (newSpeed <= -100) + speed = -100; + else if (newSpeed >= 100) + speed = 100; + else + speed = newSpeed; +} + +/** + * @brief Change the duration variable. + * + * @param newDuration the duration to be set + */ +void Gear::setDuration(double newDuration) +{ + if (newDuration < 0) + duration = 0; + else + duration = newDuration; +} + +/** + * @brief Returns a string representation of the Command + * and it's values. + * @return std::string "Gear - Speed: ... for: ...s" + */ +std::string Gear::getConfig() +{ + std::string str = IGear::gear + + " - Speed: " + + std::to_string(speed) + + " for: " + + std::to_string(duration) + + 's'; + return str; +} \ No newline at end of file diff --git a/src/Gear.h b/src/Gear.h index 50ec72c856ff9f3076b1d0d4583bd0b582cbbcdd..2a8e039cdf2ed8933059753c16c6a245a2846620 100644 --- a/src/Gear.h +++ b/src/Gear.h @@ -22,59 +22,3 @@ public: double getDuration() { return duration; } std::string getConfig(); }; - -/** - * @brief Construct a new Gear:: Gear Command setting its variables. - * - * @param newSpeed is the speed to be set - * @param newDuration the time for which to drive - */ -Gear::Gear(int newSpeed, double newDuration) : Command(IGear::gear) -{ - setSpeed(newSpeed); - setDuration(newDuration); -} - -/** - * @brief Change the speed variable, - * clipping at -100 and +100. - * @param newSpeed the speed to be set - */ -void Gear::setSpeed(int newSpeed) -{ - if (newSpeed <= -100) - speed = -100; - else if (newSpeed >= 100) - speed = 100; - else - speed = newSpeed; -} - -/** - * @brief Change the duration variable. - * - * @param newDuration the duration to be set - */ -void Gear::setDuration(double newDuration) -{ - if (newDuration < 0) - duration = 0; - else - duration = newDuration; -} - -/** - * @brief Returns a string representation of the Command - * and it's values. - * @return std::string "Gear - Speed: ... for: ...s" - */ -std::string Gear::getConfig() -{ - std::string str = IGear::gear - + " - Speed: " - + std::to_string(speed) - + " for: " - + std::to_string(duration) - + 's'; - return str; -} \ No newline at end of file diff --git a/src/Pause.cpp b/src/Pause.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d17f17dd09d83afeb4ca953843f2d96135dd878 --- /dev/null +++ b/src/Pause.cpp @@ -0,0 +1,38 @@ +#include "Pause.h" + +/** + * @brief Construct a new Pause:: Pause Command setting its duration variable + * + * @param newDuration + */ +Pause::Pause(double newDuration) : Command(IPause::pause) +{ + setDuration(newDuration); +} + +/** + * @brief Change the duration variable + * + * @param newDuration + */ +void Pause::setDuration(double newDuration) +{ + if (newDuration < 0) + duration = 0; + else + duration = newDuration; +} + +/** + * @brief Returns a string representation of the Command + * and it's value. + * @return std::string "Pause - Duration: ...s" + */ +std::string Pause::getConfig() +{ + std::string str = IPause::pause + + " - Duration: " + + std::to_string(duration) + + 's'; + return str; +}; \ No newline at end of file diff --git a/src/Pause.h b/src/Pause.h index a39058c1f0f2354d8d42b91bf1148ab9674b0347..7115d4002059bec80410449927138d722e19f6cd 100644 --- a/src/Pause.h +++ b/src/Pause.h @@ -18,40 +18,3 @@ public: double getDuration() { return duration; } std::string getConfig(); }; - -/** - * @brief Construct a new Pause:: Pause Command setting its duration variable - * - * @param newDuration - */ -Pause::Pause(double newDuration) : Command(IPause::pause) -{ - setDuration(newDuration); -} - -/** - * @brief Change the duration variable - * - * @param newDuration - */ -void Pause::setDuration(double newDuration) -{ - if (newDuration < 0) - duration = 0; - else - duration = newDuration; -} - -/** - * @brief Returns a string representation of the Command - * and it's value. - * @return std::string "Pause - Duration: ...s" - */ -std::string Pause::getConfig() -{ - std::string str = IPause::pause - + " - Duration: " - + std::to_string(duration) - + 's'; - return str; -}; \ No newline at end of file