[previous] [up] [next]     [contents] [index]
Next: Library Functions Up: MzScheme Architecture Previous: Library Functions

Objects, Classes, and Interfaces

Primitive C++-like classes can be created with scheme_make_class. Methods are added to a primitive class with scheme_add_method; all methods must be added to a class before an object is created from the class. A C function that implements a class method is similar to a closed primitive function: it is passed a pointer to the object, a interger inidicating the number of arguments passed to the method, and an array of Scheme_Object * arguments.

More general classes are created in two phases. The scheme_make_class_assembly function creates a class assembly value that represents a ``compiled'' class expression. A class is created from an assembly with scheme_create_class with a creation-time determined superclass. An initialization procedure that is passed to scheme_make_class_assembly is called whenever an instance of the class is created.

Interfaces are also created in two phases: scheme_make_interface_assembly creates a ``compiled'' interface expression, and scheme_create_interface instantiates an actual interface from an assembly.

The function scheme_make_object creates a new object from a class and list of initialization arguments. Instance variables are retrieved with scheme_find_ivar, which takes an object and a symbol and returns the instance variable's value, or NULL if it is not found. Classes and objects can be compared with scheme_is_a and scheme_is_subclass.

An object value contains one pointer field that can be used by an implementation of a primitive class; this field is set or accessed with the SCHEME_OBJ_DATA macro. There is an additional flag field - set/accessed with SCHEME_OBJ_FLAG -- that is initialized to 0; if this flag is set to a negative value, then the object will no longer be usable from Scheme. (This is useful, for example, for closing a Scheme object when a corresponding C++ object can no longer be used.)

Connecting an arbitrary C++ class library to MzScheme can be tricky and may require a large amount of glue code. There is a separate utility, xctocc, that can facilitate this process. See the document PLT xcctocc: C++ Glue Generator Manual for more information.




[previous] [up] [next]     [contents] [index]
Next: Library Functions Up: MzScheme Architecture Previous: Library Functions

PLT