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

vector container

parent 1168c820
No related branches found
No related tags found
No related merge requests found
...@@ -47,6 +47,14 @@ std::list<sax::Token> api<container::ObjectsSet>::compose(const container::Objec ...@@ -47,6 +47,14 @@ std::list<sax::Token> api<container::ObjectsSet>::compose(const container::Objec
return ToXMLComposers::containerComposer.compose(data); return ToXMLComposers::containerComposer.compose(data);
} }
   
container::ObjectsVector api<container::ObjectsVector>::parse(std::list<sax::Token>& input) {
return FromXMLParsers::containerParser.parseObjectsVector(input);
}
std::list<sax::Token> api<container::ObjectsVector>::compose(const container::ObjectsVector& data) {
return ToXMLComposers::containerComposer.compose(data);
}
alib::Object api<alib::Object>::parse(std::list<sax::Token>& input) { alib::Object api<alib::Object>::parse(std::list<sax::Token>& input) {
return FromXMLParsers::objectParser.parseObject(input); return FromXMLParsers::objectParser.parseObject(input);
} }
...@@ -448,6 +456,10 @@ void ToXMLComposers::Visit(void* data, const container::ObjectsSet& container) c ...@@ -448,6 +456,10 @@ void ToXMLComposers::Visit(void* data, const container::ObjectsSet& container) c
*((std::list<sax::Token>*) data) = std::move(api<container::ObjectsSet>::compose(container)); *((std::list<sax::Token>*) data) = std::move(api<container::ObjectsSet>::compose(container));
} }
   
void ToXMLComposers::Visit(void* data, const container::ObjectsVector& container) const {
*((std::list<sax::Token>*) data) = std::move(api<container::ObjectsVector>::compose(container));
}
void ToXMLComposers::Visit(void* data, const exception::AlibException& symbol) const { void ToXMLComposers::Visit(void* data, const exception::AlibException& symbol) const {
*((std::list<sax::Token>*) data) = std::move(api<exception::AlibException>::compose(symbol)); *((std::list<sax::Token>*) data) = std::move(api<exception::AlibException>::compose(symbol));
} }
......
...@@ -48,6 +48,18 @@ struct api<container::ObjectsSet> { ...@@ -48,6 +48,18 @@ struct api<container::ObjectsSet> {
static std::list<sax::Token> compose(const container::ObjectsSet& data); static std::list<sax::Token> compose(const container::ObjectsSet& data);
}; };
   
template<typename T>
struct api<std::vector<T>> {
static std::vector<T> parse(std::list<sax::Token>& input);
static std::list<sax::Token> compose(const std::vector<T>& data);
};
template<>
struct api<container::ObjectsVector> {
static container::ObjectsVector parse(std::list<sax::Token>& input);
static std::list<sax::Token> compose(const container::ObjectsVector& data);
};
   
