Defines

decMacroArray.h File Reference

Defines

#define ARRAY_ADD(_arg_type, _arg_name, _arg_element)
 Adds an element to an array.
#define ARRAY_COUNT(_arg_name)   _arg_name##Count
 Array Macros.
#define ARRAY_DECLARE(_arg_type, _arg_name)
 Defines an array.
#define ARRAY_FREE(_arg_name)   { if( _arg_name ) delete [] _arg_name; }
 Cleans up the array freeing memory of the array but not of elements inside.
#define ARRAY_GET_AT(_arg_name, _arg_position, _arg_variable)
 Assigns the array element at the given position to a variable.
#define ARRAY_GET_AT_NO_CHECK(_arg_name, _arg_position)   _arg_name[ (_arg_position) ]
 Retrieves the array element at the given position.
#define ARRAY_GET_HAS(_arg_name, _arg_element, _arg_variable)
 Assigns true to a variable if an array contains a given element.
#define ARRAY_GET_INDEX_OF(_arg_name, _arg_element, _arg_variable)
 Assigns the index of an element in the array to a variable or -1 if not found.
#define ARRAY_INIT(_arg_name)   { _arg_name = NULL; ARRAY_COUNT(_arg_name) = 0; ARRAY_SIZE(_arg_name) = 0; }
 Initializes the array variables to known empty values.
#define ARRAY_REMOVE(_arg_name, _arg_element)
 Removes an element from an array.
#define ARRAY_REMOVE_ALL(_arg_name)   { ARRAY_COUNT(_arg_name) = 0; }
 Removes all elements from an array.
#define ARRAY_REMOVE_FROM(_arg_name, _arg_position)
 Removes an element from the given position from an array.
#define ARRAY_RETURN_AT(_arg_name, _arg_position)
 Returns the array element at the given position.
#define ARRAY_RETURN_HAS(_arg_name, _arg_element)
 Returns true if an array contains a given element.
#define ARRAY_RETURN_INDEX_OF(_arg_name, _arg_element)
 Returns the index of an element in the array or -1 if not found.
#define ARRAY_SIZE(_arg_name)   _arg_name##Size
 Retrieves the variable {name}Size containing the capacity of the array.

Define Documentation

#define ARRAY_ADD (   _arg_type,
  _arg_name,
  _arg_element 
)
Value:
{ if( ARRAY_COUNT(_arg_name) == ARRAY_SIZE(_arg_name) ){ \
      int _var_size = ARRAY_SIZE(_arg_name) * 3 / 2 + 1; \
      _arg_type **_var_array = new _arg_type*[ _var_size ]; \
      if( ! _var_array ) THROW( dueOutOfMemory ); \
      if( _arg_name ){ \
         if( ARRAY_SIZE(_arg_name) > 0 ) memcpy( _var_array, _arg_name, sizeof( _arg_type* ) * ARRAY_SIZE(_arg_name) ); \
         delete [] _arg_name; \
      } \
      _arg_name = _var_array; \
      ARRAY_SIZE(_arg_name) = _var_size; \
   } \
   _arg_name[ ARRAY_COUNT(_arg_name)++ ] = (_arg_element); }

Adds an element to an array.

Expands to a code block. A semicolon is not required after the macro call. Throws an exception of type dueOutOfMemory if the element can not be added due to lack of memory. This call requires stdlib.h to be included to work.

#define ARRAY_COUNT (   _arg_name )    _arg_name##Count

Array Macros.

Provides macros to simulate a templated array. Avoids using templates and should work on all kinds of cranked up compilers and limited platforms where templates are not available or incorrectly compiled. These calls take only care of managing the array of pointers of a given type. Allocation and destruction of the actual elements in the array is up to the caller. These macros are intended to be used inside a class encapsulation only preventing others from having to know about this ( rather particular ) implementation detail. The following is a list of all macros defined in this file:

ARRAY_DECLARE( type, name ) ARRAY_INIT( name ) ARRAY_FREE( name ) ARRAY_COUNT( type ) ARRAY_SIZE( type ) ARRAY_RETURN_AT( name, position ) ARRAY_GET_AT( name, position, lvVariable ) ARRAY_GET_AT_NO_CHECK( name, position ) ARRAY_RETURN_HAS( name, element ) ARRAY_GET_HAS( name, element, lvVariable ) ARRAY_RETURN_INDEX_OF( name, element ) ARRAY_GET_INDEX_OF( name, element, lvVariable ) ARRAY_ADD( type, name, element ) ARRAY_REMOVE( name, element ) ARRAY_REMOVE_FROM( name, position ) ARRAY_REMOVE_ALL( name ) Retrieves the variable {name}Count containing the number of elements.

#define ARRAY_DECLARE (   _arg_type,
  _arg_name 
)
Value:
_arg_type **_arg_name; \
   int ARRAY_COUNT(_arg_name); \
   int ARRAY_SIZE(_arg_name);

Defines an array.

This macro is used in the class definition and generates three variables "{type} **{name}", "int {name}Count" and "int {name}Size". No semicolon is required after the macro call.

#define ARRAY_FREE (   _arg_name )    { if( _arg_name ) delete [] _arg_name; }

Cleans up the array freeing memory of the array but not of elements inside.

Expands to a code block. A semicolon is not required after the macro call. The variables are not changed to empty values. If you need this use ARRAY_INIT(name) right after ARRAY_FREE(name).

#define ARRAY_GET_AT (   _arg_name,
  _arg_position,
  _arg_variable 
)
Value:
{ if( (_arg_position) < 0 || (_arg_position) >= ARRAY_COUNT(_arg_name) ) THROW( dueInvalidParam ); \
   (_arg_variable) = _arg_name[ (_arg_position) ]; }

Assigns the array element at the given position to a variable.

