Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
Public Types | Public Member Functions | Friends | List of all members
ecuda::shared_ptr< T > Class Template Reference

A smart pointer that retains shared ownership of an object in device memory. More...

#include <shared_ptr.hpp>

Public Types

typedef T element_type
 type of the managed object More...
 

Public Member Functions

__HOST__ __DEVICE__ __CONSTEXPR__ shared_ptr () __NOEXCEPT__
 Default constructor constructs a shared_ptr with no managed object. More...
 
template<typename U >
__HOST__ __DEVICE__ shared_ptr (U *ptr)
 Constructs a shared_ptr with a pointer to the managed object. More...
 
template<typename U , class Deleter >
__HOST__ __DEVICE__ shared_ptr (U *ptr, Deleter deleter)
 Constructs a shared_ptr with a pointer to the managed object. More...
 
template<typename U >
__HOST__ __DEVICE__ shared_ptr (const shared_ptr< U > &src, T *ptr) __NOEXCEPT__
 The aliasing constructor. More...
 
__HOST__ __DEVICE__ shared_ptr (const shared_ptr &src) __NOEXCEPT__
 Copy constructor. More...
 
template<typename U >
__HOST__ __DEVICE__ shared_ptr (const shared_ptr< U > &src) __NOEXCEPT__
 Copy constructor. More...
 
__HOST__ __DEVICE__ ~shared_ptr ()
 Destructor. More...
 
__HOST__ __DEVICE__ shared_ptroperator= (const shared_ptr &src) __NOEXCEPT__
 Replaces the managed object. More...
 
template<typename U >
__HOST__ __DEVICE__ shared_ptroperator= (const shared_ptr< U > &src) __NOEXCEPT__
 Replaces the managed object. More...
 
__HOST__ __DEVICE__ void reset () __NOEXCEPT__
 Releases ownership of the managed object. More...
 
template<typename U >
__HOST__ __DEVICE__ void reset (U *ptr)
 Replaces the managed object with another. More...
 
template<typename U , class Deleter >
__HOST__ __DEVICE__ void reset (U *ptr, Deleter d)
 Replaces the managed object with another. More...
 
__HOST__ __DEVICE__ void swap (shared_ptr &other) __NOEXCEPT__
 Exchanges the contents of *this and other. More...
 
__HOST__ __DEVICE__ T * get () const __NOEXCEPT__
 Returns a pointer to the managed object. More...
 
__DEVICE__
ecuda::add_lvalue_reference< T >
::type 
operator* () const __NOEXCEPT__
 Dereferences pointer to the managed object. More...
 
__HOST__ __DEVICE__ T * operator-> () const __NOEXCEPT__
 Dereferences pointer to the managed object. More...
 
__HOST__ __DEVICE__ std::size_t use_count () const __NOEXCEPT__
 Returns the number of smart pointers managing the current object. More...
 
__HOST__ __DEVICE__ bool unique () const __NOEXCEPT__
 Checks if this is the only shared_ptr instance managing the current object. More...
 
__HOST__ __DEVICE__ operator bool () const __NOEXCEPT__
 Checks if this stores a non-null pointer. More...
 
template<typename U >
__HOST__ __DEVICE__ bool owner_before (const shared_ptr< U > &other) const
 
template<typename T2 >
__HOST__ __DEVICE__ bool operator== (const shared_ptr< T2 > &other) const __NOEXCEPT__
 
template<typename T2 >
__HOST__ __DEVICE__ bool operator!= (const shared_ptr< T2 > &other) const __NOEXCEPT__
 
template<typename T2 >
__HOST__ __DEVICE__ bool operator< (const shared_ptr< T2 > &other) const __NOEXCEPT__
 
template<typename T2 >
__HOST__ __DEVICE__ bool operator> (const shared_ptr< T2 > &other) const __NOEXCEPT__
 
template<typename T2 >
__HOST__ __DEVICE__ bool operator<= (const shared_ptr< T2 > &other) const __NOEXCEPT__
 
template<typename T2 >
__HOST__ __DEVICE__ bool operator>= (const shared_ptr< T2 > &other) const __NOEXCEPT__
 

