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

add UniqueObject class

parent 214afebf
No related branches found
No related tags found
No related merge requests found
/*
* UniqueObject.cpp
*
* Created on: Nov 11, 2016
* Author: Jan Travicek
*/
#include "UniqueObject.h"
#include <sstream>
#include <sax/FromXMLParserHelper.h>
#include <core/xmlApi.hpp>
namespace alib {
UniqueObject::UniqueObject(Object symbol, primitive::Integer id) : m_symbol(std::move(symbol)), m_id(std::move(id)) {
}
ObjectBase* UniqueObject::clone() const {
return new UniqueObject(*this);
}
ObjectBase* UniqueObject::plunder() && {
return new UniqueObject(std::move(*this));
}
const Object& UniqueObject::getSymbol() const {
return m_symbol;
}
const primitive::Integer& UniqueObject::getId() const {
return m_id;
}
primitive::Integer& UniqueObject::getId() {
return m_id;
}
int UniqueObject::compare(const UniqueObject& other) const {
int res = m_symbol.getData().compare(other.m_symbol.getData());
if(res == 0) res = m_id.compare(other.m_id);
return res;
}
void UniqueObject::operator>>(std::ostream& out) const {
out << "(UniqueObject " << m_symbol << ", " << m_id << ")";
}
UniqueObject::operator std::string() const {
std::stringstream ss;
ss << "{";
ss << m_symbol;
ss << ", ";
ss << m_id;
ss << "}";
return std::move(ss).str();
}
UniqueObject UniqueObject::parse(std::deque<sax::Token>::iterator& input) {
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::START_ELEMENT, UniqueObject::getXmlTagName());
Object firstSymbol = alib::xmlApi<Object>::parse(input);
primitive::Integer secondSymbol = alib::xmlApi<primitive::Integer>::parse(input);
UniqueObject data(std::move(firstSymbol), std::move(secondSymbol));
sax::FromXMLParserHelper::popToken(input, sax::Token::TokenType::END_ELEMENT, UniqueObject::getXmlTagName());
return data;
}
void UniqueObject::compose(std::deque<sax::Token>& out) const {
out.emplace_back(UniqueObject::getXmlTagName(), sax::Token::TokenType::START_ELEMENT);
alib::xmlApi<alib::Object>::compose(out, m_symbol);
alib::xmlApi<primitive::Integer>::compose(out, m_id);
out.emplace_back(UniqueObject::getXmlTagName(), sax::Token::TokenType::END_ELEMENT);
}
ObjectBase* UniqueObject::inc() && {
std::move(m_id).inc();
return NULL;
}
} /* namespace alib */
namespace alib {
auto uniqueSymbolParserRegister2 = xmlApi<alib::Object>::ParserRegister<alib::UniqueObject>();
} /* namespace alib */
/*
* UniqueObject.h
*
* Created on: Nov 11, 2016
* Author: Jan Travnicek
*/
#ifndef UNIQUE_OBJECT_H_
#define UNIQUE_OBJECT_H_
#include "Object.h"
#include <primitive/Integer.h>
namespace alib {
/**
* Represents symbol in an alib.
*/
class UniqueObject : public ObjectBase {
protected:
Object m_symbol;
primitive::Integer m_id;
public:
/**
* Creates new symbol with given name.
* @param symbol name of the symbol
*/
explicit UniqueObject ( Object symbol, primitive::Integer id );
virtual ObjectBase * clone ( ) const;
virtual ObjectBase * plunder ( ) &&;
/**
* @return name of the symbol
*/
const Object & getSymbol ( ) const;
const primitive::Integer & getId ( ) const;
primitive::Integer & getId ( );
virtual int compare ( const ObjectBase & other ) const {
if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other );
return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) );
}
virtual int compare ( const UniqueObject & other ) const;
virtual void operator >>( std::ostream & ) const;
virtual explicit operator std::string ( ) const;
static const std::string & getXmlTagName() {
static std::string xmlTagName = "UniqueObject";
return xmlTagName;
}
static UniqueObject parse ( std::deque < sax::Token >::iterator & input );
void compose ( std::deque < sax::Token > & out ) const;
virtual ObjectBase * inc ( ) &&;
};
} /* namespace alib */
#endif /* UNIQUE_OBJECT_H_ */
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