diff --git a/alib2data/src/common/wrapper.hpp b/alib2data/src/common/wrapper.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f1ba7aff7f7b6e6cb6aa7902f9e47b57e291e9a9 --- /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 5fec5b775a831ec8bca6e218a44d60dad0ccb0d3..0000000000000000000000000000000000000000 --- 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 8110de44b80739c30ba979b0d9b1262dd55de537..b42a3f670f35d49349fe4408ba06b57ae73d57a5 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 bab12b056ed0a30acc232f27fc1a3174c26afc71..465492584228a05473b6194da344fe4ca2a34ba9 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 84cd6a343f41717f3d546dcf29ef664b15e4dfe1..979172b4fd62f9f3b43507d7cfa8c37192cf8f08 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 5bf722f6b66aeb1f32c04e82157664b1d168e804..d89c6b583b022abaf7bafa539d53ee353edca494 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 {