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

templated string wrapper

parent 14455d85
No related branches found
No related tags found
No related merge requests found
/*
* String.cpp
*
* Created on: Apr 16, 2013
* Author: Jan Travnicek
*/
#include "String.h"
#include "../exception/AlibException.h"
#include <algorithm>
#include "Epsilon.h"
namespace string {
String::String() : string(NULL) {
setString(Epsilon());
}
String::String(const StringBase& string) : string(NULL) {
setString(string);
}
String::String(StringBase&& string) : string(NULL) {
setString(std::move(string));
}
String::String(const String& other) : string(other.getString().clone()) {
}
String::String(String&& other) noexcept : string(other.string) {
other.string = NULL;
}
String& String::operator=(const String& other) {
if(this == &other) return *this;
*this = String(other);
return *this;
}
String& String::operator=(String&& other) noexcept {
std::swap(this->string, other.string);
return *this;
}
String::~String() {
delete string;
}
const StringBase& String::getString() const {
return *string;
}
StringBase& String::getString() {
return *string;
}
void String::setString(const StringBase& string) {
delete this->string;
this->string = string.clone();
}
void String::setString(StringBase&& string) {
delete this->string;
this->string = std::move(string).plunder();
}
bool String::operator<(const String& other) const {
return *(this->string) < *(other.string);
}
bool String::operator!=(const String& other) const {
return !(*this == other);
}
bool String::operator==(const String& other) const {
return *string == *other.string;
}
std::ostream& operator<<(std::ostream& os, const String& string) {
os << string.getString();
return os;
}
String::operator std::string() const {
return (std::string) *string;
}
} /* namespace string */
......@@ -9,46 +9,15 @@
#define STRING_H_
 
#include "../std/visitor.hpp"
#include "../common/wrapper.hpp"
#include "StringBase.h"
#include <set>
#include "../alphabet/Symbol.h"
 
namespace string {
 
class StringBase;
/**
* Wrapper around strings.
* Wrapper around automata.
*/
class String {
protected:
StringBase* string;
public:
explicit String();
explicit String(const StringBase& string);
explicit String(StringBase&& string);
String(const String& other);
String(String&&) noexcept;
String& operator=(const String& other);
String& operator=(String&& other) noexcept;
virtual ~String() noexcept;
const StringBase& getString() const;
StringBase& getString();
void setString(const StringBase& string);
void setString(StringBase&& string);
bool operator<(const String& other) const;
bool operator!=(const String& other) const;
bool operator==(const String& other) const;
friend std::ostream& operator<<(std::ostream& os, const String& string);
operator std::string () const;
};
typedef alib::wrapper<StringBase> String;
 
} /* namespace string */
 
......
......@@ -10,7 +10,7 @@
namespace string {
 
void StringAlphabetGetter::Visit(void* userData, const String& string) const {
string.getString().Accept(userData, *this);
string.getData().Accept(userData, *this);
}
 
void StringAlphabetGetter::Visit(void* userData, const StringBase::element_type& element) const {
......@@ -34,7 +34,7 @@ void StringAlphabetGetter::Visit(void* userData, const LinearString& string) con
 
std::set<alphabet::Symbol> StringAlphabetGetter::getAlphabet(const String& string) const {
std::set<alphabet::Symbol> res;
string.getString().Accept((void*) &res, *this);
Visit((void*) &res, string);
return std::move(res);
}
 
......
......@@ -12,8 +12,7 @@
#include <ostream>
#include <set>
#include "../alphabet/Symbol.h"
#include "String.h"
namespace string {
 
class LinearString;
......
......@@ -20,7 +20,7 @@ String StringFromXMLParser::parse(std::list<sax::Token>& input) const {
}
 
String StringFromXMLParser::parse(std::list<sax::Token>& input, const std::set<FEATURES>& features) const {
String string;
String string { Epsilon {} };
parseContent(input, string, features);
 
return string;
......@@ -36,7 +36,7 @@ void StringFromXMLParser::parseContent(std::list<sax::Token>& input, String& str
epsilon.setAlphabet(alphabet);
 
popToken(input, sax::Token::TokenType::END_ELEMENT, "Epsilon");
string.setString(epsilon);
string.setData(epsilon);
} else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "LinearString")) {
if(!features.count(FEATURES::LINEAR)) throw exception::AlibException();
popToken(input, sax::Token::TokenType::START_ELEMENT, "LinearString");
......@@ -49,7 +49,7 @@ void StringFromXMLParser::parseContent(std::list<sax::Token>& input, String& str
linearString.setContent(data);
 
popToken(input, sax::Token::TokenType::END_ELEMENT, "LinearString");
string.setString(linearString);
string.setData(linearString);
} else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "CyclicString")) {
if(!features.count(FEATURES::CYCLIC)) throw exception::AlibException();
popToken(input, sax::Token::TokenType::START_ELEMENT, "CyclicString");
......@@ -61,7 +61,7 @@ void StringFromXMLParser::parseContent(std::list<sax::Token>& input, String& str
std::vector<alphabet::Symbol> data = parseContentData(input);
cyclicString.setContent(data);
 
string.setString(cyclicString);
string.setData(cyclicString);
popToken(input, sax::Token::TokenType::END_ELEMENT, "CyclicString");
} else {
throw sax::ParserException(sax::Token("Epsilon, LinearString, CyclicString", sax::Token::TokenType::START_ELEMENT), input.front());
......
......@@ -43,7 +43,7 @@ void StringToStringComposer::Visit(void* userData, const Epsilon&) {
}
 
void StringToStringComposer::Visit(void* userData, const String& string) {
string.getString().Accept(userData, *this);
string.getData().Accept(userData, *this);
 
}
 
......
......@@ -59,7 +59,7 @@ void StringToXMLComposer::Visit(void* userData, const LinearString& string) cons
}
 
void StringToXMLComposer::Visit(void* userData, const String& string) const {
string.getString().Accept(userData, *this);
string.getData().Accept(userData, *this);
}
 
std::list<sax::Token> StringToXMLComposer::compose(const String& string) 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