ArrayShare

The class ArrayShare can be used to share arrays between different objects, e.g. populations or actions.

The arrays must be registered in ArrayShare with a unique name. This name is subsequently used to access an array or to remove it.

It is implemented as a singleton.

The caller of getArray() should know the type of the array.

Code:

Typedefs

typedef std::vector<std::string> stringlist;

Structures

typedef struct arraystruct {
    int m_iSize;
    void *m_pdArr;
    // constructor
    arraystruct(int iSize, void *pdArr) : m_iSize(iSize), m_pdArr(pdArr) {};
} arraystruct;

Public Methods

constructor

ArrayShare();

The constructor. It is usually not used directly.

destructor

~ArrayShare();

The destructor. It is usually not used directly.

shareArray

int shareArray(const std::string sName, int iSize, std::string sType, void *pdArr);
int shareArray(const std::string sName, int iSize, void *pdArr);

Register an array for sharing.

sName

A name for the array.

iSize

The array’s size.

sType

An arbitrary string for the array’s type. If sType is omitted, the type string is “”.

pdArr

A pointer to the array.

Returns 0 on success, -1 on failure

getSize

int getSize(const std::string sName);

Get the size of the array with the specified name.

sName

The array’s name.

Returns size.

getType

std::string getType(const std::string sName);

Get the type string of the array with the specified name.

sName

The array’s name.

Returns the type string.

getArray

void *getArray(const std::string sName);

Get a pointer to the array with specified name.

sName

The array’s name.

Returns a void pointer to the array, or NuULL on failure.

getArrayStruct

const arraystruct *getArrayStruct(const std::string sName);

Get the entire array struct for the speicified name.

sName

The array’s name.

Retruns an the ArrayStruct for the given name, or NULL on failure

removeArray

int removeArray(const std::string sName);

Unregister array with specified name.

sName

The array’s name

Returns 0 on success, -1 on failure

getNamesLike

const stringlist &getNamesLike(const std::string sNamePattern);

Find registered array wiht names matching a pattern

sNamePattern

A regular expression to search for.

Returns a vector of strings

display

void display();

Prints a list of all registered arrays and their sizes.

getInstance

static ArrayShare *getInstance();

Returns a pointer to the ArrayShare singleton. If it doesn’t exist yet, it is created

freeInstance()

static void *freeInstance();

Deletes the singleton and frees resources.

Example

In this example, class A shares an integer array under the name “Array_A”, and class B gets the array and does something with it.

class A {
public:
    A(int iC) {
        m_pArr = new int[iC];
        ArrayShare::getInstance->shareArray("Array_A", iC, m_pArr);
    }
    //...
}
class B {
public:
    int action() {
        int iSize  = ArrayShare::getInstance->getSize("Array_A");
        // ArrayShare provides a void pointer to the array,
        // so B must cast it to the correct type
        int *pTemp = (int *)ArrayShare::getInstance->getArray("Array_A");
        // do something with it
    }
    // ...
}