Download Source Code

TMem< T > Class Template Reference

General purpose memory allocation. More...

#include <TMem.h>

Inheritance diagram for TMem< T >:

Inheritance graph
[legend]
List of all members.

Public Member Functions

 TMem (unsigned long size=0)
 Constructs an object wrapping an array of the specified size.
 TMem (T *ptr, unsigned long size=1)
 Constructs an object wrapping the specified array.
 ~TMem ()
 Destructor.
void destroy ()
 Releases encapsulated array.
T * _allocate (unsigned long size=1)
 Allocates an array of the specified size.
bool allocate (unsigned long size=1)
 Allocates an array of the specified size.
bool ensure (unsigned long size=1)
 Ensures there is at least the specified number of elements in the array.
void Zero ()
 Zeros the array. Call only for POD arrays.
void Set (BYTE ch)
 Sets all memory locations to a specific value. POD arrays only!
TMem< T > & operator= (TMem< T > &x)
 Copies the specified array.
bool copy (TMem< T > &x)
 Copies the specified array.
bool copy (const T *ptr, unsigned long len)
 Copies the specified binary buffer. POD arrays only!
bool clip (unsigned long len, char *str=NULL)
 Clips the buffer to the specified size.
bool put (const void *buf, unsigned long size)
 Copies the specified data into the buffer. POD arrays only!
bool get (void *buf, unsigned long size)
 Returns the specified number of bytes from the array.
bool grow (unsigned long size)
 Grows the array and preserves data with a binary copy.
bool realloc (unsigned long size)
 Resizes the array and preserves data with a binary copy.
void attach (T *x, unsigned long size=1)
 Attaches to the specified buffer.
T * detach ()
 Detaches from the internal array without releasing the memory.
unsigned long attach (TMem< T > &x)
 Attaches to the specified memory object buffer.
T & operator * ()
 Returns a pointer to the array.
T & operator[] (const unsigned long i)
 Returns a reference to the specified array element.
T & operator[] (const long i)
 Returns a reference to the specified array element.
T & operator[] (const int i)
 Returns a reference to the specified array element.
T * operator & ()
 Returns a reference to the array.
 operator T * ()
 Returns a pointer to the array.
 operator T ()
 Returns the first element in the array.
T * ptr (unsigned long offset=0)
 Returns a pointer to the array.
char * str (unsigned long offset=0)
 Returns a pointer to the array cast as a character string (char*).
DWORD element_size ()
 Returns the Number of bytes in a single array element.

Protected Attributes

T * pData
 Pointer to the array.
unsigned long dwSize
 Number of elements in the array.

Detailed Description

template<class T>
class TMem< T >

General purpose memory allocation.

General purpose memory allocation


Constructor & Destructor Documentation

template<class T>
TMem< T >::TMem unsigned long  size = 0  )  [inline]
 

Constructs an object wrapping an array of the specified size.

Parameters:
[in] size - Number of elements to allocate

template<class T>
TMem< T >::TMem T *  ptr,
unsigned long  size = 1
[inline]
 

Constructs an object wrapping the specified array.

Parameters:
[in] ptr - Pointer to array
[in] size - Number of array elements in ptr


Member Function Documentation

template<class T>
T* TMem< T >::_allocate unsigned long  size = 1  )  [inline]
 

Allocates an array of the specified size.

Parameters:
[in] size - Desired array size in elements
Returns:
Non-zero if success
See also:

template<class T>
bool TMem< T >::allocate unsigned long  size = 1  )  [inline]
 

Allocates an array of the specified size.

Parameters:
[in] size - Desired array size in elements
Returns:
Non-zero if success
See also:

template<class T>
unsigned long TMem< T >::attach TMem< T > &  x  )  [inline]
 

Attaches to the specified memory object buffer.

Parameters:
[in] x - Buffer to assume control of. This object will not contain a valid buffer after this call.
Returns:
Size of the array in elements
See also:

template<class T>
void TMem< T >::attach T *  x,
unsigned long  size = 1
[inline]
 

Attaches to the specified buffer.

Parameters:
[in] x - Pointer to element buffer
[in] size - Number of elements in x

template<class T>
bool TMem< T >::clip unsigned long  len,
char *  str = NULL
[inline]
 