template<> template<>
struct api<Object> { struct api<Object> {
...@@ -433,6 +445,7 @@ class ToXMLComposers : public VisitableObjectBase::const_visitor_type { ...@@ -433,6 +445,7 @@ class ToXMLComposers : public VisitableObjectBase::const_visitor_type {
void Visit(void*, const string::Epsilon& string) const; void Visit(void*, const string::Epsilon& string) const;
   
void Visit(void*, const container::ObjectsSet& set) const; void Visit(void*, const container::ObjectsSet& set) const;
void Visit(void*, const container::ObjectsVector& set) const;
   
public: public:
static const label::LabelToXMLComposer labelComposer; static const label::LabelToXMLComposer labelComposer;
...@@ -458,6 +471,16 @@ std::list<sax::Token> api<std::set<T>>::compose(const std::set<T>& input) { ...@@ -458,6 +471,16 @@ std::list<sax::Token> api<std::set<T>>::compose(const std::set<T>& input) {
return ToXMLComposers::containerComposer.compose<T>(input); return ToXMLComposers::containerComposer.compose<T>(input);
} }
   
template<typename T>
std::vector<T> api<std::vector<T>>::parse(std::list<sax::Token>& input) {
return FromXMLParsers::containerParser.parseVector<T>(input);
}
template<typename T>
std::list<sax::Token> api<std::vector<T>>::compose(const std::vector<T>& input) {
return ToXMLComposers::containerComposer.compose<T>(input);
}
} /* namespace alib */ } /* namespace alib */
   
#endif /* FROM_XML_PARSERS_HPP_ */ #endif /* FROM_XML_PARSERS_HPP_ */
...@@ -14,7 +14,8 @@ ...@@ -14,7 +14,8 @@
namespace container { namespace container {
   
typedef std::acceptor_base< typedef std::acceptor_base<
container::ObjectsSet container::ObjectsSet,
container::ObjectsVector
> VisitableContainerBase; > VisitableContainerBase;
   
/** /**
......
...@@ -11,7 +11,8 @@ ...@@ -11,7 +11,8 @@
namespace container { namespace container {
   
enum class FEATURES { enum class FEATURES {
SET SET,
VECTOR
}; };
   
} /* namespace container */ } /* namespace container */
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
namespace container { namespace container {
   
bool ContainerFromXMLParser::first(std::list<sax::Token>& input) const { bool ContainerFromXMLParser::first(std::list<sax::Token>& input) const {
if(isToken(input, sax::Token::TokenType::START_ELEMENT, "set")) { if(isToken(input, sax::Token::TokenType::START_ELEMENT, "set") || isToken(input, sax::Token::TokenType::START_ELEMENT, "vector")) {
return true; return true;
} else { } else {
return false; return false;
...@@ -19,15 +19,18 @@ bool ContainerFromXMLParser::first(std::list<sax::Token>& input) const { ...@@ -19,15 +19,18 @@ bool ContainerFromXMLParser::first(std::list<sax::Token>& input) const {
} }
   
Container ContainerFromXMLParser::parseContainer(std::list<sax::Token>& input) const { Container ContainerFromXMLParser::parseContainer(std::list<sax::Token>& input) const {
return parseContainer(input, std::set<FEATURES>({FEATURES::SET})); return parseContainer(input, std::set<FEATURES>({FEATURES::SET, FEATURES::VECTOR}));
} }
   
Container ContainerFromXMLParser::parseContainer(std::list<sax::Token>& input, const std::set<FEATURES>& features) const { Container ContainerFromXMLParser::parseContainer(std::list<sax::Token>& input, const std::set<FEATURES>& features) const {
if(isToken(input, sax::Token::TokenType::START_ELEMENT, "set")) { if(isToken(input, sax::Token::TokenType::START_ELEMENT, "set")) {
if(!features.count(FEATURES::SET)) throw exception::AlibException(); if(!features.count(FEATURES::SET)) throw exception::AlibException();
return Container(parseObjectsSet(input)); return Container(parseObjectsSet(input));
} else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "vector")) {
if(!features.count(FEATURES::VECTOR)) throw exception::AlibException();
return Container(parseObjectsVector(input));
} else { } else {
throw sax::ParserException(sax::Token("set", sax::Token::TokenType::START_ELEMENT), input.front()); throw sax::ParserException(sax::Token("set / vector", sax::Token::TokenType::START_ELEMENT), input.front());
} }
} }
   
...@@ -44,5 +47,18 @@ ObjectsSet ContainerFromXMLParser::parseObjectsSet(std::list<sax::Token>& input) ...@@ -44,5 +47,18 @@ ObjectsSet ContainerFromXMLParser::parseObjectsSet(std::list<sax::Token>& input)
return set; return set;
} }
   
ObjectsVector ContainerFromXMLParser::parseObjectsVector(std::list<sax::Token>& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, "vector");
ObjectsVector vector;
while(isTokenType(input, sax::Token::TokenType::START_ELEMENT)) {
vector.push_back(alib::api<alib::Object>::parse(input));
}
popToken(input, sax::Token::TokenType::END_ELEMENT, "vector");
return vector;
}
} /* namespace container */ } /* namespace container */
   
...@@ -11,8 +11,8 @@ ...@@ -11,8 +11,8 @@
#include "../sax/FromXMLParserHelper.h" #include "../sax/FromXMLParserHelper.h"
#include "ContainerFeatures.h" #include "ContainerFeatures.h"
   
#include <set>
#include "ObjectsSet.h" #include "ObjectsSet.h"
#include "ObjectsVector.h"
   
#include "Container.h" #include "Container.h"
   
...@@ -37,6 +37,11 @@ class ContainerFromXMLParser : public sax::FromXMLParserHelper { ...@@ -37,6 +37,11 @@ class ContainerFromXMLParser : public sax::FromXMLParserHelper {
   
ObjectsSet parseObjectsSet(std::list<sax::Token>& input) const; ObjectsSet parseObjectsSet(std::list<sax::Token>& input) const;
   
template<typename T>
std::vector<T> parseVector(std::list<sax::Token>& input) const;
ObjectsVector parseObjectsVector(std::list<sax::Token>& input) const;
template<typename T> friend class alib::api; template<typename T> friend class alib::api;
   
public: public:
...@@ -63,6 +68,20 @@ std::set<T> ContainerFromXMLParser::parseSet(std::list<sax::Token>& input) const ...@@ -63,6 +68,20 @@ std::set<T> ContainerFromXMLParser::parseSet(std::list<sax::Token>& input) const
return set; return set;
} }
   
template<typename T>
std::vector<T> ContainerFromXMLParser::parseVector(std::list<sax::Token>& input) const {
popToken(input, sax::Token::TokenType::START_ELEMENT, "vector");
std::vector<T> vector;
while(isTokenType(input, sax::Token::TokenType::START_ELEMENT)) {
vector.push_back(alib::api<T>::parse(input));
}
popToken(input, sax::Token::TokenType::END_ELEMENT, "vector");
return vector;
}
} /* namespace container */ } /* namespace container */
   
