Next: Iterators
Up: Visitors
Previous: CascadingMap
  Contents
VisitorMap
VisitorMap is declared in suifkernel/visitor_map.h.
The VisitorMap is a special-purpose Visitor which dynamically
dispatches a SuifObject and the address of state to one of
several functions, depending on its type. Each function is of type
VisitMethod:
typedef void *Address; // declared in iokernel/iokernel_forwarders.h
typedef void (*VisitMethod)( Address state, Object *object );
The constructor takes a pointer to the SuifEnv, which is used to
interact with the MetaClass system to determine the appropriate
action. It has the following operations:
- Register a method and a state for a class.
(The state parameter is passed to the method
whenever it is called.)
void register_visit_method(
Address state,
VisitMethod method,
const LString &className );
- Optionally, a default function and state can be registered;
otherwise, no action occurs when the object belongs to no registered
class name.
virtual void register_unknown_method( Address state,
VisitMethod visitMethod );
- Dispatch applies the registered function which is most specific
to the given object. (The registered method is applied to its
registered state and the given object.)
virtual void apply( Object* object );
Here is an example use:
class MyVisitorInfo {
static void do_static_procedure_definition(
void *info,
ProcedureDefinition *obj) {
(MyVisitorInfo *)info->do_procedure_definition(obj);
}
void do_procedure_definition(ProcedureDefinition *proc_def) {
...do something...
}
}
// register the visitor functions
VisitorMap *map = new VisitorMap(suif_env);
MyVisitorClass my_info;
map->registerVisitMethod( &myinfo,
&MyVisitorClass::do_static_procedure_definition,
ProcedureDefinition::get_class_name() );
// Pick an object:
SuifObject *so;
...
map->apply(so);
David Heine
1999-11-22