From 62780bdaf7ccbc29f833f94350e271a105c7de73 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 30 Sep 2014 19:02:33 +0200 Subject: [PATCH] add copy/move constructor/operator= --- alib2data/src/automaton/UnknownTransition.cpp | 55 ++++++++++++++++++- alib2data/src/automaton/UnknownTransition.h | 7 ++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/alib2data/src/automaton/UnknownTransition.cpp b/alib2data/src/automaton/UnknownTransition.cpp index 19056c681f..3ff83818eb 100644 --- a/alib2data/src/automaton/UnknownTransition.cpp +++ b/alib2data/src/automaton/UnknownTransition.cpp @@ -20,8 +20,59 @@ UnknownTransition::UnknownTransition(const State& from, const State& to, const s } -UnknownTransition::~UnknownTransition() { - +UnknownTransition::UnknownTransition(const UnknownTransition& other) : + from(NULL), to(NULL), pop(other.pop), push(other.push), input(NULL), output(NULL), shift(other.shift) { + if(other.from) + from = new State(*other.from); + if(other.to) + to = new State(*other.to); + if(other.input) + input = new std::variant<string::Epsilon, alphabet::Symbol>(*other.input); + if(other.output) + output = new std::variant<string::Epsilon, alphabet::Symbol>(*other.output); +} + +UnknownTransition::UnknownTransition(UnknownTransition&& other) noexcept : + from(NULL), to(NULL), pop(std::move(other.pop)), push(std::move(other.push)), input(NULL), output(NULL), shift(std::move(other.shift)) { + if(other.from) + from = new State(std::move(*other.from)); + if(other.to) + to = new State(std::move(*other.to)); + if(other.input) + input = new std::variant<string::Epsilon, alphabet::Symbol>(std::move(*other.input)); + if(other.output) + output = new std::variant<string::Epsilon, alphabet::Symbol>(std::move(*other.output)); +} + +const UnknownTransition& UnknownTransition::operator=(const UnknownTransition& other) { + UnknownTransition tmp(other); + + std::swap(from, tmp.from); + std::swap(to, tmp.to); + std::swap(pop, tmp.pop); + std::swap(push, tmp.push); + std::swap(input, tmp.input); + std::swap(output, tmp.output); + std::swap(shift, tmp.shift); + + return *this; +} + +const UnknownTransition& UnknownTransition::operator=(UnknownTransition&& other) noexcept { + std::swap(from, other.from); + std::swap(to, other.to); + std::swap(pop, other.pop); + std::swap(push, other.push); + std::swap(input, other.input); + std::swap(output, other.output); + std::swap(shift, other.shift); + + return *this; +} + +UnknownTransition::~UnknownTransition() noexcept { + delete from; + delete to; } void UnknownTransition::setFrom(const State& state) { diff --git a/alib2data/src/automaton/UnknownTransition.h b/alib2data/src/automaton/UnknownTransition.h index 4a6517d330..9a2e0547b8 100644 --- a/alib2data/src/automaton/UnknownTransition.h +++ b/alib2data/src/automaton/UnknownTransition.h @@ -39,7 +39,12 @@ public: explicit UnknownTransition(const State& from, const State& to, const std::vector<alphabet::Symbol>& pop, const std::vector<alphabet::Symbol>& push, const std::variant<string::Epsilon, alphabet::Symbol>& input, const std::variant<string::Epsilon, alphabet::Symbol>& output, Shift shift); - ~UnknownTransition(); + UnknownTransition(const UnknownTransition& other); + UnknownTransition(UnknownTransition&& other) noexcept; + const UnknownTransition& operator=(const UnknownTransition& other); + const UnknownTransition& operator=(UnknownTransition&& other) noexcept; + + ~UnknownTransition() noexcept; /** * @param state State from which the transition goes -- GitLab