From e7c13415e261401a5a13db8e5aaf67adb1550477 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 10 Apr 2014 16:02:25 +0200 Subject: [PATCH] move semantics --- adeterminize/makefile | 4 +- alib2/src/regexp/Alternation.cpp | 2 +- alib2/src/regexp/Alternation.h | 2 +- alib2/src/regexp/Concatenation.cpp | 23 ++++++----- alib2/src/regexp/Concatenation.h | 4 +- alib2/src/regexp/Iteration.cpp | 20 +++++----- alib2/src/regexp/Iteration.h | 4 +- alib2/src/regexp/RegExp.cpp | 61 +++++++++++++----------------- alib2/src/regexp/RegExp.h | 4 +- alib2/src/regexp/RegExpElement.cpp | 2 +- alib2/src/regexp/RegExpElement.h | 2 +- alib2/src/regexp/RegExpEmpty.cpp | 2 +- alib2/src/regexp/RegExpEpsilon.cpp | 2 +- alib2/src/regexp/RegExpSymbol.cpp | 10 ++--- alib2/src/regexp/RegExpSymbol.h | 2 +- 15 files changed, 73 insertions(+), 71 deletions(-) diff --git a/adeterminize/makefile b/adeterminize/makefile index ae6e677504..973abf7a9d 100644 --- a/adeterminize/makefile +++ b/adeterminize/makefile @@ -1,7 +1,7 @@ CC=g++ EXECUTABLE=adeterminize -CCFLAGS= -std=c++11 -O2 -c -Wall -I../alib/src -I/usr/include/libxml2/ -LDFLAGS= -L../alib/lib -lxml2 -lalib -Wl,-rpath,. +CCFLAGS= -std=c++11 -O2 -c -Wall -I../alib2/src -I/usr/include/libxml2/ +LDFLAGS= -L../alib/lib2 -lxml2 -lalib2 -Wl,-rpath,. SOURCES=$(shell find src/ -name *cpp) OBJECTS=$(patsubst src/%.cpp, obj/%.o, $(SOURCES)) diff --git a/alib2/src/regexp/Alternation.cpp b/alib2/src/regexp/Alternation.cpp index 9b3bd1e821..8745b14392 100644 --- a/alib2/src/regexp/Alternation.cpp +++ b/alib2/src/regexp/Alternation.cpp @@ -33,7 +33,7 @@ Alternation& Alternation::operator=(const Alternation& other) { return *this; } -Alternation& Alternation::operator=(Alternation&& other) { +Alternation& Alternation::operator=(Alternation&& other) noexcept { std::swap(this->elements, other.elements); return *this; diff --git a/alib2/src/regexp/Alternation.h b/alib2/src/regexp/Alternation.h index 098e601f84..fbddb2b93c 100644 --- a/alib2/src/regexp/Alternation.h +++ b/alib2/src/regexp/Alternation.h @@ -25,7 +25,7 @@ public: Alternation(const Alternation& other); Alternation(Alternation&& other) noexcept; Alternation& operator =(const Alternation& other); - Alternation& operator =(Alternation&& other); + Alternation& operator =(Alternation&& other) noexcept; ~Alternation() noexcept; /** diff --git a/alib2/src/regexp/Concatenation.cpp b/alib2/src/regexp/Concatenation.cpp index a7f4138b28..dcc3aa767e 100644 --- a/alib2/src/regexp/Concatenation.cpp +++ b/alib2/src/regexp/Concatenation.cpp @@ -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) { return *this; } - for (auto element : elements) { - delete element; - } - elements.clear(); - - for (const auto& element : other.elements) { - elements.push_back(element->clone()); - } + *this = Concatenation(other); + + return *this; +} +Concatenation& Concatenation::operator=(Concatenation&& other) noexcept { + std::swap(this->elements, other.elements); + return *this; } -Concatenation::~Concatenation() { +Concatenation::~Concatenation() noexcept { for (auto element : elements) { delete element; } diff --git a/alib2/src/regexp/Concatenation.h b/alib2/src/regexp/Concatenation.h index 3d52b41ef8..be52c02b35 100644 --- a/alib2/src/regexp/Concatenation.h +++ b/alib2/src/regexp/Concatenation.h @@ -23,8 +23,10 @@ private: public: Concatenation(); Concatenation(const Concatenation& other); + Concatenation(Concatenation&& other) noexcept; Concatenation& operator =(const Concatenation& other); - ~Concatenation(); + Concatenation& operator =(Concatenation&& other) noexcept; + ~Concatenation() noexcept; /** * @return list of operands diff --git a/alib2/src/regexp/Iteration.cpp b/alib2/src/regexp/Iteration.cpp index 436b3eaca3..12bfdeea90 100644 --- a/alib2/src/regexp/Iteration.cpp +++ b/alib2/src/regexp/Iteration.cpp @@ -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) { if (this == &other) { return *this; } - if (element != NULL) { - delete element; - } + *this = Iteration(other); - if (other.element != NULL) { - element = other.element->clone(); - } else { - element = NULL; - } + return *this; +} + +Iteration& Iteration::operator=(Iteration&& other) noexcept { + std::swap(this->element, other.element); return *this; } -regexp::Iteration::~Iteration() { +regexp::Iteration::~Iteration() noexcept { if (element != NULL) { delete element; } diff --git a/alib2/src/regexp/Iteration.h b/alib2/src/regexp/Iteration.h index 09617598a8..90d270a3a9 100644 --- a/alib2/src/regexp/Iteration.h +++ b/alib2/src/regexp/Iteration.h @@ -23,8 +23,10 @@ private: public: Iteration(); Iteration(const Iteration& other); + Iteration(Iteration&& other) noexcept; Iteration& operator =(const Iteration& other); - ~Iteration(); + Iteration& operator =(Iteration&& other) noexcept; + ~Iteration() noexcept; /** * @return element to iterate diff --git a/alib2/src/regexp/RegExp.cpp b/alib2/src/regexp/RegExp.cpp index cfa2967a77..6c190100db 100644 --- a/alib2/src/regexp/RegExp.cpp +++ b/alib2/src/regexp/RegExp.cpp @@ -6,55 +6,50 @@ */ #include "RegExp.h" +#include "../AlibException.h" #include <iostream> namespace regexp { RegExp::RegExp() { - regExp = NULL; + this->regExp = new RegExpEmpty(); } -RegExp::RegExp(const RegExp& other) { - if (other.regExp != NULL) { - regExp = other.regExp->clone(); - } else { - regExp = NULL; +RegExp::RegExp(const RegExpElement* regExp) { + if (regExp == NULL) { + throw alib::AlibException(); } + + this->regExp = regExp->clone(); } -RegExp::RegExp(const RegExpElement* regExp) { - if (regExp != NULL) { - this->regExp = regExp->clone(); - } else { - this->regExp = NULL; - } +RegExp::RegExp(const RegExp& other) : regExp(other.regExp->clone()) { + } -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) { return *this; } - if (regExp != NULL) { - delete regExp; - } - - if (other.regExp != NULL) { - regExp = other.regExp->clone(); - } else { - regExp = NULL; - } + *this = RegExp(other); return *this; } -RegExp::~RegExp() { - if (regExp != NULL) { - delete regExp; - } +RegExp& RegExp::operator=(RegExp&& other) noexcept { + std::swap(this->regExp, other.regExp); - regExp = NULL; + return *this; +} + +RegExp::~RegExp() noexcept { + delete regExp; } RegExpElement* RegExp::getRegExp() { @@ -72,8 +67,7 @@ void RegExp::setRegExp(RegExpElement* regExp) { std::set<alphabet::Symbol> RegExp::getAlphabet() const { std::set<alphabet::Symbol> alphabet; - if(regExp) - regExp->getAlphabet( alphabet ); + regExp->getAlphabet( alphabet ); return alphabet; } @@ -83,14 +77,11 @@ bool RegExp::isEmpty() const { } bool RegExp::containsEmptyString() const { - if(regExp) - return regExp->containsEmptyString(); - - return false; + return regExp->containsEmptyString(); } -std::ostream& operator <<(std::ostream& out, const RegExp& regexp) { - out << "(RegExp " << *(regexp.regExp) << ")"; +std::ostream& operator <<(std::ostream& out, const RegExp& regExp) { + out << "(RegExp " << *(regExp.regExp) << ")"; return out; } diff --git a/alib2/src/regexp/RegExp.h b/alib2/src/regexp/RegExp.h index eac9004daf..7bb157b3f4 100644 --- a/alib2/src/regexp/RegExp.h +++ b/alib2/src/regexp/RegExp.h @@ -34,8 +34,10 @@ public: * @param other RegExp to copy */ RegExp(const RegExp& other); + RegExp(RegExp&& other) noexcept; RegExp& operator =(const RegExp& other); - ~RegExp(); + RegExp& operator =(RegExp&& other) noexcept; + ~RegExp() noexcept; /** * @return Root node of the regular expression tree diff --git a/alib2/src/regexp/RegExpElement.cpp b/alib2/src/regexp/RegExpElement.cpp index dd880a8d0d..91065141e6 100644 --- a/alib2/src/regexp/RegExpElement.cpp +++ b/alib2/src/regexp/RegExpElement.cpp @@ -9,7 +9,7 @@ namespace regexp { -RegExpElement::~RegExpElement() { +RegExpElement::~RegExpElement() noexcept { } diff --git a/alib2/src/regexp/RegExpElement.h b/alib2/src/regexp/RegExpElement.h index 12db32db1b..046a0294c1 100644 --- a/alib2/src/regexp/RegExpElement.h +++ b/alib2/src/regexp/RegExpElement.h @@ -26,7 +26,7 @@ class RegExpEpsilon; */ class RegExpElement : virtual public std::elementBase<std::visitor<Alternation, Concatenation, Iteration, RegExpSymbol, RegExpEmpty, RegExpEpsilon> > { public: - virtual ~RegExpElement(); + virtual ~RegExpElement() noexcept; /** * Creates copy of the element. diff --git a/alib2/src/regexp/RegExpEmpty.cpp b/alib2/src/regexp/RegExpEmpty.cpp index 9c37ac53e7..b80ae39dad 100644 --- a/alib2/src/regexp/RegExpEmpty.cpp +++ b/alib2/src/regexp/RegExpEmpty.cpp @@ -13,7 +13,7 @@ RegExpEmpty::RegExpEmpty() { } RegExpElement* RegExpEmpty::clone() const { - return new RegExpEmpty(); + return new RegExpEmpty(*this); } bool RegExpEmpty::operator<(const RegExpElement& other) const { diff --git a/alib2/src/regexp/RegExpEpsilon.cpp b/alib2/src/regexp/RegExpEpsilon.cpp index 0ed91e5d05..364af6e7bf 100644 --- a/alib2/src/regexp/RegExpEpsilon.cpp +++ b/alib2/src/regexp/RegExpEpsilon.cpp @@ -13,7 +13,7 @@ RegExpEpsilon::RegExpEpsilon() { } RegExpElement* RegExpEpsilon::clone() const { - return new RegExpEpsilon(); + return new RegExpEpsilon(*this); } bool RegExpEpsilon::operator<(const RegExpElement& other) const { diff --git a/alib2/src/regexp/RegExpSymbol.cpp b/alib2/src/regexp/RegExpSymbol.cpp index bcc30faf6f..91fa6be5c5 100644 --- a/alib2/src/regexp/RegExpSymbol.cpp +++ b/alib2/src/regexp/RegExpSymbol.cpp @@ -9,16 +9,16 @@ namespace regexp { -RegExpSymbol::RegExpSymbol() : - symbol("") { -} - RegExpSymbol::RegExpSymbol(const std::string& symbol) : symbol(symbol) { } +RegExpSymbol::RegExpSymbol(std::string&& symbol) : + symbol(move(symbol)) { +} + RegExpElement* RegExpSymbol::clone() const { - return new RegExpSymbol(this->symbol); + return new RegExpSymbol(*this); } bool RegExpSymbol::operator<(const RegExpElement& other) const { diff --git a/alib2/src/regexp/RegExpSymbol.h b/alib2/src/regexp/RegExpSymbol.h index 8296bce29f..11faa21d97 100644 --- a/alib2/src/regexp/RegExpSymbol.h +++ b/alib2/src/regexp/RegExpSymbol.h @@ -20,8 +20,8 @@ namespace regexp { class RegExpSymbol : public RegExpElement, public std::element<RegExpSymbol, RegExpElement::visitor_type> { std::string symbol; public: - RegExpSymbol(); RegExpSymbol(const std::string& symbol); + RegExpSymbol(std::string&& symbol); /** * @copydoc RegExpElement::clone() const -- GitLab