Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
FIX::shared_array< T > Class Template Reference

Shared array with atomic reference count. More...

#include <SharedArray.h>

Inheritance diagram for FIX::shared_array< T >:
Inheritance graph
[legend]
Collaboration diagram for FIX::shared_array< T >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 shared_array ()
 shared_array (const shared_array &rhs)
 ~shared_array ()
shared_arrayoperator= (const shared_array &rhs)
std::size_t size () const
bool empty () const
 operator T * () const

Static Public Member Functions

static shared_array create (const std::size_t nSize)

Private Member Functions

 shared_array (T *buff, std::size_t nSize)
atomic_countget_counter () const
void increment_reference_count () const
long decrement_reference_count ()
void attach () const
void release ()

Private Attributes

std::size_t m_size
T * m_buffer

Detailed Description

template<typename T>
class FIX::shared_array< T >

Shared array with atomic reference count.

Definition at line 31 of file SharedArray.h.


Constructor & Destructor Documentation

template<typename T>
FIX::shared_array< T >::shared_array ( ) [inline]

Definition at line 34 of file SharedArray.h.

Referenced by FIX::shared_array< int >::create().

    : m_size(0)
    , m_buffer(0)
    {}
template<typename T>
FIX::shared_array< T >::shared_array ( const shared_array< T > &  rhs) [inline]

Definition at line 39 of file SharedArray.h.

    : m_size(rhs.m_size)
    , m_buffer(rhs.m_buffer)
    {
      rhs.attach();
    }
template<typename T>
FIX::shared_array< T >::~shared_array ( ) [inline]

Definition at line 46 of file SharedArray.h.

    { release(); }
template<typename T>
FIX::shared_array< T >::shared_array ( T *  buff,
std::size_t  nSize 
) [inline, private]

Definition at line 94 of file SharedArray.h.

    : m_size(nSize)
    , m_buffer(buff)
    {

    }

Member Function Documentation

template<typename T>
void FIX::shared_array< T >::attach ( ) const [inline, private]
template<typename T>
static shared_array FIX::shared_array< T >::create ( const std::size_t  nSize) [inline, static]

Definition at line 73 of file SharedArray.h.

    {
      if(nSize <= 0)
        return shared_array();

      //verify the needed buffer size to allocate counter object and nSize elements
      const std::size_t sizeToAllocate = nSize + ( sizeof(atomic_count) / sizeof(T) + 1 );

      //allocate and zero-fill the buffer
      T* storage = new T[ sizeToAllocate ];
      memset(storage, 0, sizeToAllocate * sizeof(T));

      // create the counter object at the end of the storage
      // with initial reference count set to 1
      new (&storage[nSize]) atomic_count( 1 );

      return shared_array(storage, nSize);
    }
template<typename T>
long FIX::shared_array< T >::decrement_reference_count ( ) [inline, private]

Definition at line 112 of file SharedArray.h.

Referenced by FIX::shared_array< int >::release().

    {
      atomic_count* counter = get_counter();
      return --(*counter);
    }
template<typename T>
bool FIX::shared_array< T >::empty ( ) const [inline]
template<typename T>
atomic_count* FIX::shared_array< T >::get_counter ( ) const [inline, private]
template<typename T>
void FIX::shared_array< T >::increment_reference_count ( ) const [inline, private]

Definition at line 106 of file SharedArray.h.

Referenced by FIX::shared_array< int >::attach().

    {
      atomic_count* counter = get_counter();
      ++(*counter);
    }
template<typename T>
FIX::shared_array< T >::operator T * ( ) const [inline]

Definition at line 69 of file SharedArray.h.

References FIX::shared_array< T >::m_buffer.

    { return m_buffer; }
template<typename T>
shared_array& FIX::shared_array< T >::operator= ( const shared_array< T > &  rhs) [inline]

Definition at line 49 of file SharedArray.h.

    {
      if( &rhs == this )
        return *this;

      rhs.attach();
      release();

      m_size = rhs.m_size;
      m_buffer = rhs.m_buffer;

      return *this;
    }
template<typename T>
void FIX::shared_array< T >::release ( ) [inline, private]

Definition at line 124 of file SharedArray.h.

Referenced by FIX::shared_array< int >::operator=(), and FIX::shared_array< int >::~shared_array().

    {
      if( empty() )
        return;

      //free object if reference count has decreased to zero
      if( decrement_reference_count() == 0)
      {
        T * tmpBuff = m_buffer;
        atomic_count* tmpCounter = get_counter();

        m_buffer = 0;
        m_size = 0;

        //explicitly call destructor for the counter object
        tmpCounter->~atomic_count();

        delete [] tmpBuff;
      }
    }
template<typename T>
std::size_t FIX::shared_array< T >::size ( ) const [inline]

Definition at line 63 of file SharedArray.h.

Referenced by FIX::shared_array< int >::get_counter().

    { return m_size; }

Member Data Documentation

template<typename T>
T* FIX::shared_array< T >::m_buffer [private]
template<typename T>
std::size_t FIX::shared_array< T >::m_size [private]

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

Generated on Mon Jun 23 2014 23:49:39 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001