Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TDataMember.cxx
Go to the documentation of this file.
1// @(#)root/meta:$Id$
2// Author: Fons Rademakers 04/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 TDataMember
13
14All ROOT classes may have RTTI (run time type identification) support
15added. The data is stored in so called DICTIONARY (look at TDictionary).
16Information about a class is stored in TClass.
17This information may be obtained via the cling api - see class TCling.
18TClass has a list of TDataMember objects providing information about all
19data members of described class.
20
21\image html base_classinfo.png
22
23TDataMember provides information about name of data member, its type,
24and comment field string. It also tries to find the TMethodCall objects
25responsible for getting/setting a value of it, and gives you pointers
26to these methods. This gives you a unique possibility to access
27protected and private (!) data members if only methods for doing that
28are defined.
29
30These methods could either be specified in a comment field, or found
31out automatically by ROOT: here's an example:
32suppose you have a class definition:
33~~~ {.cpp}
34 class MyClass{
35 private:
36 Float_t fX1;
37 ...
38 public:
39 void SetX1(Float_t x) {fX1 = x;};
40 Float_t GetX1() {return fX1;};
41 ...
42 }
43~~~
44Look at the data member name and method names: a data member name has
45a prefix letter (f) and has a base name X1 . The methods for getting and
46setting this value have names which consist of string Get/Set and the
47same base name. This convention of naming data fields and methods which
48access them allows TDataMember find this methods by itself completely
49automatically. To make this description complete, one should know,
50that names that are automatically recognized may be also:
51for data fields: either fXXX or fIsXXX; and for getter function
52GetXXX() or IsXXX() [where XXX is base name].
53
54As an example of using it let's analyse a few lines which get and set
55a fEditable field in TCanvas:
56~~~ {.cpp}
57 TCanvas *c = new TCanvas("c"); // create a canvas
58 TClass *cl = c->IsA(); // get its class description object.
59
60 TDataMember *dm = cl->GetDataMember("fEditable"); //This is our data member
61
62 TMethodCall *getter = dm->GetterMethod(c); //find a method that gets value!
63 Long_t l; // declare a storage for this value;
64
65 getter->Execute(c,"",l); // Get this Value !!!! It will appear in l !!!
66
67
68 TMethodCall *setter = dm->SetterMethod(c);
69 setter->Execute(c,"0",); // Set Value 0 !!!
70~~~
71
72This trick is widely used in ROOT TContextMenu and dialogs for obtaining
73current values and put them as initial values in dialog fields.
74
75If you don't want to follow the convention of naming used by ROOT
76you still could benefit from Getter/Setter method support: the solution
77is to instruct ROOT what the names of these routines are.
78The way to do it is putting this information in a comment string to a data
79field in your class declaration:
80
81~~~ {.cpp}
82 class MyClass{
83 Int_t mydata; // *OPTIONS={GetMethod="Get";SetMethod="Set"}
84 ...
85 Int_t Get() const { return mydata;};
86 void Set(Int_t i) {mydata=i;};
87 }
88~~~
89
90However, this getting/setting functions are not the only feature of
91this class. The next point is providing lists of possible settings
92for the concerned data member. The idea is to have a list of possible
93options for this data member, with strings identifying them. This
94is used in dialogs with parameters to set - for details see
95TMethodArg, TRootContextMenu, TContextMenu. This list not only specifies
96the allowed value, but also provides strings naming the options.
97Options are managed via TList of TOptionListItem objects. This list
98is also created automatically: if a data type is an enum type,
99the list will have items describing every enum value, and named
100according to enum name. If type is Bool_t, two options "On" and "Off"
101with values 0 and 1 are created. For other types you need to instruct
102ROOT about possible options. The way to do it is the same as in case of
103specifying getter/setter method: a comment string to a data field in
104Your header file with class definition.
105The most general format of this string is:
106~~~ {.cpp}
107*OPTIONS={GetMethod="getter";SetMethod="setter";Items=(it1="title1",it2="title2", ... ) }
108~~~
109
110While parsing this string ROOT firstly looks for command-tokens:
111GetMethod, SetMethod, Items; They must be preceded by string
112*OPTIONS= , enclosed by {} and separated by semicolons ";".
113All command token should have a form TOKEN=VALUE.
114All tokens are optional.
115The names of getter and setter method must be enclosed by double-quote
116marks (") .
117Specifications of Items is slightly more complicated: you need to
118put token ITEMS= and then enclose all options in curly brackets "()".
119You separate options by comas ",".
120Each option item may have one of the following forms:
121~~~ {.cpp}
122 IntegerValue = "Text Label"
123
124 EnumValue = "Text Label"
125
126 "TextValue" = Text Label"
127
128~~~ {.cpp}
129
130One can specify values as Integers or Enums - when data field is an
131Integer, Float or Enum type; as texts - for char (more precisely:
132Option_t).
133
134As mentioned above - this information are mainly used by contextmenu,
135but also in Dump() and Inspect() methods and by the THtml class.
136*/
137
138#include "TDataMember.h"
139
140#include "strtok.h"
141#include "strlcpy.h"
142#include "TBuffer.h"
143#include "TClass.h"
144#include "TClassEdit.h"
145#include "TDataType.h"
146#include "TEnum.h"
147#include "TEnumConstant.h"
148#include "TGlobal.h"
149#include "TInterpreter.h"
150#include "TIterator.h"
151#include "TList.h"
152#include "TListOfDataMembers.h"
153#include "TMethod.h"
154#include "TMethodArg.h"
155#include "TMethodCall.h"
156#include "TRealData.h"
157#include "TROOT.h"
158#include "TVirtualMutex.h"
159
160#include <cassert>
161#include <cctype>
162#include <cstdlib>
163#include <cstring>
164
165
166////////////////////////////////////////////////////////////////////////////////
167/// Default TDataMember ctor. TDataMembers are constructed in TClass
168/// via a call to TCling::CreateListOfDataMembers(). It parses the comment
169/// string, initializes optionlist and getter/setter methods.
170
171TDataMember::TDataMember(DataMemberInfo_t *info, TClass *cl) : TDictionary()
172{
173 fInfo = info;
174 fClass = cl;
175 fDataType = nullptr;
176 fOptions = nullptr;
177 fValueSetter = nullptr;
178 fValueGetter = nullptr;
179 fOffset = -1;
180 fProperty = -1;
181 fSTLCont = -1;
182 fArrayDim = -1;
183 fArrayMaxIndex=nullptr;
184 if (!fInfo && !fClass) return; // default ctor is called
185
186 Init(false);
187}
188
189////////////////////////////////////////////////////////////////////////////////
190/// Routines called by the constructor and Update to reset the member's
191/// information.
192/// afterReading is set when initializing after reading through Streamer().
193
194void TDataMember::Init(bool afterReading)
195{
196 if (!afterReading) {
197 // Initialize from fInfo
198 if (!fInfo || !gInterpreter->DataMemberInfo_IsValid(fInfo))
199 return;
200 // Also sets names.
201 Property();
202 }
203 const char *t = GetTitle();
204 if (t && t[0] != '!')
206 fDataType = nullptr;
207 if (IsBasic() || IsEnum()) {
208 if (IsBasic()) {
209 const char *name = GetFullTypeName();
210 if (strcmp(name, "unsigned char") != 0 &&
211 strncmp(name, "unsigned short", sizeof ("unsigned short")) != 0 &&
212 strcmp(name, "unsigned int") != 0 &&
213 strncmp(name, "unsigned long", sizeof ("unsigned long")) != 0)
214 // strncmp() also covers "unsigned long long"
215 name = GetTypeName();
217
218 if (fDataType==nullptr) {
219 // humm we did not find it ... maybe it's a typedef that has not been loaded yet.
220 // (this can happen if the executable does not have a TApplication object).
222 }
223 } else {
224 auto enumdesc = TEnum::GetEnum(GetFullTypeName(), TEnum::kNone);
225 if (enumdesc)
226 fDataType = TDataType::GetDataType(enumdesc->GetUnderlyingType());
227 else
228 fDataType = gROOT->GetType("Int_t", kTRUE); // In rare instance we are called before Int_t has been added to
229 // the list of types in TROOT, the kTRUE insures it is there.
230 }
231 // if (!fDataType)
232 // Error("TDataMember", "basic data type %s not found in list of basic types",
233 // GetTypeName());
234 }
235
236
237 if (afterReading) {
238 // Options are streamed; can't build TMethodCall for getters and setters
239 // because we deserialize a TDataMember when we do not have interpreter
240 // data. Thus do an early return.
241 return;
242 }
243
244 if (strstr(GetTitle(), "*OPTION={")) {
245 // Delay setting fOptions until it's used: the enum constants might
246 // not have been added as members yet.
247
248 // if option string does not exist but it's an Enum - parse it!!!!
249 } else if (IsEnum()) {
250 fOptions = new TList();
251 if (TEnum* enumDict = TEnum::GetEnum(GetTypeName()) ){
252 TIter iEnumConst(enumDict->GetConstants());
253 while (TEnumConstant* enumConst = (TEnumConstant*)iEnumConst()) {
255 = new TOptionListItem(this, enumConst->GetValue(),0,0,
256 enumConst->GetName(),enumConst->GetName());
257 fOptions->Add(it);
258 }
259 }
260
261 // and the case od Bool_t : we add items "ON" and "Off"
262 } else if (!strncmp(GetFullTypeName(),"Bool_t",6)){
263
264 fOptions = new TList();
265 TOptionListItem *it = new TOptionListItem(this,1,0,0,"ON",nullptr);
266 fOptions->Add(it);
267 it = new TOptionListItem(this,0,0,0,"Off",nullptr);
268 fOptions->Add(it);
269
270 } else fOptions = nullptr;
271
272}
273
274////////////////////////////////////////////////////////////////////////////////
275/// copy constructor
276
278 TDictionary(dm),
279 fInfo(gCling->DataMemberInfo_FactoryCopy(dm.fInfo)),
280 fClass(dm.fClass),
281 fDataType(dm.fDataType),
282 fOffset(dm.fOffset),
283 fSTLCont(dm.fSTLCont),
284 fProperty(dm.fProperty),
285 fArrayDim(dm.fArrayDim),
286 fArrayMaxIndex( dm.fArrayDim ? new Int_t[dm.fArrayDim] : nullptr),
287 fArrayIndex(dm.fArrayIndex),
288 fTypeName(dm.fTypeName),
289 fFullTypeName(dm.fFullTypeName),
290 fTrueTypeName(dm.fTrueTypeName),
291 fValueGetter(nullptr),
292 fValueSetter(nullptr),
293 fOptions(dm.fOptions ? (TList*)dm.fOptions->Clone() : nullptr)
294{
295 for(Int_t d = 0; d < fArrayDim; ++d)
297}
298
299////////////////////////////////////////////////////////////////////////////////
300/// assignment operator
301
303{
304 if(this!=&dm) {
306 delete fValueSetter; fValueSetter = nullptr;
307 delete fValueGetter; fValueGetter = nullptr;
308 if (fOptions) {
309 fOptions->Delete();
310 delete fOptions;
311 fOptions = nullptr;
312 }
313
316 fClass=dm.fClass;
318 fOffset=dm.fOffset;
321 fArrayDim = dm.fArrayDim;
322 delete [] fArrayMaxIndex;
323 fArrayMaxIndex = dm.fArrayDim ? new Int_t[dm.fArrayDim] : nullptr;
324 for(Int_t d = 0; d < fArrayDim; ++d)
330 fOptions = dm.fOptions ? (TList*)dm.fOptions->Clone() : nullptr;
331 }
332 return *this;
333}
334
335////////////////////////////////////////////////////////////////////////////////
336/// TDataMember dtor deletes adopted CINT DataMemberInfo object.
337
339{
340 delete [] fArrayMaxIndex;
342 delete fValueSetter;
343 delete fValueGetter;
344 if (fOptions) {
345 fOptions->Delete();
346 delete fOptions;
347 }
348}
349
350////////////////////////////////////////////////////////////////////////////////
351/// Return number of array dimensions.
352
354{
355 if (fArrayDim<0 && fInfo) {
357 TDataMember *dm = const_cast<TDataMember*>(this);
359 // fArrayMaxIndex should be zero
360 if (dm->fArrayDim) {
361 dm->fArrayMaxIndex = new Int_t[fArrayDim];
362 for(Int_t dim = 0; dim < dm->fArrayDim; ++dim) {
364 }
365 }
366 }
367 return fArrayDim;
368}
369
370////////////////////////////////////////////////////////////////////////////////
371/// If the data member is pointer and has a valid array size in its comments
372/// GetArrayIndex returns a string pointing to it;
373/// otherwise it returns an empty string.
374
375const char *TDataMember::GetArrayIndex() const
376{
377 if (!IsaPointer()) return "";
378 if (fArrayIndex.Length()==0 && fInfo) {
380 TDataMember *dm = const_cast<TDataMember*>(this);
381 const char* val = gCling->DataMemberInfo_ValidArrayIndex(fInfo);
382 if (val) dm->fArrayIndex = val;
383 else dm->fArrayIndex.Append((Char_t)0); // Make length non-zero but string still empty.
384 }
385 return fArrayIndex;
386}
387
388////////////////////////////////////////////////////////////////////////////////
389
391{
392 if (fInfo) return gInterpreter->GetDeclId(fInfo);
393 else return nullptr;
394}
395
396////////////////////////////////////////////////////////////////////////////////
397/// Return maximum index for array dimension "dim".
398
400{
401 if (fArrayDim<0 && fInfo) {
403 } else {
404 if (dim < 0 || dim >= fArrayDim) return -1;
405 return fArrayMaxIndex[dim];
406 }
407}
408
409////////////////////////////////////////////////////////////////////////////////
410/// Get the decayed type name of this data member, removing `const` and `volatile` qualifiers, and pointers `*` and
411/// references `&`. This function resolves typedefs in the type name. E.g., let `Foo_t` be a typedef to `class
412/// TDirectory`; assuming `this` is a member of type `const Foo_t*`, this function will return `TDirectory`.
413
414const char *TDataMember::GetTypeName() const
415{
416 if (fProperty==(-1)) Property();
417 return fTypeName.Data();
418}
419
420////////////////////////////////////////////////////////////////////////////////
421/// Get the concrete type name of this data member, including `const` and `volatile` qualifiers. This function does not
422/// resolve any typedef in the type name. E.g., let `Foo_t` be a typedef to `class TDirectory`; assuming `this` is a
423/// member of type `const Foo_t*`, this function will return `const Foo_t*`.
424///
425/// For the desugared type name, see TDataMember::GetTrueTypeName().
426
428{
429 if (fProperty==(-1)) Property();
430
431 return fFullTypeName.Data();
432}
433
434////////////////////////////////////////////////////////////////////////////////
435/// Get the desugared type name of this data member, including `const` and `volatile` qualifiers. This function
436/// resolves typedefs in the type name. E.g., let `Foo_t` be a typedef to `class TDirectory`; assuming `this` is a
437/// member of type `const Foo_t*`, this function will return `const TDirectory*`.
438
440{
441 return fTrueTypeName.Data();
442}
443
444////////////////////////////////////////////////////////////////////////////////
445/// Get offset from "this".
446
448{
449 if (fOffset>=0) return fOffset;
450
452 //case of an interpreted or emulated class
453 if (fClass->GetDeclFileLine() < 0) {
454 ((TDataMember*)this)->fOffset = gCling->DataMemberInfo_Offset(fInfo);
455 return fOffset;
456 }
457 //case of a compiled class
458 //Note that the offset cannot be computed in case of an abstract class
459 //for which the list of real data has not yet been computed via
460 //a real daughter class.
461 TString dmbracket;
462 dmbracket.Form("%s[",GetName());
465 TRealData *rdm;
466 Int_t offset = 0;
467 while ((rdm = (TRealData*)next())) {
468 char *rdmc = (char*)rdm->GetName();
469 //next statement required in case a class and one of its parent class
470 //have data members with the same name
471 if (this->IsaPointer() && rdmc[0] == '*') rdmc++;
472
473 if (rdm->GetDataMember() != this) continue;
474 if (strcmp(rdmc,GetName()) == 0) {
475 offset = rdm->GetThisOffset();
476 break;
477 }
478 if (strcmp(rdm->GetName(),GetName()) == 0) {
479 if (rdm->IsObject()) {
480 offset = rdm->GetThisOffset();
481 break;
482 }
483 }
484 if (strstr(rdm->GetName(),dmbracket.Data())) {
485 offset = rdm->GetThisOffset();
486 break;
487 }
488 }
489 ((TDataMember*)this)->fOffset = offset;
490 return fOffset;
491}
492
493////////////////////////////////////////////////////////////////////////////////
494/// Get offset from "this" using the information in CINT only.
495
497{
498 if (fOffset>=0) return fOffset;
499
501 TDataMember *dm = const_cast<TDataMember*>(this);
502
503 if (dm->IsValid()) return gCling->DataMemberInfo_Offset(dm->fInfo);
504 else return -1;
505}
506
507////////////////////////////////////////////////////////////////////////////////
508/// Get the sizeof the underlying type of the data member
509/// (i.e. if the member is an array sizeof(member)/length)
510
512{
513 if (IsaPointer()) return sizeof(void*);
514 if (IsEnum()) {
515 auto e = TEnum::GetEnum(GetTypeName());
516 if (e)
517 return TDataType::GetDataType(e->GetUnderlyingType())->Size();
518 else
519 return sizeof(Int_t);
520 }
521 if (IsBasic())
522 return GetDataType()->Size();
523
525 if (!cl) cl = TClass::GetClass(GetTrueTypeName());
526 if ( cl) return cl->Size();
527
528 Warning("GetUnitSize","Can not determine sizeof(%s)",GetTypeName());
529 return 0;
530}
531
532////////////////////////////////////////////////////////////////////////////////
533/// Return true if data member is a basic type, e.g. char, int, long...
534
536{
537 if (fProperty == -1) Property();
538 return (fProperty & kIsFundamental) ? kTRUE : kFALSE;
539}
540
541////////////////////////////////////////////////////////////////////////////////
542/// Return true if data member is an enum.
543
545{
546 if (fProperty == -1) Property();
547 return (fProperty & kIsEnum) ? kTRUE : kFALSE;
548}
549
550////////////////////////////////////////////////////////////////////////////////
551/// Return true if data member is a pointer.
552
554{
555 if (fProperty == -1) Property();
556 return (fProperty & kIsPointer) ? kTRUE : kFALSE;
557}
558
559////////////////////////////////////////////////////////////////////////////////
560/// The return type is defined in TDictionary (kVector, kList, etc.)
561
569
570////////////////////////////////////////////////////////////////////////////////
571/// Return true if this data member object is pointing to a currently
572/// loaded data member. If a function is unloaded after the TDataMember
573/// is created, the TDataMember will be set to be invalid.
574
576{
577 if (fOffset >= 0) return kTRUE;
578
579 // Register the transaction when checking the validity of the object.
581 DeclId_t newId = gInterpreter->GetDataMember(fClass->GetClassInfo(), fName);
582 if (newId) {
583 DataMemberInfo_t *info
584 = gInterpreter->DataMemberInfo_Factory(newId, fClass->GetClassInfo());
585 Update(info);
586 // We need to make sure that the list of data member is properly
587 // informed and updated.
589 lst->Update(this);
590 }
591 return newId != nullptr;
592 }
593 return fInfo != nullptr;
594}
595
596////////////////////////////////////////////////////////////////////////////////
597/// Get property description word. For meaning of bits see EProperty.
598
600{
601 if (fProperty!=(-1)) return fProperty;
602
604 TDataMember *t = (TDataMember*)this;
605
606 if (!fInfo || !gCling->DataMemberInfo_IsValid(fInfo)) return 0;
609 t->fProperty = (prop | propt) & ~(kIsPublic | kIsProtected | kIsPrivate);
610 // Set to the strictest access of the member and the type
611 if ((prop | propt) & kIsPrivate)
612 t->fProperty |= kIsPrivate;
613 else if ((prop | propt) & kIsProtected)
615 else
616 t->fProperty |= kIsPublic;
617
621
624
625 return fProperty;
626}
627
628
629////////////////////////////////////////////////////////////////////////////////
630/// Build TOptionListItems from the member comment `*OPTION={`
631
633{
634 if (fOptions)
635 return;
636
637 const char *optTitle = strstr(GetTitle(), "*OPTION={");
638 if (!optTitle)
639 return;
640
641 // If option string exist in comment - we'll parse it and create
642 // list of options
643
644 // Option-list string has a form:
645 // *OPTION={GetMethod="GetXXX";SetMethod="SetXXX";
646 // Items=(0="NULL ITEM","one"="First Item",kRed="Red Item")}
647 //
648 // As one can see it is possible to specify value as either numerical
649 // value , string or enum.
650 // One can also specify implicitly names of Getter/Setter methods.
651
652 char cmt[2048];
653 char opt[2048];
654 const char *ptr1 = nullptr;
655 char *ptr2 = nullptr;
656 char *ptr3 = nullptr;
657 Int_t cnt = 0;
658 Int_t token_cnt;
659 Int_t i;
660
661 strlcpy(cmt,GetTitle(),2048);
662
663 char *opt_ptr = strstr(cmt, "*OPTION={");
664
665 // If we found it - parsing...
666
667 //let's cut the part lying between {}
668 char *rest;
669 ptr1 = R__STRTOK_R(opt_ptr, "{}", &rest); // starts tokenizing:extracts "*OPTION={"
670 if (ptr1 == nullptr) {
671 Fatal("TDataMember","Internal error, found \"*OPTION={\" but not \"{}\" in %s.",GetTitle());
672 return;
673 }
674 ptr1 = R__STRTOK_R(nullptr, "{}", &rest); // And now we have what we need in ptr1!!!
675 if (ptr1 == nullptr) {
676 Fatal("TDataMember","Internal error, found \"*OPTION={\" but not \"{}\" in %s.",GetTitle());
677 return;
678 }
679
680 //and save it:
681 strlcpy(opt,ptr1,2048);
682
683 // Let's extract sub-tokens extracted by ';' sign.
684 // We'll put'em in an array for convenience;
685 // You have to do it in this manner because you cannot use nested tokenizing
686
687 std::vector<std::string> tokens; // a storage for these sub-tokens.
688 token_cnt = 0;
689 cnt = 0;
690
691 do { //tokenizing loop
692 ptr1 = R__STRTOK_R((char *)(cnt++ ? nullptr : opt), ";", &rest);
693 if (ptr1) {
694 tokens.emplace_back(ptr1);
695 token_cnt++;
696 }
697 } while (ptr1);
698
699 // OK! Now let's check whether we have Get/Set methods encode in any string
700 for (i=0;i<token_cnt;i++) {
701 if (strstr(tokens[i].c_str(),"GetMethod")) {
702 ptr1 = R__STRTOK_R(const_cast<char *>(tokens[i].c_str()), "\"", &rest); // tokenizing-strip text "GetMethod"
703 if (ptr1 == nullptr) {
704 Fatal("TDataMember","Internal error, found \"GetMethod\" but not \"\\\"\" in %s.",GetTitle());
705 return;
706 }
707 ptr1 = R__STRTOK_R(nullptr, "\"", &rest); // tokenizing - name is in ptr1!
708 if (ptr1 == nullptr) {
709 Fatal("TDataMember","Internal error, found \"GetMethod\" but not \"\\\"\" in %s.",GetTitle());
710 return;
711 }
712
713 if (GetClass()->GetMethod(ptr1,"")) // check whether such method exists
714 // FIXME: wrong in case called derives via multiple inheritance from this class
715 fValueGetter = new TMethodCall(GetClass(),ptr1,"");
716
717 continue; //next item!
718 }
719
720 if (strstr(tokens[i].c_str(),"SetMethod")) {
721 ptr1 = R__STRTOK_R(const_cast<char *>(tokens[i].c_str()), "\"", &rest);
722 if (ptr1 == nullptr) {
723 Fatal("TDataMember","Internal error, found \"SetMethod\" but not \"\\\"\" in %s.",GetTitle());
724 return;
725 }
726 ptr1 = R__STRTOK_R(nullptr, "\"", &rest); // name of Setter in ptr1
727 if (ptr1 == nullptr) {
728 Fatal("TDataMember","Internal error, found \"SetMethod\" but not \"\\\"\" in %s.",GetTitle());
729 return;
730 }
731 // setter method may accept enum data type - therefore check if it exists at all
732 if (GetClass()->GetMethod(ptr1,"1"))
733 // FIXME: wrong in case called derives via multiple inheritance from this class
734 fValueSetter = new TMethodCall(GetClass(), ptr1, "1");
735 else if (auto method = IsEnum() ? GetClass()->GetMethodAny(ptr1) : nullptr) {
736 // if data member is enum, take setter and cast 1 to argument data type
737 auto arg = dynamic_cast<TMethodArg *>(method->GetListOfMethodArgs()->First());
738 auto sarg = TString::Format("(%s) 1", arg ? arg->GetFullTypeName() : "int");
739 fValueSetter = new TMethodCall(GetClass(), ptr1, sarg.Data());
740 }
741 }
742 }
743
744 //Now let's parse option strings...
745
746 Int_t opt_cnt = 0;
747 std::unique_ptr<TList> optionlist{new TList()}; //storage for options strings
748
749 for (i=0;i<token_cnt;i++) {
750 if (strstr(tokens[i].c_str(),"Items")) {
751 ptr1 = R__STRTOK_R(const_cast<char *>(tokens[i].c_str()), "()", &rest);
752 if (ptr1 == nullptr) {
753 Fatal("TDataMember","Internal error, found \"Items\" but not \"()\" in %s.",GetTitle());
754 return;
755 }
756 ptr1 = R__STRTOK_R(nullptr, "()", &rest);
757 if (ptr1 == nullptr) {
758 Fatal("TDataMember","Internal error, found \"Items\" but not \"()\" in %s.",GetTitle());
759 return;
760 }
761
762 char opts[2048]; //and save it!
763 strlcpy(opts,ptr1,2048);
764
765 //now parse it...
766 //firstly we just store strings like: xxx="Label Name"
767 //We'll store it in TOptionListItem objects, because they're derived
768 //from TObject and thus can be stored in TList.
769 //It's not elegant but works.
770 do {
771 ptr1 = R__STRTOK_R(opt_cnt++ ? nullptr : opts, ",", &rest); // options extraction
772 if (ptr1) {
773 TOptionListItem *it = new TOptionListItem(this,1,0,0,ptr1,"");
774 optionlist->Add(it);
775 }
776 } while(ptr1);
777
778 }
779 }
780
781 //having all options extracted and put into list, we finally can parse
782 //them to create a list of options...
783
784 auto enumDict = IsEnum() ? TEnum::GetEnum(GetTypeName()) : nullptr;
785
786 fOptions = new TList(); //create the list
787
788 TIter next(optionlist.get()); //we'll iterate through all
789 //strings containing options
790 TOptionListItem *it = nullptr;
791 TOptionListItem *it1 = nullptr;
792 while ((it=(TOptionListItem*)next())) {
793
794 ptr1 = it->fOptName; // We will change the value of OptName ... but it is fine since we delete the object at the end of the loop.
795 Bool_t islabel = (ptr1[0]=='\"'); // value is label or numerical?
796 ptr2 = R__STRTOK_R((char *)ptr1, "=\"", &rest); // extract LeftHandeSide
797 ptr3 = R__STRTOK_R(nullptr, "=\"", &rest); // extract RightHandedSize
798
799 if (islabel) {
800 it1=new TOptionListItem(this,-9999,0,0,ptr3,ptr2);
801 fOptions->Add(it1);
802 } else {
803
804 char *strtolResult;
805 Long_t l = std::strtol(ptr1, &strtolResult, 10);
806 bool isnumber = (strtolResult != ptr1);
807
808 if (!isnumber) {
809 TGlobal *enumval = gROOT->GetGlobal(ptr1, kTRUE);
810 if (enumval) {
811 Int_t *value = (Int_t *)(enumval->GetAddress());
812 // We'll try to find global enum existing in ROOT...
813 l = (Long_t)(*value);
814 } else if (IsEnum()) {
815 if (auto enumConst = enumDict ? (TEnumConstant*) enumDict->GetConstants()->FindObject(ptr1) : nullptr)
816 l = enumConst->GetValue();
817 else
818 l = gInterpreter->Calc(TString::Format("%s;", ptr1));
819 } else {
820 Fatal("TDataMember", "Internal error, couldn't recognize enum/global value %s.", ptr1);
821 }
822 }
823
824 it1 = new TOptionListItem(this,l,0,0,ptr3,ptr1);
825 fOptions->Add(it1);
826 }
827
828 optionlist->Remove(it); //delete this option string from list
829 delete it; // and dispose of it.
830
831 }
832
833}
834
835
836////////////////////////////////////////////////////////////////////////////////
837/// Returns list of options - list of TOptionListItems
838
845
846////////////////////////////////////////////////////////////////////////////////
847/// Return a TMethodCall method responsible for getting the value
848/// of data member. The cl argument specifies the class of the object
849/// which will be used to call this method (in case of multiple
850/// inheritance TMethodCall needs to know this to calculate the proper
851/// offset).
852
854{
855 if (!fValueGetter || cl) {
856
858
859 if (!cl) cl = fClass;
860
861 if (fValueGetter) {
862 TString methodname = fValueGetter->GetMethodName();
863 delete fValueGetter;
864 fValueGetter = new TMethodCall(cl, methodname.Data(), "");
865
866 } else {
867 // try to guess Getter function:
868 // we strip the fist character of name of data field ('f') and then
869 // try to find the name of Getter by applying "Get", "Is" or "Has"
870 // as a prefix
871
872 const char *dataname = GetName();
873
874 TString gettername;
875 gettername.Form( "Get%s", dataname+1);
876 if (GetClass()->GetMethod(gettername, ""))
877 return fValueGetter = new TMethodCall(cl, gettername, "");
878 gettername.Form( "Is%s", dataname+1);
879 if (GetClass()->GetMethod(gettername, ""))
880 return fValueGetter = new TMethodCall(cl, gettername, "");
881 gettername.Form( "Has%s", dataname+1);
882 if (GetClass()->GetMethod(gettername, ""))
883 return fValueGetter = new TMethodCall(cl, gettername, "");
884 }
885 }
886
887 return fValueGetter;
888}
889
890////////////////////////////////////////////////////////////////////////////////
891/// Return a TMethodCall method responsible for setting the value
892/// of data member. The cl argument specifies the class of the object
893/// which will be used to call this method (in case of multiple
894/// inheritance TMethodCall needs to know this to calculate the proper
895/// offset).
896
898{
899 if (!fValueSetter || cl) {
900
902
903 if (!cl) cl = fClass;
904
905 if (fValueSetter) {
906
907 TString methodname = fValueSetter->GetMethodName();
908 TString params = fValueSetter->GetParams();
909 delete fValueSetter;
910 fValueSetter = new TMethodCall(cl, methodname.Data(), params.Data());
911
912 } else {
913
914 // try to guess Setter function:
915 // we strip the fist character of name of data field ('f') and then
916 // try to find the name of Setter by applying "Set" as a prefix
917
918 const char *dataname = GetName();
919
920 TString settername;
921 settername.Form( "Set%s", dataname+1);
922 if (strstr(settername, "Is")) settername.Form( "Set%s", dataname+3);
923 if (GetClass()->GetMethod(settername, "0"))
924 fValueSetter = new TMethodCall(cl, settername, "0");
925 if (!fValueSetter)
926 if (GetClass()->GetMethod(settername, "true"))
927 fValueSetter = new TMethodCall(cl, settername, "true");
928 }
929 }
930
931 return fValueSetter;
932}
933
934////////////////////////////////////////////////////////////////////////////////
935/// Update the TFunction to reflect the new info.
936///
937/// This can be used to implement unloading (info == 0) and then reloading
938/// (info being the 'new' decl address).
939
940Bool_t TDataMember::Update(DataMemberInfo_t *info)
941{
943
947 if (fOptions) {
948 fOptions->Delete();
950 }
951
952 if (info == nullptr) {
953 fOffset = -1;
954 fProperty = -1;
955 fSTLCont = -1;
956 fArrayDim = -1;
957 delete [] fArrayMaxIndex;
958 fArrayMaxIndex=nullptr;
960
961 fInfo = nullptr;
962 return kTRUE;
963 } else {
964 fInfo = info;
965 Init(false);
966 return kTRUE;
967 }
968}
969
970
971////////////////////////////////////////////////////////////////////////////////
972/// Stream an object of TDataMember. Forces calculation of all cached
973/// (and persistent) values.
974
976 if (b.IsReading()) {
977 b.ReadClassBuffer(Class(), this);
978 Init(true /*reading*/);
979 } else {
980 // Writing.
981 if (fProperty & kIsStatic) {
982 // We have a static member and in this case fOffset contains the
983 // actual address in memory of the data, it will be different everytime,
984 // let's not record it.
985 fOffset = -1;
986 } else {
987 GetOffset();
988 }
990 GetArrayDim();
992 Property(); // also calculates fTypeName and friends
993 b.WriteClassBuffer(Class(), this);
994 }
995}
996
997////////////////////////////////////////////////////////////////////////////////
998/// Constructor.
999
1001 Long_t tglmask,const char *name, const char *label)
1002{
1003 fDataMember = d;
1004 fValue = val;
1005 fValueMaskBit = valmask;
1006 fToggleMaskBit = tglmask;
1007 if (name) {
1008 fOptName = name;
1009 }
1010
1011 if (label) {
1012 fOptLabel = label;
1013 }
1014}
interop::TCppScope_t fClass
#define SafeDelete(p)
Definition RConfig.hxx:507
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define e(i)
Definition RSha256.hxx:103
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
long Longptr_t
Integer large enough to hold a pointer (platform-dependent)
Definition RtypesCore.h:90
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:52
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:69
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
@ kIsPublic
Definition TDictionary.h:75
@ kIsPointer
Definition TDictionary.h:78
@ kIsEnum
Definition TDictionary.h:68
@ kIsPrivate
Definition TDictionary.h:77
@ kIsFundamental
Definition TDictionary.h:70
@ kIsStatic
Definition TDictionary.h:80
@ kIsProtected
Definition TDictionary.h:76
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h prop
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:142
R__EXTERN TVirtualMutex * gInterpreterMutex
R__EXTERN TInterpreter * gCling
#define gInterpreter
#define gROOT
Definition TROOT.h:417
#define R__LOCKGUARD(mutex)
Buffer base class used for serializing objects.
Definition TBuffer.h:43
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
void BuildRealData(void *pointer=nullptr, Bool_t isTransient=kFALSE)
Build a full list of persistent data members.
Definition TClass.cxx:2043
TList * GetListOfDataMembers(Bool_t load=kTRUE)
Return list containing the TDataMembers of a class.
Definition TClass.cxx:3833
Short_t GetDeclFileLine() const
Definition TClass.h:444
TList * GetListOfRealData() const
Definition TClass.h:468
Int_t Size() const
Return size of object of this class.
Definition TClass.cxx:5869
ClassInfo_t * GetClassInfo() const
Definition TClass.h:448
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2999
TObject * Clone(const char *newname="") const override
Make a clone of an collection using the Streamer facility.
All ROOT classes may have RTTI (run time type identification) support added.
Definition TDataMember.h:31
virtual ~TDataMember()
TDataMember dtor deletes adopted CINT DataMemberInfo object.
Int_t GetMaxIndex(Int_t dim) const
Return maximum index for array dimension "dim".
TDataMember(const TDataMember &)
copy constructor
Long_t fProperty
Definition TDataMember.h:44
TString fFullTypeName
Definition TDataMember.h:50
TString fTypeName
Definition TDataMember.h:49
void Streamer(TBuffer &) override
Stream an object of TDataMember.
TMethodCall * SetterMethod(TClass *cl)
Return a TMethodCall method responsible for setting the value of data member.
virtual bool Update(DataMemberInfo_t *info)
Update the TFunction to reflect the new info.
TDataMember & operator=(const TDataMember &)
assignment operator
const char * GetTrueTypeName() const
Get the desugared type name of this data member, including const and volatile qualifiers.
Longptr_t GetOffsetCint() const
Get offset from "this" using the information in CINT only.
DataMemberInfo_t * fInfo
!pointer to CINT data member info
Definition TDataMember.h:38
Long_t Property() const override
Get property description word. For meaning of bits see EProperty.
Int_t * fArrayMaxIndex
Definition TDataMember.h:46
TString fTrueTypeName
Definition TDataMember.h:51
TMethodCall * fValueGetter
!method that returns a value;
Definition TDataMember.h:56
Int_t GetArrayDim() const
Return number of array dimensions.
Longptr_t fOffset
Definition TDataMember.h:42
Bool_t IsEnum() const
Return true if data member is an enum.
TList * GetOptions()
Returns list of options - list of TOptionListItems.
void Init(bool afterReading)
Routines called by the constructor and Update to reset the member's information.
Int_t GetUnitSize() const
Get the sizeof the underlying type of the data member (i.e.
Bool_t IsBasic() const
Return true if data member is a basic type, e.g. char, int, long...
Int_t IsSTLContainer()
The return type is defined in TDictionary (kVector, kList, etc.)
Bool_t IsaPointer() const
Return true if data member is a pointer.
Bool_t IsValid()
Return true if this data member object is pointing to a currently loaded data member.
TMethodCall * GetterMethod(TClass *cl=nullptr)
Return a TMethodCall method responsible for getting the value of data member.
TClass * fClass
!pointer to the class
Definition TDataMember.h:39
void ExtractOptionsFromComment()
Build TOptionListItems from the member comment *OPTION={
TDataType * GetDataType() const
Definition TDataMember.h:76
Longptr_t GetOffset() const
Get offset from "this".
const char * GetTypeName() const
Get the decayed type name of this data member, removing const and volatile qualifiers,...
DeclId_t GetDeclId() const
TMethodCall * fValueSetter
!method which sets value;
Definition TDataMember.h:57
TList * fOptions
Definition TDataMember.h:58
const char * GetArrayIndex() const
If the data member is pointer and has a valid array size in its comments GetArrayIndex returns a stri...
Int_t fSTLCont
Definition TDataMember.h:43
TDataType * fDataType
!pointer to data basic type descriptor
Definition TDataMember.h:40
const char * GetFullTypeName() const
Get the concrete type name of this data member, including const and volatile qualifiers.
TString fArrayIndex
Definition TDataMember.h:47
TClass * GetClass() const
Definition TDataMember.h:75
Int_t fArrayDim
Definition TDataMember.h:45
static TClass * Class()
Int_t GetType() const
Definition TDataType.h:71
static TDataType * GetDataType(EDataType type)
Given a EDataType type, get the TDataType* that represents it.
Int_t Size() const
Get size of basic typedef'ed type.
This class defines an abstract interface that must be implemented by all classes that contain diction...
Bool_t UpdateInterpreterStateMarker()
TDictionary & operator=(const TDictionary &other)
const void * DeclId_t
The TEnumConstant class implements the constants of the enum type.
The TEnum class implements the enum type.
Definition TEnum.h:33
static TEnum * GetEnum(const std::type_info &ti, ESearchAction sa=kALoadAndInterpLookup)
Definition TEnum.cxx:181
@ kNone
Definition TEnum.h:55
Global variables class (global variables are obtained from CINT).
Definition TGlobal.h:28
virtual void * GetAddress() const
Return address of global.
Definition TGlobal.cxx:80
virtual DataMemberInfo_t * DataMemberInfo_FactoryCopy(DataMemberInfo_t *) const
virtual const char * DataMemberInfo_Name(DataMemberInfo_t *) const
virtual const char * DataMemberInfo_TypeName(DataMemberInfo_t *) const
virtual const char * DataMemberInfo_ValidArrayIndex(DataMemberInfo_t *) const
virtual Longptr_t DataMemberInfo_Offset(DataMemberInfo_t *) const
virtual Bool_t DataMemberInfo_IsValid(DataMemberInfo_t *) const
virtual Long_t DataMemberInfo_TypeProperty(DataMemberInfo_t *) const
virtual Long_t DataMemberInfo_Property(DataMemberInfo_t *) const
virtual int DataMemberInfo_ArrayDim(DataMemberInfo_t *) const
virtual void DataMemberInfo_Delete(DataMemberInfo_t *) const
virtual int DataMemberInfo_MaxIndex(DataMemberInfo_t *, Int_t) const
virtual const char * DataMemberInfo_Title(DataMemberInfo_t *) const
virtual const char * DataMemberInfo_TypeTrueName(DataMemberInfo_t *) const
virtual const char * TypeName(const char *s)=0
A collection of TDataMember objects designed for fast access given a DeclId_t and for keep track of T...
void Update(TDictionary *member)
Move the member or data member to the expect set of list.
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:600
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
const char * GetMethodName() const
Definition TMethodCall.h:90
const char * GetParams() const
Definition TMethodCall.h:91
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
TString fTitle
Definition TNamed.h:33
TString fName
Definition TNamed.h:32
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1082
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:886
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition TObject.cxx:1124
Long_t fToggleMaskBit
TDataMember * fDataMember
!Data member to which this option belongs
The TRealData class manages the effective list of all data members for a given class.
Definition TRealData.h:30
const char * GetName() const override
Returns name of object.
Definition TRealData.h:52
TDataMember * GetDataMember() const
Definition TRealData.h:53
Bool_t IsObject() const
Definition TRealData.h:56
Long_t GetThisOffset() const
Definition TRealData.h:55
Basic string class.
Definition TString.h:137
Ssiz_t Length() const
Definition TString.h:426
void Clear()
Clear string without changing its capacity.
Definition TString.cxx:1242
const char * Data() const
Definition TString.h:385
TString & Append(const char *cs)
Definition TString.h:582
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2460
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2438
std::string GetLong64_Name(const char *original)
Replace 'long long' and 'unsigned long long' by 'Long64_t' and 'ULong64_t'.
ROOT::ESTLType UnderlyingIsSTLCont(std::string_view type)
Return the type of STL collection, if any, that is the underlying type of the given type.
TLine l
Definition textangle.C:4