Is it legal and safe for an object/package to have a static array? We want to create an static
array when the device is turned on and use it while we are logging. We need to have fast access to
an array so the user doesn't take a performance hit.
Packages cannot have globals, such as static arrays. Neither can you specify an array as a field
of an object. You can use the extra data portion of an object to simulate an unbounded array. You
can get a pointer to the beginning of the extra data portion of an object by calling ExtraSize() on
that object. You can also allocate a buffer (the Buffer class is nothing but extra data) by calling
NewTransientBuffer(). You can associate this buffer with an object by assigning it to an object
field of type Pointer.
You need to be careful about what type of information you put into buffers and extra data portions
of objects. Normally, Magic Cap's garbage collection will destroy objects that are not being
referenced by any other objects. Because the extra data of an object can be anything, this
reference search does not normally look in the extra data of an object unless that object's
class overrides EachExtraField() to interpret the data. If you plan to keep ObjectIDs in your
array and you use extra data or a Buffer object to implement the array, you'll need to override
EachExtraField() to help Magic Cap interpret your array data.
I want to be able to have global, initialized data. I get an error when I try to link the following
code:
What's the right way to specify global data in Magic Cap?
Global data, initialized or not, is not supported in Magic Cap packages. You'll need to use objects
in your Objects.Def for this sort of static data.
Here's how your table might look:
static long inkColorsRGB[numInkColors] =
{
inkColorRGBBlack,
inkColorRGBDarkGray,
inkColorRGBLightSilver,
inkColorRGBWhite,
inkColorRGBRed,
inkColorRGBGreen,
inkColorRGBBlue,
inkColorRGBCyan,
inkColorRGBMagenta,
inkColorRGBYellow,
inkColorRGBDarkRed,
inkColorRGBDarkGreen,
inkColorRGBDarkBlue,
inkColorRGBDarkCyan,
inkColorRGBDarkMagenta,
inkColorRGBDarkYellow
};
Instance FixedList 'Colors' 3;
length: 8;
entry1: 0xFFFFFFFF;
....
entry8: 0x88888888;
End Instance;
You can refer to this object in your code with an indexical.