Skip to content
Snippets Groups Projects
Commit c51a9b87 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

refactoring

parent e7c13415
No related branches found
No related tags found
No related merge requests found
Showing with 72 additions and 67 deletions
......@@ -9,8 +9,12 @@
 
namespace alphabet {
 
Symbol::Symbol(const std::string& symbol) {
this->symbol = symbol;
Symbol::Symbol(const std::string& symbol) : symbol(symbol) {
}
Symbol::Symbol(std::string&& symbol) : symbol(std::move(symbol)) {
}
 
const std::string& Symbol::getSymbol() const {
......
......@@ -24,6 +24,7 @@ public:
* @param symbol name of the symbol
*/
Symbol(const std::string& symbol);
Symbol(std::string&& symbol);
 
/**
* @return name of the symbol
......
......@@ -46,11 +46,11 @@ Alternation::~Alternation() noexcept {
elements.clear();
}
 
std::list<RegExpElement*>& Alternation::getElements() {
std::vector<RegExpElement*>& Alternation::getElements() {
return elements;
}
 
const std::list<RegExpElement*>& Alternation::getElements() const {
const std::vector<RegExpElement*>& Alternation::getElements() const {
return elements;
}
 
......
......@@ -8,7 +8,7 @@
#ifndef ALTERNATION_H_
#define ALTERNATION_H_
 
#include <list>
#include <vector>
#include "RegExpElement.h"
 
namespace regexp {
......@@ -18,30 +18,30 @@ namespace regexp {
* as operands of the operator.
*/
class Alternation: public RegExpElement, public std::element<Alternation, RegExpElement::visitor_type> {
private:
std::list<RegExpElement*> elements;
protected:
/**
* @copydoc RegExpElement::clone() const
*/
virtual RegExpElement* clone() const;
std::vector<RegExpElement*> elements;
public:
Alternation();
Alternation(const Alternation& other);
Alternation(Alternation&& other) noexcept;
Alternation& operator =(const Alternation& other);
Alternation& operator =(Alternation&& other) noexcept;
~Alternation() noexcept;
virtual ~Alternation() noexcept;
 
/**
* @return list of operands
*/
std::list<RegExpElement*>& getElements();
std::vector<RegExpElement*>& getElements();
 
/**
* @return list of operands
*/
const std::list<RegExpElement*>& getElements() const;
/**
* @copydoc RegExpElement::clone() const
*/
RegExpElement* clone() const;
const std::vector<RegExpElement*>& getElements() const;
 
virtual bool operator<(const RegExpElement&) const;
virtual bool operator==(const RegExpElement&) const;
......
......@@ -45,11 +45,11 @@ Concatenation::~Concatenation() noexcept {
elements.clear();
}
 
std::list<RegExpElement*>& Concatenation::getElements() {
std::vector<RegExpElement*>& Concatenation::getElements() {
return elements;
}
 
const std::list<RegExpElement*>& Concatenation::getElements() const {
const std::vector<const RegExpElement*>& Concatenation::getElements() const {
return elements;
}
 
......
......@@ -8,7 +8,7 @@
#ifndef CONCATENATION_H_
#define CONCATENATION_H_
 
#include <list>
#include <vector>
#include "RegExpElement.h"
 
namespace regexp {
......@@ -18,30 +18,30 @@ namespace regexp {
* as operands of the operator.
*/
class Concatenation: public RegExpElement, public std::element<Concatenation, RegExpElement::visitor_type> {
private:
std::list<RegExpElement*> elements;
protected:
/**
* @copydoc RegExpElement::clone() const
*/
virtual RegExpElement* clone() const;
std::vector<RegExpElement*> elements;
public:
Concatenation();
Concatenation(const Concatenation& other);
Concatenation(Concatenation&& other) noexcept;
Concatenation& operator =(const Concatenation& other);
Concatenation& operator =(Concatenation&& other) noexcept;
~Concatenation() noexcept;
virtual ~Concatenation() noexcept;
 
/**
* @return list of operands
*/
std::list<RegExpElement*>& getElements();
std::vector<RegExpElement*>& getElements();
 
/**
* @return list of operands
*/
const std::list<RegExpElement*>& getElements() const;
/**
* @copydoc RegExpElement::clone() const
*/
RegExpElement* clone() const;
const std::vector<const RegExpElement*>& getElements() const;
 
virtual bool operator<(const RegExpElement&) const;
virtual bool operator==(const RegExpElement&) const;
......
......@@ -7,19 +7,15 @@
 
#include "Iteration.h"
#include <cstdio>
#include "../AlibException.h"
 
namespace regexp {
 
Iteration::Iteration() {
element = NULL;
element = new RegExpEmpty();
}
 
Iteration::Iteration(const Iteration& other) {
if (other.element != NULL) {
element = other.element->clone();
} else {
element = NULL;
}
Iteration::Iteration(const Iteration& other) : element(other.element->clone()) {
 
}
 
......@@ -44,11 +40,7 @@ Iteration& Iteration::operator=(Iteration&& other) noexcept {
}
 
regexp::Iteration::~Iteration() noexcept {
if (element != NULL) {
delete element;
}
element = NULL;
delete element;
}
 
RegExpElement* Iteration::getElement() {
......@@ -60,6 +52,7 @@ const RegExpElement* Iteration::getElement() const {
}
 
void Iteration::setElement(RegExpElement* element) {
delete this->element;
this->element = element;
}
 
......
......@@ -10,6 +10,7 @@
 
#include <list>
#include "RegExpElement.h"
#include "RegExpEmpty.h"
 
namespace regexp {
 
......@@ -18,15 +19,20 @@ namespace regexp {
* as operand.
*/
class Iteration: public RegExpElement, public std::element<Iteration, RegExpElement::visitor_type> {
private:
protected:
RegExpElement* element;
/**
* @copydoc RegExpElement::clone() const
*/
virtual RegExpElement* clone() const;
public:
Iteration();
Iteration(const Iteration& other);
Iteration(Iteration&& other) noexcept;
Iteration& operator =(const Iteration& other);
Iteration& operator =(Iteration&& other) noexcept;
~Iteration() noexcept;
virtual ~Iteration() noexcept;
 
/**
* @return element to iterate
......@@ -37,18 +43,12 @@ public:
* @return element to iterate
*/
const RegExpElement* getElement() const;
/**
* Sets the element to iterate. Doesn't perform the copy, just stores the pointer!
* @param element RegExpElement to set
* @param element to iterate
*/
void setElement(RegExpElement* element);
 
/**
* @copydoc RegExpElement::clone() const
*/
RegExpElement* clone() const;
virtual bool operator<(const RegExpElement&) const;
virtual bool operator==(const RegExpElement&) const;
virtual bool operator>(const RegExpElement&) const;
......
......@@ -61,6 +61,8 @@ const RegExpElement* RegExp::getRegExp() const {
}
 
void RegExp::setRegExp(RegExpElement* regExp) {
delete this->regExp;
if(regExp == NULL) throw alib::AlibException();
this->regExp = regExp;
}
 
......
......@@ -22,7 +22,7 @@ namespace regexp {
* as a tree of RegExpElement.
*/
class RegExp : public std::element<RegExp, std::visitor<RegExp> > {
private:
protected:
RegExpElement* regExp;
 
public:
......
......@@ -26,14 +26,14 @@ class RegExpEpsilon;
*/
class RegExpElement : virtual public std::elementBase<std::visitor<Alternation, Concatenation, Iteration, RegExpSymbol, RegExpEmpty, RegExpEpsilon> > {
public:
virtual ~RegExpElement() noexcept;
/**
* Creates copy of the element.
* @return copy of the element
*/
virtual RegExpElement* clone() const = 0;
 
virtual ~RegExpElement() noexcept;
// RegExpEmpty < RegExpEpsilon < RegExpSymbol < RegExpIteration < RegExpAlternation < RegExpConcatenation
virtual bool operator<(const RegExpElement&) const = 0;
virtual bool operator==(const RegExpElement&) const = 0;
......
......@@ -16,13 +16,14 @@ namespace regexp {
* Represents empty regular expression in the regular expression.
*/
class RegExpEmpty: public RegExpElement, public std::element<RegExpEmpty, RegExpElement::visitor_type> {
public:
RegExpEmpty();
protected:
/**
* @copydoc RegExpElement::clone() const
*/
RegExpElement* clone() const;
virtual RegExpElement* clone() const;
public:
RegExpEmpty();
 
virtual bool operator<(const RegExpElement&) const;
virtual bool operator==(const RegExpElement&) const;
......
......@@ -17,13 +17,14 @@ namespace regexp {
* Represents epsilon in the regular expression.
*/
class RegExpEpsilon: public RegExpElement, public std::element<RegExpEpsilon, RegExpElement::visitor_type> {
public:
RegExpEpsilon();
protected:
/**
* @copydoc RegExpElement::clone() const
*/
RegExpElement* clone() const;
virtual RegExpElement* clone() const;
public:
RegExpEpsilon();
 
virtual bool operator<(const RegExpElement&) const;
virtual bool operator==(const RegExpElement&) const;
......
......@@ -62,7 +62,9 @@ Iteration* RegExpFromXMLParser::parseIteration(std::list<sax::Token>& input) {
popToken(input, sax::Token::START_ELEMENT, "iteration");
 
Iteration* iteration = new Iteration();
iteration->setElement(parseElement(input));
RegExpElement* element = parseElement(input);
if(element == NULL) throw sax::ParserException(sax::Token("", sax::Token::CHARACTER), input.front());
iteration->setElement(element);
 
popToken(input, sax::Token::END_ELEMENT, "iteration");
return iteration;
......
......@@ -18,16 +18,17 @@ namespace regexp {
* Represents symbol in the regular expression. Contains name of the symbol.
*/
class RegExpSymbol : public RegExpElement, public std::element<RegExpSymbol, RegExpElement::visitor_type> {
protected:
/**
* @copydoc RegExpElement::clone() const
*/
virtual RegExpElement* clone() const;
std::string symbol;
public:
RegExpSymbol(const std::string& symbol);
RegExpSymbol(std::string&& symbol);
 
/**
* @copydoc RegExpElement::clone() const
*/
RegExpElement* clone() const;
virtual bool operator<(const RegExpElement&) const;
virtual bool operator==(const RegExpElement&) const;
virtual bool operator>(const RegExpElement&) const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment