Validate cow_wrapper static default pattern.
A bunch of objects use the optimization to assign one single, unique default object to default-ctored instances, and decide on whether any given object is in 'default' state by ptr-comparing against this one unique instance. Simulating that pattern here. Change-Id: I88f7d8488d81bcf1a01ab6b63121e003b8f0ade9
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include "cow_wrapper_clients.hxx"
|
||||
#include <rtl/instance.hxx>
|
||||
|
||||
namespace o3tltests {
|
||||
|
||||
@@ -169,6 +170,55 @@ bool cow_wrapper_client3::operator<( const cow_wrapper_client3& rRHS ) const
|
||||
return maImpl < rRHS.maImpl;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
namespace { struct theDefaultClient4 : public rtl::Static< o3tl::cow_wrapper< int >,
|
||||
theDefaultClient4 > {}; }
|
||||
|
||||
cow_wrapper_client4::cow_wrapper_client4() :
|
||||
maImpl(theDefaultClient4::get())
|
||||
{
|
||||
}
|
||||
|
||||
cow_wrapper_client4::cow_wrapper_client4( int nVal ) :
|
||||
maImpl( nVal )
|
||||
{
|
||||
}
|
||||
|
||||
cow_wrapper_client4::~cow_wrapper_client4()
|
||||
{
|
||||
}
|
||||
|
||||
cow_wrapper_client4::cow_wrapper_client4( const cow_wrapper_client4& rSrc ) :
|
||||
maImpl(rSrc.maImpl)
|
||||
{
|
||||
}
|
||||
|
||||
cow_wrapper_client4& cow_wrapper_client4::operator=( const cow_wrapper_client4& rSrc )
|
||||
{
|
||||
maImpl = rSrc.maImpl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool cow_wrapper_client4::is_default() const
|
||||
{
|
||||
return maImpl.same_object(theDefaultClient4::get());
|
||||
}
|
||||
|
||||
bool cow_wrapper_client4::operator==( const cow_wrapper_client4& rRHS ) const
|
||||
{
|
||||
return maImpl == rRHS.maImpl;
|
||||
}
|
||||
bool cow_wrapper_client4::operator!=( const cow_wrapper_client4& rRHS ) const
|
||||
{
|
||||
return maImpl != rRHS.maImpl;
|
||||
}
|
||||
bool cow_wrapper_client4::operator<( const cow_wrapper_client4& rRHS ) const
|
||||
{
|
||||
return maImpl < rRHS.maImpl;
|
||||
}
|
||||
|
||||
} // namespace o3tltests
|
||||
|
||||
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
||||
|
@@ -115,6 +115,30 @@ private:
|
||||
o3tl::cow_wrapper< cow_wrapper_client2_impl, o3tl::ThreadSafeRefCountingPolicy > maImpl;
|
||||
};
|
||||
|
||||
/** test default-object comparison - have default-ctored-client4 share
|
||||
the same static impl instance, check if isDefault does the right
|
||||
thing
|
||||
*/
|
||||
class cow_wrapper_client4
|
||||
{
|
||||
public:
|
||||
cow_wrapper_client4();
|
||||
explicit cow_wrapper_client4(int);
|
||||
~cow_wrapper_client4();
|
||||
|
||||
cow_wrapper_client4( const cow_wrapper_client4& );
|
||||
cow_wrapper_client4& operator=( const cow_wrapper_client4& );
|
||||
|
||||
bool is_default() const;
|
||||
|
||||
bool operator==( const cow_wrapper_client4& rRHS ) const;
|
||||
bool operator!=( const cow_wrapper_client4& rRHS ) const;
|
||||
bool operator<( const cow_wrapper_client4& rRHS ) const;
|
||||
|
||||
private:
|
||||
o3tl::cow_wrapper< int > maImpl;
|
||||
};
|
||||
|
||||
} // namespace o3tltests
|
||||
|
||||
#endif /* INCLUDED_COW_WRAPPER_CLIENTS_HXX */
|
||||
|
@@ -139,12 +139,35 @@ public:
|
||||
test( aTestObj7, aTestObj8, aTestObj9 );
|
||||
}
|
||||
|
||||
void testStaticDefault()
|
||||
{
|
||||
cow_wrapper_client4 aTestObj1;
|
||||
cow_wrapper_client4 aTestObj2;
|
||||
cow_wrapper_client4 aTestObj3(4);
|
||||
|
||||
CPPUNIT_ASSERT_MESSAGE("aTestObj1.is_default()",
|
||||
aTestObj1.is_default() );
|
||||
CPPUNIT_ASSERT_MESSAGE("aTestObj2.is_default()",
|
||||
aTestObj2.is_default() );
|
||||
CPPUNIT_ASSERT_MESSAGE("!aTestObj3.is_default()",
|
||||
!aTestObj3.is_default() );
|
||||
aTestObj1 = aTestObj2;
|
||||
CPPUNIT_ASSERT_MESSAGE("aTestObj1.is_default() #2",
|
||||
aTestObj1.is_default() );
|
||||
CPPUNIT_ASSERT_MESSAGE("aTestObj2.is_default() #2",
|
||||
aTestObj2.is_default() );
|
||||
aTestObj1 = aTestObj3;
|
||||
CPPUNIT_ASSERT_MESSAGE("!aTestObj1.is_default()",
|
||||
!aTestObj1.is_default() );
|
||||
}
|
||||
|
||||
// Change the following lines only, if you add, remove or rename
|
||||
// member functions of the current class,
|
||||
// because these macros are need by auto register mechanism.
|
||||
|
||||
CPPUNIT_TEST_SUITE(cow_wrapper_test);
|
||||
CPPUNIT_TEST(testCowWrapper);
|
||||
CPPUNIT_TEST(testStaticDefault);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user