FIX::shared_array< T > Class Template Reference

Shared array with atomic reference count. More...

`#include <SharedArray.h>`

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

## Public Member Functions

| shared_array ()
| shared_array (const shared_array &rhs)
| ~shared_array ()
shared_array & | operator= (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_count * | get_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

## shared_array() [1/3]

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)
{}
FIX::shared_array::m_buffer
T * m_buffer
Definition: SharedArray.h:146
FIX::shared_array::m_size
std::size_t m_size
Definition: SharedArray.h:145

## shared_array() [2/3]

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();
}
FIX::shared_array::m_buffer
T * m_buffer
Definition: SharedArray.h:146
FIX::shared_array::m_size
std::size_t m_size
Definition: SharedArray.h:145

## ~shared_array()

template<typename T>

FIX::shared_array< T >::~shared_array | ( |  | ) |

inline

Definition at line 46 of file SharedArray.h.

{ release(); }
FIX::shared_array::release
void release()
Definition: SharedArray.h:124

## shared_array() [3/3]

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)
{
}
FIX::shared_array::m_buffer
T * m_buffer
Definition: SharedArray.h:146
FIX::shared_array::m_size
std::size_t m_size
Definition: SharedArray.h:145

## Member Function Documentation

## attach()

template<typename T>

void FIX::shared_array< T >::attach | ( |  | ) | const

inline private

Definition at line 118 of file SharedArray.h.

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

{
if( !empty() )
increment_reference_count();
}
FIX::shared_array::increment_reference_count
void increment_reference_count() const
Definition: SharedArray.h:106
FIX::shared_array::empty
bool empty() const
Definition: SharedArray.h:66

## create()

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.

Referenced by FIX::message_order::message_order(), and FIX::message_order::setOrder().

{
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);
}
FIX::shared_array::shared_array
shared_array()
Definition: SharedArray.h:34

## decrement_reference_count()

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);
}
FIX::shared_array::get_counter
atomic_count * get_counter() const
Definition: SharedArray.h:101

## empty()

template<typename T>

bool FIX::shared_array< T >::empty | ( |  | ) | const

inline

Definition at line 66 of file SharedArray.h.

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

{ return m_buffer == 0; }
FIX::shared_array::m_buffer
T * m_buffer
Definition: SharedArray.h:146

## get_counter()

template<typename T>

atomic_count* FIX::shared_array< T >::get_counter | ( |  | ) | const

inline private

Definition at line 101 of file SharedArray.h.

Referenced by FIX::shared_array< int >::decrement_reference_count(), FIX::shared_array< int >::increment_reference_count(), and FIX::shared_array< int >::release().

{
return reinterpret_cast<atomic_count*>( &m_buffer[ size() ] );
}
FIX::shared_array::m_buffer
T * m_buffer
Definition: SharedArray.h:146
FIX::shared_array::size
std::size_t size() const
Definition: SharedArray.h:63

## increment_reference_count()

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);
}
FIX::shared_array::get_counter
atomic_count * get_counter() const
Definition: SharedArray.h:101

## operator T*()

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; }
FIX::shared_array::m_buffer
T * m_buffer
Definition: SharedArray.h:146

## operator=()

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;
}
FIX::shared_array::m_buffer
T * m_buffer
Definition: SharedArray.h:146
FIX::shared_array::m_size
std::size_t m_size
Definition: SharedArray.h:145
FIX::shared_array::release
void release()
Definition: SharedArray.h:124

## release()

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;
}
}
FIX::shared_array::get_counter
atomic_count * get_counter() const
Definition: SharedArray.h:101
FIX::shared_array::m_buffer
T * m_buffer
Definition: SharedArray.h:146
FIX::shared_array::decrement_reference_count
long decrement_reference_count()
Definition: SharedArray.h:112
FIX::shared_array::m_size
std::size_t m_size
Definition: SharedArray.h:145
FIX::shared_array::empty
bool empty() const
Definition: SharedArray.h:66

## size()

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; }
FIX::shared_array::m_size
std::size_t m_size
Definition: SharedArray.h:145

## Member Data Documentation

## m_buffer

template<typename T>

T* FIX::shared_array< T >::m_buffer

private

Definition at line 146 of file SharedArray.h.

Referenced by FIX::shared_array< int >::empty(), FIX::shared_array< int >::get_counter(), FIX::shared_array< T >::operator T*(), FIX::shared_array< int >::operator=(), and FIX::shared_array< int >::release().

## m_size

template<typename T>

std::size_t FIX::shared_array< T >::m_size

private

Definition at line 145 of file SharedArray.h.

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

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

- SharedArray.h