Clips the buffer to the specified size.

Parameters:
[in] len - Position to clip buffer
[in] str - Optional append string
Warning:
POD arrays only!
Returns:
Non-zero if success
See also:

template<class T>
bool TMem< T >::copy const T *  ptr,
unsigned long  len
[inline]
 

Copies the specified binary buffer. POD arrays only!

Parameters:
[in] ptr - Pointer to buffer
[in] len - Number of bytes in ptr
Warning:
POD arrays only!
Returns:
Non-zero if success
See also:

Reimplemented in CStr.

template<class T>
bool TMem< T >::copy TMem< T > &  x  )  [inline]
 

Copies the specified array.

Parameters:
[in] x - Source array to copy
Warning:
POD arrays only!
Returns:
Non-zero if success
See also:

template<class T>
T* TMem< T >::detach  )  [inline]
 

Detaches from the internal array without releasing the memory.

Returns:
Pointer to the internal array
See also:

template<class T>
bool TMem< T >::ensure unsigned long  size = 1  )  [inline]
 

Ensures there is at least the specified number of elements in the array.

Parameters:
[in] size - Desired array size in elements
If the array size is at least as large as size, this function simply returns non-zero. Otherwise it attempts to allocate an array of specified size.

Returns:
Non-zero if success
See also:

template<class T>
bool TMem< T >::get void *  buf,
unsigned long  size
[inline]
 

Returns the specified number of bytes from the array.

Parameters:
[in] buf - Buffer that receives data
[in] size - Size of buffer in buf
Returns:
Non-zero if success
See also:

template<class T>
bool TMem< T >::grow unsigned long  size  )  [inline]
 

Grows the array and preserves data with a binary copy.

Parameters:
[in] size - New array size
This function does nothing if the array is already at least as large as size.

Warning:
POD arrays only!
Returns:
Non-zero if success
See also:

template<class T>
TMem< T >& TMem< T >::operator= TMem< T > &  x  )  [inline]
 

Copies the specified array.

Parameters:
[in] x - Source array to copy
Warning:
POD arrays only!
Returns:
Reference to this object
See also:

template<class T>
T& TMem< T >::operator[] const int  i  )  [inline]
 

Returns a reference to the specified array element.

Parameters:
[in] i - Array element index
If the index is out of range, the first element is returned.

Returns:
Reference to the specified element.
See also:

template<class T>
T& TMem< T >::operator[] const long  i  )  [inline]
 

Returns a reference to the specified array element.

Parameters:
[in] i - Array element index
If the index is out of range, the first element is returned.

Returns:
Reference to the specified element.
See also:

template<class T>
T& TMem< T >::operator[] const unsigned long  i  )  [inline]
 

Returns a reference to the specified array element.

Parameters:
[in] i - Array element index
If the index is out of range, the first element is returned.

Returns:
Reference to the specified element.
See also:

template<class T>
T* TMem< T >::ptr unsigned long  offset = 0  )  [inline]
 

Returns a pointer to the array.

Parameters:
[in] offset - Pointer offset of array element
Returns:
Pointer to the specified array element

template<class T>
bool TMem< T >::put const void *  buf,
unsigned long  size
[inline]
 

Copies the specified data into the buffer. POD arrays only!

Parameters:
[in] buf - Buffer to copy
[in] size - Number of bytes in buf
Warning:
POD arrays only!
Returns:
Non-zero if success
See also:

template<class T>
bool TMem< T >::realloc unsigned long  size  )  [inline]
 

Resizes the array and preserves data with a binary copy.

Parameters:
[in] size - New array size
Warning:
POD arrays only!
Returns:
Non-zero if success
See also:

template<class T>
void TMem< T >::Set BYTE  ch  )  [inline]
 

Sets all memory locations to a specific value. POD arrays only!

Parameters:
[in] ch - Value to fill memory

template<class T>
char* TMem< T >::str unsigned long  offset = 0  )  [inline]
 

Returns a pointer to the array cast as a character string (char*).

Parameters:
[in] offset - Pointer offset of array element
Returns:
Pointer to the specified array element cast to (char*)

Reimplemented in CStr.


The documentation for this class was generated from the following file:
  • inc/TMem.h
Copyright Robert Umbehant
This documentation is covered by the LGPL