Expands to a code block assigning the element to a variable. Hence use this like "ARRAY_GET_AT(name,position,variable)". A semicolon is not required after the macro call. If the element does not exist an exception of type dueInvalidParam is thrown.

#define ARRAY_GET_AT_NO_CHECK (   _arg_name,
  _arg_position 
)    _arg_name[ (_arg_position) ]

Retrieves the array element at the given position.

Expands to the value of the element in the array. No checks are done on this call as it is intended to be used inside loops or just any kind of code where the position is known to be valid.

#define ARRAY_GET_HAS (   _arg_name,
  _arg_element,
  _arg_variable 
)
Value:
{ int _var_i; \
   (_arg_variable) = false; \
   for( _var_i=0; _var_i<ARRAY_COUNT(_arg_name); _var_i++ ){ \
      if( _arg_name[ _var_i ] == (_arg_element) ){ \
         (_arg_variable) = true; \
         break; \
      } \
   } }

Assigns true to a variable if an array contains a given element.

Expands to a code block assigning true to the given variable if the element is found or false otherwise. Hence use this like "ARRAY_GET_HAS(name,element,variable)". A semicolon is not required after the macro call.

#define ARRAY_GET_INDEX_OF (   _arg_name,
  _arg_element,
  _arg_variable 
)
Value:
{ int _var_i; \
   (_arg_variable) = -1; \
   for( _var_i=0; _var_i<ARRAY_COUNT(_arg_name); _var_i++ ){ \
      if( _arg_name[ _var_i ] == (_arg_element) ){ \
         (_arg_variable) = _var_i; \
         break; \
      } \
   } }

Assigns the index of an element in the array to a variable or -1 if not found.

Expands to a code block assigning the element index to the given variable or -1 if not found. Hence use this like "ARRAY_GET_INDEX_OF(name,element,variable)". A semicolon is not required after the macro call.

#define ARRAY_INIT (   _arg_name )    { _arg_name = NULL; ARRAY_COUNT(_arg_name) = 0; ARRAY_SIZE(_arg_name) = 0; }

Initializes the array variables to known empty values.

Expands to a code block. A semicolon is not required after the macro call.

#define ARRAY_REMOVE (   _arg_name,
  _arg_element 
)
Value:
{ int _var_i, _var_index; \
   for( _var_index=0; _var_index<ARRAY_COUNT(_arg_name); _var_index++ ){ \
      if( _arg_name[ _var_index ] == (_arg_element) ) break; \
   } \
   if( _var_index == ARRAY_COUNT(_arg_name) ) THROW( dueInvalidParam ); \
   for( _var_i=_var_index+1; _var_i<ARRAY_COUNT(_arg_name); _var_i++ ) _arg_name[ _var_i - 1 ] = _arg_name[ _var_i ]; \
   ARRAY_COUNT(_arg_name)--; }

Removes an element from an array.

Expands to a code block. A semicolon is not required after the macro call. Throws an exception of type dueInvalidParam if the element is not part of the array. The caller is responsible to destroy the element if required.

#define ARRAY_REMOVE_ALL (   _arg_name )    { ARRAY_COUNT(_arg_name) = 0; }

Removes all elements from an array.

This simply sets the array count to 0. The caller is responsible to destroy all elements beforehand if required.

#define ARRAY_REMOVE_FROM (   _arg_name,
  _arg_position 
)
Value:
{ if( (_arg_position) < 0 || (_arg_position) >= ARRAY_COUNT(_arg_name) ) THROW( dueInvalidParam ); \
   int _var_i; \
   for( _var_i=_arg_position+1; _var_i<ARRAY_COUNT(_arg_name); _var_i++ ) _arg_name[ _var_i - 1 ] = _arg_name[ _var_i ]; \
   ARRAY_COUNT(_arg_name)--; }

Removes an element from the given position from an array.

Expands to a code block. A semicolon is not required after the macro call. Throws dueInvalidParam if the position is outside of boundaries. The caller is responsible to destroy the element if required.

#define ARRAY_RETURN_AT (   _arg_name,
  _arg_position 
)
Value:
{ if( (_arg_position) < 0 || (_arg_position) >= ARRAY_COUNT(_arg_name) ) THROW( dueInvalidParam ); \
   return _arg_name[ (_arg_position) ]; }

Returns the array element at the given position.

Expands to a code block with the element used as the value of a return statement. Hence use this like "ARRAY_RETURN_AT(name,position)". A semicolon is not required after the macro call. If the element does not exist an exception of type dueInvalidParam is thrown.

#define ARRAY_RETURN_HAS (   _arg_name,
  _arg_element 
)
Value:
{ int _var_i; \
   for( _var_i=0; _var_i<ARRAY_COUNT(_arg_name); _var_i++ ){ \
      if( _arg_name[ _var_i ] == (_arg_element) ) return true; \
   } \
   return false; }

Returns true if an array contains a given element.

Expands to a code block returning true if the element is found or false otherwise. Hence use this like "ARRAY_RETURN_HAS(name,element)". A semicolon is not required after the macro call.

#define ARRAY_RETURN_INDEX_OF (   _arg_name,
  _arg_element 
)
Value:
{ int _var_i; \
   for( _var_i=0; _var_i<ARRAY_COUNT(_arg_name); _var_i++ ){ \
      if( _arg_name[ _var_i ] == (_arg_element) ) return _var_i; \
   } \
   return -1; }

Returns the index of an element in the array or -1 if not found.

Expands to a code block returning the element index or -1 if not found. Hence use this like "ARRAY_RETURN_INDEX_OF(name,element)". A semicolon is not required after the macro call.

#define ARRAY_SIZE (   _arg_name )    _arg_name##Size

Retrieves the variable {name}Size containing the capacity of the array.