TDataMember.
All ROOT classes may have RTTI (run time type identification) support
added. The data is stored in so called DICTIONARY (look at TDictionary).
Information about a class is stored in TClass.
This information may be obtained via the CINT api - see class TCint.
TClass has a list of TDataMember objects providing information about all
data members of described class.
TDataMember provides information about name of data member, its type,
and comment field string. It also tries to find the TMethodCall objects
responsible for getting/setting a value of it, and gives you pointers
to these methods. This gives you a unique possibility to access
protected and private (!) data members if only methods for doing that
are defined.
These methods could either be specified in a comment field, or found
out automatically by ROOT: here's an example:
suppose you have a class definition:
class MyClass{
private:
Float_t fX1;
...
public:
void SetX1(Float_t x) {fX1 = x;};
Float_t GetX1() {return fX1;};
...
}
Look at the data member name and method names: a data member name has
a prefix letter (f) and has a base name X1 . The methods for getting and
setting this value have names which consist of string Get/Set and the
same base name. This convention of naming data fields and methods which
access them allows TDataMember find this methods by itself completely
automatically. To make this description complete, one should know,
that names that are automatically recognized may be also:
for data fields: either fXXX or fIsXXX; and for getter function
GetXXX() or IsXXX() [where XXX is base name].
As an example of using it let's analyse a few lines which get and set
a fEditable field in TCanvas:
TCanvas *c = new TCanvas("c"); // create a canvas
TClass *cl = c->IsA(); // get its class description object.
TDataMember *dm = cl->GetDataMember("fEditable"); //This is our data member
TMethodCall *getter = dm->GetterMethod(c); //find a method that gets value!
Long_t l; // declare a storage for this value;
getter->Execute(c,"",l); // Get this Value !!!! It will appear in l !!!
TMethodCall *setter = dm->SetterMethod(c);
setter->Execute(c,"0",); // Set Value 0 !!!
This trick is widely used in ROOT TContextMenu and dialogs for obtaining
current values and put them as initial values in dialog fields.
If you don't want to follow the convention of naming used by ROOT
you still could benefit from Getter/Setter method support: the solution
is to instruct ROOT what the names of these routines are.
The way to do it is putting this information in a comment string to a data
field in your class declaration:
class MyClass{
Int_t mydata; // *OPTIONS={GetMethod="Get";SetMethod="Set"}
...
Int_t Get() const { return mydata;};
void Set(Int_t i) {mydata=i;};
}
However, this getting/setting functions are not the only feature of
this class. The next point is providing lists of possible settings
for the concerned data member. The idea is to have a list of possible
options for this data member, with strings identifying them. This
is used in dialogs with parameters to set - for details see
TMethodArg, TRootContextMenu, TContextMenu. This list not only specifies
the allowed value, but also provides strings naming the options.
Options are managed via TList of TOptionListItem objects. This list
is also created automatically: if a data type is an enum type,
the list will have items describing every enum value, and named
according to enum name. If type is Bool_t, two options "On" and "Off"
with values 0 and 1 are created. For other types you need to instruct
ROOT about possible options. The way to do it is the same as in case of
specifying getter/setter method: a comment string to a data field in
Your header file with class definition.
The most general format of this string is:
*OPTIONS={GetMethod="getter";SetMethod="setter";Items=(it1="title1",it2="title2", ... ) }
While parsing this string ROOT firstly looks for command-tokens:
GetMethod, SetMethod, Items; They must be preceded by string
*OPTIONS= , enclosed by {} and separated by semicolons ";".
All command token should have a form TOKEN=VALUE.
All tokens are optional.
The names of getter and setter method must be enclosed by double-quote
marks (") .
Specifications of Items is slightly more complicated: you need to
put token ITEMS= and then enclose all options in curly brackets "()".
You separate options by comas ",".
Each option item may have one of the following forms:
IntegerValue = "Text Label"
EnumValue = "Text Label"
"TextValue" = Text Label"
One can sepcify values as Integers or Enums - when data field is an
Integer, Float or Enum type; as texts - for Text_t (more precisely:
Option_t).
As mentioned above - this information are mainly used by contextmenu,
but also in Dump() and Inspect() methods and by the THtml class.