#endif /* CONTAINER_FROM_XML_PARSER_H_ */ #endif /* CONTAINER_FROM_XML_PARSER_H_ */
......
...@@ -33,5 +33,17 @@ std::list<sax::Token> ContainerToXMLComposer::compose(const ObjectsSet& containe ...@@ -33,5 +33,17 @@ std::list<sax::Token> ContainerToXMLComposer::compose(const ObjectsSet& containe
return out; return out;
} }
   
std::list<sax::Token> ContainerToXMLComposer::compose(const ObjectsVector& container) const {
std::list<sax::Token> out;
out.push_back(sax::Token("vector", sax::Token::TokenType::START_ELEMENT));
for(const alib::Object& item : container) {
out.splice(out.end(), alib::api<alib::Object>::compose(item));
}
out.push_back(sax::Token("vector", sax::Token::TokenType::END_ELEMENT));
return out;
}
} /* namespace container */ } /* namespace container */
   
...@@ -9,11 +9,11 @@ ...@@ -9,11 +9,11 @@
#define CONTAINER_TO_XML_COMPOSER_H_ #define CONTAINER_TO_XML_COMPOSER_H_
   
#include <list> #include <list>
#include <set>
#include "../sax/Token.h" #include "../sax/Token.h"
   
#include "Container.h" #include "Container.h"
#include "ObjectsSet.h" #include "ObjectsSet.h"
#include "ObjectsVector.h"
   
namespace alib { namespace alib {
   
...@@ -47,6 +47,12 @@ class ContainerToXMLComposer { ...@@ -47,6 +47,12 @@ class ContainerToXMLComposer {
   
std::list<sax::Token> compose(const ObjectsSet& container) const; std::list<sax::Token> compose(const ObjectsSet& container) const;
   
template<typename T>
std::list<sax::Token> compose(const std::vector<T>& container) const;
std::list<sax::Token> compose(const ObjectsVector& container) const;
template<typename T> friend class alib::api; template<typename T> friend class alib::api;
}; };
   
...@@ -69,6 +75,19 @@ std::list<sax::Token> ContainerToXMLComposer::compose(const std::set<T>& contain ...@@ -69,6 +75,19 @@ std::list<sax::Token> ContainerToXMLComposer::compose(const std::set<T>& contain
return out; return out;
} }
   
template<typename T>
std::list<sax::Token> ContainerToXMLComposer::compose(const std::vector<T>& container) const {
std::list<sax::Token> out;
out.push_back(sax::Token("vectort", sax::Token::TokenType::START_ELEMENT));
for(const T& item : container) {
out.splice(out.end(), alib::api<T>::compose(item));
}
out.push_back(sax::Token("vector", sax::Token::TokenType::END_ELEMENT));
return out;
}
} /* namespace container */ } /* namespace container */
   
#endif /* CONTAINER_TO_XML_COMPOSER_H_ */ #endif /* CONTAINER_TO_XML_COMPOSER_H_ */
......
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
* Author: Jan Travnicek * Author: Jan Travnicek
*/ */
   
#ifndef SET_H_ #ifndef OBJECTS_SET_H_
#define SET_H_ #define OBJECTS_SET_H_
   
#include <set> #include <set>
#include <string> #include <string>
...@@ -44,4 +44,4 @@ public: ...@@ -44,4 +44,4 @@ public:
   
} /* namespace container */ } /* namespace container */
   
