Difference between revisions of "Overriding OM GET()"

From MorphOS Library

(Added code.)
 
(Contents added.)
Line 1: Line 1:
 
''Grzegorz Kraszewski''
 
''Grzegorz Kraszewski''
  
 +
The ''OM_GET()'' method, used for getting an attribute from an object, receives opGet structure as its message. The structure is defined in the ''<intuition/classusr.h>'' header file:
  
 +
struct opGet
 +
{
 +
  ULONG  MethodID;          /* always OM_GET (0x104) */
 +
  ULONG  opg_AttrID;
 +
  ULONG *opg_Storage;
 +
};
 +
 +
Unlike ''[[Overriding OM_SET()|OM_SET()]]'', this method handles only one attribute at a time. The attribute is placed in ''opg_AttrID'' field. The field ''opg_Storage'' is a '''pointer''' to a place where the attribute value should be stored. It is defined as a pointer to ULONG, but it may point to anything (for example some larger structure). It allows for passing attributes not fitting in a 32-bit variable. Because ''OM_GET()'' does not have a taglist iteration loop, its implementation is simpler:
  
 
  IPTR MyClassGet(Class *cl, Object *obj, struct opGet *msg)
 
  IPTR MyClassGet(Class *cl, Object *obj, struct opGet *msg)
Line 18: Line 27:
 
   return DoSuperMethodA(cl, obj, (Msg)msg);
 
   return DoSuperMethodA(cl, obj, (Msg)msg);
 
  }
 
  }
 +
 +
The implementation consists of a ''switch'' statement with ''cases'' for all recognized attributes. If an attribute is recognized the method should return ''TRUE''. Unknown attributes are passed to the superclass. The ''DoSuperMethodA()'' call may be alternatively placed as a ''default'' clause of

Revision as of 09:25, 4 January 2011

Grzegorz Kraszewski

The OM_GET() method, used for getting an attribute from an object, receives opGet structure as its message. The structure is defined in the <intuition/classusr.h> header file:

struct opGet
{
  ULONG  MethodID;           /* always OM_GET (0x104) */
  ULONG  opg_AttrID;
  ULONG *opg_Storage;
};

Unlike OM_SET(), this method handles only one attribute at a time. The attribute is placed in opg_AttrID field. The field opg_Storage is a pointer to a place where the attribute value should be stored. It is defined as a pointer to ULONG, but it may point to anything (for example some larger structure). It allows for passing attributes not fitting in a 32-bit variable. Because OM_GET() does not have a taglist iteration loop, its implementation is simpler:

IPTR MyClassGet(Class *cl, Object *obj, struct opGet *msg)
{
  switch (msg->opg_AttrID)
  {
    case Some_Integer_Tag:
      *msg->opg_Storage = /* value of the tag */;
    return TRUE;

    case Some_String_Tag:
      *(char**)msg->opg_Storage = "a fixed string value";
    return TRUE;
  }

  return DoSuperMethodA(cl, obj, (Msg)msg);
}

The implementation consists of a switch statement with cases for all recognized attributes. If an attribute is recognized the method should return TRUE. Unknown attributes are passed to the superclass. The DoSuperMethodA() call may be alternatively placed as a default clause of