A template-implementation of a variable-size array Elements are stored as pointers to existing objects. More...
#include <array.h>

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 |
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;
}
Default constructor.
Leave the array unallocated, set all sizes to 0.
Destructor.
Only the array is freed, no object destructors are called.
| 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().
| void array< kind >::add | ( | kind * | element, | |
| bool | dynamic = true | |||
| ) | [inline] |
| 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.
| 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().
| int array< kind >::count | ( | void | ) | [inline] |
| 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.
| foo | The object to add | |
| position | The position to add it to. | |
| dynamic | Flags auto-delete. |
| arrayOutOfBoundsException |
| 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().
| foo | The referenced object to insert. | |
| position | The position to insert at. |
| arrayOutOfBoundsException |
Referenced by array< statstring >::insert().
| 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.
| kind& array< kind >::operator[] | ( | int | _pos | ) | [inline] |
| void array< kind >::remove | ( | int | _pos | ) | [inline] |
Remove the node at a given position.
| _pos | Array position, if negative measured from the right. |
| arrayOutOfBoundsException |
| void array< kind >::swap | ( | int | a, | |
| int | b | |||
| ) | [inline] |
Swap two elements in the array.
This method does not accept negative offsets.
| a | Position of the first element. | |
| b | Position of the second element. |
| arrayOutOfBoundsException |
1.6.1