#endif /* SET_H_ */ #endif /* OBJECTS_SET_H_ */
/*
* Set.cpp
*
* Created on: Apr 1, 2013
* Author: Jan Travnicek
*/
#include "ObjectsVector.h"
#include "../std/vector.hpp"
#include <cstdlib>
#include <iostream>
#include <sstream>
namespace container {
ContainerBase* ObjectsVector::clone() const {
return new ObjectsVector(*this);
}
ContainerBase* ObjectsVector::plunder() && {
return new ObjectsVector(std::move(*this));
}
bool ObjectsVector::operator==(const ObjectBase& other) const {
return other == *this;
}
bool ObjectsVector::operator<(const ObjectBase& other) const {
return other > *this;
}
bool ObjectsVector::operator>(const ObjectBase& other) const {
return other < *this;
}
bool ObjectsVector::operator==(const ObjectsVector& other) const {
return static_cast<const std::vector<alib::Object>>(*this) == static_cast<const std::vector<alib::Object>>(other);
}
bool ObjectsVector::operator<(const ObjectsVector& other) const {
return static_cast<const std::vector<alib::Object>>(*this) < static_cast<const std::vector<alib::Object>>(other);
}
void ObjectsVector::operator>>(std::ostream& os) const {
os << *this;
}
ObjectsVector::operator std::string() const {
std::stringstream ss;
ss << *this;
return std::move(ss).str();
}
} /* namespace container */
/*
* Set.h
*
* Created on: Apr 1, 2013
* Author: Jan Travnicek
*/
#ifndef OBJECTS_VECTOR_H_
#define OBJECTS_VECTOR_H_
#include <vector>
#include <string>
#include "../object/Object.h"
#include "ContainerBase.h"
namespace container {
/**
* Basic container from which are derived all other containers.
* Contains reason why the container occured.
*/
class ObjectsVector : public std::vector<alib::Object>, public std::acceptor<ObjectsVector, VisitableContainerBase, std::acceptor<ObjectsVector, alib::VisitableObjectBase, ContainerBase> > {
public:
virtual ContainerBase* clone() const;
virtual ContainerBase* plunder() &&;
virtual bool operator>(const alib::ObjectBase& other) const;
virtual bool operator==(const alib::ObjectBase& other) const;
virtual bool operator<(const alib::ObjectBase& other) const;
virtual bool operator==(const ObjectsVector& other) const;
virtual bool operator<(const ObjectsVector& other) const;
virtual void operator>>(std::ostream& os) const;
virtual operator std::string() const;
virtual int selfTypeId() const {
return typeId(*this);
}
};
} /* namespace container */
#endif /* OBJECTS_VECTOR_H_ */
...@@ -91,6 +91,7 @@ class EndSymbol; ...@@ -91,6 +91,7 @@ class EndSymbol;
namespace container { namespace container {
   
class ObjectsSet; class ObjectsSet;
class ObjectsVector;
   
} }
   
...@@ -104,7 +105,7 @@ typedef std::acceptor_base< ...@@ -104,7 +105,7 @@ typedef std::acceptor_base<
regexp::UnboundedRegExp, regexp::FormalRegExp, regexp::UnboundedRegExp, regexp::FormalRegExp,
string::Epsilon, string::LinearString, string::CyclicString, string::Epsilon, string::LinearString, string::CyclicString,
alphabet::LabeledSymbol, alphabet::BlankSymbol, alphabet::BottomOfTheStackSymbol, alphabet::EndSymbol, alphabet::LabeledSymbol, alphabet::BlankSymbol, alphabet::BottomOfTheStackSymbol, alphabet::EndSymbol,
container::ObjectsSet container::ObjectsSet, container::ObjectsVector
> VisitableObjectBase; > VisitableObjectBase;
   
class ObjectBase : class ObjectBase :
...@@ -117,7 +118,7 @@ class ObjectBase : ...@@ -117,7 +118,7 @@ class ObjectBase :
regexp::UnboundedRegExp, regexp::FormalRegExp, regexp::UnboundedRegExp, regexp::FormalRegExp,
string::Epsilon, string::LinearString, string::CyclicString, string::Epsilon, string::LinearString, string::CyclicString,
alphabet::LabeledSymbol, alphabet::BlankSymbol, alphabet::BottomOfTheStackSymbol, alphabet::EndSymbol, alphabet::LabeledSymbol, alphabet::BlankSymbol, alphabet::BottomOfTheStackSymbol, alphabet::EndSymbol,
container::ObjectsSet container::ObjectsSet, container::ObjectsVector
>, public VisitableObjectBase { >, public VisitableObjectBase {
}; };
   
......
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