Skip to content
Snippets Groups Projects
Commit e7c13415 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

move semantics

parent 98baf15e
No related branches found
No related tags found
No related merge requests found
Showing with 73 additions and 71 deletions
CC=g++ CC=g++
EXECUTABLE=adeterminize EXECUTABLE=adeterminize
CCFLAGS= -std=c++11 -O2 -c -Wall -I../alib/src -I/usr/include/libxml2/ CCFLAGS= -std=c++11 -O2 -c -Wall -I../alib2/src -I/usr/include/libxml2/
LDFLAGS= -L../alib/lib -lxml2 -lalib -Wl,-rpath,. LDFLAGS= -L../alib/lib2 -lxml2 -lalib2 -Wl,-rpath,.
   
SOURCES=$(shell find src/ -name *cpp) SOURCES=$(shell find src/ -name *cpp)
OBJECTS=$(patsubst src/%.cpp, obj/%.o, $(SOURCES)) OBJECTS=$(patsubst src/%.cpp, obj/%.o, $(SOURCES))
......
...@@ -33,7 +33,7 @@ Alternation& Alternation::operator=(const Alternation& other) { ...@@ -33,7 +33,7 @@ Alternation& Alternation::operator=(const Alternation& other) {
return *this; return *this;
} }
   
Alternation& Alternation::operator=(Alternation&& other) { Alternation& Alternation::operator=(Alternation&& other) noexcept {
std::swap(this->elements, other.elements); std::swap(this->elements, other.elements);
return *this; return *this;
......
...@@ -25,7 +25,7 @@ public: ...@@ -25,7 +25,7 @@ public:
Alternation(const Alternation& other); Alternation(const Alternation& other);
Alternation(Alternation&& other) noexcept; Alternation(Alternation&& other) noexcept;
Alternation& operator =(const Alternation& other); Alternation& operator =(const Alternation& other);
Alternation& operator =(Alternation&& other); Alternation& operator =(Alternation&& other) noexcept;
~Alternation() noexcept; ~Alternation() noexcept;
   
/** /**
......
...@@ -18,24 +18,27 @@ Concatenation::Concatenation(const Concatenation& other) { ...@@ -18,24 +18,27 @@ Concatenation::Concatenation(const Concatenation& other) {
} }
} }
   
Concatenation& Concatenation::operator =(const Concatenation& other) { Concatenation::Concatenation(Concatenation&& other) noexcept : elements(std::move(other.elements)) {
other.elements.clear();
}
Concatenation& Concatenation::operator=(const Concatenation& other) {
if(this == &other) { if(this == &other) {
return *this; return *this;
} }
   
for (auto element : elements) { *this = Concatenation(other);
delete element;
} return *this;
elements.clear(); }
for (const auto& element : other.elements) {
elements.push_back(element->clone());
}
   
Concatenation& Concatenation::operator=(Concatenation&& other) noexcept {
std::swap(this->elements, other.elements);
return *this; return *this;
} }
   
Concatenation::~Concatenation() { Concatenation::~Concatenation() noexcept {
for (auto element : elements) { for (auto element : elements) {
delete element; delete element;
} }
......
...@@ -23,8 +23,10 @@ private: ...@@ -23,8 +23,10 @@ private:
public: public:
Concatenation(); Concatenation();
Concatenation(const Concatenation& other); Concatenation(const Concatenation& other);
Concatenation(Concatenation&& other) noexcept;
Concatenation& operator =(const Concatenation& other); Concatenation& operator =(const Concatenation& other);
~Concatenation(); Concatenation& operator =(Concatenation&& other) noexcept;
~Concatenation() noexcept;
   
/** /**
* @return list of operands * @return list of operands
......
...@@ -23,25 +23,27 @@ Iteration::Iteration(const Iteration& other) { ...@@ -23,25 +23,27 @@ Iteration::Iteration(const Iteration& other) {
   
} }
   
Iteration::Iteration(Iteration&& other) noexcept : element(other.element) {
other.element = NULL;
}
Iteration& Iteration::operator=(const Iteration& other) { Iteration& Iteration::operator=(const Iteration& other) {
if (this == &other) { if (this == &other) {
return *this; return *this;
} }
   
if (element != NULL) { *this = Iteration(other);
delete element;
}
   
if (other.element != NULL) { return *this;
element = other.element->clone(); }
} else {
element = NULL; Iteration& Iteration::operator=(Iteration&& other) noexcept {
} std::swap(this->element, other.element);
   
return *this; return *this;
} }
   
regexp::Iteration::~Iteration() { regexp::Iteration::~Iteration() noexcept {
if (element != NULL) { if (element != NULL) {
delete element; delete element;
} }
......
...@@ -23,8 +23,10 @@ private: ...@@ -23,8 +23,10 @@ private:
public: public:
Iteration(); Iteration();
Iteration(const Iteration& other); Iteration(const Iteration& other);
Iteration(Iteration&& other) noexcept;
Iteration& operator =(const Iteration& other); Iteration& operator =(const Iteration& other);
~Iteration(); Iteration& operator =(Iteration&& other) noexcept;
~Iteration() noexcept;
   
/** /**
* @return element to iterate * @return element to iterate
......
...@@ -6,55 +6,50 @@ ...@@ -6,55 +6,50 @@
*/ */
   
#include "RegExp.h" #include "RegExp.h"
#include "../AlibException.h"
   
#include <iostream> #include <iostream>
   
namespace regexp { namespace regexp {
   
RegExp::RegExp() { RegExp::RegExp() {
regExp = NULL; this->regExp = new RegExpEmpty();
} }
   
RegExp::RegExp(const RegExp& other) { RegExp::RegExp(const RegExpElement* regExp) {
if (other.regExp != NULL) { if (regExp == NULL) {
regExp = other.regExp->clone(); throw alib::AlibException();
} else {
regExp = NULL;
} }
this->regExp = regExp->clone();
} }
   
RegExp::RegExp(const RegExpElement* regExp) { RegExp::RegExp(const RegExp& other) : regExp(other.regExp->clone()) {
if (regExp != NULL) {
this->regExp = regExp->clone();
} else {
this->regExp = NULL;
}
} }
   
RegExp& RegExp::operator =(const RegExp& other) { RegExp::RegExp(RegExp&& other) noexcept : regExp(other.regExp) {
other.regExp = NULL;
}
RegExp& RegExp::operator=(const RegExp& other) {
if (this == &other) { if (this == &other) {
return *this; return *this;
} }
   
if (regExp != NULL) { *this = RegExp(other);
delete regExp;
}
if (other.regExp != NULL) {
regExp = other.regExp->clone();
} else {
regExp = NULL;
}
   
return *this; return *this;
} }
   
RegExp::~RegExp() { RegExp& RegExp::operator=(RegExp&& other) noexcept {
if (regExp != NULL) { std::swap(this->regExp, other.regExp);
delete regExp;
}
   
regExp = NULL; return *this;
}
RegExp::~RegExp() noexcept {
delete regExp;
} }
   
RegExpElement* RegExp::getRegExp() { RegExpElement* RegExp::getRegExp() {
...@@ -72,8 +67,7 @@ void RegExp::setRegExp(RegExpElement* regExp) { ...@@ -72,8 +67,7 @@ void RegExp::setRegExp(RegExpElement* regExp) {
std::set<alphabet::Symbol> RegExp::getAlphabet() const { std::set<alphabet::Symbol> RegExp::getAlphabet() const {
std::set<alphabet::Symbol> alphabet; std::set<alphabet::Symbol> alphabet;
   
if(regExp) regExp->getAlphabet( alphabet );
regExp->getAlphabet( alphabet );
   
return alphabet; return alphabet;
} }
...@@ -83,14 +77,11 @@ bool RegExp::isEmpty() const { ...@@ -83,14 +77,11 @@ bool RegExp::isEmpty() const {
} }
   
bool RegExp::containsEmptyString() const { bool RegExp::containsEmptyString() const {
if(regExp) return regExp->containsEmptyString();
return regExp->containsEmptyString();
return false;
} }
   
std::ostream& operator <<(std::ostream& out, const RegExp& regexp) { std::ostream& operator <<(std::ostream& out, const RegExp& regExp) {
out << "(RegExp " << *(regexp.regExp) << ")"; out << "(RegExp " << *(regExp.regExp) << ")";
return out; return out;
} }
   
......
...@@ -34,8 +34,10 @@ public: ...@@ -34,8 +34,10 @@ public:
* @param other RegExp to copy * @param other RegExp to copy
*/ */
RegExp(const RegExp& other); RegExp(const RegExp& other);
RegExp(RegExp&& other) noexcept;
RegExp& operator =(const RegExp& other); RegExp& operator =(const RegExp& other);
~RegExp(); RegExp& operator =(RegExp&& other) noexcept;
~RegExp() noexcept;
   
/** /**
* @return Root node of the regular expression tree * @return Root node of the regular expression tree
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
   
namespace regexp { namespace regexp {
   
RegExpElement::~RegExpElement() { RegExpElement::~RegExpElement() noexcept {
   
} }
   
......
...@@ -26,7 +26,7 @@ class RegExpEpsilon; ...@@ -26,7 +26,7 @@ class RegExpEpsilon;
*/ */
class RegExpElement : virtual public std::elementBase<std::visitor<Alternation, Concatenation, Iteration, RegExpSymbol, RegExpEmpty, RegExpEpsilon> > { class RegExpElement : virtual public std::elementBase<std::visitor<Alternation, Concatenation, Iteration, RegExpSymbol, RegExpEmpty, RegExpEpsilon> > {
public: public:
virtual ~RegExpElement(); virtual ~RegExpElement() noexcept;
   
/** /**
* Creates copy of the element. * Creates copy of the element.
......
...@@ -13,7 +13,7 @@ RegExpEmpty::RegExpEmpty() { ...@@ -13,7 +13,7 @@ RegExpEmpty::RegExpEmpty() {
} }
   
RegExpElement* RegExpEmpty::clone() const { RegExpElement* RegExpEmpty::clone() const {
return new RegExpEmpty(); return new RegExpEmpty(*this);
} }
   
bool RegExpEmpty::operator<(const RegExpElement& other) const { bool RegExpEmpty::operator<(const RegExpElement& other) const {
......
...@@ -13,7 +13,7 @@ RegExpEpsilon::RegExpEpsilon() { ...@@ -13,7 +13,7 @@ RegExpEpsilon::RegExpEpsilon() {
} }
   
RegExpElement* RegExpEpsilon::clone() const { RegExpElement* RegExpEpsilon::clone() const {
return new RegExpEpsilon(); return new RegExpEpsilon(*this);
} }
   
bool RegExpEpsilon::operator<(const RegExpElement& other) const { bool RegExpEpsilon::operator<(const RegExpElement& other) const {
......
...@@ -9,16 +9,16 @@ ...@@ -9,16 +9,16 @@
   
namespace regexp { namespace regexp {
   
RegExpSymbol::RegExpSymbol() :
symbol("") {
}
RegExpSymbol::RegExpSymbol(const std::string& symbol) : RegExpSymbol::RegExpSymbol(const std::string& symbol) :
symbol(symbol) { symbol(symbol) {
} }
   
RegExpSymbol::RegExpSymbol(std::string&& symbol) :
symbol(move(symbol)) {
}
RegExpElement* RegExpSymbol::clone() const { RegExpElement* RegExpSymbol::clone() const {
return new RegExpSymbol(this->symbol); return new RegExpSymbol(*this);
} }
   
bool RegExpSymbol::operator<(const RegExpElement& other) const { bool RegExpSymbol::operator<(const RegExpElement& other) const {
......
...@@ -20,8 +20,8 @@ namespace regexp { ...@@ -20,8 +20,8 @@ namespace regexp {
class RegExpSymbol : public RegExpElement, public std::element<RegExpSymbol, RegExpElement::visitor_type> { class RegExpSymbol : public RegExpElement, public std::element<RegExpSymbol, RegExpElement::visitor_type> {
std::string symbol; std::string symbol;
public: public:
RegExpSymbol();
RegExpSymbol(const std::string& symbol); RegExpSymbol(const std::string& symbol);
RegExpSymbol(std::string&& symbol);
   
/** /**
* @copydoc RegExpElement::clone() const * @copydoc RegExpElement::clone() const
......
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