From 238b79a638fd57675730732f15b35f338b6d6dcf Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Sat, 12 Apr 2014 12:52:32 +0200
Subject: [PATCH] no shared_ptr keep pointers hidden

---
 alib2/src/regexp/Alternation.cpp        | 13 ++++++++-----
 alib2/src/regexp/Alternation.h          |  5 ++---
 alib2/src/regexp/Concatenation.cpp      | 13 ++++++++-----
 alib2/src/regexp/Concatenation.h        |  5 ++---
 alib2/src/regexp/Iteration.cpp          | 14 ++++++++------
 alib2/src/regexp/Iteration.h            |  5 ++---
 alib2/src/regexp/RegExp.cpp             | 16 +++++++++-------
 alib2/src/regexp/RegExp.h               |  5 ++---
 alib2/src/regexp/RegExpElement.h        |  7 +++++++
 alib2/src/regexp/RegExpToXMLPrinter.cpp |  4 ++--
 10 files changed, 50 insertions(+), 37 deletions(-)

diff --git a/alib2/src/regexp/Alternation.cpp b/alib2/src/regexp/Alternation.cpp
index 8f5c6618e7..d12fce3d4c 100644
--- a/alib2/src/regexp/Alternation.cpp
+++ b/alib2/src/regexp/Alternation.cpp
@@ -16,7 +16,7 @@ Alternation::Alternation() {
 
 Alternation::Alternation(const Alternation& other) {
 	for (const auto& element : other.elements) {
-		elements.insert(std::shared_ptr<RegExpElement>(element->clone()));
+		elements.insert(element->clone());
 	}
 }
 
@@ -41,19 +41,22 @@ Alternation& Alternation::operator=(Alternation&& other) noexcept {
 }
 
 Alternation::~Alternation() noexcept {
+	for (auto element : elements) {
+		delete element;
+	}
 	elements.clear();
 }
 
-const std::set<std::shared_ptr<const RegExpElement>, std::owner_less<std::shared_ptr<const RegExpElement> > > & Alternation::getElements() const {
-	return * reinterpret_cast<const std::set<std::shared_ptr<const RegExpElement>, std::owner_less<std::shared_ptr<const RegExpElement> > > * > (&elements);
+const std::set<const RegExpElement*, RegExpElement::PointerLess > & Alternation::getElements() const {
+	return * reinterpret_cast<const std::set<const RegExpElement*, RegExpElement::PointerLess > * > (&elements);
 }
 
 void Alternation::insertElement(const RegExpElement& element) {
-	this->elements.insert(std::shared_ptr<RegExpElement>(element.clone()));
+	this->elements.insert(element.clone());
 }
 
 void Alternation::insertElement(RegExpElement&& element) {
-	this->elements.insert(std::shared_ptr<RegExpElement>(std::move(element).plunder()));
+	this->elements.insert(std::move(element).plunder());
 }
 
 RegExpElement* Alternation::clone() const {
diff --git a/alib2/src/regexp/Alternation.h b/alib2/src/regexp/Alternation.h
index d5fc72c94b..75c9b1c5eb 100644
--- a/alib2/src/regexp/Alternation.h
+++ b/alib2/src/regexp/Alternation.h
@@ -9,7 +9,6 @@
 #define ALTERNATION_H_
 
 #include <set>
-#include <memory>
 #include "RegExpElement.h"
 
 namespace regexp {
@@ -27,7 +26,7 @@ protected:
 	
 	virtual RegExpElement* plunder() &&;
 
-	std::set<std::shared_ptr<RegExpElement>, std::owner_less<std::shared_ptr<RegExpElement> > > elements;
+	std::set<RegExpElement*, RegExpElement::PointerLess > elements;
 public:
 	Alternation();
 	Alternation(const Alternation& other);
@@ -39,7 +38,7 @@ public:
 	/**
 	 * @return elements
 	 */
-	const std::set<std::shared_ptr<const RegExpElement>, std::owner_less<std::shared_ptr<const RegExpElement> > > & getElements() const;
+	const std::set<const RegExpElement*, RegExpElement::PointerLess > & getElements() const;
 
 	/**
 	 * @param element to insert
diff --git a/alib2/src/regexp/Concatenation.cpp b/alib2/src/regexp/Concatenation.cpp
index baff263883..c658061daa 100644
--- a/alib2/src/regexp/Concatenation.cpp
+++ b/alib2/src/regexp/Concatenation.cpp
@@ -15,7 +15,7 @@ Concatenation::Concatenation() {
 
 Concatenation::Concatenation(const Concatenation& other) {
 	for (auto element : other.elements) {
-		elements.push_back(std::shared_ptr<RegExpElement>(element->clone()));
+		elements.push_back(element->clone());
 	}
 }
 
@@ -40,19 +40,22 @@ Concatenation& Concatenation::operator=(Concatenation&& other) noexcept {
 }
 
 Concatenation::~Concatenation() noexcept {
+	for (auto element : elements) {
+		delete element;
+	}
 	elements.clear();
 }
 
-const std::vector<std::shared_ptr<const RegExpElement> > & Concatenation::getElements() const {
-	return * reinterpret_cast<const std::vector<std::shared_ptr<const RegExpElement> > * > (&elements);
+const std::vector<const RegExpElement*> & Concatenation::getElements() const {
+	return * reinterpret_cast<const std::vector<const RegExpElement*> * > (&elements);
 }
 
 void Concatenation::appendElement(const RegExpElement& element) {
-	this->elements.push_back(std::shared_ptr<RegExpElement>(element.clone()));
+	this->elements.push_back(element.clone());
 }
 
 void Concatenation::appendElement(RegExpElement&& element) {
-	this->elements.push_back(std::shared_ptr<RegExpElement>(std::move(element).plunder()));
+	this->elements.push_back(std::move(element).plunder());
 }
 
 RegExpElement* Concatenation::clone() const {
diff --git a/alib2/src/regexp/Concatenation.h b/alib2/src/regexp/Concatenation.h
index 504e4570e0..3370011c57 100644
--- a/alib2/src/regexp/Concatenation.h
+++ b/alib2/src/regexp/Concatenation.h
@@ -9,7 +9,6 @@
 #define CONCATENATION_H_
 
 #include <vector>
-#include <memory>
 #include "RegExpElement.h"
 
 namespace regexp {
@@ -27,7 +26,7 @@ protected:
 	
 	virtual RegExpElement* plunder() &&;
 
-	std::vector<std::shared_ptr<RegExpElement> > elements;
+	std::vector<RegExpElement*> elements;
 public:
 	Concatenation();
 	Concatenation(const Concatenation& other);
@@ -39,7 +38,7 @@ public:
 	/**
 	 * @return elements
 	 */
-	const std::vector<std::shared_ptr<const RegExpElement> > & getElements() const;
+	const std::vector<const RegExpElement*> & getElements() const;
 
 	/**
 	 * @param element to append
diff --git a/alib2/src/regexp/Iteration.cpp b/alib2/src/regexp/Iteration.cpp
index 06335296dd..b3da1aac9f 100644
--- a/alib2/src/regexp/Iteration.cpp
+++ b/alib2/src/regexp/Iteration.cpp
@@ -11,7 +11,7 @@
 namespace regexp {
 
 Iteration::Iteration() {
-	element = std::shared_ptr<RegExpElement>(new RegExpEmpty());
+	element = new RegExpEmpty();
 }
 
 Iteration::Iteration(const Iteration& other) : element(other.element->clone()) {
@@ -39,19 +39,21 @@ Iteration& Iteration::operator=(Iteration&& other) noexcept {
 }
 
 regexp::Iteration::~Iteration() noexcept {
-
+	delete element;
 }
 
-const std::shared_ptr<const RegExpElement> & Iteration::getElement() const {
-	return * reinterpret_cast<const std::shared_ptr<const RegExpElement> * >  (&element);
+const RegExpElement & Iteration::getElement() const {
+	return *element;
 }
 
 void Iteration::setElement(const RegExpElement& element) {
-	this->element = std::shared_ptr<RegExpElement>(element.clone());
+	delete this->element;
+	this->element = element.clone();
 }
 
 void Iteration::setElement(RegExpElement&& element) {
-	this->element = std::shared_ptr<RegExpElement>(std::move(element).plunder());
+	delete this->element;
+	this->element = std::move(element).plunder();
 }
 
 RegExpElement* Iteration::clone() const {
diff --git a/alib2/src/regexp/Iteration.h b/alib2/src/regexp/Iteration.h
index 8b1d8d5e0e..687fdff80d 100644
--- a/alib2/src/regexp/Iteration.h
+++ b/alib2/src/regexp/Iteration.h
@@ -8,7 +8,6 @@
 #ifndef ITERATION_H_
 #define ITERATION_H_
 
-#include <memory>
 #include "RegExpElement.h"
 #include "RegExpEmpty.h"
 
@@ -20,7 +19,7 @@ namespace regexp {
  */
 class Iteration: public RegExpElement, public std::element<Iteration, RegExpElement::visitor_type> {
 protected:
-	std::shared_ptr<RegExpElement> element;
+	RegExpElement* element;
 
 	/**
 	 * @copydoc RegExpElement::clone() const
@@ -39,7 +38,7 @@ public:
 	/**
 	 * @return element
 	 */
-	const std::shared_ptr<const RegExpElement> & getElement() const;
+	const RegExpElement & getElement() const;
 	
 	/**
 	 * @param element to iterate
diff --git a/alib2/src/regexp/RegExp.cpp b/alib2/src/regexp/RegExp.cpp
index 9435cde0e6..7d3e079a40 100644
--- a/alib2/src/regexp/RegExp.cpp
+++ b/alib2/src/regexp/RegExp.cpp
@@ -13,11 +13,11 @@
 namespace regexp {
 
 RegExp::RegExp() {
-	this->regExp = std::shared_ptr<RegExpElement>(new RegExpEmpty());
+	this->regExp = new RegExpEmpty();
 }
 
 RegExp::RegExp(const RegExpElement& regExp) {
-	this->regExp = std::shared_ptr<RegExpElement>(regExp.clone());
+	this->regExp = regExp.clone();
 }
 
 RegExp::RegExp(const RegExp& other) : regExp(other.regExp->clone()) {
@@ -45,19 +45,21 @@ RegExp& RegExp::operator=(RegExp&& other) noexcept {
 }
 
 RegExp::~RegExp() noexcept {
-
+	delete regExp;
 }
 
-const std::shared_ptr<const RegExpElement> RegExp::getRegExp() const {
-	return std::shared_ptr<const RegExpElement>(regExp);
+const RegExpElement& RegExp::getRegExp() const {
+	return *regExp;
 }
 
 void RegExp::setRegExp(const RegExpElement& regExp) {
-	this->regExp = std::shared_ptr<RegExpElement>(regExp.clone());
+	delete this->regExp;
+	this->regExp = regExp.clone();
 }
 
 void RegExp::setRegExp(RegExpElement&& regExp) {
-	this->regExp = std::shared_ptr<RegExpElement>(std::move(regExp).plunder());
+	delete this->regExp;
+	this->regExp = std::move(regExp).plunder();
 }
 
 std::set<alphabet::Symbol> RegExp::getAlphabet() const {
diff --git a/alib2/src/regexp/RegExp.h b/alib2/src/regexp/RegExp.h
index 6db74c39f1..b0fb4f242e 100644
--- a/alib2/src/regexp/RegExp.h
+++ b/alib2/src/regexp/RegExp.h
@@ -11,7 +11,6 @@
 #include <vector>
 #include <list>
 #include <string>
-#include <memory>
 #include "RegExpElement.h"
 #include "RegExpEmpty.h"
 #include "../std/visitor.hpp"
@@ -24,7 +23,7 @@ namespace regexp {
  */
 class RegExp : public std::element<RegExp, std::visitor<RegExp> > {
 protected:
-	std::shared_ptr<RegExpElement> regExp;
+	RegExpElement* regExp;
 
 public:
 	RegExp();
@@ -43,7 +42,7 @@ public:
 	/**
 	 * @return Root node of the regular expression tree
 	 */
-	const std::shared_ptr<const RegExpElement> getRegExp() const;
+	const RegExpElement& getRegExp() const;
 
 	/**
 	 * Sets the root node of the regular expression tree. Doesn't perform copy of the regExp param,
diff --git a/alib2/src/regexp/RegExpElement.h b/alib2/src/regexp/RegExpElement.h
index b08ef261d1..7fc1beb87a 100644
--- a/alib2/src/regexp/RegExpElement.h
+++ b/alib2/src/regexp/RegExpElement.h
@@ -25,6 +25,13 @@ class RegExpEpsilon;
  * Abstract class representing element in the regular expression. Can be operator or symbol.
  */
 class RegExpElement : virtual public std::elementBase<std::visitor<Alternation, Concatenation, Iteration, RegExpSymbol, RegExpEmpty, RegExpEpsilon> > {
+protected:
+	class PointerLess {
+	public:
+		bool operator()(const RegExpElement* a, const RegExpElement* b) {
+			return *a < *b;
+		}
+	};
 public:
 	/**
 	 * Creates copy of the element.
diff --git a/alib2/src/regexp/RegExpToXMLPrinter.cpp b/alib2/src/regexp/RegExpToXMLPrinter.cpp
index 5d6d2e797e..2d5553de8b 100644
--- a/alib2/src/regexp/RegExpToXMLPrinter.cpp
+++ b/alib2/src/regexp/RegExpToXMLPrinter.cpp
@@ -44,7 +44,7 @@ void RegExpToXMLPrinter::Visit(const Concatenation& concatenation) {
 
 void RegExpToXMLPrinter::Visit(const Iteration& iteration) {
 	m_Out << "<iteration>" << std::endl;
-	const RegExpElement::element_type& object = static_cast<const RegExpElement::element_type&>(*iteration.getElement());
+	const RegExpElement::element_type& object = static_cast<const RegExpElement::element_type&>(iteration.getElement());
 	object.Accept(*this);
 	m_Out << "</iteration>" << std::endl;
 }
@@ -67,7 +67,7 @@ void RegExpToXMLPrinter::Visit(const RegExpEmpty& empty) {
 
 void RegExpToXMLPrinter::Visit(const RegExp& regexp) {
 	m_Out << "<regexp>" << std::endl;
-	regexp.getRegExp()->Accept(*this);
+	regexp.getRegExp().Accept(*this);
 	m_Out << "</regexp>" << std::endl;
 }
 
-- 
GitLab