diff --git a/include/ComHandler.h b/include/ComHandler.h index a973c57034ab7fccafd578a70c3440e9754fed20..2e23bb4520e1f6f757b161a9e9525e7af0f23a22 100644 --- a/include/ComHandler.h +++ b/include/ComHandler.h @@ -19,7 +19,7 @@ public: static ComHandler &getInstance(); void registerComListener(IComListener *cl); void unregisterComListener(IComListener *cl); - bool start(std::vector<std::shared_ptr<Command>> commands, std::optional<Rover> rover); + bool start(std::vector<std::shared_ptr<ICommand>> commands, std::optional<Rover> rover); bool stop() { return true; } }; @@ -40,7 +40,7 @@ void ComHandler::unregisterComListener(IComListener *cl) comListener.reset(); } -bool ComHandler::start(std::vector<std::shared_ptr<Command>> commands, std::optional<Rover> rover) +bool ComHandler::start(std::vector<std::shared_ptr<ICommand>> commands, std::optional<Rover> rover) { if (rover) { diff --git a/include/IComListener.h b/include/IComListener.h index 57fcf06b0f75a6717f451a43e355956eb420db62..f355db39d0d62aab83b0d2890af5f74e49f2d112 100644 --- a/include/IComListener.h +++ b/include/IComListener.h @@ -7,5 +7,5 @@ class IComListener { public: - virtual void commandPerformed(std::optional<std::shared_ptr<Command>>, ComState _state) = 0; + virtual void commandPerformed(std::optional<std::shared_ptr<ICommand>>, ComState _state) = 0; }; \ No newline at end of file diff --git a/src/Command.h b/src/Command.h index 42218daf84a1ea286d1922f21a85d68ae6b890c1..7d17b627d1e206d4aa4f18710185f48846bc63f2 100644 --- a/src/Command.h +++ b/src/Command.h @@ -7,7 +7,7 @@ * @brief Base class for the specific Command implementations * e.g. Gear, Direction and Pause. */ -class Command : ICommand +class Command : public ICommand { private: std::string name; diff --git a/src/CommandListOWN.h b/src/CommandListOWN.h index 61cf455319b3658e4acfb5bee189417e8b8997ef..ae0a89767575eb41088dd74ca88f1f35c2d0128b 100644 --- a/src/CommandListOWN.h +++ b/src/CommandListOWN.h @@ -23,17 +23,17 @@ public: int getSize(); void clear(); - int getPos(std::shared_ptr<Command> _cmd); - std::shared_ptr<Command> getCommand(unsigned int _pos); + int getPos(std::shared_ptr<ICommand> _cmd); + std::shared_ptr<ICommand> getCommand(unsigned int _pos); template <class Type> - std::shared_ptr<Command> add(const Type _cmd); - std::shared_ptr<Command> remove(unsigned int _pos); + std::shared_ptr<ICommand> add(const Type _cmd); + std::shared_ptr<ICommand> remove(unsigned int _pos); - std::shared_ptr<Command> moveUp(unsigned int _pos); - std::shared_ptr<Command> moveDown(unsigned int _pos); + std::shared_ptr<ICommand> moveUp(unsigned int _pos); + std::shared_ptr<ICommand> moveDown(unsigned int _pos); - std::shared_ptr<Command> add(std::shared_ptr<Command> _cmd) + std::shared_ptr<ICommand> add(std::shared_ptr<ICommand> _cmd) { std::shared_ptr<Element> newElement = std::make_shared<Element>(_cmd); std::shared_ptr<Element> end = getElement(getSize()); @@ -88,7 +88,7 @@ void CommandList::clear() * @return std::shared_ptr<Command> to the newly instanciated Command */ template <typename Type> -std::shared_ptr<Command> CommandList::add(const Type _cmd) +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()); @@ -123,7 +123,9 @@ void CommandList::printCommands() { //In this order to not print out the root element. current = current->getNext(); - std::cout << current->getCommand()->getConfig() << '\n'; + std::shared_ptr<Command> command = std::dynamic_pointer_cast<Command>(current->getCommand()); + if (command != nullptr) + std::cout << command->getConfig() << '\n'; } } @@ -134,7 +136,7 @@ void CommandList::printCommands() * @return std::shared_ptr<Command> to the Command the Element was holding * or nullptr, if unsuccessful */ -std::shared_ptr<Command> CommandList::remove(unsigned int _pos) +std::shared_ptr<ICommand> CommandList::remove(unsigned int _pos) { std::shared_ptr<Element> toRemove = getElement(_pos); if (toRemove == nullptr | toRemove == root) @@ -177,7 +179,7 @@ std::shared_ptr<Element> CommandList::getElement(unsigned int _pos) * @param _pos of the wanted Command * @return std::shared_ptr<Command> to the wanted Command or nullptr if unsuccessful. */ -std::shared_ptr<Command> CommandList::getCommand(unsigned int _pos) +std::shared_ptr<ICommand> CommandList::getCommand(unsigned int _pos) { std::shared_ptr<Element> elem = getElement(_pos); if (elem == nullptr) @@ -196,7 +198,7 @@ std::shared_ptr<Command> CommandList::getCommand(unsigned int _pos) * @param _cmd to look up. * @return int position of the std::shared_ptr<Command> or -1 if unsuccessful. */ -int CommandList::getPos(std::shared_ptr<Command> _cmd) +int CommandList::getPos(std::shared_ptr<ICommand> _cmd) { std::shared_ptr<Element> elem = root; int i = 0; @@ -219,7 +221,7 @@ int CommandList::getPos(std::shared_ptr<Command> _cmd) * @param _pos of the Element to move up * @return std::shared_ptr<Command> held by the moved Element or nullptr if unsuccessful. */ -std::shared_ptr<Command> CommandList::moveUp(unsigned int _pos) +std::shared_ptr<ICommand> CommandList::moveUp(unsigned int _pos) { if (_pos <= 1) { @@ -231,7 +233,7 @@ std::shared_ptr<Command> CommandList::moveUp(unsigned int _pos) { return nullptr; } - std::shared_ptr<Command> toReturn = ptr->getCommand(); + std::shared_ptr<ICommand> toReturn = ptr->getCommand(); prevPtr->getPrev()->setNext(ptr); ptr->setPrev(prevPtr->getPrev()); @@ -252,7 +254,7 @@ std::shared_ptr<Command> CommandList::moveUp(unsigned int _pos) * @param _pos of the Element to move down * @return std::shared_ptr<Command> held by the moved Element or nullptr if unsuccessful. */ -std::shared_ptr<Command> CommandList::moveDown(unsigned int _pos) +std::shared_ptr<ICommand> CommandList::moveDown(unsigned int _pos) { std::shared_ptr<Element> ptr = getElement(_pos); std::shared_ptr<Element> nextPtr = getElement(_pos +1); @@ -260,7 +262,7 @@ std::shared_ptr<Command> CommandList::moveDown(unsigned int _pos) { return nullptr; } - std::shared_ptr<Command> toReturn = ptr->getCommand(); + std::shared_ptr<ICommand> toReturn = ptr->getCommand(); ptr->getPrev()->setNext(nextPtr); nextPtr->setPrev(ptr->getPrev()); diff --git a/src/CommandType.h b/src/CommandType.h index c091d3585c5eca832c55aaf31643a15646d2fffe..2a1de73cd4c4f1698e3a004beb4435abde17341c 100644 --- a/src/CommandType.h +++ b/src/CommandType.h @@ -16,7 +16,7 @@ public: std::string getName() { return name; } std::string toString() { return getName(); } - std::shared_ptr<Command> createInstance(); + std::shared_ptr<ICommand> createInstance(); }; CommandType::CommandType(std::string _name) @@ -25,7 +25,7 @@ CommandType::CommandType(std::string _name) } -std::shared_ptr<Command> CommandType::createInstance() +std::shared_ptr<ICommand> CommandType::createInstance() { if (name == IGear::gear) return std::make_shared<Gear>(0, 0); diff --git a/src/ControlModel.h b/src/ControlModel.h index 0a444d7a6dd1e4876fdb10b5e83ced492abb181b..f89435228bd9fb2ebb0b2b16935454ce1acc19c5 100644 --- a/src/ControlModel.h +++ b/src/ControlModel.h @@ -26,7 +26,7 @@ private: public: static ControlModel& getInstance(); - void commandPerformed(std::optional<std::shared_ptr<Command>> _command, ComState _state); + void commandPerformed(std::optional<std::shared_ptr<ICommand>> _command, ComState _state); bool start(); bool stop(); @@ -78,11 +78,12 @@ ControlModel& ControlModel::getInstance() return instance; } -void ControlModel::commandPerformed(std::optional<std::shared_ptr<Command>> _command, ComState _state) +void ControlModel::commandPerformed(std::optional<std::shared_ptr<ICommand>> _command, ComState _state) { std::string commandPart = ""; - if (_command) - commandPart = _command.value()->getConfig(); + std::shared_ptr<Command> command = std::dynamic_pointer_cast<Command>(_command.value_or(nullptr)); + if (command) + commandPart = command->getConfig(); else commandPart = "no Command"; @@ -113,7 +114,7 @@ void ControlModel::commandPerformed(std::optional<std::shared_ptr<Command>> _com bool ControlModel::start() { - std::vector<std::shared_ptr<Command>> commands; + std::vector<std::shared_ptr<ICommand>> commands; uint size = list.getSize(); //0 is root element // iterator would help greatly with of by one errors @@ -133,7 +134,7 @@ bool ControlModel::stop() void ControlModel::readCommands(std::string _fileName) { ObjectFileHandler fH = ObjectFileHandler(_fileName); - std::vector<std::shared_ptr<Command>> v; + std::vector<std::shared_ptr<ICommand>> v; fH.read(v); for (auto &&i : v) { @@ -143,7 +144,7 @@ void ControlModel::readCommands(std::string _fileName) void ControlModel::writeCommands(std::string _fileName) { - std::vector<std::shared_ptr<Command>> v; + std::vector<std::shared_ptr<ICommand>> v; uint size = list.getSize(); // 0 is root element // iterator would help greatly with of by one errors diff --git a/src/Element.h b/src/Element.h index a9e67d019025b1e6a161ec1c5bd61149a0fcc958..8deeb300ecf00e4c4ea155f36c15037dda5de4dd 100644 --- a/src/Element.h +++ b/src/Element.h @@ -12,7 +12,7 @@ class Element private: std::shared_ptr<Element> next; std::shared_ptr<Element> prev; - std::shared_ptr<Command> cmd; + std::shared_ptr<ICommand> cmd; public: /** @@ -23,7 +23,7 @@ public: */ template <typename Type> Element(Type _cmd) { cmd = std::make_shared<Type>(_cmd); } - Element(std::shared_ptr<Command> _cmd) { cmd = _cmd; } + Element(std::shared_ptr<ICommand> _cmd) { cmd = _cmd; } /** * @brief Construct an empty Element object. Dont use this constructor! @@ -37,7 +37,7 @@ public: * * @return std::shared_ptr<Command> */ - std::shared_ptr<Command> getCommand() { return cmd; } + std::shared_ptr<ICommand> getCommand() { return cmd; } /** * @brief Set the next Element diff --git a/src/ObjectFileHandler.h b/src/ObjectFileHandler.h index 9fad1ceaab4e63dfcf31f87b5a1b59c3b56b04c3..d41141f139b5189c8cbeb6ec6073c868c5830c8c 100644 --- a/src/ObjectFileHandler.h +++ b/src/ObjectFileHandler.h @@ -16,8 +16,8 @@ private: public: ObjectFileHandler(std::string _file); std::string getFileName() { return fileName; } - bool read(std::vector <std::shared_ptr<Command>> &destination); - bool write(const std::vector<std::shared_ptr<Command>> &source); + bool read(std::vector <std::shared_ptr<ICommand>> &destination); + bool write(const std::vector<std::shared_ptr<ICommand>> &source); }; ObjectFileHandler::ObjectFileHandler(std::string _file) @@ -25,19 +25,19 @@ ObjectFileHandler::ObjectFileHandler(std::string _file) fileName = _file; } -bool ObjectFileHandler::write(const std::vector <std::shared_ptr<Command>> &source) +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<Command> i : source) + for (std::shared_ptr<ICommand> i : source) { bool typeWasFound = false; std::stringstream s; - std::shared_ptr<Gear> pG = std::dynamic_pointer_cast<Gear>(i); + std::shared_ptr<IGear> pG = std::dynamic_pointer_cast<IGear>(i); if (pG) { typeWasFound = true; @@ -46,7 +46,7 @@ bool ObjectFileHandler::write(const std::vector <std::shared_ptr<Command>> &sour s << pG->getDuration(); } - std::shared_ptr<Direction> pD = std::dynamic_pointer_cast<Direction>(i); + std::shared_ptr<IDirection> pD = std::dynamic_pointer_cast<IDirection>(i); if (pD) { typeWasFound = true; @@ -54,7 +54,7 @@ bool ObjectFileHandler::write(const std::vector <std::shared_ptr<Command>> &sour s << pD->getDegree(); } - std::shared_ptr<Pause> pP = std::dynamic_pointer_cast<Pause>(i); + std::shared_ptr<IPause> pP = std::dynamic_pointer_cast<IPause>(i); if (pP) { typeWasFound = true; @@ -72,7 +72,7 @@ bool ObjectFileHandler::write(const std::vector <std::shared_ptr<Command>> &sour return true; } -bool ObjectFileHandler::read(std::vector <std::shared_ptr<Command>> &destination) +bool ObjectFileHandler::read(std::vector <std::shared_ptr<ICommand>> &destination) { std::fstream file; file.open(fileName, file.in); diff --git a/src/testCommandList.cpp b/src/testCommandList.cpp index a6832d7c8ebdc8b572ea2e42c18a8d6aee393ccd..a2ceb41f0a837b77fc71e6483781c53943541ac7 100644 --- a/src/testCommandList.cpp +++ b/src/testCommandList.cpp @@ -24,7 +24,7 @@ int main(int argc, char const *argv[]) list.printCommands(); cout << "removing 4th\n"; - std::shared_ptr<Command> cmdPtr = list.remove(4); + std::shared_ptr<ICommand> cmdPtr = list.remove(4); if (!cmdPtr) { cout << "It's NULL\n"; diff --git a/src/testObjectFileHandler.cpp b/src/testObjectFileHandler.cpp index 57e2f300d3641a3d5ebffcb8f99da9d56a1dd01c..65f9fdb5cb30e0e4d11616d7d5fc44fc58d50341 100644 --- a/src/testObjectFileHandler.cpp +++ b/src/testObjectFileHandler.cpp @@ -11,19 +11,21 @@ int main() { ObjectFileHandler f("../test.txt"); - std::cout << f.getFileName() << '\n'; - std::vector<std::shared_ptr<Command>> v; + std::cout << "Writing: " << f.getFileName() << '\n'; + std::vector<std::shared_ptr<ICommand>> v; v.emplace_back(std::make_shared<Gear>(1,2)); v.emplace_back(std::make_shared<Direction>(3)); v.emplace_back(std::make_shared<Pause>(4)); - std::cout << f.write(v) << std::endl; + std::cout << "Success: " << f.write(v) << std::endl; - std::vector<std::shared_ptr<Command>> vR; + std::vector<std::shared_ptr<ICommand>> vR; f.read(vR); std::cout << "Now reading\n"; for (auto &&i : vR) { - std::cout << i->getConfig() << std::endl; + auto command = std::dynamic_pointer_cast<Command>(i); + if (command) + std::cout << command->getConfig() << std::endl; } return 0;