tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
tensor::RefPointer< value_type > Class Template Reference

A reference counting pointer with copy-on-write. More...

Public Types

typedef value_type elt_t
 Type of data pointed to.
 

Public Member Functions

elt_tbegin ()
 Retreive the pointer without caring for references (unsafe). More...
 
const elt_tbegin () const
 Retreive the pointer without caring for references (unsafe). More...
 
const elt_tbegin_const () const
 Retreive the pointer without caring for references (unsafe). More...
 
const elt_tend () const
 Retreive the pointer without caring for references (unsafe). More...
 
elt_tend ()
 Retreive the pointer without caring for references (unsafe). More...
 
const elt_tend_const () const
 Retreive the pointer without caring for references (unsafe). More...
 
RefPointer< elt_t > & operator= (const RefPointer< elt_t > &p)
 Copy a pointer increasing the reference count. More...
 
void reallocate (size_t new_size)
 Replace the pointer with newly allocated data. More...
 
size_t ref_count () const
 Number of references to the internal data.
 
 RefPointer ()
 Create an empty reference.
 
 RefPointer (size_t new_size)
 Allocate a pointer of s bytes. More...
 
 RefPointer (elt_t *data, size_t size, bool owned=true)
 Wrap around the given data.
 
 RefPointer (const RefPointer< elt_t > &p)
 Copy constructor that increases the reference count. More...
 
size_t size () const
 Size of pointed-to data. More...
 
 ~RefPointer ()
 Destructor that deletes no longer reference data. More...
 

Detailed Description

template<class value_type>
class tensor::RefPointer< value_type >

A reference counting pointer with copy-on-write.

This is a pointer that keeps track of whether the same data is shared by other RefPointer structures. It internally keeps a reference counter to store how many pointers look at the data. When the total number of references drops to zero, it destroys the pointed object.

This allows us to have different parts of the code use the same physical (tensor) data, thus avoiding expensive copy operations. The RefPointer behaves thus similar to pointers, but with internal bookkeeping and easier use.

As soon as you manipulate the data (access it through a non-const pointer), it will be silently and transparently copied to another location if the data is shared with other RefPointers. That is, the data encapsulated by a RefPointer is guaranteed not to be modified through side effects.

Note that pointers returned by the various begin() and end() functions are not reference-counted, so you should not store the returned pointers.

Definition at line 49 of file refcount.h.

Constructor & Destructor Documentation

template<class value_type>
tensor::RefPointer< value_type >::RefPointer ( size_t  new_size)

Allocate a pointer of s bytes.

template<class value_type>
tensor::RefPointer< value_type >::RefPointer ( const RefPointer< elt_t > &  p)

Copy constructor that increases the reference count.

template<class value_type>
tensor::RefPointer< value_type >::~RefPointer ( )

Destructor that deletes no longer reference data.

Member Function Documentation

template<class value_type>
elt_t* tensor::RefPointer< value_type >::begin ( )
inline

Retreive the pointer without caring for references (unsafe).

Definition at line 70 of file refcount.h.

template<class value_type>
const elt_t* tensor::RefPointer< value_type >::begin ( ) const
inline

Retreive the pointer without caring for references (unsafe).

Definition at line 72 of file refcount.h.

template<class value_type>
const elt_t* tensor::RefPointer< value_type >::begin_const ( ) const
inline

Retreive the pointer without caring for references (unsafe).

Definition at line 74 of file refcount.h.

template<class value_type>
const elt_t* tensor::RefPointer< value_type >::end ( ) const
inline

Retreive the pointer without caring for references (unsafe).

Definition at line 78 of file refcount.h.

template<class value_type>
elt_t* tensor::RefPointer< value_type >::end ( )
inline

Retreive the pointer without caring for references (unsafe).

Definition at line 80 of file refcount.h.

template<class value_type>
const elt_t* tensor::RefPointer< value_type >::end_const ( ) const
inline

Retreive the pointer without caring for references (unsafe).

Definition at line 76 of file refcount.h.

template<class value_type>
RefPointer<elt_t>& tensor::RefPointer< value_type >::operator= ( const RefPointer< elt_t > &  p)

Copy a pointer increasing the reference count.

template<class value_type>
void tensor::RefPointer< value_type >::reallocate ( size_t  new_size)

Replace the pointer with newly allocated data.

template<class value_type>
size_t tensor::RefPointer< value_type >::size ( ) const
inline

Size of pointed-to data.

Definition at line 83 of file refcount.h.