Friends

template<typename U >
class shared_ptr
 
template<typename U , typename V >
std::basic_ostream< U, V > & operator<< (std::basic_ostream< U, V > &out, const shared_ptr &ptr)
 Inserts a shared_ptr into a std::basic_ostream. More...
 

Detailed Description

template<typename T>
class ecuda::shared_ptr< T >

A smart pointer that retains shared ownership of an object in device memory.

ecuda::shared_ptr is a smart pointer that retains shared ownership of an object in device memory through a pointer. Several shared_ptr objects may own the same object. The object is destroyed and its memory deallocated when either of the following happens:

The object is destroyed using ecuda::default_device_delete or a custom deleter that is supplied to shared_ptr during construction.

A shared_ptr can share ownership of an object while storing a pointer to another object. This feature can be used to point to member objects while owning the object they belong to. The stored pointer is the one accessed by get(), the dereference and the comparison operators. The managed poitner is the one passed to the deleter when use count reaches zero.

A shared_ptr may also own no objects, in which case it is called empty (an empty shared_ptr may have a non-null stored pointer if the aliasing constructor was used to create it).

All specializations of shared_ptr meet the requirements of CopyConstructible, CopyAssignable, and LessThanComparable and are contextually convertible to bool.

All member functions (including copy constructor and copy assignment) can be called by multiple threads on different instances of shared_ptr without additional synchronization even if these instances are copies and share ownership of the same object. If multiple threads of execution access the same shared_ptr without synchronization and any of those accesses uses a non-const member function of shared_ptr then a data race will occur; the shared_ptr overloads of atomic functions can be used to prevent the data race.

Definition at line 122 of file shared_ptr.hpp.

Member Typedef Documentation

template<typename T>
typedef T ecuda::shared_ptr< T >::element_type

type of the managed object

Definition at line 126 of file shared_ptr.hpp.

Constructor & Destructor Documentation

template<typename T>
__HOST__ __DEVICE__ __CONSTEXPR__ ecuda::shared_ptr< T >::shared_ptr ( )
inline

Default constructor constructs a shared_ptr with no managed object.

Definition at line 139 of file shared_ptr.hpp.

template<typename T>
template<typename U >
__HOST__ __DEVICE__ ecuda::shared_ptr< T >::shared_ptr ( U *  ptr)
inlineexplicit

Constructs a shared_ptr with a pointer to the managed object.

U must be a complete type and ptr must be convertible to T*. Additionally, uses ecuda::default_device_delete as the deleter.

Parameters
ptra pointer to an object to manage

Definition at line 150 of file shared_ptr.hpp.

template<typename T>
template<typename U , class Deleter >
__HOST__ __DEVICE__ ecuda::shared_ptr< T >::shared_ptr ( U *  ptr,
Deleter  deleter 
)
inline

Constructs a shared_ptr with a pointer to the managed object.

U must be a complete type and ptr must be convertible to T*. Additionally, uses ecuda::default_device_delete as the deleter.

Parameters
ptra pointer to an object to manage
deletera deleter to use to destroy the object

Definition at line 167 of file shared_ptr.hpp.

template<typename T>
template<typename U >
__HOST__ __DEVICE__ ecuda::shared_ptr< T >::shared_ptr ( const shared_ptr< U > &  src,
T *  ptr 
)
inline

The aliasing constructor.

Constructs a shared_ptr which shares ownership information with src, but holds an unrelated and unmanaged pointer ptr. Even if this shared_ptr is the last of the group to go out of scope, it will call the destructor for the object originally managed by src. However, calling get() on this will always return a copy of ptr. It is the responsibility of the programmer to make sure that this ptr remains valid as long as this shared_ptr exists, such as in the typical use cases where ptr is a member of the object managed by src or is an alias (e.g. downcast) of src.get().

Parameters
srcanother smart pointer to share ownership to or acquire the ownership from
ptra pointer to an object to manage

Definition at line 190 of file shared_ptr.hpp.

template<typename T>
__HOST__ __DEVICE__ ecuda::shared_ptr< T >::shared_ptr ( const shared_ptr< T > &  src)
inline

