ROOT  6.06/09
Reference Guide
Public Types | Public Member Functions | Private Attributes | List of all members
ROOT::TCoopPtr< POINTEE > Class Template Reference

template<class POINTEE>
class ROOT::TCoopPtr< POINTEE >

Several pointers point to the same object, any of them can delete the object, setting all of them to nullptr.

Once all TCoopPtr-s have been destructed, the pointed-to object is also destructed.

Internally, this is modelled as a shared_ptr<unique_ptr<T>>: all shared_ptr-s point to the same pointer (unique_ptr, actually), and a change to that pointer value is visible to all of them.

This is a PAW-style ownership management, useful if deletion needs to be an explicit action (unlike shared_ptr), but shared resource handling is required (like unique_ptr).

Example: TCoopPtr<TH1D> pHist(new TH1D({{10, 0., 1.}})); TCoopPtr<TCanvas> pad = gROOT.Create<TCanvas>(); pad->Draw(pHist); // histogram is drawn; the pad co-owns the histogram pHist.Delete(); // histogram vanishes from pad. Or the histogram could have been deleted on the pad, interactively.

Definition at line 46 of file TCoopPtr.h.

Public Types

using Pointee_t = std::remove_reference_t< POINTEE >
 The type pointed to. More...
 
using Pointer_t = std::add_pointer_t< Pointee_t >
 The primitive pointer type. More...
 
using SharedPtr_t = std::shared_ptr< void * >
 The underlying std::shared_ptr. More...
 

Public Member Functions

 TCoopPtr ()=default
 
 TCoopPtr (const TCoopPtr &other)=default
 Creates a synchronized TCoopPtr: once any of them call Delete(), all others will "contain" a nullptr. More...
 
 TCoopPtr (TCoopPtr &&other)=default
 
 TCoopPtr (Pointer_t ptr)
 Initialize from a raw pointer. More...
 
template<class SELF = TCoopPtr, typename std::enable_if< std::is_move_constructible< typename SELF::Pointee_t >::value >::type * = nullptr>
 TCoopPtr (POINTEE &&obj)
 Create a TCoopPtr from an object. More...
 
 TCoopPtr (std::unique_ptr< POINTEE > &&ptr)
 Initialize from a unique_ptr; takes ownership. More...
 
template<class DERIVED , typename std::enable_if< std::is_base_of< POINTEE, DERIVED >{}>::type * = nullptr>
 TCoopPtr (const TCoopPtr< DERIVED > &derived)
 Conversion from a pointer to derived to a pointer to base. More...
 
Pointer_t Get () const
 Get the raw pointer. More...
 
const SharedPtr_tGetShared () const
 
POINTEE * Delete () const
 Delete the object pointed to. More...
 
Pointer_t operator-> () const
 Access the object pointed to. More...
 
POINTEE & operator* () const
 Dereference the object pointed to. More...
 
 operator bool () const
 Returns true if the pointer is non-null. More...
 

Private Attributes

SharedPtr_t fPtrBuf
 

#include <ROOT/TCoopPtr.h>

+ Inheritance diagram for ROOT::TCoopPtr< POINTEE >:

Member Typedef Documentation

template<class POINTEE>
using ROOT::TCoopPtr< POINTEE >::Pointee_t = std::remove_reference_t<POINTEE>

The type pointed to.

Definition at line 49 of file TCoopPtr.h.

template<class POINTEE>
using ROOT::TCoopPtr< POINTEE >::Pointer_t = std::add_pointer_t<Pointee_t>

The primitive pointer type.

Definition at line 51 of file TCoopPtr.h.

template<class POINTEE>
using ROOT::TCoopPtr< POINTEE >::SharedPtr_t = std::shared_ptr<void*>

The underlying std::shared_ptr.

Definition at line 53 of file TCoopPtr.h.

Constructor & Destructor Documentation

template<class POINTEE>
ROOT::TCoopPtr< POINTEE >::TCoopPtr ( )
default
template<class POINTEE>
ROOT::TCoopPtr< POINTEE >::TCoopPtr ( const TCoopPtr< POINTEE > &  other)
default

Creates a synchronized TCoopPtr: once any of them call Delete(), all others will "contain" a nullptr.

The last synchronized TCoopPtr to be destructed will delete the object they pointed to.

template<class POINTEE>
ROOT::TCoopPtr< POINTEE >::TCoopPtr ( TCoopPtr< POINTEE > &&  other)
default
template<class POINTEE>
ROOT::TCoopPtr< POINTEE >::TCoopPtr ( Pointer_t  ptr)
inline

Initialize from a raw pointer.

Definition at line 63 of file TCoopPtr.h.

template<class POINTEE>
template<class SELF = TCoopPtr, typename std::enable_if< std::is_move_constructible< typename SELF::Pointee_t >::value >::type * = nullptr>
ROOT::TCoopPtr< POINTEE >::TCoopPtr ( POINTEE &&  obj)
inline

Create a TCoopPtr from an object.

Tries to invoke POINTEE's move constructor. Is only available if POINTEE is move constructable.

Definition at line 72 of file TCoopPtr.h.

template<class POINTEE>
ROOT::TCoopPtr< POINTEE >::TCoopPtr ( std::unique_ptr< POINTEE > &&  ptr)
inline

Initialize from a unique_ptr; takes ownership.

Call as TCoopPtr p(std::move(uniquePtr));

Definition at line 79 of file TCoopPtr.h.

template<class POINTEE>
template<class DERIVED , typename std::enable_if< std::is_base_of< POINTEE, DERIVED >{}>::type * = nullptr>
ROOT::TCoopPtr< POINTEE >::TCoopPtr ( const TCoopPtr< DERIVED > &  derived)
inline

Conversion from a pointer to derived to a pointer to base.

Definition at line 85 of file TCoopPtr.h.

Member Function Documentation

template<class POINTEE>
POINTEE* ROOT::TCoopPtr< POINTEE >::Delete ( ) const
inline

Delete the object pointed to.

Safe to be called mutiple times. All other synchronized TCoopPtr will be notified.

Definition at line 98 of file TCoopPtr.h.

template<class POINTEE>
Pointer_t ROOT::TCoopPtr< POINTEE >::Get ( ) const
inline
template<class POINTEE>
const SharedPtr_t& ROOT::TCoopPtr< POINTEE >::GetShared ( ) const
inline
template<class POINTEE>
ROOT::TCoopPtr< POINTEE >::operator bool ( ) const
inline

Returns true if the pointer is non-null.

Definition at line 108 of file TCoopPtr.h.

template<class POINTEE>
POINTEE& ROOT::TCoopPtr< POINTEE >::operator* ( ) const
inline

Dereference the object pointed to.

Definition at line 106 of file TCoopPtr.h.

template<class POINTEE>
Pointer_t ROOT::TCoopPtr< POINTEE >::operator-> ( ) const
inline

Access the object pointed to.

Definition at line 104 of file TCoopPtr.h.

Member Data Documentation

template<class POINTEE>
SharedPtr_t ROOT::TCoopPtr< POINTEE >::fPtrBuf
private

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