Lunchbox  1.11.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lunchbox::RefPtr< T > Class Template Reference

A smart reference pointer, aka boost::intrusive_ptr. More...

#include <refPtr.h>

+ Collaboration diagram for lunchbox::RefPtr< T >:

Public Member Functions

 RefPtr ()
 Construct a new, empty reference pointer. More...
 
 RefPtr (T *const ptr)
 Construct a reference pointer from a C pointer. More...
 
 RefPtr (const RefPtr &from)
 Construct a copy of a reference pointer. More...
 
template<class O >
 RefPtr (RefPtr< O > from)
 Construct a copy of a reference pointer of a different type. More...
 
 ~RefPtr ()
 Destruct this reference pointer. More...
 
RefPtroperator= (const RefPtr &rhs)
 Assign another RefPtr to this reference pointer. More...
 
RefPtroperator= (T *ptr)
 Assign a C pointer to this reference pointer. More...
 
bool operator== (const RefPtr &rhs) const
 
bool operator!= (const RefPtr &rhs) const
 
 operator bool_t () const
 
bool operator! () const
 
bool operator< (const RefPtr &rhs) const
 
bool operator> (const RefPtr &rhs) const
 
bool operator== (const T *ptr) const
 
bool operator!= (const T *ptr) const
 
T * operator-> ()
 Access the held object. More...
 
const T * operator-> () const
 Access the held object. More...
 
T & operator* ()
 Access the held object. More...
 
const T & operator* () const
 Access the held object. More...
 
T * get ()
 
const T * get () const
 
bool isValid () const
 

Detailed Description

template<class T>
class lunchbox::RefPtr< T >

A smart reference pointer, aka boost::intrusive_ptr.

Relies on the held object to implement ref() and unref() correctly. Serializable using boost.serialization.

Deprecated:
Use boost::intrusive_ptr

Example:

