From 43d18309bfb4c90abdc22c6b4598996aad4750cc Mon Sep 17 00:00:00 2001
From: Martin Zak <zakmart1@fit.cvut.cz>
Date: Sat, 23 Nov 2013 21:47:36 +0100
Subject: [PATCH] Adds copy-constructor, operator = and destructor to RegExp
 elements

---
 alib/src/regexp/Alternation.cpp   | 54 +++++++++++++++++++++++++++++++
 alib/src/regexp/Alternation.h     |  6 ++++
 alib/src/regexp/Iteration.cpp     | 30 +++++++++++++++++
 alib/src/regexp/Iteration.h       |  4 +++
 alib/src/regexp/RegExp.cpp        | 26 +++++++++++++--
 alib/src/regexp/RegExp.h          |  9 +++++-
 alib/src/regexp/RegExpElement.cpp |  5 +--
 alib/src/regexp/RegExpElement.h   |  2 +-
 alib/src/regexp/RegExpSymbol.cpp  | 13 +++++---
 alib/src/regexp/RegExpSymbol.h    |  2 +-
 10 files changed, 139 insertions(+), 12 deletions(-)

diff --git a/alib/src/regexp/Alternation.cpp b/alib/src/regexp/Alternation.cpp
index 23e98e68b9..721641b07e 100644
--- a/alib/src/regexp/Alternation.cpp
+++ b/alib/src/regexp/Alternation.cpp
@@ -9,7 +9,47 @@
 
 namespace regexp {
 
+Alternation::Alternation() {
+}
+
+Alternation::Alternation(const Alternation& other) {
+	for (auto element : other.first) {
+		first.push_back(element->clone());
+	}
+	for (auto element : other.second) {
+		second.push_back(element->clone());
+	}
+}
+
+Alternation& Alternation::operator =(const Alternation& other) {
+	for (auto element : first) {
+		delete element;
+	}
+	first.clear();
+
+	for (auto element : second) {
+		delete element;
+	}
+	first.clear();
+
+	for (auto element : other.first) {
+		first.push_back(element->clone());
+	}
+	for (auto element : other.second) {
+		second.push_back(element->clone());
+	}
+}
+
 Alternation::~Alternation() {
+	for (auto element : first) {
+		delete element;
+	}
+	first.clear();
+
+	for (auto element : second) {
+		delete element;
+	}
+	first.clear();
 }
 
 list<RegExpElement*>& Alternation::getFirst() {
@@ -20,5 +60,19 @@ list<RegExpElement*>& Alternation::getSecond() {
 	return second;
 }
 
+RegExpElement* Alternation::clone() {
+	Alternation* toReturn = new Alternation();
+
+	for (auto element : first) {
+		toReturn->getFirst().push_back(element->clone());
+	}
+
+	for (auto element : second) {
+		toReturn->getSecond().push_back(element->clone());
+	}
+
+	return toReturn;
+}
+
 } /* namespace regexp */
 
diff --git a/alib/src/regexp/Alternation.h b/alib/src/regexp/Alternation.h
index 6e1cdf5ed4..8ea6abee3f 100644
--- a/alib/src/regexp/Alternation.h
+++ b/alib/src/regexp/Alternation.h
@@ -20,9 +20,15 @@ private:
 	list<RegExpElement*> first;
 	list<RegExpElement*> second;
 public:
+	Alternation();
+	Alternation(const Alternation& other);
+	Alternation& operator = (const Alternation& other);
 	~Alternation();
+
 	list<RegExpElement*>& getFirst();
 	list<RegExpElement*>& getSecond();
+
+	RegExpElement* clone();
 };
 
 } /* namespace regexp */
diff --git a/alib/src/regexp/Iteration.cpp b/alib/src/regexp/Iteration.cpp
index 6e95c17edf..f43c577dc5 100644
--- a/alib/src/regexp/Iteration.cpp
+++ b/alib/src/regexp/Iteration.cpp
@@ -8,15 +8,45 @@
 #include "Iteration.h"
 
 namespace regexp {
+
+Iteration::Iteration() {
+}
+
+Iteration::Iteration(const Iteration& other) {
+	for (auto element : other.elements) {
+		elements.push_back(element->clone());
+	}
+}
+
+Iteration& Iteration::operator=(const Iteration& other) {
+	for (auto element : elements) {
+		delete element;
+	}
+	elements.clear();
+
+	for (auto element : other.elements) {
+		elements.push_back(element->clone());
+	}
+}
+
 regexp::Iteration::~Iteration() {
 	for (auto element : elements) {
 		delete element;
 	}
+	elements.clear();
 }
 
 list<RegExpElement*>& regexp::Iteration::getElements() {
 	return elements;
 }
 
+RegExpElement* Iteration::clone() {
+	Iteration* toReturn = new Iteration();
+	for (auto element : elements) {
+		toReturn->getElements().push_back(element->clone());
+	}
+	return toReturn;
+}
+
 } /* namespace regexp */
 
