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
29
30////////////////////////////////////////////////////////////////////////////////
31/// Default TFunction ctor. TFunctions are constructed in TROOT via
32/// a call to TCling::UpdateListOfGlobalFunctions().
33
34TFunction::TFunction(MethodInfo_t *info) : TDictionary()
35{
36 fInfo = info;
37 fMethodArgs = nullptr;
38 if (fInfo) {
39 // The next calls into TClingMethodInfo methods lock the interpreter. Lock
40 // once here instead of locking/unlocking every time.
42 SetName(gCling->MethodInfo_Name(fInfo));
43 SetTitle(gCling->MethodInfo_Title(fInfo));
44 fMangledName = gCling->MethodInfo_GetMangledName(fInfo);
45 }
46}
47
48////////////////////////////////////////////////////////////////////////////////
49/// Copy operator.
50
52{
53 if (orig.fInfo) {
54 // The next call locks the interpreter mutex.
55 fInfo = gCling->MethodInfo_FactoryCopy(orig.fInfo);
56 fMangledName = orig.fMangledName;
57 } else
58 fInfo = nullptr;
59 fMethodArgs = nullptr;
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// Assignment operator.
64
66{
67 if (this != &rhs) {
68 // The next calls lock the interpreter mutex. Lock once here instead of
69 // locking/unlocking every time.
71 gCling->MethodInfo_Delete(fInfo);
72 if (fMethodArgs) fMethodArgs->Delete();
73 delete fMethodArgs;
74 if (rhs.fInfo) {
75 fInfo = gCling->MethodInfo_FactoryCopy(rhs.fInfo);
76 SetName(gCling->MethodInfo_Name(fInfo));
77 SetTitle(gCling->MethodInfo_Title(fInfo));
78 fMangledName = gCling->MethodInfo_GetMangledName(fInfo);
79 } else
80 fInfo = nullptr;
81 fMethodArgs = nullptr;
82 }
83 return *this;
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// TFunction dtor deletes adopted CINT MethodInfo.
88
90{
91 gCling->MethodInfo_Delete(fInfo);
92
93 if (fMethodArgs) fMethodArgs->Delete();
94 delete fMethodArgs;
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Clone method.
99
100TObject *TFunction::Clone(const char *newname) const
101{
102 // The constructor locks the interpreter mutex.
103 TNamed *newobj = new TFunction(*this);
104 if (newname && strlen(newname)) newobj->SetName(newname);
105 return newobj;
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// Using the CINT method arg information to create a complete signature string.
110
112{
113 // The next call locks the interpreter mutex. The result is cached in the
114 // fSignature data member.
115 gCling->MethodInfo_CreateSignature(fInfo, fSignature);
116}
117
118////////////////////////////////////////////////////////////////////////////////
119/// Return signature of function.
120
122{
123 if (fInfo && fSignature.IsNull())
124 // The next call locks the interpreter mutex.
125 // The result is cached in the fSignature data member.
127
128 return fSignature.Data();
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// Return list containing the TMethodArgs of a TFunction.
133
135{
136 if (!fMethodArgs && fInfo) {
137 if (!gInterpreter)
138 Fatal("GetListOfMethodArgs", "gInterpreter not initialized");
139
140 // The next call locks the interpreter mutex.
141 gInterpreter->CreateListOfMethodArgs(this);
142 }
143 return fMethodArgs;
144}
145
146////////////////////////////////////////////////////////////////////////////////
147/// Get full type description of function return type, e,g.: "class TDirectory*".
148
150{
151 // The next calls lock the interpreter mutex.
152 if (fInfo == nullptr || gCling->MethodInfo_Type(fInfo) == nullptr) return "Unknown";
153 return gCling->MethodInfo_TypeName(fInfo);
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// Get the normalized name of the return type. A normalized name is fully
158/// qualified and has all typedef desugared except for the 'special' typedef
159/// which include Double32_t, Float16_t, [U]Long64_t and std::string. It
160/// also has std:: removed [This is subject to change].
161///
162
164{
165 // The next calls lock the interpreter mutex.
166 if (fInfo == nullptr || gCling->MethodInfo_Type(fInfo) == nullptr) return "Unknown";
167 return gCling->MethodInfo_TypeNormalizedName(fInfo);
168}
169
170////////////////////////////////////////////////////////////////////////////////
171/// Number of function arguments.
172
174{
175 if (fInfo) return gCling->MethodInfo_NArg(fInfo);
176 else if (fMethodArgs) return fMethodArgs->GetEntries();
177 else return 0;
178}
179
180////////////////////////////////////////////////////////////////////////////////
181/// Number of function optional (default) arguments.
182
184{
185 // FIXME: when unload this is an over-estimate.
186 return fInfo ? gCling->MethodInfo_NDefaultArg(fInfo) : GetNargs();
187}
188
189////////////////////////////////////////////////////////////////////////////////
190/// Get property description word. For meaning of bits see EProperty.
191
193{
194 // The next call locks the interpreter mutex.
195 return fInfo ? gCling->MethodInfo_Property(fInfo) : 0;
196}
197
198////////////////////////////////////////////////////////////////////////////////
199/// Get property description word. For meaning of bits see EProperty.
200
202{
203 // The next call locks the interpreter mutex.
204 return fInfo ? gCling->MethodInfo_ExtraProperty(fInfo) : 0;
205}
206
207////////////////////////////////////////////////////////////////////////////////
208
210{
211 // The next call locks the interpreter mutex.
212 return gInterpreter->GetDeclId(fInfo);
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Return pointer to the interface method. Using this pointer we
217/// can find which TFunction belongs to a CINT MethodInfo object.
218/// Both need to have the same InterfaceMethod pointer.
219
221{
222 // The next call locks the interpreter mutex.
223 return fInfo ? gCling->MethodInfo_InterfaceMethod(fInfo) : nullptr;
224}
225
226////////////////////////////////////////////////////////////////////////////////
227/// Return true if this function object is pointing to a currently
228/// loaded function. If a function is unloaded after the TFunction
229/// is created, the TFunction will be set to be invalid.
230
232{
233 // Register the transaction when checking the validity of the object.
235 // Only for global functions. For data member functions TMethod does it.
236 // The next calls lock the interpreter mutex.
237 DeclId_t newId = gInterpreter->GetFunction(nullptr, fName);
238 if (newId) {
239 // The next call locks the interpreter mutex. (TODO: why?)
240 MethodInfo_t *info = gInterpreter->MethodInfo_Factory(newId);
241 Update(info);
242 }
243 return newId != nullptr;
244 }
245 return fInfo != nullptr;
246}
247
248////////////////////////////////////////////////////////////////////////////////
249/// Returns the mangled name as defined by CINT, or 0 in case of error.
250
251const char *TFunction::GetMangledName() const
252{
253 return fMangledName;
254}
255
256////////////////////////////////////////////////////////////////////////////////
257/// Returns the prototype of a function as defined by CINT, or 0 in
258/// case of error.
259
260const char *TFunction::GetPrototype() const
261{
262 if (fInfo) {
263 // The next call locks the interpreter mutex.
264 return gCling->MethodInfo_GetPrototype(fInfo);
265 } else
266 return nullptr;
267}
268
269////////////////////////////////////////////////////////////////////////////////
270/// List TFunction name and title.
271
272void TFunction::ls(Option_t *options /* ="" */) const
273{
274 TDictionary::ls(options);
276 std::cout << " " << GetPrototype() << '\n';
277}
278
279////////////////////////////////////////////////////////////////////////////////
280/// Print TFunction name and title.
281
282void TFunction::Print(Option_t *options /* ="" */) const
283{
284 TDictionary::Print(options);
285}
286
287////////////////////////////////////////////////////////////////////////////////
288/// Update the TFunction to reflect the new info.
289///
290/// This can be used to implement unloading (info == 0) and then reloading
291/// (info being the 'new' decl address).
292
293Bool_t TFunction::Update(MethodInfo_t *info)
294{
295 // This function needs to lock access to the interpreter multiple times.
296 // Take the lock at the beginning of the function so that we don't incur
297 // in too much locking/unlocking.
299 if (info == nullptr) {
300
301 if (fInfo) {
302 // The next call locks the interpreter mutex.
303 gCling->MethodInfo_Delete(fInfo);
304 }
305 fInfo = nullptr;
306 if (fMethodArgs) {
307 for (Int_t i = 0; i < fMethodArgs->LastIndex() + 1; i ++) {
308 TMethodArg *arg = (TMethodArg *) fMethodArgs->At( i );
309 arg->Update(nullptr);
310 }
311 }
312 return kTRUE;
313 } else {
314 if (fInfo) {
315 // The next call locks the interpreter mutex.
316 gCling->MethodInfo_Delete(fInfo);
317 }
318 fInfo = info;
319 // The next call locks the interpreter mutex.
320 TString newMangledName = gCling->MethodInfo_GetMangledName(fInfo);
321 if (newMangledName != fMangledName) {
322 Error("Update","TFunction object updated with the 'wrong' MethodInfo (%s vs %s).",
323 fMangledName.Data(),newMangledName.Data());
324 fInfo = nullptr;
325 return false;
326 }
327 // The next call locks the interpreter mutex.
328 SetTitle(gCling->MethodInfo_Title(fInfo));
329 if (fMethodArgs) {
330 // TODO: Check for MethodArgInfo thread-safety
331 MethodArgInfo_t *arg = gCling->MethodArgInfo_Factory(fInfo);
332 Int_t i = 0;
333 while (gCling->MethodArgInfo_Next(arg)) {
334 if (gCling->MethodArgInfo_IsValid(arg)) {
335 MethodArgInfo_t *new_arg = gCling->MethodArgInfo_FactoryCopy(arg);
336 ((TMethodArg *) fMethodArgs->At( i ))->Update(new_arg);
337 ++i;
338 }
339 }
340 }
341 return kTRUE;
342 }
343}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
#define gInterpreter
externTInterpreter * gCling
externTVirtualMutex * gInterpreterMutex
#define R__LOCKGUARD(mutex)
Bool_t UpdateInterpreterStateMarker()
const void * DeclId_t
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:34
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:89
TFunction & operator=(const TFunction &rhs)
Assignment operator.
Definition TFunction.cxx:65
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:173
TNamed()
Definition TNamed.h:38
void Print(Option_t *option="") const override
Print TNamed name and title.
Definition TNamed.cxx:127
TString fName
Definition TNamed.h:32
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
void ls(Option_t *option="") const override
List TNamed name and title.
Definition TNamed.cxx:112
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1098
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1126
TObject()
TObject constructor.
Definition TObject.h:259
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:3052
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384