11.9 PROPERTY (RUN-TIME EXTENSIBILITY)

Purpose:

Objects that have PROPERTY may be extended at run time by any number of labelled attributes, called PROPERTIES. A property can be an integer, a float, a character, a string, or an array of these.

Property exists on the object itself, it is not associated with a hyper-object. There can be only one set of properties per object.

Memory requirements:

Declaring PROPERTY on an object adds 1 pointer to all objects of this type. If the property is not used, the pointer is NULL. When allocating or initializing objects, this pointer is automatically set to NULL.

Each single value property has an overhead of 2 additional pointers plus the string for the property name. Each array property has an overhead of 5 additional pointers plus the string for the property name.

Saving an object with properties:

save() and open() consider properties to be part of the object. Properties are automatically saved/restored from disk with the object.

Available functions/macros:

setProp() adds a new property. If a property with this name already exists on the object, the value/array is reset. If the type of the property or the time of the array is changed, a warning message is issued.
getProp() searches properties within a given object, looking for the given property name, and returns its value/array.
prtProp() is a general utility which, for a given property value, label, etc, prints the property in human readable format. This function is different from the other PROPERTY functions in that it belongs in the hyper_class UTILITIES.
<type>_propIterator declares and initializes an iterator for an object of the class type.
start() restarts a loop on an iterator, which has already been declared.
next() is an iterator function which moves to the next property on the same object.
delProp() deletes a property.

Syntax:

Note that all methods are associated with the PROPERTY, except for prtProp(), which is a part of UTILITIES, because it does not depend on property, objects, or anything else. It is just a generic printing function.

// type is the property type: int, float, char,string
class TYPE;
ZZ_HYPER_PROPERTY(TYPE);
ZZ_HYPER_UTILITIES(util);
void TYPE::setProp(char *type,char *propName,char*val[],int size)
;
adds/resets property with name propName on obj. The value of the property must be in val, whether a single value or an array. size=1 for a single value, >1 for an array.
char** TYPE::getProp(char *propName,char **type,int*size); for a given object and property name, it returns the property value/values (as in array val) and then, through parameters, returns the property type, and the size of the array. size=0 means the property was not found.
void TYPE::delProp(char *propName); deletes the property with the given name.
void util.prtProp(FILE *fp,char *type,char*propName,char *val[],int size); prints the given property as received for example from getProp(). It prints this into file fp in human readable format.
TYPE_propIterator it(obj); // initiates iterator
it.start(obj); // restarts the iterator on obj
char** it.next(char *propName,char **type,int *size)
returns the array value (val), and associated attributes, and can be used to loop through all properties on one object. It returns NULL after the last property. For example:
// printing all properties on obj
type_propIterator it(obj);
while(val=it.next(&propName,&type,&size)){
util.prtProp(fp,propName,type,val,size);

Examples:

Note that the syntax of functions working with PROPERTY is quite different in C and C++.

  1. Look at the test which adds various types of properties to an object of type Apple (test4d.c). This program also issues a warning message when the property type is changed. If you are used to the C version of Organized C, compare this program with its C equivalent in test4b.c.
  2. Another C example is the NIAL-like language in test12a.c which works with objects organized in a tree. Each object can have an atom - which is a single value/array-like attribute. In test12a.c atoms are implemented as PROPERTY. Sorry, this example is not yet available in C++.

 

Previous Section 11.8 Entity-Relationship Model Next Section 11.10 Run-Time Type Detection