diff --git a/alib2common/src/object/UniqueObject.cpp b/alib2common/src/object/UniqueObject.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad422577814a08c2734f34f65ac49ba08610bc0a --- /dev/null +++ b/alib2common/src/object/UniqueObject.cpp @@ -0,0 +1,88 @@ +/* + * 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 */ diff --git a/alib2common/src/object/UniqueObject.h b/alib2common/src/object/UniqueObject.h new file mode 100644 index 0000000000000000000000000000000000000000..9b0c5c82716b3016bfdf39692029a7e7c7d2db82 --- /dev/null +++ b/alib2common/src/object/UniqueObject.h @@ -0,0 +1,71 @@ +/* + * 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_ */