AutoArrayPtr< T > Class Template Reference

Provides pointer management. More...

List of all members.

Public Member Functions

void Attach (T *in_ptr, simba_size_t in_length)
 Replaces the contained pointer with the one passed in.
 AutoArrayPtr (const AutoArrayPtr< T > &in_obj)
 Copy constructor.
 AutoArrayPtr (size_t in_length)
 Constructor which allocates a new array with new T[].
 AutoArrayPtr (T *in_ptr=NULL, simba_size_t in_length=0)
 Default constructor.
AutoArrayPtr Clone () const
 Create a copy of this object.
T * Detach ()
 Releases ownership of the pointer and sets it to NULL.
T * Get () const
 Gives access to the contained pointer.
simba_size_t GetLength () const
 Gets the length of the array (count of elements of T).
bool IsNull () const
 Check if the contained pointer is NULL.
T & operator* () const
 Return a const reference to the object pointed to by the owned pointer.
AutoArrayPtroperator= (const AutoArrayPtr< T > &in_rhs)
 Assignment operator.
AutoArrayPtroperator= (T *in_rhs)
 Assignment operator.
T & operator[] (simba_size_t in_index) const
 Return a const reference to the array item at the specified index.
void Resize (simba_size_t in_newSize)
 Allocates a new buffer of the requested size.
AutoArrayPtrShrink (simba_size_t in_newSize)
 Shrink reported size of the array without reallocation.
void Swap (AutoArrayPtr &in_other)
 Swap this object with another AutoArrayPtr.
 ~AutoArrayPtr ()
 Destructor.

Static Public Member Functions

static AutoArrayPtr CopyOf (const AutoArrayPtr &in_array)
 Create an AutoArrayPtr which is a copy of the given array.
static AutoArrayPtr CopyOf (const T *in_array, size_t in_count)
 Create an AutoArrayPtr which is a copy of the given array.
template<size_t N>
static AutoArrayPtr CopyOf (const T(&in_array)[N])
 Create an AutoArrayPtr which is a copy of the given array.

Detailed Description

template<typename T>
class Simba::Support::AutoArrayPtr< T >

Provides pointer management.

Wraps the owned array pointer to guarantee its automatic deletion when the object goes out of scope. Note that this is NOT for an array of pointers, but for a pointer to an array.

You can copy and assign between AutoArrayPtr objects. The ownership is always passed along. If trying to assign to a AutoArrayPtr that already wraps an array pointer, that pointer will be deleted before the next one is assigned.

We will protect against using NULL contained array pointers with asserts to catch programmer errors. We will depend on callers using the new [] operator to throw exceptions on failure to protect at runtime.

This object cannot be used in a container that requires that the objects have a copy constructor and assignment operator that have a const parameter. This means that it wants to make an exact copy. This is not the case with this object as the the AutoArrayPtr objects being used for the copy construction or assignment are changed. These AutoArrayPtr objects lose the pointers they contain when the other object is initialized with it.

Here are some guidelines for using the AutoArrayPtr.


Constructor & Destructor Documentation

AutoArrayPtr ( T *  in_ptr = NULL,
simba_size_t  in_length = 0 
) [inline, explicit]

Default constructor.

Parameters:
in_ptr Pointer of explicit T type to initialize the owned pointer. (OWN)
in_length The length of the buffer to allocate.
AutoArrayPtr ( size_t  in_length  )  [inline]

Constructor which allocates a new array with new T[].

This can only be used if T has a public default constructor.

Parameters:
in_length Length of the buffer to allocate (number of elements of T, not size in bytes)
AutoArrayPtr ( const AutoArrayPtr< T > &  in_obj  )  [inline]

Copy constructor.

The AutoArrayPtr object used to construct this object will give up ownership of its contained pointer to the object being constructed.

This is not like a typical copy constructor since the other AutoArrayPtr is modified.

Parameters:
in_obj Const reference to another object containing the same type of owned object pointer. The other object will lose its owned pointer by passing ownership to this instance.
~AutoArrayPtr (  )  [inline]

Destructor.

Automatically cleans up the owned pointer using operator delete.


Member Function Documentation

void Attach ( T *  in_ptr,
simba_size_t  in_length 
) [inline]

Replaces the contained pointer with the one passed in.

If the pointer being passed in is not the same as the contained pointer, then the contained pointer is deleted and the new pointer takes its place.

