General Rules and Purpose of Subclassing

From MorphOS Library

Revision as of 12:25, 30 December 2010 by Krashan (talk | contribs) (Contents++.)

Grzegorz Kraszewski


Introduction

Subclassing is one of essential object oriented programming techniques. In MUI subclassing is used for the following purposes:

  • Implementing program functionality as a set of methods. The Application class is usually used for this purpose.
  • Customizing classes by writing methods intentionally left unimplemented (or having some default implementations) in standard MUI classes. The most common example is the List class, but also Numeric one and others.
  • Writing custom drawn gadgets or areas. The Area class is subclassed in this case.

Regardless of the reason of subclassing, it is always done in the same way. A programmer must write new or overridden methods, create a dispatcher function, define an instance data structure (an empty one in some cases), then create the class. It is worth noting, subclassing MUI classes is done the same as subclassing BOOPSI ones. The only difference is that MUI provides own functions for class creation and disposition.


Object Data

Object data are stored in a memory area automatically allocated for every object created. The object data area is used for storing attribute values and for internal variables and buffers. This area is usually defined as a structure. Size of the area is passed to MUI_CreateCustomClass() function. In a class hierarchy, every class may add its own contribution to the object data area. Unlike in C++, a class has no direct access to anything except its own data area. It can't access data defined in the superclass (In C++ it is possible if a field is declared as protected or public). Object data defined in any of superclasses may be only accessed using methods or attributes provided by these superclasses.

There is no limit on object data area size, other than system memory available. The area data is always cleared to all zeros at object creation. If a clas does not need any object instance data it can pass 0 as the area size to MUI_CreateCustomClass().


Writing Methods

A MUI method is just a plain C function, but with partially fixed prototype.

IPTR MethodName(Class *cl, Object *obj, MessageType *msg);

The method return value may be either integer or pointer to anything. That is why it uses IPTR type which has meaning of "integer big enough to hold a pointer". In current MorphOS it is just 32-bit integer (the same as LONG). If a method has no meaningfull value to return, it can just return 0. Two first, fixed arguments are: pointer to the class and pointer to the object. The last one is a method message. When a method is being overridden, the type of message is determined by the superclass. For a new method, message type is defined by programmer. Some methods may have empty messages (containing only a method identifier), in this case the third argument may be ommitted.

Most of methods need the access to the object instance data. To get a pointer to the data area, one uses INST_DATA macro, defined in <intuition/classes.h>. An example below shows the macro usage:

struct ObjData
{
  LONG SomeVal;
  /* ... */
};

IPTR SomeMethod(Class *cl, Object *obj)
{
  struct ObjData *d = (struct ObjData*)INST_DATA(cl, obj);

  d->SomeVal = 14;
  /* ... */
  return 0;
}

If a method is an overridden method from a superclass, it may want to perform superclass method. There are no implicit super method calls in MUI. The superclass method must be always called explicitly with DoSuperMethodA() call:

result = DoSuperMethodA(cl, obj, msg);
result = DoSuperMethod(cl, obj, MethodID, ...);

The second form rebuilds the method message from variable arguments, and is used when the message is modified before calling the superclass method.