ROOT  6.06/09
Reference Guide
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
13 Global functions class (global functions are obtained from CINT).
14 This class describes one single global function.
15 The TROOT class contains a list of all currently defined global
16 functions (accessible via TROOT::GetListOfGlobalFunctions()).
17 */
18 
19 #include "TFunction.h"
20 #include "TMethodArg.h"
21 #include "TROOT.h"
22 #include "TInterpreter.h"
23 #include "Strlen.h"
24 
25 #include <iostream>
26 #include "TVirtualMutex.h"
27 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Default TFunction ctor. TFunctions are constructed in TROOT via
32 /// a call to TCling::UpdateListOfGlobalFunctions().
33 
34 TFunction::TFunction(MethodInfo_t *info) : TDictionary()
35 {
36  fInfo = info;
37  fMethodArgs = 0;
38  if (fInfo) {
40  SetTitle(gCling->MethodInfo_Title(fInfo));
41  fMangledName = gCling->MethodInfo_GetMangledName(fInfo);
42  }
43 }
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 /// Copy operator.
47 
49 {
50  if (orig.fInfo) {
54  } else
55  fInfo = 0;
56  fMethodArgs = 0;
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Assignment operator.
61 
63 {
64  if (this != &rhs) {
68  delete fMethodArgs;
69  if (rhs.fInfo) {
74  } else
75  fInfo = 0;
76  fMethodArgs = 0;
77  }
78  return *this;
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// TFunction dtor deletes adopted CINT MethodInfo.
83 
85 {
88 
90  delete fMethodArgs;
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Clone method.
95 
96 TObject *TFunction::Clone(const char *newname) const
97 {
98  TNamed *newobj = new TFunction(*this);
99  if (newname && strlen(newname)) newobj->SetName(newname);
100  return newobj;
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Using the CINT method arg information to create a complete signature string.
105 
107 {
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Return signature of function.
114 
116 {
117  if (fInfo && fSignature.IsNull())
118  CreateSignature();
119 
120  return fSignature.Data();
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Return list containing the TMethodArgs of a TFunction.
125 
127 {
128  if (!fMethodArgs && fInfo) {
129  if (!gInterpreter)
130  Fatal("GetListOfMethodArgs", "gInterpreter not initialized");
131 
132  gInterpreter->CreateListOfMethodArgs(this);
133  }
134  return fMethodArgs;
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// Get full type description of function return type, e,g.: "class TDirectory*".
139 
140 const char *TFunction::GetReturnTypeName() const
141 {
143  if (fInfo == 0 || gCling->MethodInfo_Type(fInfo) == 0) return "Unknown";
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// Get the normalized name of the return type. A normalized name is fully
149 /// qualified and has all typedef desugared except for the 'special' typedef
150 /// which include Double32_t, Float16_t, [U]Long64_t and std::string. It
151 /// also has std:: removed [This is subject to change].
152 ///
153 
155 {
157  if (fInfo == 0 || gCling->MethodInfo_Type(fInfo) == 0) return "Unknown";
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Number of function arguments.
163 
165 {
166  if (fInfo) return gCling->MethodInfo_NArg(fInfo);
167  else if (fMethodArgs) return fMethodArgs->GetEntries();
168  else return 0;
169 }
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 /// Number of function optional (default) arguments.
173 
175 {
176  // FIXME: when unload this is an over-estimate.
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// Get property description word. For meaning of bits see EProperty.
182 
184 {
185  return fInfo ? gCling->MethodInfo_Property(fInfo) : 0;
186 }
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 /// Get property description word. For meaning of bits see EProperty.
190 
192 {
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 
199 {
200  return gInterpreter->GetDeclId(fInfo);
201 }
202 
203 ////////////////////////////////////////////////////////////////////////////////
204 /// Return pointer to the interface method. Using this pointer we
205 /// can find which TFunction belongs to a CINT MethodInfo object.
206 /// Both need to have the same InterfaceMethod pointer.
207 
209 {
211 }
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 /// Return true if this function object is pointing to a currently
215 /// loaded function. If a function is unloaded after the TFunction
216 /// is created, the TFunction will be set to be invalid.
217 
219 {
220  // Register the transaction when checking the validity of the object.
222  // Only for global functions. For data member functions TMethod does it.
223  DeclId_t newId = gInterpreter->GetFunction(0, fName);
224  if (newId) {
225  MethodInfo_t *info = gInterpreter->MethodInfo_Factory(newId);
226  Update(info);
227  }
228  return newId != 0;
229  }
230  return fInfo != 0;
231 }
232 
233 ////////////////////////////////////////////////////////////////////////////////
234 /// Returns the mangled name as defined by CINT, or 0 in case of error.
235 
236 const char *TFunction::GetMangledName() const
237 {
238  return fMangledName;
239 }
240 
241 ////////////////////////////////////////////////////////////////////////////////
242 /// Returns the prototype of a function as defined by CINT, or 0 in
243 /// case of error.
244 
245 const char *TFunction::GetPrototype() const
246 {
247  if (fInfo) {
250  } else
251  return 0;
252 }
253 
254 ////////////////////////////////////////////////////////////////////////////////
255 /// List TFunction name and title.
256 
257 void TFunction::ls(Option_t *options /* ="" */) const
258 {
259  TDictionary::ls(options);
261  std::cout << " " << GetPrototype() << '\n';
262 }
263 
264 ////////////////////////////////////////////////////////////////////////////////
265 /// Print TFunction name and title.
266 
267 void TFunction::Print(Option_t *options /* ="" */) const
268 {
269  TDictionary::Print(options);
270 }
271 
272 ////////////////////////////////////////////////////////////////////////////////
273 /// Update the TFunction to reflect the new info.
274 ///
275 /// This can be used to implement unloading (info == 0) and then reloading
276 /// (info being the 'new' decl address).
277 
278 Bool_t TFunction::Update(MethodInfo_t *info)
279 {
280  if (info == 0) {
281 
282  if (fInfo) {
285  }
286  fInfo = 0;
287  if (fMethodArgs) {
288  for (Int_t i = 0; i < fMethodArgs->LastIndex() + 1; i ++) {
289  TMethodArg *arg = (TMethodArg *) fMethodArgs->At( i );
290  arg->Update(0);
291  }
292  }
293  return kTRUE;
294  } else {
295  if (fInfo) {
298  }
299  fInfo = info;
300  TString newMangledName = gCling->MethodInfo_GetMangledName(fInfo);
301  if (newMangledName != fMangledName) {
302  Error("Update","TFunction object updated with the 'wrong' MethodInfo (%s vs %s).",
303  fMangledName.Data(),newMangledName.Data());
304  fInfo = 0;
305  return false;
306  }
308  if (fMethodArgs) {
309  MethodArgInfo_t *arg = gCling->MethodArgInfo_Factory(fInfo);
310  Int_t i = 0;
312  while (gCling->MethodArgInfo_Next(arg)) {
313  if (gCling->MethodArgInfo_IsValid(arg)) {
314  MethodArgInfo_t *new_arg = gCling->MethodArgInfo_FactoryCopy(arg);
315  ((TMethodArg *) fMethodArgs->At( i ))->Update(new_arg);
316  ++i;
317  }
318  }
319  }
320  return kTRUE;
321  }
322 }
virtual Int_t GetEntries() const
Definition: TCollection.h:92
virtual void CreateSignature()
Using the CINT method arg information to create a complete signature string.
Definition: TFunction.cxx:106
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:404
virtual const char * MethodInfo_GetMangledName(MethodInfo_t *) const
Definition: TInterpreter.h:443
virtual MethodInfo_t * MethodInfo_FactoryCopy(MethodInfo_t *) const
Definition: TInterpreter.h:433
virtual const char * MethodInfo_Title(MethodInfo_t *) const
Definition: TInterpreter.h:448
Bool_t UpdateInterpreterStateMarker()
the Cling ID of the transaction that last updated the object
const char Option_t
Definition: RtypesCore.h:62
R__EXTERN TVirtualMutex * gInterpreterMutex
Definition: TInterpreter.h:46
const char * GetReturnTypeName() const
Get full type description of function return type, e,g.: "class TDirectory*".
Definition: TFunction.cxx:140
Int_t GetNargs() const
Number of function arguments.
Definition: TFunction.cxx:164
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
void * InterfaceMethod() const
Return pointer to the interface method.
Definition: TFunction.cxx:208
virtual int MethodArgInfo_Next(MethodArgInfo_t *) const
Definition: TInterpreter.h:456
Int_t LastIndex() const
TFunction(MethodInfo_t *info=0)
virtual void Print(Option_t *option="") const
Print TFunction name and title.
Definition: TFunction.cxx:267
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual MethodArgInfo_t * MethodArgInfo_FactoryCopy(MethodArgInfo_t *) const
Definition: TInterpreter.h:454
#define gInterpreter
Definition: TInterpreter.h:502
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition: TMethodArg.h:33
std::string GetReturnTypeNormalizedName() const
Get the normalized name of the return type.
Definition: TFunction.cxx:154
const void * DeclId_t
Definition: TDictionary.h:209
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:310
MethodInfo_t * fInfo
Definition: TFunction.h:36
virtual Bool_t MethodArgInfo_IsValid(MethodArgInfo_t *) const
Definition: TInterpreter.h:455
virtual void ls(Option_t *option="") const
List TFunction name and title.
Definition: TFunction.cxx:257
virtual int MethodInfo_NDefaultArg(MethodInfo_t *) const
Definition: TInterpreter.h:437
const char * Data() const
Definition: TString.h:349
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition: TObject.cxx:946
TString fSignature
Definition: TFunction.h:38
TFunction & operator=(const TFunction &rhs)
Assignment operator.
Definition: TFunction.cxx:62
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:33
UChar_t mod R__LOCKGUARD2(gSrvAuthenticateMutex)
virtual void MethodInfo_CreateSignature(MethodInfo_t *, TString &) const
Definition: TInterpreter.h:428
virtual TypeInfo_t * MethodInfo_Type(MethodInfo_t *) const
Definition: TInterpreter.h:441
virtual const char * MethodInfo_Name(MethodInfo_t *) const
Definition: TInterpreter.h:445
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
TList * fMethodArgs
Definition: TFunction.h:39
virtual void ls(Option_t *option="") const
List TNamed name and title.
Definition: TNamed.cxx:102
virtual TObject * Clone(const char *newname="") const
Clone method.
Definition: TFunction.cxx:96
A doubly linked list.
Definition: TList.h:47
Int_t GetNargsOpt() const
Number of function optional (default) arguments.
Definition: TFunction.cxx:174
DeclId_t GetDeclId() const
Definition: TFunction.cxx:198
This class defines an abstract interface that must be implemented by all classes that contain diction...
Definition: TDictionary.h:162
virtual MethodArgInfo_t * MethodArgInfo_Factory() const
Definition: TInterpreter.h:452
virtual std::string MethodInfo_TypeNormalizedName(MethodInfo_t *) const
Definition: TInterpreter.h:447
Long_t Property() const
Get property description word. For meaning of bits see EProperty.
Definition: TFunction.cxx:183
Bool_t IsNull() const
Definition: TString.h:387
virtual void Print(Option_t *option="") const
Print TNamed name and title.
Definition: TNamed.cxx:117
TString fName
Definition: TNamed.h:36
virtual bool Update(MethodInfo_t *info)
Update the TFunction to reflect the new info.
Definition: TFunction.cxx:278
long Long_t
Definition: RtypesCore.h:50
TString fMangledName
Definition: TFunction.h:37
virtual void MethodInfo_Delete(MethodInfo_t *) const
Definition: TInterpreter.h:429
virtual const char * GetPrototype() const
Returns the prototype of a function as defined by CINT, or 0 in case of error.
Definition: TFunction.cxx:245
TList * GetListOfMethodArgs()
Return list containing the TMethodArgs of a TFunction.
Definition: TFunction.cxx:126
virtual ~TFunction()
TFunction dtor deletes adopted CINT MethodInfo.
Definition: TFunction.cxx:84
void Update(MethodArgInfo_t *info)
Update fInfo (to 0 for unloading and non-zero for reloading).
Definition: TMethodArg.cxx:130
virtual const char * MethodInfo_GetPrototype(MethodInfo_t *) const
Definition: TInterpreter.h:444
#define R__LOCKGUARD(mutex)
virtual int MethodInfo_NArg(MethodInfo_t *) const
Definition: TInterpreter.h:436
virtual Long_t MethodInfo_ExtraProperty(MethodInfo_t *) const =0
Mother of all ROOT objects.
Definition: TObject.h:58
Global functions class (global functions are obtained from CINT).
Definition: TFunction.h:30
virtual void * MethodInfo_InterfaceMethod(MethodInfo_t *) const
Definition: TInterpreter.h:434
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2461
Long_t ExtraProperty() const
Get property description word. For meaning of bits see EProperty.
Definition: TFunction.cxx:191
R__EXTERN TInterpreter * gCling
Definition: TInterpreter.h:504
virtual Bool_t IsValid()
Return true if this function object is pointing to a currently loaded function.
Definition: TFunction.cxx:218
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
gr SetName("gr")
virtual const char * MethodInfo_TypeName(MethodInfo_t *) const
Definition: TInterpreter.h:446
ClassImp(TFunction) TFunction
Default TFunction ctor.
Definition: TFunction.cxx:28
const char * GetSignature()
Return signature of function.
Definition: TFunction.cxx:115
virtual const char * GetMangledName() const
Returns the mangled name as defined by CINT, or 0 in case of error.
Definition: TFunction.cxx:236
virtual Long_t MethodInfo_Property(MethodInfo_t *) const =0