Previous Next Table of Contents

5. Typechecking Custom Objects

If you write own functions (apart from the ones, that you supply with the initialization) that get objects directly from the selection, you need to check if the selected object is of correct type.

The following code fragment shows how you can be sure an object is really a custom object of yours:


/* global variables */
static char *foo_name = "Foo";
static char *foo_customid = NULL;

Foo_Init()
{

  mops_register_custom_objecttype(...);

  /* get custom-id from name */
  entry = Tcl_FindHashEntry(&mops_custom_id_ht, foo_name);
  if (entry != NULL)
    {
      foo_customid = Tcl_GetHashValue(entry);
    }
  else
    {
      mops_error(interp, MOPS_HASH, fname, NULL);
      return TCL_OK;
    }

}

void
some_function()
{
 mops_list_object *sel = mops_current_selection;
 mops_object *o = NULL;
 mops_custom_object *custom = NULL;
 foo *my_object = NULL;

 /* is there something selected? */
 if(!sel)
  return TCL_OK;

 /* get mops_object from selection */
 o = sel->object;
 if(!o)
  return TCL_OK;

 /* is it a custom object? */
 if(o->type != MOPS_OTCUSTOM)
  return TCL_OK;

 /* get mops_custom_object from mops_object */
 custom = (mops_custom_object *)o->object;

 if(!custom)
  return TCL_OK;

 if(custom->type != foo_customid)
  return TCL_OK;

 /* let him pass... */

 my_object = (foo *)custom->object;


Previous Next Table of Contents