diff --git a/alib/src/regexp/Iteration.h b/alib/src/regexp/Iteration.h
index 0bec4d49f5..d07a5dad07 100644
--- a/alib/src/regexp/Iteration.h
+++ b/alib/src/regexp/Iteration.h
@@ -19,8 +19,12 @@ class Iteration : public RegExpElement {
 private:
 	list<RegExpElement*> elements;
 public:
+	Iteration();
+	Iteration(const Iteration& other);
+	Iteration& operator = (const Iteration& other);
 	~Iteration();
 	list<RegExpElement*>& getElements();
+	RegExpElement* clone();
 };
 
 } /* namespace regexp */
diff --git a/alib/src/regexp/RegExp.cpp b/alib/src/regexp/RegExp.cpp
index 0151ffd4f3..cdcf4ae055 100644
--- a/alib/src/regexp/RegExp.cpp
+++ b/alib/src/regexp/RegExp.cpp
@@ -8,14 +8,16 @@
 #include "RegExp.h"
 #include "RegExpPrinter.h"
 
+#include <iostream>
+
 namespace regexp {
 
 RegExp::RegExp() {
 }
 
-RegExp::~RegExp() {
-	for(auto element : regexp) {
-		delete element;
+RegExp::RegExp(const RegExp& other) {
+	for (auto element : other.regexp) {
+		regexp.push_back(element->clone());
 	}
 }
 
@@ -23,6 +25,24 @@ RegExp::RegExp(const list<RegExpElement*>& regexp) :
 		regexp(regexp) {
 }
 
+RegExp& RegExp::operator =(const RegExp& other) {
+	for (auto element : regexp) {
+		delete element;
+	}
+	regexp.clear();
+
+	for (auto element : other.regexp) {
+		regexp.push_back(element->clone());
+	}
+}
+
+RegExp::~RegExp() {
+	for (auto element : regexp) {
+		delete element;
+	}
+	regexp.clear();
+}
+
 list<RegExpElement*>& RegExp::getRegExp() {
 	return regexp;
 }
diff --git a/alib/src/regexp/RegExp.h b/alib/src/regexp/RegExp.h
index 35a2f77235..94e0c43320 100644
--- a/alib/src/regexp/RegExp.h
+++ b/alib/src/regexp/RegExp.h
@@ -23,10 +23,17 @@ private:
 
 public:
 	RegExp();
-	~RegExp();
 	RegExp(const list<RegExpElement*>& regexp) ;
+
+	RegExp(const RegExp& other);
+	RegExp& operator = (const RegExp& other);
+	~RegExp();
+
 	list<RegExpElement*>& getRegExp();
 
+
+
+
 	void toXML(ostream& out);
 };
 
diff --git a/alib/src/regexp/RegExpElement.cpp b/alib/src/regexp/RegExpElement.cpp
index eacf72d353..d7231b3c07 100644
--- a/alib/src/regexp/RegExpElement.cpp
+++ b/alib/src/regexp/RegExpElement.cpp
@@ -9,7 +9,8 @@
 
 namespace regexp {
 
+RegExpElement::~RegExpElement() {
+}
+
 } /* namespace regexp */
 
-regexp::RegExpElement::~RegExpElement() {
-}
diff --git a/alib/src/regexp/RegExpElement.h b/alib/src/regexp/RegExpElement.h
index b2ed1df406..225f3ef48a 100644
--- a/alib/src/regexp/RegExpElement.h
+++ b/alib/src/regexp/RegExpElement.h
@@ -15,7 +15,7 @@ using namespace std;
 class RegExpElement {
 public:
 	virtual ~RegExpElement();
-
+	virtual RegExpElement* clone() = 0;
 };
 
 } /* namespace regexp */
diff --git a/alib/src/regexp/RegExpSymbol.cpp b/alib/src/regexp/RegExpSymbol.cpp
index 85c6f159dd..ed17cfd453 100644
--- a/alib/src/regexp/RegExpSymbol.cpp
+++ b/alib/src/regexp/RegExpSymbol.cpp
@@ -9,12 +9,17 @@
 
 namespace regexp {
 
-} /* namespace regexp */
-
-regexp::RegExpSymbol::RegExpSymbol() :
+RegExpSymbol::RegExpSymbol() :
 		Symbol("") {
 }
 
-regexp::RegExpSymbol::RegExpSymbol(const string& symbol) :
+RegExpSymbol::RegExpSymbol(const string& symbol) :
 		Symbol(symbol) {
 }
+
+RegExpElement* RegExpSymbol::clone() {
+	return new RegExpSymbol(this->symbol);
+}
+
+} /* namespace regexp */
+
diff --git a/alib/src/regexp/RegExpSymbol.h b/alib/src/regexp/RegExpSymbol.h
index c2bac3c251..b07319284f 100644
--- a/alib/src/regexp/RegExpSymbol.h
+++ b/alib/src/regexp/RegExpSymbol.h
@@ -21,7 +21,7 @@ class RegExpSymbol: public RegExpElement, public Symbol {
 public:
 	RegExpSymbol();
 	RegExpSymbol(const string& symbol);
-
+	RegExpElement* clone();
 };
 
 } /* namespace regexp */
-- 
GitLab