next up previous contents
Next: Implementation of IR Modules Up: Generating SUIF Intermediate Representation Previous: From Hoof to C++   Contents


Creating Objects

Next we look at the declarations in my_factory.h, shown in Figure 7. You cannot use new to create instances of objects, because the constructor is protected. Instead you must call a create method from the object factory. The name of this routine is the name of the class, converted to lower case and with underlines inserted before capitals, preceded by create. For the example class above, the create routine would be called create_example. For a class SymbolTableObject it would be called create_symbol_table_object.

Figure 7: my_factory.h
  #ifndef MY_FACTORY_H
  #define MY_FACTORY_H
  #include "common/suif_list.h"
  #include "common/MString.h"
  #include "common/i_integer.h"
  #include "my_forwarders.h"
  #include "iokernel/meta_class.h"
  #include "suifkernel/real_object_factory.h"
  #include "iokernel/stl_meta_class.h"
  Example* create_example(SuifEnv *env,int  x);

  class MyObjectFactory : public RealObjectFactory {
    public:
      static const LString &get_class_name();
      virtual const LString &getName();
      virtual Example* create_example(int  x);

      virtual void init_io( ObjectFactory* of );
    protected:
      };

  #endif

The parameters for the create routine consist of a parameter for each field in the class, excluding list types, followed by the list of parameters for creating of the parent class, if any. The actual list of parameters can be modified by some options on the field declarations. These options are described in the section on the syntax of Hoof files. In particular, because of the limitations of C++, all parameters with default values are gathered together at the end of the parameter list. To call these create methods, you need a pointer to an instance of the associated object factory. This pointer is usually initialized during initialization of the system.

Figure 8: My Module Initialization
   #include "common/system_specific.h"
   #include <string.h>
   #include "my.h"
   #include "my_factory.h"
   #include "iokernel/aggregate_meta_class.h"
   #include "iokernel/union_meta_class.h"
   #include "iokernel/clone_stream.h"
   #include "iokernel/pointer_meta_class.h"
   #include "iokernel/stl_meta_class.h"
   #include "iokernel/object_factory.h"
   #include "iokernel/virtual_iterator.h"
   #include "iokernel/field_description.h"
   #include "suifkernel/real_object_factory.h"
   #include "suifkernel/suif_env.h"
   #include "suifkernel/module_subsystem.h"
   #include "suifkernel/module.h"
   #include "basicnodes/basic_factory.h"
   class MyModule : public Module {
     public:
       MyModule( SuifEnv* suif ) : Module( suif ,"my") {}
       virtual void initialize() {
         MyObjectFactory * current = new MyObjectFactory();
         _suif_env->add_object_factory( current );
         }
       virtual Module *clone() const {
         return((Module*)this);
         }
       };

   extern "C" void EXPORT init_basic( SuifEnv* suif );
   extern "C" void EXPORT init_suif( SuifEnv* suif );
   extern "C" void EXPORT init_mynodes( SuifEnv* suif ) {
     ModuleSubSystem* mSubSystem = suif->get_module_subsystem();
     if (!mSubSystem->retrieve_module("my")) {
       init_basic( suif );
       init_suif( suif );
       mSubSystem -> register_module( new MyModule( suif ) );
     }
   };

As well as the create methods in the object factory, there are equivalent global functions. By using the global functions, you do not need to know which module contains a given class, nor do you need to keep pointers to all the object factories in hand. The global functions take the active Suif Environment (a pointer to SuifEnv) as the first parameter. The remaining parameters are identical to the parameters of the corresponding create methods. These methods are a little slower than the calls to the object factory methods since they have to look up the correct object factory on each call. In practice, this overhead will be negligible.


next up previous contents
Next: Implementation of IR Modules Up: Generating SUIF Intermediate Representation Previous: From Hoof to C++   Contents
David Heine
1999-11-22