Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TToggle.cxx
Go to the documentation of this file.
1// @(#)root/meta:$Id$
2// Author: Piotr Golonka 30/07/97
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 TToggle
13
14This class defines toggling facility for both - object's method or
15variables.
16Assume that user provides an object with a two-state field , and
17methods to Get/Set value of this field. This object enables to switch
18values via this method when the only thing you know about the field
19is the name of the method (or method itself) which sets the field.
20This facility is required in context Pop-Up menu, when the only
21information about how to toggle a field is a name of methhod which
22sets it.
23This class may be also used for toggling an integer variable,
24which may be important while building universal objects...
25When user provides a "set-method" of name SetXXX this object tries
26automaticaly find a matching "get-method" by lookin for a method
27with name GetXXX, IsXXX or HasXXX for given object.
28*/
29
30#include "TToggle.h"
31
32#include "TMethod.h"
33#include "TMethodCall.h"
34#include "TDataMember.h"
35
36#include <cstdio>
37
38////////////////////////////////////////////////////////////////////////////////
39/// TToggle default constructor. You have to initialize it before using
40/// by making a call to SetToggledVariable() or SetToggledObject().
41
45
46////////////////////////////////////////////////////////////////////////////////
47/// Initializes object for use with a variable - you pass it via reference
48/// so it will be modified by Toggle.
49
51{
52 fTglVariable = &var;
53 fValue = var;
54}
55
56////////////////////////////////////////////////////////////////////////////////
57/// Returns the state of Toggle according to its current value and
58/// fOnValue, returns true if they match.
59
61{
62 if (auto fget = GetGetter())
63 fget->Execute(fObject, fValue);
64
65 fState = (fValue == fOnValue);
66 return fState;
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Sets the value of toggle to fOnValue or fOffValue according to passed
71/// argument.
72
74{
75 SetValue(state ? fOnValue : fOffValue);
76}
77
78////////////////////////////////////////////////////////////////////////////////
79/// Sets the value of toggle and modifies its state according to whether
80/// the value is equal to fOnValue.
81
83{
84 if (auto fset = GetSetter()) {
85 fset->Execute(fObject, TString::Format("%li", val).Data());
86 fState = (val == fOnValue);
87 fValue = val;
88 }
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// Toggles the Values and State of this object and connected data!
93
95{
96 if (IsInitialized()) {
97 if (fTglVariable){
98 *fTglVariable = !(*fTglVariable);
99 fValue = (*fTglVariable);
101 }
102 auto fget = GetGetter();
103 auto fset = GetSetter();
104 if (fget && fset) {
105 fget->Execute(fObject, fValue);
107 fState = (fValue == fOnValue);
108 fset->Execute(fObject, TString::Format("%zi", (size_t)fValue).Data());
109 }
110 }
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Initializes it to toggle an object's datamember using this object's method.
115
117{
118 TDataMember *member = anymethod->FindDataMember();
119 if (member)
120 TToggle::SetToggledObject(obj, member);
121 else if (!anymethod)
122 Error("SetToggledObject", "Method pointer not provided");
123 else if (!anymethod->GetterMethod())
124 Error("SetToggledObject", "cannot determine getter method for %s", anymethod->GetName());
125 else {
126 // try to see if the TMethod has a getter associated via the *GETTER= comment string
127 fObject = obj;
128 // TMethodCall pointer can be stored - it is not recerated
129 fGetter = anymethod->GetterMethod();
130 fSetter = anymethod->SetterMethod();
131 fInitialized = 2;
132 }
133}
134
135////////////////////////////////////////////////////////////////////////////////
136/// Initializes it to toggle an object's datamember
137
139{
140 fObject = obj;
141 fMember = member;
142 fInitialized = 1;
143
144 // one cannot remember TMethodCall pointers from TDataMember
145 // while they will be recreated any time they requested again
146 fGetter = nullptr;
147 fSetter = nullptr;
148}
149
150
151////////////////////////////////////////////////////////////////////////////////
152/// return getter method
153/// request new for the TDataMember while it can be recreated at any time
154
156{
157 if ((fInitialized == 1) && fMember && fObject)
158 return const_cast<TDataMember *>(fMember)->GetterMethod(fObject->IsA());
159
160 return fGetter;
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// return setter method
165/// request new for the TDataMember while it can be recreated at any time
166
168{
169 if ((fInitialized == 1) && fMember && fObject)
170 return const_cast<TDataMember *>(fMember)->SetterMethod(fObject->IsA());
171
172 return fSetter;
173}
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:69
All ROOT classes may have RTTI (run time type identification) support added.
Definition TDataMember.h:31
Method or function calling interface.
Definition TMethodCall.h:37
Each ROOT class (see TClass) has a linked list of methods.
Definition TMethod.h:38
virtual TDataMember * FindDataMember()
Tries to guess DataMember from comment string and Method's name <==(only if 1 Argument!...
Definition TMethod.cxx:132
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
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
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Mother of all ROOT objects.
Definition TObject.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1096
virtual TClass * IsA() const
Definition TObject.h:248
const char * Data() const
Definition TString.h:385
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
TToggle()
TToggle default constructor.
Definition TToggle.cxx:42
virtual void SetToggledVariable(Int_t &var)
Initializes object for use with a variable - you pass it via reference so it will be modified by Togg...
Definition TToggle.cxx:50
Int_t * fTglVariable
Definition TToggle.h:64
virtual void SetValue(Long_t val)
Sets the value of toggle and modifies its state according to whether the value is equal to fOnValue.
Definition TToggle.cxx:82
TObject * fObject
Definition TToggle.h:59
Long_t fOffValue
Definition TToggle.h:54
virtual void Toggle()
Toggles the Values and State of this object and connected data!
Definition TToggle.cxx:94
TMethodCall * fSetter
Definition TToggle.h:62
virtual Bool_t IsInitialized()
Definition TToggle.h:76
Longptr_t fValue
Definition TToggle.h:55
TMethodCall * fGetter
Definition TToggle.h:61
Long_t fOnValue
Definition TToggle.h:53
TMethodCall * GetSetter() const
return setter method request new for the TDataMember while it can be recreated at any time
Definition TToggle.cxx:167
TMethodCall * GetGetter() const
return getter method request new for the TDataMember while it can be recreated at any time
Definition TToggle.cxx:155
virtual void SetToggledObject(TObject *obj, TMethod *anymethod)
Initializes it to toggle an object's datamember using this object's method.
Definition TToggle.cxx:116
TDataMember * fMember
Definition TToggle.h:60
Bool_t fState
Definition TToggle.h:52
virtual void SetState(Bool_t state)
Sets the value of toggle to fOnValue or fOffValue according to passed argument.
Definition TToggle.cxx:73
virtual Bool_t GetState()
Returns the state of Toggle according to its current value and fOnValue, returns true if they match.
Definition TToggle.cxx:60
Int_t fInitialized
Definition TToggle.h:58