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

tune ptr_value casts

parent ba4691ab
No related branches found
No related tags found
No related merge requests found
...@@ -166,6 +166,28 @@ public: ...@@ -166,6 +166,28 @@ public:
return m_data; return m_data;
} }
   
/**
* \brief
* Allow automatic cast to const reference.
*
* \return the holded value
*/
operator T & ( ) & {
return * m_data;
}
/**
* \brief
* Allow automatic cast to const rvalue reference which can be itself binded to const reference.
*
* The expected cast type const T & causes ambuguity in cast resolution, therefore a hack like this was found.
*
* \return the holded value
*/
operator const T && ( ) const & {
return std::move ( * m_data );
}
/** /**
* \brief * \brief
* Allow automatic cast to rvalue reference which can be itself binded to const reference. * Allow automatic cast to rvalue reference which can be itself binded to const reference.
......
...@@ -13,7 +13,7 @@ void ParamPassTest::setUp ( ) { ...@@ -13,7 +13,7 @@ void ParamPassTest::setUp ( ) {
void ParamPassTest::tearDown ( ) { void ParamPassTest::tearDown ( ) {
} }
   
int instances = 0; int instances = 0; // set to expected number of instances created in parameter pass test including the original
   
class FooCloneable { class FooCloneable {
int m_data; int m_data;
...@@ -94,8 +94,7 @@ public: ...@@ -94,8 +94,7 @@ public:
int dest ( FooCloneable && foo ) { int dest ( FooCloneable && foo ) {
std::cout << "Destination called" << std::endl; std::cout << "Destination called" << std::endl;
   
if ( instances < 2 ) CPPUNIT_ASSERT ( instances == 0 );
throw std::exception ( );
   
return foo.getData ( ); return foo.getData ( );
} }
...@@ -103,39 +102,58 @@ int dest ( FooCloneable && foo ) { ...@@ -103,39 +102,58 @@ int dest ( FooCloneable && foo ) {
int dest ( FooCopyable && foo ) { int dest ( FooCopyable && foo ) {
std::cout << "Destination called" << std::endl; std::cout << "Destination called" << std::endl;
   
if ( instances < 2 ) CPPUNIT_ASSERT ( instances == 0 );
throw std::exception ( );
   
return foo.getData ( ); return foo.getData ( );
} }
   
int test ( FooCloneable && foo ) { int test ( FooCloneable && foo ) {
return dest ( std::move ( foo ) ); return dest ( std::move ( foo ) ) + 1;
} }
   
int test ( const FooCloneable & foo ) { int test ( const FooCloneable & foo ) {
return test ( ext::move_copy ( foo ) ); return test ( ext::move_copy ( foo ) ) + 1;
} }
   
int test ( FooCopyable && foo ) { int test ( FooCopyable && foo ) {
return dest ( std::move ( foo ) ); return dest ( std::move ( foo ) ) + 1;
} }
   
int test ( const FooCopyable & foo ) { int test ( const FooCopyable & foo ) {
return test ( ext::move_copy ( foo ) ); return test ( ext::move_copy ( foo ) ) + 1;
} }
   
void ParamPassTest::testParameterPassing ( ) { void ParamPassTest::testParameterPassing ( ) {
{ {
instances = -2;
const FooCloneable foo ( 1 ); const FooCloneable foo ( 1 );
int res = 0; int res = 0;
CPPUNIT_ASSERT_NO_THROW ( res = test ( foo ) ); res = test ( foo );
CPPUNIT_ASSERT ( res == 1 && instances == 1 ); CPPUNIT_ASSERT ( res == 3 );
} }
std::cout << std::endl;
{ {
instances = -2;
const FooCopyable foo ( 1 ); const FooCopyable foo ( 1 );
int res = 0; int res = 0;
CPPUNIT_ASSERT_NO_THROW ( res = test ( foo ) ); res = test ( foo );
CPPUNIT_ASSERT ( res == 1 && instances == 1 ); CPPUNIT_ASSERT ( res == 3 );
}
std::cout << std::endl;
{
instances = -2;
ext::ptr_value < FooCopyable > foo ( FooCopyable ( 1 ) );
CPPUNIT_ASSERT ( instances == -1 );
int res = 0;
res = test ( foo );
CPPUNIT_ASSERT ( res == 3 );
}
std::cout << std::endl;
{
instances = -1;
ext::ptr_value < FooCopyable > foo ( FooCopyable ( 1 ) );
int res = 0;
res = test ( std::move ( foo ) );
CPPUNIT_ASSERT ( res == 2 );
} }
} }
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