From 6184d7b74991bb33535ddbe600fa13be288b20ab Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 12 Aug 2014 18:38:46 +0200 Subject: [PATCH] automaton wrapper class --- alib2data/src/automaton/Automaton.cpp | 67 +------------------ alib2data/src/automaton/Automaton.h | 37 ++-------- .../src/automaton/AutomatonToXMLComposer.cpp | 2 +- .../FSM/FiniteAutomatonToStringComposer.cpp | 2 +- alib2data/src/factory/AutomatonFactory.cpp | 8 +-- .../src/grammar/GrammarToXMLComposer.cpp | 2 +- .../test-src/automaton/AutomatonTest.cpp | 2 +- 7 files changed, 13 insertions(+), 107 deletions(-) diff --git a/alib2data/src/automaton/Automaton.cpp b/alib2data/src/automaton/Automaton.cpp index 6ae5c87843..1643c3f3d0 100644 --- a/alib2data/src/automaton/Automaton.cpp +++ b/alib2data/src/automaton/Automaton.cpp @@ -12,72 +12,7 @@ namespace automaton { -Automaton::Automaton(const AutomatonBase& automaton) : automaton(automaton.clone()) { - -} - -Automaton::Automaton(AutomatonBase&& automaton) : automaton(std::move(automaton).plunder()) { - -} - -Automaton::Automaton(const Automaton& other) : automaton(other.getAutomaton().clone()) { - -} - -Automaton::Automaton(Automaton&& other) noexcept : automaton(other.automaton) { - other.automaton = NULL; -} - -Automaton& Automaton::operator=(const Automaton& other) { - if(this == &other) return *this; - - delete automaton; - automaton = other.getAutomaton().clone(); - - return *this; -} - -Automaton& Automaton::operator=(Automaton&& other) noexcept { - std::swap(this->automaton, other.automaton); - return *this; -} - -Automaton::~Automaton() { - delete automaton; -} - -const AutomatonBase& Automaton::getAutomaton() const { - return *automaton; -} - -AutomatonBase& Automaton::getAutomaton() { - return *automaton; -} - -void Automaton::setAutomaton(const AutomatonBase& automaton) { - delete this->automaton; - this->automaton = automaton.clone(); -} - -void Automaton::setAutomaton(AutomatonBase&& automaton) { - delete this->automaton; - this->automaton = std::move(automaton).plunder(); -} - -bool Automaton::operator!=(const Automaton& other) const { - return !(*this == other); -} - -bool Automaton::operator==(const Automaton& other) const { - return this->getAutomaton() == other.getAutomaton(); -} - -std::ostream& operator<<(std::ostream& os, const Automaton& automaton) { - os << automaton.getAutomaton(); - return os; -} - -State Automaton::createUniqueState(const State& base, const std::set<State>& other) { +State createUniqueState(const State& base, const std::set<State>& other) { label::NextLabel nextLabelCreator; label::Label nextLabel = base.getName(); diff --git a/alib2data/src/automaton/Automaton.h b/alib2data/src/automaton/Automaton.h index cbd7c62f5e..bc1a70076f 100644 --- a/alib2data/src/automaton/Automaton.h +++ b/alib2data/src/automaton/Automaton.h @@ -10,6 +10,7 @@ #include "../std/visitor.hpp" #include "AutomatonBase.h" +#include "../common/wrapper.hpp" #include "common/State.h" #include <set> @@ -19,39 +20,9 @@ namespace automaton { /** * Wrapper around automata. */ -class Automaton { -protected: - AutomatonBase* automaton; -public: - explicit Automaton(const AutomatonBase& automaton); - explicit Automaton(AutomatonBase&& automaton); - Automaton(const Automaton& other); - Automaton(Automaton&&) noexcept; - Automaton& operator=(const Automaton& other); - Automaton& operator=(Automaton&& other) noexcept; - virtual ~Automaton() noexcept; - - const AutomatonBase& getAutomaton() const; - AutomatonBase& getAutomaton(); - - void setAutomaton(const AutomatonBase& automaton); - void setAutomaton(AutomatonBase&& automaton); - - bool operator!=(const Automaton& other) const; - - bool operator==(const Automaton& other) const; - - friend std::ostream& operator<<(std::ostream& os, const Automaton& automaton); - - /** - * Creates and adds unique state to automaton. If given state name is - * already used, appends apostrophe or integer suffix - * @param name name of the state - * @throws AutomatonException if state could not be created - * @return created state - */ - static State createUniqueState(const State& base, const std::set<State>& other); -}; +typedef alib::wrapper<AutomatonBase> Automaton; + +State createUniqueState(const State& base, const std::set<State>& other); } /* namespace automaton */ diff --git a/alib2data/src/automaton/AutomatonToXMLComposer.cpp b/alib2data/src/automaton/AutomatonToXMLComposer.cpp index ddbda3d42b..dbde61c28c 100644 --- a/alib2data/src/automaton/AutomatonToXMLComposer.cpp +++ b/alib2data/src/automaton/AutomatonToXMLComposer.cpp @@ -505,7 +505,7 @@ std::list<sax::Token> AutomatonToXMLComposer::compose(const OneTapeDTM& automato std::list<sax::Token> AutomatonToXMLComposer::compose(const Automaton& automaton) const { std::list<sax::Token> out; - automaton.getAutomaton().Accept((void*) &out, *this); + automaton.getData().Accept((void*) &out, *this); return out; } diff --git a/alib2data/src/automaton/FSM/FiniteAutomatonToStringComposer.cpp b/alib2data/src/automaton/FSM/FiniteAutomatonToStringComposer.cpp index fab84651ef..1764765029 100644 --- a/alib2data/src/automaton/FSM/FiniteAutomatonToStringComposer.cpp +++ b/alib2data/src/automaton/FSM/FiniteAutomatonToStringComposer.cpp @@ -158,7 +158,7 @@ std::string FiniteAutomatonToStringComposer::compose(const EpsilonNFA& automaton std::string FiniteAutomatonToStringComposer::compose(const Automaton& automaton) const { std::stringstream out; - automaton.getAutomaton().Accept((void*) &out, *this); + automaton.getData().Accept((void*) &out, *this); return out.str(); } diff --git a/alib2data/src/factory/AutomatonFactory.cpp b/alib2data/src/factory/AutomatonFactory.cpp index 44315d746b..725900046a 100644 --- a/alib2data/src/factory/AutomatonFactory.cpp +++ b/alib2data/src/factory/AutomatonFactory.cpp @@ -40,19 +40,19 @@ Automaton AutomatonFactory::fromTokens(std::list<sax::Token> tokens) { } void AutomatonFactory::toFile(const Automaton& automaton, const std::string& filename) { - toFile(automaton.getAutomaton(), filename); + toFile(automaton.getData(), filename); } std::string AutomatonFactory::toString(const Automaton& automaton) { - return toString(automaton.getAutomaton()); + return toString(automaton.getData()); } void AutomatonFactory::toStdout(const Automaton& automaton) { - return AutomatonFactory::toStdout(automaton.getAutomaton()); + return AutomatonFactory::toStdout(automaton.getData()); } void AutomatonFactory::toStream(const Automaton& automaton, std::ostream& out) { - toStream(automaton.getAutomaton(), out); + toStream(automaton.getData(), out); } void AutomatonFactory::toFile(const AutomatonBase& automaton, const std::string& filename) { diff --git a/alib2data/src/grammar/GrammarToXMLComposer.cpp b/alib2data/src/grammar/GrammarToXMLComposer.cpp index 008ce281ba..80af97c508 100644 --- a/alib2data/src/grammar/GrammarToXMLComposer.cpp +++ b/alib2data/src/grammar/GrammarToXMLComposer.cpp @@ -632,7 +632,7 @@ void GrammarToXMLComposer::composeRuleRightLGRHS(std::list<sax::Token>& out, con std::list<sax::Token> GrammarToXMLComposer::compose(const Grammar& grammar) const { std::list<sax::Token> out; - Visit((void*) &out, grammar); + grammar.getData().Accept((void*) &out, *this); return out; } diff --git a/alib2data/test-src/automaton/AutomatonTest.cpp b/alib2data/test-src/automaton/AutomatonTest.cpp index 92c3fc5820..d9a6f4ad80 100644 --- a/alib2data/test-src/automaton/AutomatonTest.cpp +++ b/alib2data/test-src/automaton/AutomatonTest.cpp @@ -54,7 +54,7 @@ void AutomatonTest::testXMLParser() { std::string tmp = automaton::AutomatonFactory::toString(automaton); automaton::Automaton automaton2 = automaton::AutomatonFactory::fromString(tmp); - CPPUNIT_ASSERT( automaton == automaton2.getAutomaton() ); + CPPUNIT_ASSERT( automaton == automaton2.getData() ); } } -- GitLab