Copy constructor.

Constructs a shared_ptr which shares ownership of the object managed by src. If src manages no object, *this manages no object too.

Parameters
srcanother smart pointer to share ownership to or acquire the ownership from

Definition at line 205 of file shared_ptr.hpp.

template<typename T>
template<typename U >
__HOST__ __DEVICE__ ecuda::shared_ptr< T >::shared_ptr ( const shared_ptr< U > &  src)
inline

Copy constructor.

Constructs a shared_ptr which shares ownership of the object managed by src. If src manages no object, this manages no object too. This overload won't participate in overload resolution if U is not implicitly convertible to T*.

Parameters
srcanother smart pointer to share ownership to or acquire the ownership from

Definition at line 223 of file shared_ptr.hpp.

template<typename T>
__HOST__ __DEVICE__ ecuda::shared_ptr< T >::~shared_ptr ( )
inline

Destructor.

If *this owns an object and it is the last shared_ptr owning it, the object is destroyed through the owned deleter. After the destruction, the smart pointers that shared ownership with *this, if any, will report a use_count() that is one less than the previous value.

Definition at line 285 of file shared_ptr.hpp.

Member Function Documentation

template<typename T>
__HOST__ __DEVICE__ T* ecuda::shared_ptr< T >::get ( ) const
inline

Returns a pointer to the managed object.

Returns a null pointer if no object is being managed.

Returns
a pointer to the managed object

Definition at line 406 of file shared_ptr.hpp.

template<typename T>
__HOST__ __DEVICE__ ecuda::shared_ptr< T >::operator bool ( ) const
inline

Checks if this stores a non-null pointer.

Returns
true if *this stores a pointer, false otherwise.

Definition at line 457 of file shared_ptr.hpp.

template<typename T>
template<typename T2 >
__HOST__ __DEVICE__ bool ecuda::shared_ptr< T >::operator!= ( const shared_ptr< T2 > &  other) const
inline

Definition at line 476 of file shared_ptr.hpp.

template<typename T>
__DEVICE__ ecuda::add_lvalue_reference<T>::type ecuda::shared_ptr< T >::operator* ( ) const
inline

Dereferences pointer to the managed object.

This is only callable from the device, since objects in device memory are only accessible from device code.

Returns
reference to the managed object

Definition at line 416 of file shared_ptr.hpp.

template<typename T>
__HOST__ __DEVICE__ T* ecuda::shared_ptr< T >::operator-> ( ) const
inline

Dereferences pointer to the managed object.

Returns
pointer to the managed object

Definition at line 423 of file shared_ptr.hpp.

template<typename T>
template<typename T2 >
__HOST__ __DEVICE__ bool ecuda::shared_ptr< T >::operator< ( const shared_ptr< T2 > &  other) const
inline

Definition at line 477 of file shared_ptr.hpp.

template<typename T>
template<typename T2 >
__HOST__ __DEVICE__ bool ecuda::shared_ptr< T >::operator<= ( const shared_ptr< T2 > &  other) const
inline

Definition at line 479 of file shared_ptr.hpp.

template<typename T>
__HOST__ __DEVICE__ shared_ptr& ecuda::shared_ptr< T >::operator= ( const shared_ptr< T > &  src)
inline

Replaces the managed object.

Shares ownership of the object managed by src. If src manages no object, *this manages no object too.

Parameters
srcanother smart pointer to share ownership to or acquire the ownership from

Definition at line 307 of file shared_ptr.hpp.

template<typename T>
template<typename U >
__HOST__ __DEVICE__ shared_ptr& ecuda::shared_ptr< T >::operator= ( const shared_ptr< U > &  src)
inline

Replaces the managed object.

Shares ownership of the object managed by src. If src manages no object, this manages no object too. This overload doesn't participate in overload resolution if U is not implicitly convertible to T*.

Parameters
srcanother smart pointer to share ownership to or acquire the ownership from

Definition at line 322 of file shared_ptr.hpp.

template<typename T>
template<typename T2 >
__HOST__ __DEVICE__ bool ecuda::shared_ptr< T >::operator== ( const shared_ptr< T2 > &  other) const
inline

