toast::unintrusive_ptr< T > Class Template Reference
[toast unintrusive pointer]

#include <toast/unintrusive_ptr.hpp>

List of all members.


Detailed Description

template<class T>
class toast::unintrusive_ptr< T >

An unintrusive smart pointer that is faster than boost::shared_ptr.

This templated class is a smart pointer type that maintains a reference count for the object it points to. Its interface is slightly different from most smart pointers in that it requires you to pass an in place factory to its constructor instead of a pointer to an already allocated object. This is because it creates the object in place adjacent to its reference count in memory. This allows the performance benefits of an intrusive smart pointer without being intrusive. In Boost 1.39+ you can use make_shared to get similar benefits with shared_ptr, but a shared_ptr will still be twice as large and have a less safe interface (unintrusive_ptr construct their contents via factories so that it's guaranteed they manage the object).

Warning:
This class does not support base class pointers to derived class objects when the derived class has multiple base classes, or when there is a class with multiple base classes between Base and Derived. What does this mean? If C inherits B inherits A, you can have an unintrusive_ptr to a C object just fine. But if C inherits from B and A, an unintrusive_ptr<A> or an unintrusive_ptr<B> WILL NOT WORK. Generally speaking, when you have a base class unintrusive pointer to a derived class object, there must be no has-multiple-base-classes classes in the hierarchy between the base class and the derived class and the derived class itself must not have multiple bases. Consider this example: E inherits from D inherits from C inherits from B and A. An unintrusive_ptr<C> can point to C, D, and E objects (the base class having multiple bases is not problematic). An unintrusive_ptr can only point to A objects (any of the derived classes in the example have multiple bases or require crossing a class with multiple bases). Similarly unintrusive_ptr<B>'s can only point to B objects.

unintrusive_ptr will do its best to detect if you are violating the previous warning. It adds cheap startup checks to determine if any templates were instantiated for base class pointers to derived class objects with MI in their hierarchy. However, there is one case it will not catch, which is if a derived object is pointed to by a base class pointer where the base class is the first class in list of the derived class's bases. unintrusive_ptr will actually work correctly in this situation! However, since this usage might create the expectation that unintrusive_ptr will work with other bases (which it won't) we suggest against doing this intentionally.


Public Types

typedef T element_type
typedef T *(unintrusive_ptr< T >::* unspecified_bool_type )() const

Public Member Functions

template<class TypedInPlaceFactory>
 unintrusive_ptr (TypedInPlaceFactory const &factory)
 unintrusive_ptr (unintrusive_ptr const &toCopy)
template<class Y>
 unintrusive_ptr (unintrusive_ptr< Y > const &other)
template<class Y>
 unintrusive_ptr (weak_unintrusive_ptr< Y > const &r)
unintrusive_ptroperator= (unintrusive_ptr const &target)
template<class Y>
unintrusive_ptr< T > & operator= (unintrusive_ptr< Y > const &target)
void reset ()
template<class TypedInPlaceFactory>
void reset (TypedInPlaceFactory const &factory)
T & operator* () const
T * operator-> () const
T * get () const
 operator unspecified_bool_type () const
void swap (unintrusive_ptr< T > &other)

Friends

class unintrusive_ptr
class weak_unintrusive_ptr< T >
template<class Y>
int detail::get_use_count (unintrusive_ptr< Y > const &x)
template<class Y>
bool detail::get_unique (unintrusive_ptr< Y > const &x)
template<class X, class Y>
unintrusive_ptr< X > static_pointer_cast (unintrusive_ptr< Y > const &r)
template<class X, class Y>
unintrusive_ptr< X > const_pointer_cast (unintrusive_ptr< Y > const &r)
template<class X, class Y>
unintrusive_ptr< X > dynamic_pointer_cast (unintrusive_ptr< Y > const &r)
template<class X, class Y>
bool operator== (unintrusive_ptr< X > const &a, unintrusive_ptr< Y > const &b)
template<class X, class Y>
bool operator!= (unintrusive_ptr< X > const &a, unintrusive_ptr< Y > const &b)
template<class X, class Y>
bool operator< (unintrusive_ptr< X > const &a, unintrusive_ptr< Y > const &b)

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

SourceForge.net Logo