/* Copyright (c) 2006-2013, Stefan Eilemann <eile@equalizergraphics.com>
* 2012-2013, Daniel Nachbaur <danielnachbaur@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define TEST_RUNTIME 300 // seconds
#include <test.h>
#include <lunchbox/clock.h>
#include <lunchbox/refPtr.h>
#include <lunchbox/referenced.h>
#include <lunchbox/thread.h>
#include <iostream>
#include <boost/intrusive_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/serialization/access.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#define NTHREADS 24
#define NREFS 200000
class Foo : public lunchbox::Referenced
{
public:
Foo() {}
private:
virtual ~Foo() {}
friend class boost::serialization::access;
template< class Archive > void serialize( Archive&, unsigned int ) {}
};
typedef lunchbox::RefPtr<Foo> FooPtr;
FooPtr foo;
class TestThread : public lunchbox::Thread
{
public:
virtual void run()
{
FooPtr myFoo;
for( size_t i = 0; i<NREFS; ++i )
{
myFoo = foo;
foo = myFoo;
myFoo = 0;
}
}
};
typedef boost::intrusive_ptr<Foo> BoostPtr;
BoostPtr bFoo;
class BThread : public lunchbox::Thread
{
public:
virtual void run()
{
BoostPtr myBoost;
for( size_t i = 0; i<NREFS; ++i )
{
myBoost = bFoo;
bFoo = myBoost;
myBoost = 0;
}
}
};
class Bar : public lunchbox::Referenced
{
public:
Bar() {}
virtual ~Bar() {}
};
typedef boost::shared_ptr<Bar> BarPtr;
BarPtr bBar;
class BarThread : public lunchbox::Thread
{
public:
virtual void run()
{
BarPtr myBar;
for( size_t i = 0; i<NREFS; ++i )
{
myBar = bBar;
bBar = myBar;
myBar.reset();
}
}
};
int main( int, char** )
{
foo = new Foo;
TestThread threads[NTHREADS];
for( size_t i=0; i<NTHREADS; ++i )
TEST( threads[i].start( ));
for( size_t i=0; i<NTHREADS; ++i )
TEST( threads[i].join( ));
const float time = clock.getTimef();
std::cout << time << " ms for " << 3*NREFS << " lunchbox::RefPtr operations"
<< " in " << NTHREADS << " threads ("
<< time/(3*NREFS*NTHREADS)*1000000 << "ns/op)" << std::endl;
TEST( foo->getRefCount() == 1 );
bFoo = new Foo;
BThread bThreads[NTHREADS];
clock.reset();
for( size_t i=0; i<NTHREADS; ++i )
TEST( bThreads[i].start( ));
for( size_t i=0; i<NTHREADS; ++i )
TEST( bThreads[i].join( ));
const float bTime = clock.getTimef();
std::cout << bTime << " ms for " << 3*NREFS << " boost::intrusive_ptr ops "
<< "in " << NTHREADS << " threads ("
<< bTime/(3*NREFS*NTHREADS)*1000000 << "ns/op)" << std::endl;
TEST( bFoo->getRefCount() == 1 );
boost::intrusive_ptr< Foo > boostFoo( foo.get( ));
TEST( foo->getRefCount() == 2 );
boostFoo = 0;
TEST( foo->getRefCount() == 1 );
bBar = BarPtr( new Bar );
BarThread barThreads[NTHREADS];
clock.reset();
for( size_t i=0; i<NTHREADS; ++i )
TEST( barThreads[i].start( ));
for( size_t i=0; i<NTHREADS; ++i )
TEST( barThreads[i].join( ));
const float barTime = clock.getTimef();
std::cout << barTime << " ms for " << 3*NREFS <<" boost::shared_ptr ops in "
<< NTHREADS << " threads (" << barTime/(3*NREFS*NTHREADS)*1000000
<< "ns/op)" << std::endl;
bBar = boost::make_shared< Bar >();
clock.reset();
for( size_t i=0; i<NTHREADS; ++i )
TEST( barThreads[i].start( ));
for( size_t i=0; i<NTHREADS; ++i )
TEST( barThreads[i].join( ));
const float barTime2 = clock.getTimef();
std::cout << barTime2 << " ms for " << 3*NREFS<<" boost::shared_ptr ops in "
<< NTHREADS << " threads (" << barTime2/(3*NREFS*NTHREADS)*1000000
<< "ns/op) using make_shared" << std::endl;
foo = 0;
FooPtr inFoo1( new Foo );
TEST( inFoo1->getRefCount() == 1 );
FooPtr inFoo2 = inFoo1;
TEST( inFoo2->getRefCount() == 2 );
FooPtr outFoo1;
std::stringstream stream;
boost::archive::text_oarchive oar( stream );
oar & inFoo1;
boost::archive::text_iarchive iar( stream );
iar & outFoo1;
TEST( outFoo1->getRefCount() == 1 );
FooPtr outFoo2 = outFoo1;
TEST( outFoo2->getRefCount() == 2 );
return EXIT_SUCCESS;
}

Definition at line 39 of file refPtr.h.

Constructor & Destructor Documentation

template<class T>
lunchbox::RefPtr< T >::RefPtr ( )
inline

Construct a new, empty reference pointer.

Version
1.0

Definition at line 45 of file refPtr.h.

template<class T>
lunchbox::RefPtr< T >::RefPtr ( T *const  ptr)
inline

Construct a reference pointer from a C pointer.

Version
1.0

Definition at line 48 of file refPtr.h.

template<class T>
lunchbox::RefPtr< T >::RefPtr ( const RefPtr< T > &  from)
inline

Construct a copy of a reference pointer.

Version
1.0

Definition at line 51 of file refPtr.h.

template<class T>
template<class O >
lunchbox::RefPtr< T >::RefPtr ( RefPtr< O >  from)
inline

Construct a copy of a reference pointer of a different type.

Version
1.0

Definition at line 57 of file refPtr.h.

template<class T>
lunchbox::RefPtr< T >::~RefPtr ( )
inline

Destruct this reference pointer.

Version
1.0

Definition at line 61 of file refPtr.h.

Member Function Documentation

template<class T>
T* lunchbox::RefPtr< T >::get ( )
inline
Returns
the C pointer.
Version
1.0

Definition at line 150 of file refPtr.h.

Referenced by lunchbox::PerThreadRef< T >::operator=().

+ Here is the caller graph for this function:

template<class T>
const T* lunchbox::RefPtr< T >::get ( ) const
inline
Returns
the C pointer.
Version
1.0

Definition at line 152 of file refPtr.h.

template<class T>
bool lunchbox::RefPtr< T >::isValid ( ) const
inline
Returns
true if the RefPtr holds a non-0 pointer.
Version
1.0

Definition at line 155 of file refPtr.h.

template<class T>
lunchbox::RefPtr< T >::operator bool_t ( ) const
inline
Returns
true if a pointer is held, false otherwise.
Version
1.1.5

Definition at line 107 of file refPtr.h.

template<class T>
bool lunchbox::RefPtr< T >::operator! ( ) const
inline
Returns
true if the RefPtr is empty.
Version
1.0

Definition at line 110 of file refPtr.h.

template<class T>
bool lunchbox::RefPtr< T >::operator!= ( const RefPtr< T > &  rhs) const
inline
Returns
true if both reference pointer hold different C pointer.
Version
1.0

Definition at line 100 of file refPtr.h.

template<class T>
bool lunchbox::RefPtr< T >::operator!= ( const T *  ptr) const
inline
Returns
true if the reference pointers does not hold the C pointer
Version
1.0

Definition at line 134 of file refPtr.h.

template<class T>
T& lunchbox::RefPtr< T >::operator* ( )
inline

Access the held object.

Version
1.0

Definition at line 143 of file refPtr.h.

template<class T>
const T& lunchbox::RefPtr< T >::operator* ( ) const
inline

Access the held object.

Version
1.0

Definition at line 146 of file refPtr.h.

template<class T>
T* lunchbox::RefPtr< T >::operator-> ( )
inline

Access the held object.

Version
1.0

Definition at line 137 of file refPtr.h.

template<class T>
const T* lunchbox::RefPtr< T >::operator-> ( ) const
inline

Access the held object.

Version
1.0

Definition at line 140 of file refPtr.h.

template<class T>
bool lunchbox::RefPtr< T >::operator< ( const RefPtr< T > &  rhs) const
inline
Returns
true if the left RefPtr is smaller then the right.
Version
1.0

Definition at line 116 of file refPtr.h.

template<class T>
RefPtr& lunchbox::RefPtr< T >::operator= ( const RefPtr< T > &  rhs)
inline

Assign another RefPtr to this reference pointer.

Version
1.0

Definition at line 64 of file refPtr.h.

template<class T>
RefPtr& lunchbox::RefPtr< T >::operator= ( T *  ptr)
inline

Assign a C pointer to this reference pointer.

Version
1.0

Definition at line 77 of file refPtr.h.

template<class T>
bool lunchbox::RefPtr< T >::operator== ( const RefPtr< T > &  rhs) const
inline
Returns
true if both reference pointers hold the same C pointer.
Version
1.0

Definition at line 93 of file refPtr.h.

template<class T>
bool lunchbox::RefPtr< T >::operator== ( const T *  ptr) const
inline
Returns
true if the reference pointers holds the C pointer.
Version
1.0

Definition at line 128 of file refPtr.h.

template<class T>
bool lunchbox::RefPtr< T >::operator> ( const RefPtr< T > &  rhs) const
inline
Returns
true if the right RefPtr is smaller then the left.
Version
1.0

Definition at line 122 of file refPtr.h.


The documentation for this class was generated from the following file: