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