#include "TInterpreter.h"
#include "TMethodCall.h"
#include "TMethod.h"
#include "TClass.h"
#include "TROOT.h"
#include "Strlen.h"
#include "TVirtualMutex.h"
#include "TError.h"
ClassImp(TMethodCall)
TMethodCall::TMethodCall()
{
fFunc = 0;
fOffset = 0;
fClass = 0;
fMetPtr = 0;
fMethod = "";
fParams = "";
fProto = "";
fDtorOnly = kFALSE;
fRetType = kNone;
}
TMethodCall::TMethodCall(TClass *cl, const char *method, const char *params)
{
fFunc = 0;
Init(cl, method, params);
}
TMethodCall::TMethodCall(const char *function, const char *params)
{
fFunc = 0;
Init(function, params);
}
TMethodCall::TMethodCall(const TMethodCall &orig) : TObject(orig)
{
fFunc = orig.fFunc ? gCint->CallFunc_FactoryCopy(orig.fFunc) : 0;
fOffset = orig.fOffset;
fClass = orig.fClass;
fMethod = orig.fMethod;
fParams = orig.fParams;
fProto = orig.fProto;
fDtorOnly = orig.fDtorOnly;
fRetType = orig.fRetType;
fMetPtr = 0;
}
TMethodCall &TMethodCall::operator=(const TMethodCall &rhs)
{
if (this != &rhs) {
gCint->CallFunc_Delete(fFunc);
fFunc = rhs.fFunc ? gCint->CallFunc_FactoryCopy(rhs.fFunc) : 0;
fOffset = rhs.fOffset;
fClass = rhs.fClass;
fMethod = rhs.fMethod;
fParams = rhs.fParams;
fProto = rhs.fProto;
fDtorOnly = rhs.fDtorOnly;
fRetType = rhs.fRetType;
delete fMetPtr;
fMetPtr = 0;
}
return *this;
}
TMethodCall::~TMethodCall()
{
gCint->CallFunc_Delete(fFunc);
delete fMetPtr;
}
TObject *TMethodCall::Clone(const char *) const
{
TObject *newobj = new TMethodCall(*this);
return newobj;
}
static TClass *R__FindScope(const char *function, UInt_t &pos, ClassInfo_t *cinfo)
{
if (function) {
UInt_t nested = 0;
for(int i=strlen(function); i>=0; --i) {
switch(function[i]) {
case '<': ++nested; break;
case '>': if (nested==0) { Error("TMethodCall R__FindScope","%s is not well formed function name",function); return 0; }
--nested; break;
case ':':
if (nested==0) {
if (i>2 && function[i-1]==':') {
TString scope(function);
scope[i-1] = 0;
pos = i+1;
TClass *cl = TClass::GetClass(scope);
if (!cl) gCint->ClassInfo_Init(cinfo, scope);
return cl;
}
}
break;
}
}
}
return 0;
}
void TMethodCall::Init(TClass *cl, const char *method, const char *params)
{
ClassInfo_t *cinfo = gCint->ClassInfo_Factory();
if (!cl) {
UInt_t pos = 0;
cl = R__FindScope(method,pos,cinfo);
method = method+pos;
}
InitImplementation(method,params,0,cl,cinfo);
gCint->ClassInfo_Delete(cinfo);
}
void TMethodCall::Init(const char *function, const char *params)
{
UInt_t pos = 0;
ClassInfo_t *cinfo = gCint->ClassInfo_Factory();
TClass *cl = R__FindScope(function,pos,cinfo);
InitImplementation(function+pos, params, 0, cl, cinfo);
gCint->ClassInfo_Delete(cinfo);
}
void TMethodCall::InitImplementation(const char *methodname, const char *params,
const char *proto, TClass *cl,
const ClassInfo_t *cinfo)
{
if (!fFunc) {
R__LOCKGUARD2(gCINTMutex);
fFunc = gCint->CallFunc_Factory();
} else
gCint->CallFunc_Init(fFunc);
fClass = cl;
fMetPtr = 0;
fMethod = methodname;
fParams = params ? params : "";
fProto = proto ? proto : "";
fDtorOnly = kFALSE;
fRetType = kNone;
ClassInfo_t *scope = 0;
ClassInfo_t *global = gCint->ClassInfo_Factory();
if (cl) scope = (ClassInfo_t*)cl->GetClassInfo();
else scope = (ClassInfo_t*)cinfo;
if (!scope) return;
R__LOCKGUARD2(gCINTMutex);
if (params && params[0]) {
gCint->CallFunc_SetFunc(fFunc, scope, (char *)methodname, (char *)params, &fOffset);
} else if (proto && proto[0]) {
gCint->CallFunc_SetFuncProto(fFunc, scope, (char *)methodname, (char *)proto, &fOffset);
} else {
gCint->CallFunc_SetFunc(fFunc, scope, (char *)methodname, "", &fOffset);
}
gCint->ClassInfo_Delete(global);
}
void TMethodCall::InitWithPrototype(TClass *cl, const char *method, const char *proto)
{
ClassInfo_t *cinfo = gCint->ClassInfo_Factory();
if (!cl) {
UInt_t pos = 0;
cl = R__FindScope(method,pos,cinfo);
method = method+pos;
}
InitImplementation(method, 0, proto, cl, cinfo);
gCint->ClassInfo_Delete(cinfo);
}
void TMethodCall::InitWithPrototype(const char *function, const char *proto)
{
UInt_t pos = 0;
ClassInfo_t *cinfo = gCint->ClassInfo_Factory();
TClass *cl = R__FindScope(function,pos,cinfo);
InitImplementation(function+pos, 0, proto, cl, cinfo);
gCint->ClassInfo_Delete(cinfo);
}
Bool_t TMethodCall::IsValid() const
{
return fFunc ? gCint->CallFunc_IsValid(fFunc) : kFALSE;
}
TFunction *TMethodCall::GetMethod()
{
if (!fMetPtr) {
if (fClass) {
if (fProto == "") {
fMetPtr = fClass->GetMethod(fMethod.Data(), fParams.Data());
} else {
fMetPtr = fClass->GetMethodWithPrototype(fMethod.Data(), fProto.Data());
}
TMethod *met = dynamic_cast<TMethod*>(fMetPtr);
if (met) fMetPtr = new TMethod(*met);
} else {
if (fProto == "")
fMetPtr = gROOT->GetGlobalFunction(fMethod.Data(), fParams.Data(), kTRUE);
else
fMetPtr = gROOT->GetGlobalFunctionWithPrototype(fMethod.Data(), fProto.Data(), kTRUE);
if (fMetPtr) fMetPtr = new TFunction(*fMetPtr);
}
}
return fMetPtr;
}
void TMethodCall::Execute(void *object)
{
if (!fFunc) return;
R__LOCKGUARD2(gCINTMutex);
void *address = 0;
if (object) address = (void*)((Long_t)object + fOffset);
gCint->SetTempLevel(1);
if (fDtorOnly) {
Long_t saveglobalvar = gCint->Getgvp();
gCint->Setgvp((Long_t)address);
gCint->CallFunc_Exec(fFunc,address);
gCint->Setgvp(saveglobalvar);
} else
gCint->CallFunc_Exec(fFunc,address);
gCint->SetTempLevel(-1);
}
void TMethodCall::Execute(void *object, const char *params)
{
if (!fFunc) return;
R__LOCKGUARD2(gCINTMutex);
gCint->CallFunc_SetArgs(fFunc, (char *)params);
void *address = 0;
if (object) address = (void*)((Long_t)object + fOffset);
gCint->SetTempLevel(1);
gCint->CallFunc_Exec(fFunc,address);
gCint->SetTempLevel(-1);
}
void TMethodCall::Execute(void *object, Long_t &retLong)
{
if (!fFunc) return;
R__LOCKGUARD2(gCINTMutex);
void *address = 0;
if (object) address = (void*)((Long_t)object + fOffset);
gCint->SetTempLevel(1);
retLong = gCint->CallFunc_ExecInt(fFunc,address);
gCint->SetTempLevel(-1);
}
void TMethodCall::Execute(void *object, const char *params, Long_t &retLong)
{
if (!fFunc) return;
R__LOCKGUARD2(gCINTMutex);
gCint->CallFunc_SetArgs(fFunc, (char *)params);
void *address = 0;
if (object) address = (void*)((Long_t)object + fOffset);
gCint->SetTempLevel(1);
retLong = gCint->CallFunc_ExecInt(fFunc,address);
gCint->SetTempLevel(-1);
}
void TMethodCall::Execute(void *object, Double_t &retDouble)
{
if (!fFunc) return;
R__LOCKGUARD2(gCINTMutex);
void *address = 0;
if (object) address = (void*)((Long_t)object + fOffset);
gCint->SetTempLevel(1);
retDouble = gCint->CallFunc_ExecDouble(fFunc,address);
gCint->SetTempLevel(-1);
}
void TMethodCall::Execute(void *object, const char *params, Double_t &retDouble)
{
if (!fFunc) return;
R__LOCKGUARD2(gCINTMutex);
gCint->CallFunc_SetArgs(fFunc, (char *)params);
void *address = 0;
if (object) address = (void*)((Long_t)object + fOffset);
gCint->SetTempLevel(1);
retDouble = gCint->CallFunc_ExecDouble(fFunc,address);
gCint->SetTempLevel(-1);
}
void TMethodCall::Execute(void *object, char **retText)
{
if (!fFunc) return;
R__LOCKGUARD2(gCINTMutex);
void *address = 0;
if (object) address = (void*)((Long_t)object + fOffset);
gCint->SetTempLevel(1);
*retText =(char*) (gCint->CallFunc_ExecInt(fFunc,address));
gCint->SetTempLevel(-1);
}
void TMethodCall::Execute(void *object, const char *params, char **retText)
{
if (!fFunc) return;
R__LOCKGUARD2(gCINTMutex);
gCint->CallFunc_SetArgs(fFunc, (char *)params);
void *address = 0;
if (object) address = (void*)((Long_t)object + fOffset);
gCint->SetTempLevel(1);
*retText =(char*)(gCint->CallFunc_ExecInt(fFunc,address));
gCint->SetTempLevel(-1);
}
TMethodCall::EReturnType TMethodCall::ReturnType()
{
if ( fRetType == kNone) {
R__LOCKGUARD2(gCINTMutex);
if ( fRetType != kNone) {
return fRetType;
}
TFunction *func = GetMethod();
if (func == 0) {
fRetType = kOther;
Error("ReturnType","Unknown method");
return kOther;
}
Int_t nstar = 0;
const char* rettype = func->GetReturnTypeName();
const char* returntype = rettype;
while (*returntype) {
if (*returntype == '*') nstar++;
returntype++;
}
TypedefInfo_t *atype = gCint->TypedefInfo_Factory();
gCint->TypedefInfo_Init(atype,gCint->TypeName(rettype));
const char *name = gCint->TypedefInfo_TrueName(atype);
Bool_t isEnum = kFALSE;
TypeInfo_t *typed = 0;
if (name && !strcmp("(unknown)",name)) {
typed = gCint->TypeInfo_Factory();
gCint->TypeInfo_Init(typed,func->GetReturnTypeName());
name = gCint->TypeInfo_TrueName(typed);
if (gCint->TypeInfo_Property(typed)&kIsEnum) {
isEnum = kTRUE;
}
}
if (name == 0)
fRetType = kOther;
else if ((nstar==1) &&
(!strcmp("unsigned char", name) || !strcmp("char", name) ||
!strcmp("UChar_t", name) || !strcmp("Char_t", name) ||
!strcmp("const unsigned char", name) || !strcmp("const char", name) ||
!strcmp("const UChar_t", name) || !strcmp("const Char_t", name) ||
!strcmp("unsigned char*", name) || !strcmp("char*", name) ||
!strcmp("UChar_t*", name) || !strcmp("Char_t*", name) ||
!strcmp("const unsigned char*", name) || !strcmp("const char*", name) ||
!strcmp("const UChar_t*", name) || !strcmp("const Char_t*", name)))
fRetType = kString;
else if (!strcmp("unsigned int", name) || !strcmp("int", name) ||
!strcmp("unsigned long", name) || !strcmp("long", name) ||
!strcmp("unsigned long long", name) || !strcmp("long long", name) ||
!strcmp("unsigned short", name) || !strcmp("short", name) ||
!strcmp("unsigned char", name) || !strcmp("char", name) ||
!strcmp("UInt_t", name) || !strcmp("Int_t", name) ||
!strcmp("ULong_t", name) || !strcmp("Long_t", name) ||
!strcmp("ULong64_t", name) || !strcmp("Long_t64", name) ||
!strcmp("UShort_t", name) || !strcmp("Short_t", name) ||
!strcmp("UChar_t", name) || !strcmp("Char_t", name) ||
!strcmp("Bool_t", name) || !strcmp("bool", name) ||
strstr(name, "enum"))
fRetType = kLong;
else if (!strcmp("float", name) || !strcmp("double", name) ||
!strcmp("Float_t", name) || !strcmp("Double_t", name))
fRetType = kDouble;
else if (isEnum)
fRetType = kLong;
else
fRetType = kOther;
gCint->TypeInfo_Delete(typed);
gCint->TypedefInfo_Delete(atype);
}
return fRetType;
}
void TMethodCall::SetParamPtrs(void *paramArr, Int_t nparam)
{
if (!fFunc) return;
R__LOCKGUARD2(gCINTMutex);
gCint->CallFunc_SetArgArray(fFunc,(Long_t *)paramArr, nparam);
}
void TMethodCall::ResetParam()
{
if (!fFunc) return;
gCint->CallFunc_ResetArg(fFunc);
}
void TMethodCall::SetParam(Long_t l)
{
if (!fFunc) return;
gCint->CallFunc_SetArg(fFunc,l);
}
void TMethodCall::SetParam(Double_t d)
{
if (!fFunc) return;
gCint->CallFunc_SetArg(fFunc,d);
}
void TMethodCall::SetParam(Long64_t ll)
{
if (!fFunc) return;
gCint->CallFunc_SetArg(fFunc,ll);
}
void TMethodCall::SetParam(ULong64_t ull)
{
if (!fFunc) return;
gCint->CallFunc_SetArg(fFunc,ull);
}