Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TMethod.cxx
Go to the documentation of this file.
1// @(#)root/meta:$Id$
2// Author: Rene Brun 09/02/95
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 TMethod
13 Each ROOT class (see TClass) has a linked list of methods.
14 This class describes one single method (member function).
15 The method info is obtained via the CINT api. See class TCling.
16
17 The method information is used a.o. by the THml class and by the
18 TTree class.
19*/
20
21#include "strtok.h"
22#include "strlcpy.h"
23#include "snprintf.h"
24#include "TClass.h"
25#include "TList.h"
26#include "TMethod.h"
27#include "TMethodArg.h"
28#include "TMethodCall.h"
29#include "TInterpreter.h"
30#include "Strlen.h"
31#include "TDataMember.h"
32
33
34
35////////////////////////////////////////////////////////////////////////////////
36/// Default TMethod ctor. TMethods are constructed in TClass.
37/// Comment strings are pre-parsed to find out whether the method is
38/// a context-menu item.
39
40TMethod::TMethod(MethodInfo_t *info, TClass *cl) : TFunction(info)
41{
42 fClass = cl;
43 fGetterMethod = nullptr;
44 fSetterMethod = nullptr;
46
47 if (fInfo) {
48 SetMenuItem(gCling->MethodInfo_Title(fInfo));
49 }
50}
51
52////////////////////////////////////////////////////////////////////////////////
53/// Copy ctor.
54
56{
57 fClass = orig.fClass;
58 fMenuItem = orig.fMenuItem;
59 fGetter = orig.fGetter;
60 fGetterMethod = nullptr;
61 fSetterMethod = nullptr;
62}
63
64////////////////////////////////////////////////////////////////////////////////
65/// Assignment operator.
66
68{
69 if (this != &rhs) {
71 fClass = rhs.fClass;
72 fMenuItem = rhs.fMenuItem;
73 fGetter = rhs.fGetter;
74 if (fGetterMethod)
75 delete fGetterMethod;
76 fGetterMethod = nullptr;
77 if (fSetterMethod)
78 delete fSetterMethod;
79 fSetterMethod = nullptr;
80 }
81 return *this;
82}
83
84////////////////////////////////////////////////////////////////////////////////
85/// Cleanup.
86
88{
89 delete fGetterMethod;
90 delete fSetterMethod;
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Clone method.
95
96TObject *TMethod::Clone(const char *newname) const
97{
98 TNamed *newobj = new TMethod(*this);
99 if (newname && strlen(newname)) newobj->SetName(newname);
100 return newobj;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Returns a comment string from the class declaration.
105
107{
108 return fInfo ? gCling->MethodInfo_Title(fInfo) : "";
109}
110
111
112////////////////////////////////////////////////////////////////////////////////
113/// Using the CINT method arg information create a complete signature string.
114
116{
118
119 if (Property() & kIsConstMethod) fSignature += " const";
120}
121
122////////////////////////////////////////////////////////////////////////////////
123/// Tries to guess DataMember from comment string
124/// and Method's name <==(only if 1 Argument!).
125/// If more then one argument=> returns pointer to the last argument.
126/// It also sets MethodArgs' pointers to point to specified data members.
127///
128/// The form of comment string defining arguments is:
129/// void XXX(Int_t x1, Float_t y2) //*ARGS={x1=>fX1,y2=>fY2}
130/// where fX1, fY2 are data fields in the same class.
131/// ("pointers" to data members)
132
134{
135 Char_t *argstring = (char*)strstr(GetCommentString(),"*ARGS={");
136
137 // the following statement has been commented (Rene). Not needed
138 // it was making troubles in BuildRealData for classes with protected
139 // default constructors.
140 // if (!(GetClass()->GetListOfRealData())) GetClass()->BuildRealData();
141
142 if (argstring) {
143
144 // if we found any argument-specifying hints - parse it
145
146 if (!fMethodArgs) return nullptr;
147
148 Int_t nchs = strlen(argstring); // workspace...
149 char *argstr = new char[nchs+1]; // workspace...
150 char *ptr1 = nullptr;
151 char *tok = nullptr;
152 char *ptr2 = nullptr;
153 Int_t i;
154
155 strlcpy(argstr,argstring,nchs+1); //let's move it to "workspace" copy
156 char *rest;
157 ptr2 = R__STRTOK_R(argstr, "{}", &rest); // extract the data!
158 if (ptr2 == nullptr) {
159 Fatal("FindDataMember","Internal error found '*ARGS=\"' but not \"{}\" in %s",GetCommentString());
160 delete [] argstr;
161 return nullptr;
162 }
163 ptr2 = R__STRTOK_R((char *)nullptr, "{}", &rest);
164
165 //extract argument tokens//
166 char *tokens[20];
167 Int_t cnt = 0;
168 Int_t token_cnt = 0;
169 do {
170 ptr1 = R__STRTOK_R((char *)(cnt++ ? nullptr : ptr2), ",;", &rest); // extract tokens
171 // separated by , or ;
172 if (ptr1) {
173 Int_t nch = strlen(ptr1);
174 tok = new char[nch+1];
175 strlcpy(tok,ptr1,nch+1);
176 tokens[token_cnt] = tok; //store this token.
177 token_cnt++;
178 }
179 } while (ptr1);
180
181 //now let's parse all argument tokens...
182 TClass *cl = nullptr;
183 TMethodArg *a = nullptr;
184 TMethodArg *ar = nullptr;
185 TDataMember *member = nullptr;
186
187 for (i=0; i<token_cnt;i++) {
188 ptr1 = R__STRTOK_R(tokens[i], "=>", &rest); // LeftHandedSide=methodarg
189 ptr2 = R__STRTOK_R((char *) nullptr, "=>", &rest); // RightHandedSide-points to datamember
190
191 //find the MethodArg
192 a = nullptr;
193 ar = nullptr;
194 member = nullptr;
195 TIter nextarg(fMethodArgs); // iterate through all arguments.
196 while ((ar = (TMethodArg*)nextarg())) {
197 if (!strcmp(ptr1,ar->GetName())) {
198 a = ar;
199 break;
200 }
201 }
202
203 //now find the data member
204 cl = GetClass()->GetBaseDataMember(ptr2);
205 if (cl) {
206 member = cl->GetDataMember(ptr2);
207 if (a) a->fDataMember = member; //SET THE APROPRIATE FIELD !!!
208 //We can do it - friend decl. in MethodArg
209 }
210 delete [] tokens[i];
211 }
212 delete [] argstr;
213 return member; // nothing else to do! We return a pointer to the last
214 // found data member
215
216 // if not found in comment string - try to guess it from name!
217 } else {
218 if (fMethodArgs)
219 if (fMethodArgs->GetSize() != 1) return nullptr;
220
221 TMethodArg *a = nullptr;
222 if (fMethodArgs) a = (TMethodArg*)(fMethodArgs->First());
223
224 char dataname[67] = "";
225 char basename[64] = "";
226 const char *funcname = GetName();
227 if ( strncmp(funcname,"Get",3) == 0 || strncmp(funcname,"Set",3) == 0 )
228 snprintf(basename,64,"%s",funcname+3);
229 else if ( strncmp(funcname,"Is",2) == 0 )
230 snprintf(basename,64,"%s",funcname+2);
231 else if (strncmp(funcname, "Has", 3) == 0)
232 snprintf(basename,64,"%s", funcname+3);
233 else
234 return nullptr;
235
236 snprintf(dataname,67,"f%s",basename);
237
238 TClass *cl = GetClass()->GetBaseDataMember(dataname);
239 if (cl) {
240 TDataMember *member = cl->GetDataMember(dataname);
241 if (a) a->fDataMember = member;
242 return member;
243 } else {
244 snprintf(dataname,67,"fIs%s",basename); //in case of IsEditable()
245 //and fIsEditable
246 cl = GetClass()->GetBaseDataMember(dataname);
247 if (cl) {
248 TDataMember *member = cl->GetDataMember(dataname);
249 if (a) a->fDataMember = member;
250 return member;
251 }
252 }
253 }
254
255 //if nothing found - return null -pointer:
256 return nullptr;
257}
258
259////////////////////////////////////////////////////////////////////////////////
260/// Return call environment for the getter method in case this is a
261/// *TOGGLE method (for the context menu).
262
264{
265 if (!fGetterMethod && fMenuItem == kMenuToggle && fGetter != "" && fClass) {
267 }
268 return fGetterMethod;
269}
270
271////////////////////////////////////////////////////////////////////////////////
272/// Return true if this function object is pointing to a currently
273/// loaded function. If a function is unloaded after the TMethod
274/// is created, the TMethod will be set to be invalid.
275
277{
278 // Register the transaction when checking the validity of the object.
280 DeclId_t newId = gInterpreter->GetFunction(fClass->GetClassInfo(), fName);
281 if (newId) {
282 MethodInfo_t *info = gInterpreter->MethodInfo_Factory(newId);
283 Update(info);
284 }
285 return newId != nullptr;
286 }
287 return fInfo != nullptr;
288}
289
290////////////////////////////////////////////////////////////////////////////////
291/// Return call environment for this method in case this is a
292/// *TOGGLE method which takes a single boolean or integer argument.
293
301
302////////////////////////////////////////////////////////////////////////////////
303/// Returns methodarg list and additionally updates fDataMember in TMethod by
304/// calling FindDataMember();
305
314
315////////////////////////////////////////////////////////////////////////////////
316/// Set the menu item as prescribed in the doctstring.
317
318void TMethod::SetMenuItem(const char *docstring)
319{
320 if (docstring && strstr(docstring, "*TOGGLE")) {
322 const char *s;
323 if ((s = strstr(docstring, "*GETTER="))) {
324 fGetter = s+8;
326 }
327 } else
328 if (docstring && strstr(docstring, "*MENU"))
330 else
331 if (docstring && strstr(docstring, "*SUBMENU"))
333 else
335}
336
337////////////////////////////////////////////////////////////////////////////////
338/// Update the TMethod to reflect the new info.
339///
340/// This can be used to implement unloading (info == 0) and then reloading
341/// (info being the 'new' decl address).
342
343Bool_t TMethod::Update(MethodInfo_t *info)
344{
345 if (TFunction::Update(info)) {
346 delete fGetterMethod; fGetterMethod = nullptr;
347 delete fSetterMethod; fSetterMethod = nullptr;
348 if (fInfo) {
349 SetMenuItem(gCling->MethodInfo_Title(fInfo));
350 }
351 return kTRUE;
352 } else {
353 return kFALSE;
354 }
355}
#define a(i)
Definition RSha256.hxx:99
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
char Char_t
Character 1 byte (char).
Definition RtypesCore.h:51
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
@ kIsConstMethod
Definition TDictionary.h:96
#define gInterpreter
externTInterpreter * gCling
@ kMenuSubMenu
Definition TMethod.h:35
@ kMenuDialog
Definition TMethod.h:33
@ kMenuToggle
Definition TMethod.h:34
@ kMenuNoMenu
Definition TMethod.h:32
#define snprintf
Definition civetweb.c:1579
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
TDataMember * GetDataMember(const char *datamember) const
Return pointer to datamember object with name "datamember".
Definition TClass.cxx:3501
TClass * GetBaseDataMember(const char *datamember)
Return pointer to (base) class that contains datamember.
Definition TClass.cxx:2848
All ROOT classes may have RTTI (run time type identification) support added.
Definition TDataMember.h:31
Bool_t UpdateInterpreterStateMarker()
const void * DeclId_t
friend class TMethodCall
Definition TFunction.h:33
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.
TFunction(MethodInfo_t *info=nullptr)
Default TFunction ctor.
Definition TFunction.cxx:34
Long_t Property() const override
Get property description word. For meaning of bits see EProperty.
MethodInfo_t * fInfo
Definition TFunction.h:36
virtual bool Update(MethodInfo_t *info)
Update the TFunction to reflect the new info.
TList * GetListOfMethodArgs()
Return list containing the TMethodArgs of a TFunction.
TFunction & operator=(const TFunction &rhs)
Assignment operator.
Definition TFunction.cxx:65
A doubly linked list.
Definition TList.h:38
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition TMethodArg.h:36
TMethodCall * fSetterMethod
Definition TMethod.h:45
virtual const char * GetCommentString()
Returns a comment string from the class declaration.
Definition TMethod.cxx:106
TString fGetter
Definition TMethod.h:43
TMethod & operator=(const TMethod &rhs)
Assignment operator.
Definition TMethod.cxx:67
virtual const char * Getter() const
Definition TMethod.h:59
TClass * fClass
Definition TMethod.h:41
virtual TDataMember * FindDataMember()
Tries to guess DataMember from comment string and Method's name <==(only if 1 Argument!...
Definition TMethod.cxx:133
TClass * GetClass() const
Definition TMethod.h:55
Bool_t Update(MethodInfo_t *info) override
Update the TMethod to reflect the new info.
Definition TMethod.cxx:343
void SetMenuItem(const char *docstring)
Set the menu item as prescribed in the doctstring.
Definition TMethod.cxx:318
virtual ~TMethod()
Cleanup.
Definition TMethod.cxx:87
EMenuItemKind fMenuItem
Definition TMethod.h:42
virtual TMethodCall * SetterMethod()
Return call environment for this method in case this is a *TOGGLE method which takes a single boolean...
Definition TMethod.cxx:294
TMethodCall * fGetterMethod
Definition TMethod.h:44
Bool_t IsValid() override
Return true if this function object is pointing to a currently loaded function.
Definition TMethod.cxx:276
virtual TList * GetListOfMethodArgs()
Returns methodarg list and additionally updates fDataMember in TMethod by calling FindDataMember();.
Definition TMethod.cxx:306
void CreateSignature() override
Using the CINT method arg information create a complete signature string.
Definition TMethod.cxx:115
TMethod(MethodInfo_t *info=nullptr, TClass *cl=nullptr)
Default TMethod ctor.
Definition TMethod.cxx:40
virtual TMethodCall * GetterMethod()
Return call environment for the getter method in case this is a *TOGGLE method (for the context menu)...
Definition TMethod.cxx:263
TObject * Clone(const char *newname="") const override
Clone method.
Definition TMethod.cxx:96
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
TNamed()
Definition TNamed.h:38
TString fName
Definition TNamed.h:32
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:149
Mother of all ROOT objects.
Definition TObject.h:42
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1126
@ kBoth
Definition TString.h:284