Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TFunction.cxx
Go to the documentation of this file.
1// @(#)root/meta:$Id$
2// Author: Fons Rademakers 07/02/97
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/** \class TFunction
13Global functions class (global functions are obtained from CINT).
14This class describes one single global function.
15The TROOT class contains a list of all currently defined global
16functions (accessible via TROOT::GetListOfGlobalFunctions()).
17*/
18
19#include "TFunction.h"
20#include "TMethodArg.h"
21#include "TList.h"
22#include "TROOT.h"
23#include "TInterpreter.h"
24#include "Strlen.h"
25
26#include <iostream>
27#include "TVirtualMutex.h"
28
30
31////////////////////////////////////////////////////////////////////////////////
32/// Default TFunction ctor. TFunctions are constructed in TROOT via
33/// a call to TCling::UpdateListOfGlobalFunctions().
34
35TFunction::TFunction(MethodInfo_t *info) : TDictionary()
36{
37 fInfo = info;
38 fMethodArgs = nullptr;
39 if (fInfo) {
40 // The next calls into TClingMethodInfo methods lock the interpreter. Lock
41 // once here instead of locking/unlocking every time.
46 }
47}
48
49////////////////////////////////////////////////////////////////////////////////
50/// Copy operator.
51
53{
54 if (orig.fInfo) {
55 // The next call locks the interpreter mutex.
58 } else
59 fInfo = nullptr;
60 fMethodArgs = nullptr;
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Assignment operator.
65
67{
68 if (this != &rhs) {
69 // The next calls lock the interpreter mutex. Lock once here instead of
70 // locking/unlocking every time.
74 delete fMethodArgs;
75 if (rhs.fInfo) {
80 } else
81 fInfo = nullptr;
82 fMethodArgs = nullptr;
83 }
84 return *this;
85}
86
87////////////////////////////////////////////////////////////////////////////////
88/// TFunction dtor deletes adopted CINT MethodInfo.
89
91{
93
95 delete fMethodArgs;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Clone method.
100
101TObject *TFunction::Clone(const char *newname) const
102{
103 // The constructor locks the interpreter mutex.
104 TNamed *newobj = new TFunction(*this);
105 if (newname && strlen(newname)) newobj->SetName(newname);
106 return newobj;
107}
108
109////////////////////////////////////////////////////////////////////////////////
110/// Using the CINT method arg information to create a complete signature string.
111
113{
114 // The next call locks the interpreter mutex. The result is cached in the
115 // fSignature data member.
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// Return signature of function.
121
123{
124 if (fInfo && fSignature.IsNull())
125 // The next call locks the interpreter mutex.
126 // The result is cached in the fSignature data member.
128
129 return fSignature.Data();
130}
131
132////////////////////////////////////////////////////////////////////////////////
133/// Return list containing the TMethodArgs of a TFunction.
134
136{
137 if (!fMethodArgs && fInfo) {
138 if (!gInterpreter)
139 Fatal("GetListOfMethodArgs", "gInterpreter not initialized");
140
141 // The next call locks the interpreter mutex.
142 gInterpreter->CreateListOfMethodArgs(this);
143 }
144 return fMethodArgs;
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// Get full type description of function return type, e,g.: "class TDirectory*".
149
151{
152 // The next calls lock the interpreter mutex.
153 if (fInfo == nullptr || gCling->MethodInfo_Type(fInfo) == nullptr) return "Unknown";
155}
156
157////////////////////////////////////////////////////////////////////////////////
158/// Get the normalized name of the return type. A normalized name is fully
159/// qualified and has all typedef desugared except for the 'special' typedef
160/// which include Double32_t, Float16_t, [U]Long64_t and std::string. It
161/// also has std:: removed [This is subject to change].
162///
163
165{
166 // The next calls lock the interpreter mutex.
167 if (fInfo == nullptr || gCling->MethodInfo_Type(fInfo) == nullptr) return "Unknown";
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Number of function arguments.
173
175{
176 if (fInfo) return gCling->MethodInfo_NArg(fInfo);
177 else if (fMethodArgs) return fMethodArgs->GetEntries();
178 else return 0;
179}
180
181////////////////////////////////////////////////////////////////////////////////
182/// Number of function optional (default) arguments.
183
185{
186 // FIXME: when unload this is an over-estimate.
188}
189
190////////////////////////////////////////////////////////////////////////////////
191/// Get property description word. For meaning of bits see EProperty.
192
194{
195 // The next call locks the interpreter mutex.
196 return fInfo ? gCling->MethodInfo_Property(fInfo) : 0;
197}
198
199////////////////////////////////////////////////////////////////////////////////
200/// Get property description word. For meaning of bits see EProperty.
201
203{
204 // The next call locks the interpreter mutex.
206}
207
208////////////////////////////////////////////////////////////////////////////////
209
211{
212 // The next call locks the interpreter mutex.
213 return gInterpreter->GetDeclId(fInfo);
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// Return pointer to the interface method. Using this pointer we
218/// can find which TFunction belongs to a CINT MethodInfo object.
219/// Both need to have the same InterfaceMethod pointer.
220
222{
223 // The next call locks the interpreter mutex.
224 return fInfo ? gCling->MethodInfo_InterfaceMethod(fInfo) : nullptr;
225}
226
227////////////////////////////////////////////////////////////////////////////////
228/// Return true if this function object is pointing to a currently
229/// loaded function. If a function is unloaded after the TFunction
230/// is created, the TFunction will be set to be invalid.
231
233{
234 // Register the transaction when checking the validity of the object.
236 // Only for global functions. For data member functions TMethod does it.
237 // The next calls lock the interpreter mutex.
238 DeclId_t newId = gInterpreter->GetFunction(nullptr, fName);
239 if (newId) {
240 // The next call locks the interpreter mutex. (TODO: why?)
241 MethodInfo_t *info = gInterpreter->MethodInfo_Factory(newId);
242 Update(info);
243 }
244 return newId != nullptr;
245 }
246 return fInfo != nullptr;
247}
248
249////////////////////////////////////////////////////////////////////////////////
250/// Returns the mangled name as defined by CINT, or 0 in case of error.
251
252const char *TFunction::GetMangledName() const
253{
254 return fMangledName;
255}
256
257////////////////////////////////////////////////////////////////////////////////
258/// Returns the prototype of a function as defined by CINT, or 0 in
259/// case of error.
260
261const char *TFunction::GetPrototype() const
262{
263 if (fInfo) {
264 // The next call locks the interpreter mutex.
266 } else
267 return nullptr;
268}
269
270////////////////////////////////////////////////////////////////////////////////
271/// List TFunction name and title.
272
273void TFunction::ls(Option_t *options /* ="" */) const
274{
275 TDictionary::ls(options);
277 std::cout << " " << GetPrototype() << '\n';
278}
279
280////////////////////////////////////////////////////////////////////////////////
281/// Print TFunction name and title.
282
283void TFunction::Print(Option_t *options /* ="" */) const
284{
285 TDictionary::Print(options);
286}
287
288////////////////////////////////////////////////////////////////////////////////
289/// Update the TFunction to reflect the new info.
290///
291/// This can be used to implement unloading (info == 0) and then reloading
292/// (info being the 'new' decl address).
293
294Bool_t TFunction::Update(MethodInfo_t *info)
295{
296 // This function needs to lock access to the interpreter multiple times.
297 // Take the lock at the beginning of the function so that we don't incur
298 // in too much locking/unlocking.
300 if (info == nullptr) {
301
302 if (fInfo) {
303 // The next call locks the interpreter mutex.
305 }
306 fInfo = nullptr;
307 if (fMethodArgs) {
308 for (Int_t i = 0; i < fMethodArgs->LastIndex() + 1; i ++) {
309 TMethodArg *arg = (TMethodArg *) fMethodArgs->At( i );
310 arg->Update(nullptr);
311 }
312 }
313 return kTRUE;
314 } else {
315 if (fInfo) {
316 // The next call locks the interpreter mutex.
318 }
319 fInfo = info;
320 // The next call locks the interpreter mutex.
322 if (newMangledName != fMangledName) {
323 Error("Update","TFunction object updated with the 'wrong' MethodInfo (%s vs %s).",
324 fMangledName.Data(),newMangledName.Data());
325 fInfo = nullptr;
326 return false;
327 }
328 // The next call locks the interpreter mutex.
330 if (fMethodArgs) {
331 // TODO: Check for MethodArgInfo thread-safety
332 MethodArgInfo_t *arg = gCling->MethodArgInfo_Factory(fInfo);
333 Int_t i = 0;
334 while (gCling->MethodArgInfo_Next(arg)) {
335 if (gCling->MethodArgInfo_IsValid(arg)) {
336 MethodArgInfo_t *new_arg = gCling->MethodArgInfo_FactoryCopy(arg);
337 ((TMethodArg *) fMethodArgs->At( i ))->Update(new_arg);
338 ++i;
339 }
340 }
341 }
342 return kTRUE;
343 }
344}
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
R__EXTERN TVirtualMutex * gInterpreterMutex
R__EXTERN TInterpreter * gCling
#define gInterpreter
#define R__LOCKGUARD(mutex)
virtual Int_t GetEntries() const
This class defines an abstract interface that must be implemented by all classes that contain diction...
Bool_t UpdateInterpreterStateMarker()
the Cling ID of the transaction that last updated the object
const void * DeclId_t
Global functions class (global functions are obtained from CINT).
Definition TFunction.h:30
virtual const char * GetMangledName() const
Returns the mangled name as defined by CINT, or 0 in case of error.
TList * fMethodArgs
Definition TFunction.h:39
TString fSignature
Definition TFunction.h:38
virtual void CreateSignature()
Using the CINT method arg information to create a complete signature string.
TString fMangledName
Definition TFunction.h:37
virtual const char * GetPrototype() const
Returns the prototype of a function as defined by CINT, or 0 in case of error.
TFunction(MethodInfo_t *info=nullptr)
Default TFunction ctor.
Definition TFunction.cxx:35
void * InterfaceMethod() const
Return pointer to the interface method.
virtual Bool_t IsValid()
Return true if this function object is pointing to a currently loaded function.
Long_t Property() const override
Get property description word. For meaning of bits see EProperty.
MethodInfo_t * fInfo
Definition TFunction.h:36
const char * GetSignature()
Return signature of function.
TObject * Clone(const char *newname="") const override
Clone method.
void Print(Option_t *option="") const override
Print TFunction name and title.
virtual bool Update(MethodInfo_t *info)
Update the TFunction to reflect the new info.
Int_t GetNargsOpt() const
Number of function optional (default) arguments.
Int_t GetNargs() const
Number of function arguments.
DeclId_t GetDeclId() const
Long_t ExtraProperty() const
Get property description word. For meaning of bits see EProperty.
TList * GetListOfMethodArgs()
Return list containing the TMethodArgs of a TFunction.
virtual ~TFunction()
TFunction dtor deletes adopted CINT MethodInfo.
Definition TFunction.cxx:90
TFunction & operator=(const TFunction &rhs)
Assignment operator.
Definition TFunction.cxx:66
const char * GetReturnTypeName() const
Get full type description of function return type, e,g.: "class TDirectory*".
void ls(Option_t *option="") const override
List TFunction name and title.
std::string GetReturnTypeNormalizedName() const
Get the normalized name of the return type.
virtual int MethodInfo_NArg(MethodInfo_t *) const
virtual Long_t MethodInfo_ExtraProperty(MethodInfo_t *) const =0
virtual const char * MethodInfo_Title(MethodInfo_t *) const
virtual const char * MethodInfo_GetPrototype(MethodInfo_t *) const
virtual TypeInfo_t * MethodInfo_Type(MethodInfo_t *) const
virtual int MethodArgInfo_Next(MethodArgInfo_t *) const
virtual void MethodInfo_Delete(MethodInfo_t *) const
virtual int MethodInfo_NDefaultArg(MethodInfo_t *) const
virtual Bool_t MethodArgInfo_IsValid(MethodArgInfo_t *) const
virtual const char * MethodInfo_TypeName(MethodInfo_t *) const
virtual MethodArgInfo_t * MethodArgInfo_Factory() const
virtual void * MethodInfo_InterfaceMethod(MethodInfo_t *) const
virtual MethodInfo_t * MethodInfo_FactoryCopy(MethodInfo_t *) const
virtual const char * MethodInfo_Name(MethodInfo_t *) const
virtual MethodArgInfo_t * MethodArgInfo_FactoryCopy(MethodArgInfo_t *) const
virtual void MethodInfo_CreateSignature(MethodInfo_t *, TString &) const
virtual std::string MethodInfo_TypeNormalizedName(MethodInfo_t *) const
virtual Long_t MethodInfo_Property(MethodInfo_t *) const =0
virtual const char * MethodInfo_GetMangledName(MethodInfo_t *) const
A doubly linked list.
Definition TList.h:38
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:468
TObject * At(Int_t idx) const override
Returns the object at position idx. Returns 0 if idx is out of range.
Definition TList.cxx:355
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition TMethodArg.h:36
void Update(MethodArgInfo_t *info)
Update fInfo (to 0 for unloading and non-zero for reloading).
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
void Print(Option_t *option="") const override
Print TNamed name and title.
Definition TNamed.cxx:128
TString fName
Definition TNamed.h:32
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
void ls(Option_t *option="") const override
List TNamed name and title.
Definition TNamed.cxx:113
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:993
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1021
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2895
Int_t LastIndex() const
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
Bool_t IsNull() const
Definition TString.h:414