Logo ROOT   6.16/01
Reference Guide
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 "TMethod.h"
31#include "TToggle.h"
32#include "TDataMember.h"
33
34
36
37////////////////////////////////////////////////////////////////////////////////
38/// TToggle default constructor. You have to initialize it before using
39/// by making a call to SetToggledVariable() or SetToggledObject().
40
42{
43 fState = kFALSE;
44 fValue = -1;
45 fOnValue = 1;
46 fOffValue = 0;
47 fInitialized = 0;
48 fObject = 0;
49 fGetter = 0;
50 fSetter = 0;
51 fTglVariable = 0;
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Initializes object for use with a variable - you pass it via reference
56/// so it will be modified by Toggle.
57
59{
60 fTglVariable=&var;
61 fValue=var;
62}
63
64////////////////////////////////////////////////////////////////////////////////
65/// Returns the state of Toggle according to its current value and
66/// fOnValue, returns true if they match.
67
69{
70 if (fInitialized)
72 return (fState = (fValue == fOnValue));
73}
74
75////////////////////////////////////////////////////////////////////////////////
76/// Sets the value of toggle to fOnValue or fOffValue according to passed
77/// argument.
78
80{
81 if (fInitialized) {
82 char stringon[20];
83 char stringoff[20];
84 snprintf(stringon,sizeof(stringon),"%li",fOnValue);
85 snprintf(stringoff,sizeof(stringoff),"%li",fOffValue);
86
87 fSetter->Execute(fObject, state ? stringon:stringoff);
88 fState=state;
89 fValue= ( state ? fOnValue : fOffValue);
90 }
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Sets the value of toggle and modifies its state according to whether
95/// the value is equal to fOnValue.
96
98{
99 if (fInitialized) {
100 char stringval[20];
101 snprintf(stringval,sizeof(stringval),"%li",val);
102 fSetter->Execute(fObject, stringval);
103 fState=(val==fOnValue);
104 fValue= val;
105 }
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// Toggles the Values and State of this object and connected data!
110
112{
113 if (fInitialized){
114 if (fTglVariable){
115 *fTglVariable = !(*fTglVariable);
116 fValue=(*fTglVariable);
118 }
119 if (fGetter && fSetter){
122 fState=(!(fValue!=fOnValue));
123 char stringon[20];
124 snprintf(stringon,sizeof(stringon),"%li",fValue);
125 fSetter->Execute(fObject, stringon);
126 }
127 }
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Initializes it to toggle an object's datamember using this object's
132/// method.
133
135{
136 fObject = obj;
137 TDataMember *m = anymethod->FindDataMember();
138 if (!m) {
139 // try to see if the TMethod has a getter associated via the *GETTER=
140 // comment string
141 if (anymethod->GetterMethod()) {
142 fGetter = anymethod->GetterMethod();
143 fSetter = anymethod->SetterMethod();
144 fInitialized = 1;
145 } else
146 Error("SetToggledObject", "cannot determine getter method for %s", anymethod->GetName());
147 } else {
148 fGetter = m->GetterMethod(obj->IsA());
149 fSetter = m->SetterMethod(obj->IsA());
150 fInitialized = 1;
151 }
152}
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
long Long_t
Definition: RtypesCore.h:50
bool Bool_t
Definition: RtypesCore.h:59
#define ClassImp(name)
Definition: Rtypes.h:363
#define snprintf
Definition: civetweb.c:1540
All ROOT classes may have RTTI (run time type identification) support added.
Definition: TDataMember.h:31
void Execute(const char *, const char *, int *=0)
Execute method on this object with the given parameter string, e.g.
Definition: TMethodCall.h:64
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:294
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
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
This class defines toggling facility for both - object's method or variables.
Definition: TToggle.h:43
Long_t fValue
Definition: TToggle.h:49
TToggle()
TToggle default constructor.
Definition: TToggle.cxx:41
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:58
Int_t * fTglVariable
Definition: TToggle.h:57
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:97
TObject * fObject
Definition: TToggle.h:53
Long_t fOffValue
Definition: TToggle.h:48
virtual void Toggle()
Toggles the Values and State of this object and connected data!
Definition: TToggle.cxx:111
TMethodCall * fSetter
Definition: TToggle.h:55
TMethodCall * fGetter
Definition: TToggle.h:54
Long_t fOnValue
Definition: TToggle.h:47
Bool_t fInitialized
Definition: TToggle.h:52
virtual void SetToggledObject(TObject *obj, TMethod *anymethod)
Initializes it to toggle an object's datamember using this object's method.
Definition: TToggle.cxx:134
Bool_t fState
Definition: TToggle.h:46
virtual void SetState(Bool_t state)
Sets the value of toggle to fOnValue or fOffValue according to passed argument.
Definition: TToggle.cxx:79
virtual Bool_t GetState()
Returns the state of Toggle according to its current value and fOnValue, returns true if they match.
Definition: TToggle.cxx:68
auto * m
Definition: textangle.C:8