#include "TPluginManager.h"
#include "TEnv.h"
#include "TRegexp.h"
#include "TROOT.h"
#include "THashList.h"
#include "TOrdCollection.h"
#include "Varargs.h"
#include "TClass.h"
#include "TCint.h"
#include "TMethod.h"
#include "TMethodArg.h"
#include "TDataType.h"
#include "TMethodCall.h"
#include "TVirtualMutex.h"
ClassImp(TPluginHandler)
TPluginHandler::TPluginHandler(const char *base, const char *regexp,
const char *className, const char *pluginName,
const char *ctor)
{
fBase = base;
fRegexp = regexp;
fClass = className;
fPlugin = pluginName;
fCtor = ctor;
fCallEnv = 0;
fCanCall = 0;
fIsMacro = kFALSE;
fIsGlobal = kFALSE;
if (gROOT->LoadMacro(pluginName, 0, kTRUE) == 0)
fIsMacro = kTRUE;
if (fCtor.Contains("::")) {
fIsGlobal = kTRUE;
fCtor = fCtor.Strip(TString::kLeading, ':');
}
}
TPluginHandler::~TPluginHandler()
{
delete fCallEnv;
}
Bool_t TPluginHandler::CanHandle(const char *base, const char *uri)
{
if (fBase != base)
return kFALSE;
if (!uri || fRegexp == "*")
return kTRUE;
Bool_t wildcard = kFALSE;
if (!fRegexp.MaybeRegexp())
wildcard = kTRUE;
TRegexp re(fRegexp, wildcard);
TString ruri = uri;
if (ruri.Index(re) != kNPOS)
return kTRUE;
return kFALSE;
}
void TPluginHandler::SetupCallEnv()
{
fCanCall = -1;
TClass *cl = gROOT->GetClass(fClass);
if (!cl && !fIsGlobal) {
Error("SetupCallEnv", "class %s not found in plugin %s", fClass.Data(),
fPlugin.Data());
return;
}
TString method = fCtor(0, fCtor.Index("("));
TString proto = fCtor(fCtor.Index("(")+1, fCtor.Index(")")-fCtor.Index("(")-1);
if (fIsGlobal) {
cl = 0;
if (fIsMacro)
fMethod = gROOT->GetGlobalFunction(method, 0, kTRUE);
else
fMethod = gROOT->GetGlobalFunctionWithPrototype(method, proto, kTRUE);
} else {
if (fIsMacro)
fMethod = cl->GetMethodAny(method);
else
fMethod = cl->GetMethodWithPrototype(method, proto);
}
if (!fMethod) {
if (fIsGlobal)
Error("SetupCallEnv", "global function %s not found", method.Data());
else
Error("SetupCallEnv", "method %s not found in class %s", method.Data(),
fClass.Data());
return;
}
if (!fIsGlobal && !(fMethod->Property() & kIsPublic)) {
Error("SetupCallEnv", "method %s is not public", method.Data());
return;
}
fCallEnv = new TMethodCall;
fCallEnv->InitWithPrototype(cl, method, proto);
fCanCall = 1;
return;
}
Int_t TPluginHandler::CheckPlugin()
{
if (fIsMacro) {
if (gROOT->GetClass(fClass)) return 0;
return gROOT->LoadMacro(fPlugin, 0, kTRUE);
} else
return gROOT->LoadClass(fClass, fPlugin, kTRUE);
}
Int_t TPluginHandler::LoadPlugin()
{
if (fIsMacro) {
if (gROOT->GetClass(fClass)) return 0;
return gROOT->LoadMacro(fPlugin);
} else
return gROOT->LoadClass(fClass, fPlugin);
}
Long_t TPluginHandler::ExecPlugin(Int_t va_(nargs), ...)
{
if (fCtor.IsNull()) {
Error("ExecPlugin", "no ctor specified for this handler %s", fClass.Data());
return 0;
}
if (!fCallEnv && !fCanCall)
SetupCallEnv();
if (fCanCall == -1)
return 0;
if (nargs < fMethod->GetNargs() - fMethod->GetNargsOpt() ||
nargs > fMethod->GetNargs()) {
Error("ExecPlugin", "nargs (%d) not consistent with expected number of arguments ([%d-%d])",
nargs, fMethod->GetNargs() - fMethod->GetNargsOpt(),
fMethod->GetNargs());
return 0;
}
R__LOCKGUARD2(gCINTMutex);
fCallEnv->ResetParam();
if (nargs > 0) {
TIter next(fMethod->GetListOfMethodArgs());
TMethodArg *arg;
va_list ap;
va_start(ap, va_(nargs));
for (int i = 0; i < nargs; i++) {
arg = (TMethodArg*) next();
TString type = arg->GetFullTypeName();
TDataType *dt = gROOT->GetType(type);
if (dt)
type = dt->GetFullTypeName();
if (arg->Property() & (kIsPointer | kIsArray | kIsReference))
fCallEnv->SetParam((Long_t) va_arg(ap, void*));
else if (type == "bool")
fCallEnv->SetParam((Long_t) va_arg(ap, int));
else if (type == "char" || type == "unsigned char")
fCallEnv->SetParam((Long_t) va_arg(ap, int));
else if (type == "short" || type == "unsigned short")
fCallEnv->SetParam((Long_t) va_arg(ap, int));
else if (type == "int" || type == "unsigned int")
fCallEnv->SetParam((Long_t) va_arg(ap, int));
else if (type == "long" || type == "unsigned long")
fCallEnv->SetParam((Long_t) va_arg(ap, long));
else if (type == "long long")
fCallEnv->SetParam((Long64_t) va_arg(ap, Long64_t));
else if (type == "unsigned long long")
fCallEnv->SetParam((ULong64_t) va_arg(ap, ULong64_t));
else if (type == "float")
fCallEnv->SetParam((Double_t) va_arg(ap, double));
else if (type == "double")
fCallEnv->SetParam((Double_t) va_arg(ap, double));
}
va_end(ap);
}
Long_t ret;
fCallEnv->Execute(ret);
return ret;
}
ClassImp(TPluginManager)
TPluginManager::~TPluginManager()
{
delete fHandlers;
}
void TPluginManager::LoadHandlersFromEnv(TEnv *env)
{
if (!env) return;
TIter next(env->GetTable());
TEnvRec *er;
while ((er = (TEnvRec*) next())) {
const char *s;
if ((s = strstr(er->GetName(), "Plugin."))) {
const char *val = env->GetValue(s, (const char*)0);
if (val) {
Int_t cnt = 0;
char *v = StrDup(val);
s += 7;
while (1) {
TString regexp = strtok(!cnt ? v : 0, "; ");
if (regexp.IsNull()) break;
TString clss = strtok(0, "; ");
if (clss.IsNull()) break;
TString plugin = strtok(0, "; ");
if (plugin.IsNull()) break;
TString ctor = strtok(0, ";\"");
if (!ctor.Contains("("))
ctor = strtok(0, ";\"");
AddHandler(s, regexp, clss, plugin, ctor);
cnt++;
}
delete [] v;
}
}
}
}
void TPluginManager::AddHandler(const char *base, const char *regexp,
const char *className, const char *pluginName,
const char *ctor)
{
if (!fHandlers) {
fHandlers = new TList;
fHandlers->IsOwner();
}
RemoveHandler(base, regexp);
TPluginHandler *h = new TPluginHandler(base, regexp, className,
pluginName, ctor);
fHandlers->Add(h);
}
void TPluginManager::RemoveHandler(const char *base, const char *regexp)
{
if (!fHandlers) return;
TIter next(fHandlers);
TPluginHandler *h;
while ((h = (TPluginHandler*) next())) {
if (h->fBase == base) {
if (!regexp || h->fRegexp == regexp) {
fHandlers->Remove(h);
delete h;
}
}
}
}
TPluginHandler *TPluginManager::FindHandler(const char *base, const char *uri)
{
if (!fHandlers) return 0;
TIter next(fHandlers);
TPluginHandler *h;
while ((h = (TPluginHandler*) next())) {
if (h->CanHandle(base, uri)) {
if (gDebug > 0)
Info("FindHandler", "found plugin for %s", h->GetClass());
return h;
}
}
if (gDebug > 0) {
if (uri)
Info("FindHandler", "did not find plugin for class %s and uri %s", base, uri);
else
Info("FindHandler", "did not find plugin for class %s", base);
}
return 0;
}
void TPluginManager::Print(Option_t *opt) const
{
if (!fHandlers) return;
TIter next(fHandlers);
TPluginHandler *h;
Printf("=====================================================================");
Printf("Base Regexp Class Plugin");
Printf("=====================================================================");
while ((h = (TPluginHandler*) next())) {
const char *exist = "";
if (h->CheckPlugin() == -1)
exist = " [*]";
Printf("%-20s %-13s %-18s %s%s", h->fBase.Data(), h->fRegexp.Data(),
h->fClass.Data(), h->fPlugin.Data(), exist);
if (strchr(opt, 'a'))
Printf(" [Ctor: %s]", h->fCtor.Data());
}
Printf("=====================================================================");
Printf("[*] plugin not available");
Printf("=====================================================================\n");
}
ROOT page - Class index - Class Hierarchy - Top of the page
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.