From 90ad67d556be9343315cdfa6b720574cd71b7663 Mon Sep 17 00:00:00 2001
From: tobiglaser <76131623+tobiglaser@users.noreply.github.com>
Date: Fri, 22 Apr 2022 23:57:45 +0200
Subject: [PATCH] update List to WS2 + resolve polymorphism hiccups

---
 src/CommandListOWN.h | 26 ++++++++++++++++++++++----
 src/Element.h        |  5 +++--
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/src/CommandListOWN.h b/src/CommandListOWN.h
index b709c5b..b5cde4e 100644
--- a/src/CommandListOWN.h
+++ b/src/CommandListOWN.h
@@ -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';
     }
 }
 
diff --git a/src/Element.h b/src/Element.h
index a87b644..24f6af2 100644
--- a/src/Element.h
+++ b/src/Element.h
@@ -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; }
-- 
GitLab