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

abstraction: push some code into cpp file

parent 93a34ce4
No related branches found
No related tags found
1 merge request!207Merge jt
Pipeline #172669 passed with warnings
#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 */
......@@ -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 */
......
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