VistaIOtype(3) registering Vista object types

DESCRIPTION

Introduction

An object such as an image or edge set is represented in a Vista data file by an attribute. That attribute's value includes a type name, an attribute list, and possibly some binary data. Although the object is easily stored and communicated in that form, some other representation is often more useful when working with the object in memory. An image, for example, is best represented in memory by a structure with fixed fields to record the properties of the image that are frequently accessed.

The Vista library allows you to define a type that has its own internal representation for objects. By registering your type with the library you can arrange to have objects of that type automatically translated to and from your internal representation whenever data files are read and written. And when you use attribute lists to organize your objects, the objects will be correctly copied and released by routines such as VistaIOCopyAttrList(3) and VistaIODestroyAttrList(3).

Since Vista allows you to invent your own types of objects and to store those objects in data files, it must provide some way for standard programs to gracefully handle objects that are unfamiliar to them. A program must at least be able to copy your custom objects intact, binary data and all, from input to output. Unfamiliar objects are represented internally by the Vista library using the VistaIOBundle data structure, which is described separately in VistaIOBundle(3). You should be read it before reading this.

This manual page explains how to register a type, what routines you must supply to support a type you are registering, and how to find out about a registered type.

Registering a Type


typedef struct {
VistaIOCopyMethod *copy; /* copy object's value */ VistaIODestroyMethod *destroy;/* destroy object's value */ VistaIODecodeMethod *decode;/* decode object's binary data */ VistaIOEncodeAttrMethod *encode_attr;/* encode object's attr list */ VistaIOEncodeDataMethod *encode_data;/* encode object's binary data */
} VistaIOTypeMethods; VistaIORepnKind VistaIORegisterType (VistaIOStringConst *
name, VistaIOTypeMethods *methods)

Before registering a custom object type you will need to establish

  • a unique name identifying the type. The name must match the regular expression [a-zA-Z0-9_.+-]+ (see grep(1)), and it must not be bit, ubyte, sbyte, short, long, float, double, attr-list, boolean, bundle, list, pointer, string, edges, image, or any type name already registered.
  • a data structure for representing instances of the type in memory. A single pointer must suffice for referring to an instance, and the memory for each instance must be dynamically allocated.
  • a set of routines for copying, destroying, encoding, and decoding instances of the type. These routine are called methods. They're described in more detail below.
  • any other routines or macros your programs will be using to access or manipulate instances of the type.

The type is registered with a call to VistaIORegisterType prior to any use of the type within a program. Pass to VistaIORegisterType the type's name and a pointer to a VistaIOTypeMethods structure listing the type's methods. VistaIORegisterType will return a unique code from the VistaIORepnKind series, which can subsequently be used to refer to the type whenever a VistaIORepnKind value is called for (e.g., in a call to VistaIOSetAttr(3)).

Methods

The type you register must be accompanied by these five methods:

typedef VistaIOPointer VistaIOCopyMethod (VistaIOPointer value);

The Copy method is passed an instance of your type; it returns a copy of that object made with newly-allocated storage.

typedef void VistaIODestroyMethod (VistaIOPointer value);

The Destroy method releases all storage occupied by the object value.
typedef VistaIOPointer VistaIODecodeMethod (VistaIOStringConst name, VistaIOBundle bundle);
The Decode method creates an instance of your type. Data for the object is supplied via bundle in the form of an attribute list and an optional block of binary data, both of which may be modified by the method. An attribute name, name, is supplied for use in any error messages produced by decode (e.g., ``The name attribute has the wrong amount of binary data''). The method returns the newly-created object, or NULL if an error is encountered.
Storage for the new object can be freshly allocated, or it can be taken from bundle provided bundle itself is left in a state such that it can be destroyed without releasing storage used by the new object. For example, the new object can incorporate the attribute list bundle->list provided bundle->list is then set to a new, empty attribute list. It can incorporate the data block bundle->data provided bundle->length is then set to zero.
typedef VistaIOAttrList VistaIOEncodeAttrMethod (VistaIOPointer value, size_t *length);
typedef VistaIOPointer VistaIOEncodeDataMethod (VistaIOPointer value, VistaIOAttrList list, size_t length, VistaIOBoolean *free_it);
These two methods produce an attribute list value and a block of binary data from an instance of your type. For any particular object the two methods are always called in sequence. First the EncodeAttr method is called with value identifying the object. It returns the attribute list value while setting length to the number of bytes required for the block of binary data. Later, the EncodeData method is called with value identifying the same object, and list and length supplying the values returned by the EncodeAttr method. It returns a pointer to memory containing the block of binary data while setting free_it to TRUE if the memory should be released once the binary data has been recorded. Setting free_it to FALSE indicates that the memory may be part of the object, value, and the memory contents should be recorded before taking any action that might modify the object.
The attribute list returned by the EncodeAttr method can be assumed to exist only until the subsequent call to the EncodeData method. If this list is created by the EncodeAttr method, then the EncodeData method is responsible for destroying it. A practice adopted for the image and edges types is for the EncodeAttr method to return a list that belongs to the object being encoded, but with some standard attributes prepended to it; the EncodeData method then removes the prepended attributes so that the object is left in the correct form.
If either method encounters an error, it signals the error by returning NULL. Note that both methods are called even if the first indicates, by setting length to zero, that there is no binary data associated with value. When there is no binary data to return, the EncodeData method should return a non-NULL pointer and set free_it to FALSE to avoid signalling an error.

Querying Registered Types

The following routine and macro provide information about registered types:

VistaIORepnKind VistaIOLookupType (VistaIOStringConst name)

VistaIOLookupType returns the representation code associated with the type named name. (This will be the same as that returned by VistaIORegisterObjectType when the type was registered.) If name is not the name of a known type, VistaIOLookupType returns VistaIOUnknownRepn.

VistaIOTypeMethods *VistaIORepnMethods (VistaIORepnKind repn)

The macro VistaIORepnMethods provides a handle to the methods for the registered type identified by repn.

Standard Object Types

The library implements some standard object types using the mechanism described by this manual page. Currently, these standard object types are:
edges
Set of edges. See VistaIOEdges(3).
image
Multi-band two-dimensional array of pixels. See VistaIOImage(3).

If you are developing your own custom type, you're encouraged to first consult these as examples; their methods are implemented in the files /usr/src/vista/EdgesType.c and /usr/src/vista/ImageType.c.

AUTHOR

Art Pope <[email protected]>

Adaption to vistaio: Gert Wollny <[email protected]>