diff --git a/alib2common/src/object/AnyObject.h b/alib2common/src/object/AnyObject.h new file mode 100644 index 0000000000000000000000000000000000000000..1ceb91312e329db1a7b789777f0d1e290092d7fb --- /dev/null +++ b/alib2common/src/object/AnyObject.h @@ -0,0 +1,110 @@ +/* + * AnyObject.h + * + * Created on: Mar 26, 2013 + * Author: Jan Travnicek + */ + +#ifndef ANY_OBJECT_H_ +#define ANY_OBJECT_H_ + +#include "ObjectBase.h" +#include <deque> +#include "../sax/Token.h" + +#include "../sax/FromXMLParserHelper.h" +#include "Object.h" +#include "../core/xmlApi.hpp" +#include "UniqueObject.h" + +namespace alib { + +/** + * Represents void. + */ +template < class T > +class AnyObject : public ObjectBase { + T m_data; +public: + /** + * Creates a blank symbol. + * @param symbol name of the symbol + */ + explicit AnyObject ( const T & data ); + explicit AnyObject ( T && data ); + + virtual ObjectBase * clone ( ) const; + virtual ObjectBase * plunder ( ) &&; + + const T & getData ( ) const; + + 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 AnyObject & other ) const; + + virtual void operator >>( std::ostream & out ) const; + + virtual explicit operator std::string ( ) const; + + void compose ( std::deque < sax::Token > & out ) const; + + virtual ObjectBase * inc ( ) &&; +}; + +template < class T > +AnyObject < T >::AnyObject ( const T & data ) : m_data ( data ) { +} + +template < class T > +AnyObject < T >::AnyObject ( T && data ) : m_data ( std::move ( data ) ) { +} + +template < class T > +ObjectBase* AnyObject < T >::clone() const { + return new AnyObject(*this); +} + +template < class T > +ObjectBase* AnyObject < T >::plunder() && { + return new AnyObject(std::move(*this)); +} + +template < class T > +const T & AnyObject < T >::getData ( ) const { + return m_data; +} + +template < class T > +int AnyObject < T >::compare(const AnyObject < T > & other ) const { + std::compare < decltype ( m_data ) > comp; + + return comp ( m_data, other.m_data ); +} + +template < class T > +void AnyObject < T >::operator>>(std::ostream& out) const { + out << "(AnyObject" << m_data << ")"; +} + +template < class T > +AnyObject < T > ::operator std::string () const { + return "V"; +} + +template < class T > +void AnyObject < T >::compose(std::deque<sax::Token>& out) const { + alib::xmlApi < T >::compose(out, m_data); +} + +template < class T > +ObjectBase* AnyObject < T >::inc() && { + return new UniqueObject(Object(std::move(*this)), primitive::Integer(0)); +} + +} /* namespace alib */ + +#endif /* ANY_OBJECT_H_ */ diff --git a/alib2common/test-src/object/AnyObjectTest.cpp b/alib2common/test-src/object/AnyObjectTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7af28e6cfd36a33277787b1aa9e343be795845b --- /dev/null +++ b/alib2common/test-src/object/AnyObjectTest.cpp @@ -0,0 +1,37 @@ +#include <list> +#include "AnyObjectTest.h" + +#include "sax/SaxParseInterface.h" +#include "sax/SaxComposeInterface.h" + +#include "object/AnyObject.h" +#include "container/ObjectsPair.h" +#include <primitive/Character.h> + +#include "factory/XmlDataFactory.hpp" + +#define CPPUNIT_IMPLY( x, y ) CPPUNIT_ASSERT ( !( x ) || ( y ) ) +#define CPPUNIT_EXCLUSIVE_OR( x, y ) CPPUNIT_ASSERT ( ( !( x ) && ( y ) ) || ( ( x ) && !( y ) ) ) + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( AnyObjectTest, "object" ); +CPPUNIT_TEST_SUITE_REGISTRATION ( AnyObjectTest ); + +void AnyObjectTest::setUp ( ) { +} + +void AnyObjectTest::tearDown ( ) { +} + +void AnyObjectTest::testXMLParser ( ) { + + alib::AnyObject < std::pair < int, char > > object ( std::make_pair ( 1, 'a' ) ); + alib::Object res ( container::ObjectsPair < alib::Object, alib::Object > ( alib::Object ( primitive::Integer ( 1 ) ), alib::Object ( primitive::Character ( 'a' ) ) ) ); + + { + std::string tmp = alib::XmlDataFactory::toString ( object ); + alib::Object object2 = alib::XmlDataFactory::fromString < alib::Object > ( tmp ); + + CPPUNIT_ASSERT ( res == object2 ); + } +} + diff --git a/alib2common/test-src/object/AnyObjectTest.h b/alib2common/test-src/object/AnyObjectTest.h new file mode 100644 index 0000000000000000000000000000000000000000..eb8c990879366ab6c539a0354858d525956873dd --- /dev/null +++ b/alib2common/test-src/object/AnyObjectTest.h @@ -0,0 +1,18 @@ +#ifndef ANY_OBJECT_TEST_H_ +#define ANY_OBJECT_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class AnyObjectTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE ( AnyObjectTest ); + CPPUNIT_TEST ( testXMLParser ); + CPPUNIT_TEST_SUITE_END ( ); + +public: + void setUp ( ); + void tearDown ( ); + + void testXMLParser ( ); +}; + +#endif // ANY_OBJECT_TEST_H_