From 0c02950483ede6258b9a2412ac5316c3bc2caa25 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 11 Aug 2014 21:33:48 +0200 Subject: [PATCH] make base data classes templated --- alib2data/src/common/wrapper.hpp | 90 +++++++++++++++++++ alib2data/src/regexp/RegExp.cpp | 82 ----------------- alib2data/src/regexp/RegExp.h | 27 +----- alib2data/src/regexp/RegExpAlphabetGetter.cpp | 4 +- .../src/regexp/RegExpToStringComposer.cpp | 4 +- alib2data/src/regexp/RegExpToXMLComposer.cpp | 2 +- 6 files changed, 97 insertions(+), 112 deletions(-) create mode 100644 alib2data/src/common/wrapper.hpp delete mode 100644 alib2data/src/regexp/RegExp.cpp diff --git a/alib2data/src/common/wrapper.hpp b/alib2data/src/common/wrapper.hpp new file mode 100644 index 0000000000..f1ba7aff7f --- /dev/null +++ b/alib2data/src/common/wrapper.hpp @@ -0,0 +1,90 @@ +/* + * wrapper.h + * + * Created on: Apr 10, 2013 + * Author: Jan Travnicek + */ + +#ifndef WRAPPER_H_ +#define WRAPPER_H_ + +#include <ostream> + +namespace alib { + +template<typename T> +class wrapper { +protected: + T* data; + +public: + explicit wrapper(const T& data) : data(data.clone()) { + + } + + explicit wrapper(T&& data) : data(std::move(data).plunder()) { + + } + + wrapper(const wrapper& other) : data(other.data->clone()) { + + } + + wrapper(wrapper&& other) noexcept : data(other.data) { + other.data = NULL; + } + + wrapper& operator=(const wrapper& other) { + if(this == &other) return *this; + + delete this->data; + this->data = other.data->clone(); + + return *this; + } + + wrapper& operator=(wrapper&& other) noexcept { + std::swap(this->data, other.data); + return *this; + } + + virtual ~wrapper() noexcept { + delete this->data; + } + + const T& getData() const { + return *data; + } + + T& getData() { + return *data; + } + + void setData(const T& data) { + delete this->data; + this->data = data.clone(); + } + + void setData(T&& data) { + delete this->data; + this->data = std::move(data).plunder(); + } + + bool operator<(const wrapper& other) const { + return *(this->data) < *(other.data); + } + + bool operator==(const wrapper& other) const { + return *(this->data) == *(other.data); + } + + friend std::ostream& operator<<(std::ostream& os, const wrapper& instance) { + os << *(instance.data); + return os; + } +}; + +} /* namespace alib */ + +#endif /* WRAPPER_H_ */ + diff --git a/alib2data/src/regexp/RegExp.cpp b/alib2data/src/regexp/RegExp.cpp deleted file mode 100644 index 5fec5b775a..0000000000 --- a/alib2data/src/regexp/RegExp.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* - * RegExp.cpp - * - * Created on: Apr 16, 2013 - * Author: Martin Zak - */ - -#include "RegExp.h" - -namespace regexp { - -RegExp::RegExp(const RegExpBase& regExp) : regExp(regExp.clone()) { - -} - -RegExp::RegExp(RegExpBase&& regExp) : regExp(std::move(regExp).plunder()) { - -} - -RegExp::RegExp(const RegExp& other) : regExp(other.getRegExp().clone()) { - -} - -RegExp::RegExp(RegExp&& other) noexcept : regExp(other.regExp) { - other.regExp = NULL; -} - -RegExp& RegExp::operator=(const RegExp& other) { - if(this == &other) return *this; - - delete regExp; - regExp = other.getRegExp().clone(); - - return *this; -} - -RegExp& RegExp::operator=(RegExp&& other) noexcept { - std::swap(this->regExp, other.regExp); - return *this; -} - -RegExp::~RegExp() { - delete regExp; -} - -const RegExpBase& RegExp::getRegExp() const { - return *regExp; -} - -RegExpBase& RegExp::getRegExp() { - return *regExp; -} - -void RegExp::setRegExp(const RegExpBase& regExp) { - delete this->regExp; - this->regExp = regExp.clone(); -} - -void RegExp::setRegExp(RegExpBase&& regExp) { - delete this->regExp; - this->regExp = std::move(regExp).plunder(); -} - -bool RegExp::operator<(const RegExp& other) const { - return this->getRegExp() < other.getRegExp(); -} - -bool RegExp::operator!=(const RegExp& other) const { - return !(*this == other); -} - -bool RegExp::operator==(const RegExp& other) const { - return this->getRegExp() == other.getRegExp(); -} - -std::ostream& operator<<(std::ostream& os, const RegExp& regExp) { - os << regExp.getRegExp(); - return os; -} - -} /* namespace regexp */ - diff --git a/alib2data/src/regexp/RegExp.h b/alib2data/src/regexp/RegExp.h index 8110de44b8..b42a3f670f 100644 --- a/alib2data/src/regexp/RegExp.h +++ b/alib2data/src/regexp/RegExp.h @@ -9,6 +9,7 @@ #define REG_EXP_H_ #include "../std/visitor.hpp" +#include "../common/wrapper.hpp" #include "RegExpBase.h" namespace regexp { @@ -16,31 +17,7 @@ namespace regexp { /** * Wrapper around automata. */ -class RegExp { -protected: - RegExpBase* regExp; -public: - explicit RegExp(const RegExpBase& regExp); - explicit RegExp(RegExpBase&& regExp); - RegExp(const RegExp& other); - RegExp(RegExp&&) noexcept; - RegExp& operator=(const RegExp& other); - RegExp& operator=(RegExp&& other) noexcept; - virtual ~RegExp() noexcept; - - const RegExpBase& getRegExp() const; - RegExpBase& getRegExp(); - - void setRegExp(const RegExpBase& regExp); - void setRegExp(RegExpBase&& regExp); - - bool operator<(const RegExp& other) const; - bool operator!=(const RegExp& other) const; - bool operator==(const RegExp& other) const; - - friend std::ostream& operator<<(std::ostream& os, const RegExp& regExp); - -}; +typedef alib::wrapper<RegExpBase> RegExp; } /* namespace regexp */ diff --git a/alib2data/src/regexp/RegExpAlphabetGetter.cpp b/alib2data/src/regexp/RegExpAlphabetGetter.cpp index bab12b056e..4654925842 100644 --- a/alib2data/src/regexp/RegExpAlphabetGetter.cpp +++ b/alib2data/src/regexp/RegExpAlphabetGetter.cpp @@ -10,7 +10,7 @@ namespace regexp { void RegExpAlphabetGetter::Visit(void* userData, const RegExp& regexp) const { - regexp.getRegExp().Accept(userData, *this); + regexp.getData().Accept(userData, *this); } void RegExpAlphabetGetter::Visit(void* userData, const RegExpBase::element_type& element) const { @@ -29,7 +29,7 @@ void RegExpAlphabetGetter::Visit(void* userData, const FormalRegExp& regexp) con std::set<alphabet::Symbol> RegExpAlphabetGetter::getAlphabet(const RegExp& regexp) const { std::set<alphabet::Symbol> res; - regexp.getRegExp().Accept((void*) &res, *this); + Visit((void*) &res, regexp); return std::move(res); } diff --git a/alib2data/src/regexp/RegExpToStringComposer.cpp b/alib2data/src/regexp/RegExpToStringComposer.cpp index 84cd6a343f..979172b4fd 100644 --- a/alib2data/src/regexp/RegExpToStringComposer.cpp +++ b/alib2data/src/regexp/RegExpToStringComposer.cpp @@ -13,7 +13,7 @@ namespace regexp { void RegExpToStringComposer::Visit(void* userData, const RegExp& regexp) { - regexp.getRegExp().Accept(userData, *this); + regexp.getData().Accept(userData, *this); } void RegExpToStringComposer::Visit(void* userData, const RegExpBase::element_type& element) { @@ -194,7 +194,7 @@ void RegExpToStringComposer::Visit(void* userData, const FormalRegExpEmpty&) { std::string RegExpToStringComposer::compose(const RegExp& regexp) { std::pair<Priority, std::stringstream> out; out.first = Priority::ALTERNATION; - regexp.getRegExp().Accept((void*) &out, *this); + Visit((void*) &out, regexp); return std::move(out.second).str(); } diff --git a/alib2data/src/regexp/RegExpToXMLComposer.cpp b/alib2data/src/regexp/RegExpToXMLComposer.cpp index 5bf722f6b6..d89c6b583b 100644 --- a/alib2data/src/regexp/RegExpToXMLComposer.cpp +++ b/alib2data/src/regexp/RegExpToXMLComposer.cpp @@ -12,7 +12,7 @@ namespace regexp { void RegExpToXMLComposer::Visit(void* userData, const RegExp& regexp) const { - regexp.getRegExp().Accept(userData, *this); + regexp.getData().Accept(userData, *this); } void RegExpToXMLComposer::Visit(void* userData, const RegExpBase::element_type& element) const { -- GitLab