Defining CPad content

The first step in setting up a C++ program to use a CPad is to identify the types of data that the CPad is to store. If you plan to have several different types of data in the CPad, consider creating a structure type to define the content. The CPad examples shown here use a structure type called Root.

Each CPad provides access to a portion of memory that can be thought of as the root of a tree. The objects stored in the CPad can be reached only from the root object. In a root object, you can store a single object (such as int, double), an array (such as char*, int*), or a structure.

For example, assume that you plan to use a CPad to store a string of characters. The root structure you create might contain a pointer to the string and a size value to help set some boundaries for the string size and the memory that is consumed by the CPad. The following code defines such a structure:
#include "udxinc.h"
#include <string.h>

using namespace nz::udx_ver2;

struct Root
 char* data;
 int size;
A structure shows an interesting aspect of root objects. For example, the following sample code shows a root object that implements a simple dictionary:
struct MyValue
    char* name;
    int value;

struct MyLookup
    MyValue *values;
    int numallocated;
    int numused;

In this example, the root object is an instance of MyLookup, which can contain an arbitrary number of MyValue objects. All of the objects, plus the char* strings, are allocated through the CPad allocation mechanisms, but the only way to get to a value (or the name in a value) is through the MyLookup root object.

If several C++ files define UDXs that share the same CPad, make sure that you repeat your Root structure definition in each C++ file. If you define a number of common structures or definitions, you can create a single include file to define those objects in one location.