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
41{
42 fClass = cl;
43 fGetterMethod = nullptr;
44 fSetterMethod = nullptr;
46
47 if (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
205 if (cl) {
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;
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
239 if (cl) {
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
247 if (cl) {
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.
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
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
344{
345 if (TFunction::Update(info)) {
346 delete fGetterMethod; fGetterMethod = nullptr;
347 delete fSetterMethod; fSetterMethod = nullptr;
348 if (fInfo) {
350 }
351 return kTRUE;
352 } else {
353 return kFALSE;
354 }
355}
#define a(i)
Definition RSha256.hxx:99
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:51
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
@ kIsConstMethod
Definition TDictionary.h:96
R__EXTERN TInterpreter * gCling
#define gInterpreter
@ 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:3470
ClassInfo_t * GetClassInfo() const
Definition TClass.h:445
TClass * GetBaseDataMember(const char *datamember)
Return pointer to (base) class that contains datamember.
Definition TClass.cxx:2832
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
All ROOT classes may have RTTI (run time type identification) support added.
Definition TDataMember.h:31
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
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.
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
virtual const char * MethodInfo_Title(MethodInfo_t *) const
A doubly linked list.
Definition TList.h:38
TObject * First() const override
Return the first object in the list. Returns 0 when list is empty.
Definition TList.cxx:656
Each ROOT method (see TMethod) has a linked list of its arguments.
Definition TMethodArg.h:36
Method or function calling interface.
Definition TMethodCall.h:37
Each ROOT class (see TClass) has a linked list of methods.
Definition TMethod.h:38
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
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
TString fName
Definition TNamed.h:32
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1099
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Definition TString.cxx:1170
@ kBoth
Definition TString.h:284
static byte * ptr1
Definition gifdecode.c:16
static byte * ptr2
Definition gifdecode.c:17