RE: [ROOT] linking and calling comlied code at runtime

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Tue Oct 31 2000 - 10:12:01 MET


Hi Philippe, Sven, et al, 
  
On Mon, 30 Oct 2000 15:11:35 -0600
Philippe Canal <pcanal@popgtw.fnal.gov> wrote
concerning ": RE: [ROOT] linking and calling comlied code at runtime":
> A more flexible way is to introduce a new top class, for example AllTasks :

Good, idea. One can also have 

  // Start of main file
  #include <TROOT.h>
  #include "MyClass.h" 

  TROOT myRoot("myRoot","My ROOT");

  int main(int argc, char** argv) {
    gROOT->Macro("MySetup.C"); // which makes a MyClass instance 

    MyClass* myObject = MyClass::Instance();

    myObject->DoIt();
  }
  // EOF 

with 

  // -*- mode: c++ -*- For Emacs :-) 
  // Start of file MyClass.h 
  #ifndef FOO_MyClass
  #define FOO_MyClass
  #ifndef ROOT_TObject
  #include <TObject.h>
  #endif 

  class MyClass : public TObject {
  private:
    static MyClass* fgInstance;  // Pointer to instance 
    Int_t           fMyData;     // Data 
  public:
    MyClass();
    MyClass(int myArg) : fMyData(myArg) { fgInstance = this; }
    virtual ~MyData() {}
    static MyClass* Instance() 

    virtual void  DoIt();   
    virtual Int_t GetMyData() const { return fMyData; }
    ClassDef(MyClass,0) // My singleton class 
  };
  #endif 
  // EOF 

And, 

  // Start of file MyClass.cxx
  #include "MyClass.h"
  #include <iostream.h> // Micros**t VC++ doesn't get <iostream>

  ClassImp(MyClass); // You can have `;' after this, so Emacs is happy

  MyClass* MyClass::fgInstance = 0;

  MyClass* MyClass::Instance() { 
    if (!fgInstance) fgInstance = new MyClass;
    return fgInstance;
  }

  void MyClass::DoIt() { 
    cout << "MyObject has the data: " << fMyData << endl;
  }
  // EOF 

Then you can derive any class from MyClass, say MyMain, kept in shared
library "libMyMain.so", and "MySetup.C", could look like 

  // Start of file 
  {
    gSystem->Load("libMyMain.so");

    MyMain* myObject = new MyMain("fubar");
    myObject->SetHuttelihut(foo, bar, baz, gnus, gnat);
  }

You see the point. Setup of your class is done in a (uncompiled)
script, while everything else is in compiled code. 

Maybe it's also possible to do 

  // Start of main file
  #include <TROOT.h>
  #include "MyClass.h" 

  void Config(); // Forward declaration of function 

  TROOT myRoot("myRoot","My ROOT");

  int main(int argc, char** argv) {
    gROOT->Load("MySetup.C"); // which makes a MyClass instance 

    Config(); // Using function defined in script 

    MyClass* myObject = MyClass::Instance();

    myObject->DoIt();
  }
  // EOF 
 
and

  // Start of file 
  void Config()
  {
    gSystem->Load("libMyMain.so");

    MyMain* myObject = new MyMain("fubar");
    myObject->SetHuttelihut(foo, bar, baz, gnus, gnat);
  }

but I haven't tried it. 

Yours, 

Christian  -----------------------------------------------------------
Holm Christensen                             Phone:  (+45) 35 35 96 91 
  Sankt Hansgade 23, 1. th.                  Office: (+45) 353  25 305 
  DK-2200 Copenhagen N                       Web:    www.nbi.dk/~cholm    
  Denmark                                    Email:       cholm@nbi.dk



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:36 MET