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.
43 SetName(gCling->MethodInfo_Name(fInfo));
44 SetTitle(gCling->MethodInfo_Title(fInfo));
45 fMangledName = gCling->MethodInfo_GetMangledName(fInfo);
46 }
47}
48
49////////////////////////////////////////////////////////////////////////////////
50/// Copy operator.
51
53{
54 if (orig.fInfo) {
55 // The next call locks the interpreter mutex.
56 fInfo = gCling->MethodInfo_FactoryCopy(orig.fInfo);
57 fMangledName = orig.fMangledName;
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.
72 gCling->MethodInfo_Delete(fInfo);
73 if (fMethodArgs) fMethodArgs->Delete();
74 delete fMethodArgs;
75 if (rhs.fInfo) {
76 fInfo = gCling->MethodInfo_FactoryCopy(rhs.fInfo);
77 SetName(gCling->MethodInfo_Name(fInfo));
78 SetTitle(gCling->MethodInfo_Title(fInfo));
79 fMangledName = gCling->MethodInfo_GetMangledName(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{
92 gCling->MethodInfo_Delete(fInfo);
93
94 if (fMethodArgs) fMethodArgs->Delete();
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.
116 gCling->MethodInfo_CreateSignature(fInfo, fSignature);
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";
154 return gCling->MethodInfo_TypeName(fInfo);
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";
168 return gCling->MethodInfo_TypeNormalizedName(fInfo);
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.
187 return fInfo ? gCling->MethodInfo_NDefaultArg(fInfo) : GetNargs();
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.
205 return fInfo ? gCling->MethodInfo_ExtraProperty(fInfo) : 0;
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.
265 return gCling->MethodInfo_GetPrototype(fInfo);
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.
304 gCling->MethodInfo_Delete(fInfo);
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.
317 gCling->MethodInfo_Delete(fInfo);
318 }
319 fInfo = info;
320 // The next call locks the interpreter mutex.
321 TString newMangledName = gCling->MethodInfo_GetMangledName(fInfo);
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.
329 SetTitle(gCling->MethodInfo_Title(fInfo));
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}
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
Int_t i
R__EXTERN TVirtualMutex * gInterpreterMutex
R__EXTERN TInterpreter * gCling
#define gInterpreter
#define R__LOCKGUARD(mutex)
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.
A doubly linked list.
Definition TList.h:38
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).
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
TNamed()
Definition TNamed.h:36
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
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:987
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1015
TObject()
TObject constructor.
Definition TObject.h:251
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2919
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376