Skip to content
Snippets Groups Projects
Commit 90ad67d5 authored by tobiglaser's avatar tobiglaser
Browse files

update List to WS2 + resolve polymorphism hiccups

parent 3659d430
No related branches found
No related tags found
No related merge requests found
......@@ -5,12 +5,15 @@
#include "Command.h"
#include "Element.h"
#include "Gear.h"
#include "Direction.h"
#include "Pause.h"
class CommandList
{
private:
std::shared_ptr<Element> root = std::make_shared<Element>(Command("root"));
std::shared_ptr<Element> root = std::make_shared<Element>();
std::shared_ptr<Element> getElement(unsigned int _pos);
public:
......@@ -20,12 +23,14 @@ public:
int getPos(std::shared_ptr<Command> _cmd);
std::shared_ptr<Command> getCommand(unsigned int _pos);
std::shared_ptr<Command> add(Command _cmd);
template <class Type>
std::shared_ptr<Command> add(const Type _cmd);
std::shared_ptr<Command> remove(unsigned int _pos);
std::shared_ptr<Command> moveUp(unsigned int _pos);
std::shared_ptr<Command> moveDown(unsigned int _pos);
void createCommands();
void printCommands();
};
......@@ -54,7 +59,8 @@ void CommandList::clear()
}
}
std::shared_ptr<Command> CommandList::add(Command _cmd)
template <class Type>
std::shared_ptr<Command> CommandList::add(const Type _cmd)
{
std::shared_ptr<Element> newElement = std::make_shared<Element>(_cmd);
std::shared_ptr<Element> end = getElement(getSize());
......@@ -63,6 +69,18 @@ std::shared_ptr<Command> CommandList::add(Command _cmd)
return newElement->getCommand();
}
//clears list before adding 5 Commands
void CommandList::createCommands()
{
clear();
add(Gear(10, 2.71));
add(Direction(90));
add(Pause(2.5));
add(Direction(-90));
add(Gear(-10, 2.71));
}
void CommandList::printCommands()
{
std::shared_ptr<Element> current = root;
......@@ -70,7 +88,7 @@ void CommandList::printCommands()
{
//In this order to not print out the root element.
current = current->getNext();
std::cout << current->getCommand()->getName() << '\n';
std::cout << current->getCommand()->getConfig() << '\n';
}
}
......
......@@ -11,8 +11,9 @@ private:
std::shared_ptr<Command> cmd;
public:
Element(Command _cmd) { cmd = std::make_shared<Command>(_cmd); }
~Element() { std::cout << "~Element - " << cmd->getName() << '\n'; }
template <class Type>
Element(Type _cmd) { cmd = std::make_shared<Type>(_cmd); }
Element() {}
std::shared_ptr<Command> getCommand() { return cmd; }
void setNext(std::shared_ptr<Element> _next) { next = _next; }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment