Public Member Functions

fastjet::SharedPtr< T > Class Template Reference
[Advanced usage]

an implementation of C++0x shared pointers (or boost's) More...

#include <SharedPtr.hh>

Inheritance diagram for fastjet::SharedPtr< T >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 SharedPtr ()
 default ctor
template<class Y >
 SharedPtr (Y *ptr)
 initialise with the main data
 SharedPtr (SharedPtr const &share)
 overload the copy ctor so that it updates count
template<class Y >
 SharedPtr (SharedPtr< Y > const &share)
 overload the copy ctor so that it updates count
 ~SharedPtr ()
 default dtor
void reset ()
 reset the pointer to default value (NULL)
template<class Y >
void reset (Y *ptr)
 reset from a pointer
template<class Y >
void reset (SharedPtr< Y > const &share)
 do a smart copy
SharedPtroperator= (SharedPtr const &share)
 overload the = operator so that it updates count
template<class Y >
SharedPtroperator= (SharedPtr< Y > const &share)
 overload the = operator so that it updates count
T * operator() () const
 return the pointer we're pointing to
T & operator* () const
 indirection, get a reference to the stored pointer
T * operator-> () const
 indirection, get the stored pointer
T * get () const
 get the stored pointer
bool unique () const
 check if the instance is unique
long use_count () const
 return the number of counts
 operator bool () const
 conversion to bool This will allow you to use the indirection nicely
void swap (SharedPtr &share)
 exchange the content of the two pointers

Detailed Description

template<class T>
class fastjet::SharedPtr< T >

an implementation of C++0x shared pointers (or boost's)

this class implements a smart pointer, based on the shared+ptr proposal. A description of shared_ptr can be found in Section 2.2.3 of the first C++ Technical Report (TR1) http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1745.pdf or, alternatively, on the Boost C++ library website at http://www.boost.org/doc/libs/1_42_0/libs/smart_ptr/shared_ptr.htm

Our implementation is compatible with both of these apart from a series of members and functions that have not been implemented:

The class has been tested against the existing boost (v1.42) implementation (for the parts that we have implemented).

Definition at line 107 of file SharedPtr.hh.


Constructor & Destructor Documentation

template<class T>
template<class Y >
fastjet::SharedPtr< T >::SharedPtr ( Y *  ptr  )  [inline, explicit]

initialise with the main data

Parameters:
t : the object we want a smart pointer to

Definition at line 117 of file SharedPtr.hh.

                                              {
    _ptr = new __SharedCountingPtr(ptr);
  }

template<class T>
fastjet::SharedPtr< T >::SharedPtr ( SharedPtr< T > const &  share  )  [inline]

overload the copy ctor so that it updates count

Parameters:
share : the object we want to copy

Definition at line 123 of file SharedPtr.hh.

                                     : _ptr(NULL){
    reset(share);
  }

template<class T>
template<class Y >
fastjet::SharedPtr< T >::SharedPtr ( SharedPtr< Y > const &  share  )  [inline]

overload the copy ctor so that it updates count

Parameters:
share : the object we want to copy

Definition at line 129 of file SharedPtr.hh.

                                                          : _ptr(NULL){
    reset(share);
  }


Member Function Documentation

template<class T>
template<class Y >
void fastjet::SharedPtr< T >::reset ( SharedPtr< Y > const &  share  )  [inline]

do a smart copy

Parameters:
share : the object we want to copy Q? Do we need a non-template<Y> version as for the ctor and the assignment?

Definition at line 162 of file SharedPtr.hh.

                                                          {
    // if we already are pointing to sth, be sure to decrease its count
    if (_ptr!=NULL){
      // in the specific case where we're having the same
      // share,reset() has actually no effect. However if *this is the
      // only instance still alive (implying share==*this) bringing
      // the count down to 0 and deleting the object will not have the
      // expected effect. So we just avoid that situation explicitly
      if (_ptr == share._get_container()) return;
    
      _decrease_count();
    }
    
    // Watch out: if share is empty, construct an empty shared_ptr
    
    // copy the container
    _ptr = share._get_container();  // Note: automatically set it to NULL if share is empty
    
    if (_ptr!=NULL)
      (*_ptr)++;
  }

template<class T>
SharedPtr& fastjet::SharedPtr< T >::operator= ( SharedPtr< T > const &  share  )  [inline]

overload the = operator so that it updates count

Parameters:
share : the object we want to copy

Definition at line 186 of file SharedPtr.hh.

                                               {
    reset(share);
    return *this;
  }

template<class T>
template<class Y >
SharedPtr& fastjet::SharedPtr< T >::operator= ( SharedPtr< Y > const &  share  )  [inline]

overload the = operator so that it updates count

Parameters:
share : the object we want to copy

Definition at line 193 of file SharedPtr.hh.

                                                                    {
    reset(share);
    return *this;
  }

template<class T>
T& fastjet::SharedPtr< T >::operator* (  )  const [inline]

indirection, get a reference to the stored pointer

!!! WATCH OUT It fails the requirement that the stored pointer must no be NULL!! So you need explicitly to check the validity in your code

Definition at line 209 of file SharedPtr.hh.

                             {
    return *(_ptr->get());
  }

template<class T>
T* fastjet::SharedPtr< T >::operator-> (  )  const [inline]

indirection, get the stored pointer

!!! WATCH OUT It fails the requirement that the stored pointer must no be NULL!! So you need explicitly to check the validity in your code

Definition at line 218 of file SharedPtr.hh.

                              {
    if (_ptr==NULL) return NULL;
    return _ptr->get();
  }  


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