From 2a2f8acd88f152d170530b86a403b84c0aea1a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Tr=C3=A1vn=C3=AD=C4=8Dek?= <jan.travnicek@fit.cvut.cz> Date: Wed, 12 Jan 2022 22:41:10 +0100 Subject: [PATCH] abstraction: push some code into cpp file --- alib2abstraction/src/object/Object.cpp | 98 ++++++++++++++++++++++++++ alib2abstraction/src/object/Object.h | 93 +++++------------------- 2 files changed, 116 insertions(+), 75 deletions(-) create mode 100644 alib2abstraction/src/object/Object.cpp diff --git a/alib2abstraction/src/object/Object.cpp b/alib2abstraction/src/object/Object.cpp new file mode 100644 index 0000000000..8047ee3461 --- /dev/null +++ b/alib2abstraction/src/object/Object.cpp @@ -0,0 +1,98 @@ +#include <object/Object.h> + +namespace object { + +void Object::unify ( Object & other ) { + if ( this->m_data.use_count ( ) > other.m_data.use_count ( ) ) + other.m_data = this->m_data; + else + this->m_data = other.m_data; +} + +Object::Object ( AnyObjectBase * data ) : m_data ( data ) { +} + +void Object::setData ( AnyObjectBase * data ) { + if ( & getData ( ) == data ) + return; + + this->m_data = ext::cow_shared_ptr < AnyObjectBase > ( data ); +} + +Object::Object ( const char * string ) : Object ( std::string ( string ) ) { +} + +Object::Object ( const AnyObject < object::Object > & data ) : Object ( data.getData ( ) ) { +} + +Object::Object ( AnyObject < object::Object > && data ) : Object ( std::move ( data ).getData ( ) ) { +} + +const AnyObjectBase & Object::getData ( ) const { + return * m_data; +} + +AnyObjectBase & Object::getData ( ) { + return * m_data; +} + +void Object::setData ( const AnyObjectBase & data ) { + setData ( data.clone ( ) ); +} + +void Object::setData ( AnyObjectBase && data ) { + setData ( std::move ( data ).clone ( ) ); +} + +std::strong_ordering Object::operator <=> ( const Object & other ) const { + if ( this->m_data.get ( ) == other.m_data.get ( ) ) + return std::strong_ordering::equal; + + std::strong_ordering res = ( * this->m_data ) <=> ( * other.m_data ); + if ( res == 0 ) + const_cast < Object * > ( this )->unify ( const_cast < Object & > ( other ) ); + + return res; +} + +bool Object::operator ==( const Object & other ) const { + if ( this->m_data.get ( ) == other.m_data.get ( ) ) + return true; + + bool res = ( * this->m_data ) == ( * other.m_data ); + if ( res ) + const_cast < Object * > ( this )->unify ( const_cast < Object & > ( other ) ); + + return res; +} + +ext::ostream & operator <<( ext::ostream & os, const Object & instance ) { + instance.getData ( ) >> os; + return os; +} + +Object::operator std::string ( ) const { + return m_data->operator std::string ( ); +} + +Object & Object::operator ++ ( ) { + this->getData ( ).increment ( 1 ); + return *this; +} + +Object Object::operator ++ ( int ) { + Object res = * this; + ++ * this; + return res; +} + +Object Object::operator += ( unsigned by ) { + this->getData ( ).increment ( by ); + return *this; +} + +unsigned Object::getId ( ) const { + return this->getData ( ).getId ( ); +} + +} /* namespace object */ diff --git a/alib2abstraction/src/object/Object.h b/alib2abstraction/src/object/Object.h index 67f8b05c89..5377c76cfd 100644 --- a/alib2abstraction/src/object/Object.h +++ b/alib2abstraction/src/object/Object.h @@ -18,12 +18,7 @@ class Object { * \brief * Unifies the shared pointers. For internal use when two pointer eval equal. */ - void unify ( Object & other ) { - if ( this->m_data.use_count ( ) > other.m_data.use_count ( ) ) - other.m_data = this->m_data; - else - this->m_data = other.m_data; - } + void unify ( Object & other ); /** * \brief @@ -41,28 +36,21 @@ class Object { /** * Constructor that wraps raw pointer. Takes ownership of the pointer. */ - explicit Object ( AnyObjectBase * data ) : m_data ( data ) { - } + explicit Object ( AnyObjectBase * data ); /** * Sets the wrapped object from raw pointer. Takes ownership of the pointer. * * Internally handles situation like storing the same value that is already wrapped. Usefull in normalisation. */ - void setData ( AnyObjectBase * data ) { - if ( & getData ( ) == data ) - return; - - this->m_data = ext::cow_shared_ptr < AnyObjectBase > ( data ); - } + void setData ( AnyObjectBase * data ); public: /** * \brief * Specialisation of the make method for c-strings. */ - explicit Object ( const char * string ) : Object ( std::string ( string ) ) { - } + explicit Object ( const char * string ); /** * \brief @@ -120,46 +108,36 @@ public: /** * Constructor that wraps an object given by constant reference. Uses clone of the parameter internally. */ - explicit Object ( const AnyObject < object::Object > & data ) : Object ( data.getData ( ) ) { - } + explicit Object ( const AnyObject < object::Object > & data ); /** * Constructor that wraps an object given by r-value reference. Uses clone of the parameter internally. */ - explicit Object ( AnyObject < object::Object > && data ) : Object ( std::move ( data ).getData ( ) ) { - } + explicit Object ( AnyObject < object::Object > && data ); /** * Gets the wrapped object. * * \returns wrapped object. */ - const AnyObjectBase & getData ( ) const { - return * m_data; - } + const AnyObjectBase & getData ( ) const; /** * Gets the wrapped object. * * \returns wrapped object. */ - AnyObjectBase & getData ( ) { - return * m_data; - } + AnyObjectBase & getData ( ); /** * Sets the wrapped object from constant reference. Uses clone of the parameter internally. */ - void setData ( const AnyObjectBase & data ) { - setData ( data.clone ( ) ); - } + void setData ( const AnyObjectBase & data ); /** * Sets the wrapped object from r-value reference. Uses clone of the parameter internally. */ - void setData ( AnyObjectBase && data ) { - setData ( std::move ( data ).clone ( ) ); - } + void setData ( AnyObjectBase && data ); /** * The three way comparison implementation @@ -168,16 +146,7 @@ public: * * \returns the strong ordering between this object and the @p other. */ - std::strong_ordering operator <=> ( const Object & other ) const { - if ( this->m_data.get ( ) == other.m_data.get ( ) ) - return std::strong_ordering::equal; - - std::strong_ordering res = ( * this->m_data ) <=> ( * other.m_data ); - if ( res == 0 ) - const_cast < Object * > ( this )->unify ( const_cast < Object & > ( other ) ); - - return res; - } + std::strong_ordering operator <=> ( const Object & other ) const; /** * The equality comparison implementation. @@ -186,16 +155,7 @@ public: * * \returns true if this and other objects are equal, false othervise */ - bool operator ==( const Object & other ) const { - if ( this->m_data.get ( ) == other.m_data.get ( ) ) - return true; - - bool res = ( * this->m_data ) == ( * other.m_data ); - if ( res ) - const_cast < Object * > ( this )->unify ( const_cast < Object & > ( other ) ); - - return res; - } + bool operator ==( const Object & other ) const; /** * Print the wrapped object as raw representation to ostream. @@ -205,19 +165,14 @@ public: * * \returns modified output stream */ - friend ext::ostream & operator <<( ext::ostream & os, const Object & instance ) { - instance.getData ( ) >> os; - return os; - } + friend ext::ostream & operator <<( ext::ostream & os, const Object & instance ); /** * Casts the wrapped object to as compact as possible string representation. * * \returns string representation of the wrapped object */ - explicit operator std::string ( ) const { - return m_data->operator std::string ( ); - } + explicit operator std::string ( ) const; /** * \brief @@ -225,10 +180,7 @@ public: * * \return this instance */ - Object & operator ++ ( ) { - this->getData ( ).increment ( 1 ); - return *this; - } + Object & operator ++ ( ); /** * \brief @@ -236,11 +188,7 @@ public: * * \return this instance */ - Object operator ++ ( int ) { - Object res = * this; - ++ * this; - return res; - } + Object operator ++ ( int ); /** * \brief @@ -248,19 +196,14 @@ public: * * \param by how much to increment */ - Object operator += ( unsigned by ) { - this->getData ( ).increment ( by ); - return *this; - } + Object operator += ( unsigned by ); /** * Getter of unique identifier * * \return the unique identifier */ - unsigned getId ( ) const { - return this->getData ( ).getId ( ); - } + unsigned getId ( ) const; }; } /* namespace object */ -- GitLab