Definition at line 475 of file shared_ptr.hpp.

template<typename T>
template<typename T2 >
__HOST__ __DEVICE__ bool ecuda::shared_ptr< T >::operator> ( const shared_ptr< T2 > &  other) const
inline

Definition at line 478 of file shared_ptr.hpp.

template<typename T>
template<typename T2 >
__HOST__ __DEVICE__ bool ecuda::shared_ptr< T >::operator>= ( const shared_ptr< T2 > &  other) const
inline

Definition at line 480 of file shared_ptr.hpp.

template<typename T>
template<typename U >
__HOST__ __DEVICE__ bool ecuda::shared_ptr< T >::owner_before ( const shared_ptr< U > &  other) const
inline

Checks whether this shared_ptr precedes other in implementation defined owner-based (as opposed to value-based) order. The order is such that two smart pointers compare equivalent only if they are both empty or if they both own the same object, even if the values of the pointers obtained by get() are different (e.g. because they point at different subobjects within the same object).

The ordering is used to make shared pointers usable as keys in associative containers, typically through ecuda::owner_less.

Returns
true if *this precedes other, false otherwise.

Definition at line 473 of file shared_ptr.hpp.

template<typename T>
__HOST__ __DEVICE__ void ecuda::shared_ptr< T >::reset ( )
inline

Releases ownership of the managed object.

After the call, *this manages no object.

Definition at line 363 of file shared_ptr.hpp.

template<typename T>
template<typename U >
__HOST__ __DEVICE__ void ecuda::shared_ptr< T >::reset ( U *  ptr)
inline

Replaces the managed object with another.

Replaces the managed object with an object pointed to by ptr. U must be a complete type and implicitly convertible to T. Additionally, uses ecuda::default_device_delete as the deleter.

Parameters
ptrpointer to an object to acquire ownership of

Definition at line 374 of file shared_ptr.hpp.

template<typename T>
template<typename U , class Deleter >
__HOST__ __DEVICE__ void ecuda::shared_ptr< T >::reset ( U *  ptr,
Deleter  d 
)
inline

Replaces the managed object with another.

Replaces the managed object with an object pointed to by ptr. U must be a complete type and implicitly convertible to T. Additionally, uses the specified deleter as the deleter.

Parameters
ptrpointer to an object to acquire ownership of
ddeleter to store for deletion of the object

Definition at line 386 of file shared_ptr.hpp.

template<typename T>
__HOST__ __DEVICE__ void ecuda::shared_ptr< T >::swap ( shared_ptr< T > &  other)
inline

Exchanges the contents of *this and other.

Parameters
othersmart pointer to exchange the contents with

Definition at line 393 of file shared_ptr.hpp.

template<typename T>
__HOST__ __DEVICE__ bool ecuda::shared_ptr< T >::unique ( ) const
inline

Checks if this is the only shared_ptr instance managing the current object.

i.e. whether use_count() == 1.

Returns
true if *this is the only shared_ptr instance managing the current object, false otherwise

Definition at line 442 of file shared_ptr.hpp.

template<typename T>
__HOST__ __DEVICE__ std::size_t ecuda::shared_ptr< T >::use_count ( ) const
inline

Returns the number of smart pointers managing the current object.

Returns the number of different shared_ptr instances (this included) managing the current object. If there is no managed object, 0 is returned.

Returns
the number of shared_ptr instances managing the current object or 0 if there is no managed object

Definition at line 433 of file shared_ptr.hpp.

Friends And Related Function Documentation

template<typename T>
template<typename U , typename V >
std::basic_ostream<U,V>& operator<< ( std::basic_ostream< U, V > &  out,
const shared_ptr< T > &  ptr 
)
friend

Inserts a shared_ptr into a std::basic_ostream.

Equivalent to out << ptr.get().

Parameters
outa std::basic_ostream to insert ptr into
ptrthe data to be inserted into os
Returns
out

Definition at line 492 of file shared_ptr.hpp.

template<typename T>
template<typename U >
friend class shared_ptr
friend

Definition at line 132 of file shared_ptr.hpp.


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