Parameters:
in_ptr Pointer to replace the contained pointer. (OWN)
in_length The length of the passed in buffer.
AutoArrayPtr Clone (  )  const [inline]

Create a copy of this object.

Returns:
A copy of this object.
static AutoArrayPtr CopyOf ( const AutoArrayPtr< T > &  in_array  )  [inline, static]

Create an AutoArrayPtr which is a copy of the given array.

Parameters:
in_array The array to copy.
Returns:
An AutoArrayPtr which is a copy of in_array.
static AutoArrayPtr CopyOf ( const T *  in_array,
size_t  in_count 
) [inline, static]

Create an AutoArrayPtr which is a copy of the given array.

Parameters:
in_array The array to copy. If NULL, a NULL AutoArrayPtr is returned.
in_count The number of elements in in_array to copy.
Returns:
An AutoArrayPtr which is a copy of in_array.
static AutoArrayPtr CopyOf ( const T(&)  in_array[N]  )  [inline, static]

Create an AutoArrayPtr which is a copy of the given array.

Parameters:
in_array The array to copy.
Returns:
An AutoArrayPtr which is a copy of in_array.
T* Detach (  )  [inline]

Releases ownership of the pointer and sets it to NULL.

The responsibility for managing and deleting the pointer is transferred to the caller of this method. The object retains no knowledge of its former contained pointer.

Returns:
the previously contained pointer. (OWN)
T* Get (  )  const [inline]

Gives access to the contained pointer.

Warning:
This method does not pass off ownership of the contained pointer.
Returns:
the contained pointer. (NOT OWN)
simba_size_t GetLength (  )  const [inline]

Gets the length of the array (count of elements of T).

Note: Depending on the context an AutoArrayPtr is used in, the length may not be set (default to 0), or be unknown (eg. nul-terminated string).

Returns:
The length of the array.
bool IsNull (  )  const [inline]

Check if the contained pointer is NULL.

This will only check if the pointer is NULL or not. It would still be possible to fool the object by passing in a bogus pointer.

Returns:
true if NULL, otherwise false.
T& operator* (  )  const [inline]

Return a const reference to the object pointed to by the owned pointer.

Warning:
De-referencing a NULL pointer will cause a segfault in Unix. Check the validity of the pointer by calling AutoArrayPtr::IsNull() before dereferencing.
Returns:
A reference to the object pointed to by the contained pointer, const-qualified.
AutoArrayPtr& operator= ( const AutoArrayPtr< T > &  in_rhs  )  [inline]

Assignment operator.

Note that the AutoArrayPtr on the right hand hand of the assignment will give up ownership of its contained pointer. The receiving object will delete its own contained pointer and replace it with the one being passed in.

This is not a normal assignment operator because the right-hand side is modified.

Parameters:
in_rhs A AutoArrayPtr object to initialize this object with its contained pointer.
Returns:
a reference to the object on the left-hand side of the assignment.
AutoArrayPtr& operator= ( T *  in_rhs  )  [inline]

Assignment operator.

The receiving object will delete its own contained pointer and replace it with the one being passed in. Note: This sets the length to 0 because it can't be known with this operator.

Parameters:
in_rhs A pointer that this object will own.
Returns:
a reference to the object on the left-hand side of the assignment.
T& operator[] ( simba_size_t  in_index  )  const [inline]

Return a const reference to the array item at the specified index.

Parameters:
in_index The index into the array.
Returns:
A const reference to the array item at the specified index.
void Resize ( simba_size_t  in_newSize  )  [inline]

Allocates a new buffer of the requested size.

NOTE: This will delete the currently held buffer, if there is one.

Parameters:
in_newSize The size of the new buffer to allocate.
AutoArrayPtr& Shrink ( simba_size_t  in_newSize  )  [inline]

Shrink reported size of the array without reallocation.

After this method is called, GetLength() will return the smaller size, even though the actual array length has not changed.

Parameters:
in_newSize The size to shrink to. Will only be used if smaller than GetLength().
Returns:
*this
void Swap ( AutoArrayPtr< T > &  in_other  )  [inline]

Swap this object with another AutoArrayPtr.

in_other The object to swap this one with.


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

Generated on Wed May 17 14:21:18 2017 for SimbaEngine 10.1.3.1011 by simba