The wxCLIPS type system

To communicate between C++ and CLIPS, wxCLIPS uses long integers to represent objects. For convenience, they are simply the addresses of the C++ objects. However, when these handles are passed back to C++, wxCLIPS cannot simply coerce these integers back to objects, since the object may not exist or the type may be wrong. The program would simply crash without an error message, which is clearly unacceptable.

Instead, there is an explicit type system which, in conjunction with a hash table for the objects, allows C++ implementations of CLIPS functions to check that an objects exists and to check that the type is suitable for the intended operation. The type does not have to be an exact match, as with C++, so long as the passed type is at least a subtype of the intended type. For example, say someone passed a wxFrame to the window-centre function. If a wxWindow has a virtual member function Centre, and wxFrame is a subtype of wxWindow, then it's permissable to coerce the wxFrame to a wxWindow and call the Centre function.

For example, window-centre is implemented as follows:

long clipsWindowCentre()
{
  if (ArgCountCheck("clipsWindowCentre", AT_LEAST, 1) == -1)
    return 0;

  int no_args = RtnArgCount();
  long id = RtnLong(1);
  int direction = wxHORIZONTAL;
  if (no_args > 1)
    direction = (int) RtnLong(2);

  wxWindow *win = (wxWindow *)wxGetTypedObject(id, wxTYPE_WINDOW);
  if (win)
  {
    win->Centre(direction);
    return 1;
  }
  else
  {
    ClipsError << "Error in window-centre: could not find window " << id << ".\n";
    return 0;
  }
}
Types are represented by integers (see the file wx_cmds.h for some examples; the user may add types above the value wxTYPE_USER. User-defined types must be added to the type database with wxAllTypes.AddType, as the following example shows:

  wxAllTypes.AddType(wxTYPE_WINDOW,      wxTYPE_ANY,                        "window");
  wxAllTypes.AddType(wxTYPE_PANEL,       wxTYPE_WINDOW,                     "panel");
::wxGetTypedObject

::wxGetTypeName

::wxStoreThingInTable

::wxSubType

::wxDeleteThingInTable

::wxGetThingFromTable