array< kind > Class Template Reference

A template-implementation of a variable-size array Elements are stored as pointers to existing objects. More...

#include <array.h>

Collaboration diagram for array< kind >:
Collaboration graph
[legend]

List of all members.

Classes

struct  arraynode
 Internal storage for an array node. More...

Public Member Functions

 array (void)
 ~array (void)
void add (kind &element)
void add (kind *element, bool dynamic=true)
void insert (kind &foo, int position)
void insert (kind *foo, int position, bool dynamic=true)
void remove (int _pos)
void swap (int a, int b)
void move (int from, int to)
void removelast (void)
kind & operator[] (int _pos)
int count (void)
kind * visitchild (int pos)

Protected Member Functions

void _swap (int a, int b)

Protected Attributes

arraynode_array
int _count
int _arraysz

Detailed Description

template<class kind>
class array< kind >

A template-implementation of a variable-size array Elements are stored as pointers to existing objects.

Memory management for these objects is outside the scope of this class. Accessing the array resolves to an object reference. The array itself is dynamically grown with a power-of-two allocation, with a starting size of 8.

Example usage of an array container:

#include <grace/application.h>
#include <grace/str.h>
#include <grace/array.h>

class myApp : public application
{
             myApp (void) : application ("tld.example.app.test.1")
             {
             }
            ~myApp (void);
    
    int      main (void);
};

class messageDestination
{
            messageDestination (const string &pHost, int pPort)
            {
                hostName = pHost;
                port = pPort;
            }
            
    string  hostName;
    int     port;
};

int myApp::test (void)
{
    array<messageDestination> dest;

    dest.add (new messageDestination ("localhost",1515));
    dest.add (new messageDestination ("192.168.10.10",4343));
    
    for (int i=0; i < dest.count(); ++i)
    {
        fout.printf ("Host %s port %i\n", dest[i].hostName.str(),
                     dest[i].port);
    }
    return 0;
}

Constructor & Destructor Documentation

template<class kind>
array< kind >::array ( void   )  [inline]

Default constructor.

Leave the array unallocated, set all sizes to 0.

template<class kind>
array< kind >::~array ( void   )  [inline]

Destructor.

Only the array is freed, no object destructors are called.


Member Function Documentation

template<class kind>
void array< kind >::_swap ( int  a,
int  b 
) [inline, protected]

Internal swap-method, assumes bounds checking already took place.

Referenced by array< statstring >::move(), and array< statstring >::swap().

template<class kind>
void array< kind >::add ( kind *  element,
bool  dynamic = true 
) [inline]

Add a new entry to the array.

Grows the storage for the array as needed using power-of-two preallocation. If dynamic is true, the object will be deleted if its node is removed.

Parameters:
element Pointer to the entry
template<class kind>
void array< kind >::add ( kind &  element  )  [inline]

Add a non-dynamic entry to the array.

Grows the storage for the array as needed using power-of-two preallocation.

Parameters:
element The elemnt-reference to add. Deleting this element before the array will lead to nasty behavior.

Referenced by array< statstring >::add(), stringdict::get(), and array< statstring >::insert().

template<class kind>
int array< kind >::count ( void   )  [inline]

Item count.

Returns the number of entries in the array.

Referenced by stringdict::get().

template<class kind>
void array< kind >::insert ( kind *  foo,
int  position,
bool  dynamic = true 
) [inline]

Insert a new element at a specific array position.

This position must be within the range of 0...count(). If dynamic is true, the object will be deleted if its node is removed.

Parameters:
foo The object to add
position The position to add it to.
dynamic Flags auto-delete.
Exceptions:
arrayOutOfBoundsException 
template<class kind>
void array< kind >::insert ( kind &  foo,
int  position 
) [inline]

Insert a reference at a specific array position.

This position must be within the range of 0...count().

Parameters:
foo The referenced object to insert.
position The position to insert at.
Exceptions:
arrayOutOfBoundsException 

Referenced by array< statstring >::insert().

template<class kind>
void array< kind >::move ( int  from,
int  to 
) [inline]

Move an element to another position in the array, all other nodes will be shifted.

This method does not accept negative offsets.

template<class kind>
kind& array< kind >::operator[] ( int  _pos  )  [inline]

Access operator.

If a negative index is provided, the position is measured from the right (with the last element of the array being represented as -1).

Parameters:
_pos Requested array position.
Exceptions:
arrayOutOfBoundsException 
template<class kind>
void array< kind >::remove ( int  _pos  )  [inline]

Remove the node at a given position.

Parameters:
_pos Array position, if negative measured from the right.
Exceptions:
arrayOutOfBoundsException 
template<class kind>
void array< kind >::swap ( int  a,
int  b 
) [inline]

Swap two elements in the array.

This method does not accept negative offsets.

Parameters:
a Position of the first element.
b Position of the second element.
Exceptions:
arrayOutOfBoundsException 

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

Generated on Sun Feb 28 10:35:12 2010 for GRACE by  doxygen 1.6.1