(no subject)

From: Pasha Murat (630)840-8237 FNAL (630)859-3463 home (murat@murat.fnal.gov)
Date: Sun Feb 27 2000 - 19:14:06 MET


hi - find below one of the possible implementations. Note that direct calling 
of an interpreted function from a compiled one in general may not be possible - 
in this case a compiler may need to know about the internals of the intepreter... 
- best, pasha

-------------------------------------- MyClass.h

#include <TObject.h>
#include <TInterpreter.h>

class TMyClass {
public:
  static Float_t fgRetVal;
private:
  char fName[100];
public:
  void    SetFunction(char* f)    { strcpy(fName,f); }

  Float_t Function() { 
    char command[100];
    sprintf(command,"%s();",fName);
    gInterpreter->ProcessLine(command); 
    return fgRetVal;
  }
};
------------------------------- TMyClass.C
#include "TMyClass.h"

Float_t TMyClass::fgRetVal;

---------------------------   MyFunc.C
#include "TMyClass.h"

void MyFunc() {
  printf("the answer is ...\n");
  TMyClass::fgRetVal = 42.;
}
------------------------------------------------------------ end of sources

now you start ROOT and load TMyClass in compiled mode 
and MyFunc - in interpeted mode:

root [0] .L TMyClass.C+
Creating shared library /home/murat/./TMyClass.so
Note: operator new() masked 1c
Note: operator delete() masked 1c
Class TMyClass: Streamer() not declared
Class TMyClass: ShowMembers() not declared
root [1] .L MyFunc.C
root [2] TMyClass x;
root [3] x.SetFunction(MyFunc);
root [4] x.Function()
the answer is ...
(Float_t)4.20000000000000000e+01
root [5] 
--------------------------------------------------------------------------------------

Anton Fokin wrote:
> 
> Hello!
> 
> I am in troubles with calling interpreter function from compiled code.
> 
> Let's say I have compiled class
> 
> class TMyClass {
> private:
>   Float_t (*fFunc) ();
> public:
>      void SetFunction(const char* Macro);
>   Float_t Function()                      { return (*fFunc)() }
> };
> 
> and a macro in Macro.C file
> 
> Float_t
> MyFunc()
> {
>   printf("the answer is ...\n");
>   return 42;
> }
> 
> now I would like to write in my compiled program:
> 
> TMyClass *MyClass = new TMyClass();
> MyClass->SetFunction("Macro.C");
> Float_t Answer = MyClass->Function();
> 
> How TMyClass::SetFunction(const char* Macro) should look like? I am sure you
> suggest me to look into TF1 implementation, but could you write a few lines
> of example code instead? I hope it helps others too ...
> 
> Best regards,
> Anton



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