From 5631923cd8e24eee1bbdbbdde2ed3139ed9ffa2c Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 30 Sep 2014 19:36:21 +0200 Subject: [PATCH] add copy/move constructor/operator= --- alib2data/src/automaton/UnknownAutomaton.cpp | 44 ++++++++++++++++++++ alib2data/src/automaton/UnknownAutomaton.h | 6 ++- alib2data/src/grammar/UnknownGrammar.cpp | 33 +++++++++++++++ alib2data/src/grammar/UnknownGrammar.h | 6 ++- 4 files changed, 87 insertions(+), 2 deletions(-) diff --git a/alib2data/src/automaton/UnknownAutomaton.cpp b/alib2data/src/automaton/UnknownAutomaton.cpp index 8404685214..b106fe4636 100644 --- a/alib2data/src/automaton/UnknownAutomaton.cpp +++ b/alib2data/src/automaton/UnknownAutomaton.cpp @@ -19,7 +19,51 @@ UnknownAutomaton::UnknownAutomaton() : } +UnknownAutomaton::UnknownAutomaton(const UnknownAutomaton& other) : + states(other.states), initialStates(other.initialStates), finalStates(other.finalStates), inputAlphabet(other.inputAlphabet), stackAlphabet(other.stackAlphabet), initialSymbols(other.initialSymbols), tapeAlphabet(other.tapeAlphabet), blankSymbol(NULL), transitions(other.transitions) { + if(other.blankSymbol) + blankSymbol = new alphabet::Symbol(*other.blankSymbol); +} + +UnknownAutomaton::UnknownAutomaton(UnknownAutomaton&& other) noexcept : + states(std::move(other.states)), initialStates(std::move(other.initialStates)), finalStates(std::move(other.finalStates)), inputAlphabet(std::move(other.inputAlphabet)), stackAlphabet(std::move(other.stackAlphabet)), initialSymbols(std::move(other.initialSymbols)), tapeAlphabet(std::move(other.tapeAlphabet)), blankSymbol(NULL), transitions(std::move(other.transitions)) { + if(other.blankSymbol) + blankSymbol = new alphabet::Symbol(std::move(*other.blankSymbol)); + +} + +const UnknownAutomaton& UnknownAutomaton::operator=(const UnknownAutomaton& other) { + UnknownAutomaton tmp(other); + + std::swap(states, tmp.states); + std::swap(inputAlphabet, tmp.inputAlphabet); + std::swap(initialStates, tmp.initialStates); + std::swap(finalStates, tmp.finalStates); + std::swap(stackAlphabet, tmp.stackAlphabet); + std::swap(initialSymbols, tmp.initialSymbols); + std::swap(tapeAlphabet, tmp.tapeAlphabet); + std::swap(blankSymbol, tmp.blankSymbol); + std::swap(transitions, tmp.transitions); + + return *this; +} + +const UnknownAutomaton& UnknownAutomaton::operator=(UnknownAutomaton&& other) noexcept { + std::swap(states, other.states); + std::swap(inputAlphabet, other.inputAlphabet); + std::swap(initialStates, other.initialStates); + std::swap(finalStates, other.finalStates); + std::swap(stackAlphabet, other.stackAlphabet); + std::swap(initialSymbols, other.initialSymbols); + std::swap(tapeAlphabet, other.tapeAlphabet); + std::swap(blankSymbol, other.blankSymbol); + std::swap(transitions, other.transitions); + + return *this; +} + UnknownAutomaton::~UnknownAutomaton() { + delete blankSymbol; } AutomatonBase* UnknownAutomaton::clone() const { diff --git a/alib2data/src/automaton/UnknownAutomaton.h b/alib2data/src/automaton/UnknownAutomaton.h index c9151c3ed7..e739898d4b 100644 --- a/alib2data/src/automaton/UnknownAutomaton.h +++ b/alib2data/src/automaton/UnknownAutomaton.h @@ -36,7 +36,11 @@ protected: public: explicit UnknownAutomaton(); - //TODO destructor and operator= and copy constructor + UnknownAutomaton(const UnknownAutomaton& other); + UnknownAutomaton(UnknownAutomaton&& other) noexcept; + const UnknownAutomaton& operator=(const UnknownAutomaton& other); + const UnknownAutomaton& operator=(UnknownAutomaton&& other) noexcept; + ~UnknownAutomaton() noexcept; virtual AutomatonBase* clone() const; diff --git a/alib2data/src/grammar/UnknownGrammar.cpp b/alib2data/src/grammar/UnknownGrammar.cpp index 70ba9029a8..5ce9446e77 100644 --- a/alib2data/src/grammar/UnknownGrammar.cpp +++ b/alib2data/src/grammar/UnknownGrammar.cpp @@ -17,6 +17,39 @@ UnknownGrammar::UnknownGrammar() : initialSymbol(NULL) { } +UnknownGrammar::UnknownGrammar(const UnknownGrammar& other) : + nonterminalAlphabet(other.nonterminalAlphabet), terminalAlphabet(other.terminalAlphabet), initialSymbol(NULL), rules(other.rules) { + if(other.initialSymbol) + initialSymbol = new alphabet::Symbol(*other.initialSymbol); + +} + +UnknownGrammar::UnknownGrammar(UnknownGrammar&& other) noexcept : + nonterminalAlphabet(std::move(other.nonterminalAlphabet)), terminalAlphabet(std::move(other.terminalAlphabet)), initialSymbol(NULL), rules(std::move(other.rules)) { + if(other.initialSymbol) + initialSymbol = new alphabet::Symbol(std::move(*other.initialSymbol)); +} + +const UnknownGrammar& UnknownGrammar::operator=(const UnknownGrammar& other) { + UnknownGrammar tmp(other); + + std::swap(nonterminalAlphabet, tmp.nonterminalAlphabet); + std::swap(terminalAlphabet, tmp.terminalAlphabet); + std::swap(initialSymbol, tmp.initialSymbol); + std::swap(rules, tmp.rules); + + return *this; +} + +const UnknownGrammar& UnknownGrammar::operator=(UnknownGrammar&& other) noexcept { + std::swap(nonterminalAlphabet, other.nonterminalAlphabet); + std::swap(terminalAlphabet, other.terminalAlphabet); + std::swap(initialSymbol, other.initialSymbol); + std::swap(rules, other.rules); + + return *this; +} + GrammarBase* UnknownGrammar::clone() const { return new UnknownGrammar(*this); } diff --git a/alib2data/src/grammar/UnknownGrammar.h b/alib2data/src/grammar/UnknownGrammar.h index 7bd44a975c..9bd94895d2 100644 --- a/alib2data/src/grammar/UnknownGrammar.h +++ b/alib2data/src/grammar/UnknownGrammar.h @@ -24,7 +24,11 @@ class UnknownGrammar : public std::acceptor<UnknownGrammar, VisitableGrammarBase alphabet::Symbol* initialSymbol; public: explicit UnknownGrammar(); - //TODO destructor and operator= and copy constructor + + UnknownGrammar(const UnknownGrammar& other); + UnknownGrammar(UnknownGrammar&& other) noexcept; + const UnknownGrammar& operator=(const UnknownGrammar& other); + const UnknownGrammar& operator=(UnknownGrammar&& other) noexcept; virtual GrammarBase* clone() const; -- GitLab