Logo ROOT  
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 
14 This class defines toggling facility for both - object's method or
15 variables.
16 Assume that user provides an object with a two-state field , and
17 methods to Get/Set value of this field. This object enables to switch
18 values via this method when the only thing you know about the field
19 is the name of the method (or method itself) which sets the field.
20 This facility is required in context Pop-Up menu, when the only
21 information about how to toggle a field is a name of methhod which
22 sets it.
23 This class may be also used for toggling an integer variable,
24 which may be important while building universal objects...
25 When user provides a "set-method" of name SetXXX this object tries
26 automaticaly find a matching "get-method" by lookin for a method
27 with name GetXXX, IsXXX or HasXXX for given object.
28 */
29 
30 #include "TMethod.h"
31 #include "TMethodCall.h"
32 #include "TToggle.h"
33 #include "TDataMember.h"
34 #include "snprintf.h"
35 
37 
38 ////////////////////////////////////////////////////////////////////////////////
39 /// TToggle default constructor. You have to initialize it before using
40 /// by making a call to SetToggledVariable() or SetToggledObject().
41 
43 {
44  fState = kFALSE;
45  fValue = -1;
46  fOnValue = 1;
47  fOffValue = 0;
48  fInitialized = 0;
49  fObject = 0;
50  fGetter = 0;
51  fSetter = 0;
52  fTglVariable = 0;
53 }
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Initializes object for use with a variable - you pass it via reference
57 /// so it will be modified by Toggle.
58 
60 {
61  fTglVariable=&var;
62  fValue=var;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Returns the state of Toggle according to its current value and
67 /// fOnValue, returns true if they match.
68 
70 {
71  if (fInitialized)
73  return (fState = (fValue == fOnValue));
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Sets the value of toggle to fOnValue or fOffValue according to passed
78 /// argument.
79 
81 {
82  if (fInitialized) {
83  char stringon[20];
84  char stringoff[20];
85  snprintf(stringon,sizeof(stringon),"%li",fOnValue);
86  snprintf(stringoff,sizeof(stringoff),"%li",fOffValue);
87 
88  fSetter->Execute(fObject, state ? stringon:stringoff);
89  fState=state;
90  fValue= ( state ? fOnValue : fOffValue);
91  }
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Sets the value of toggle and modifies its state according to whether
96 /// the value is equal to fOnValue.
97 
99 {
100  if (fInitialized) {
101  char stringval[20];
102  snprintf(stringval,sizeof(stringval),"%li",val);
103  fSetter->Execute(fObject, stringval);
104  fState=(val==fOnValue);
105  fValue= val;
106  }
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Toggles the Values and State of this object and connected data!
111 
113 {
114  if (fInitialized){
115  if (fTglVariable){
116  *fTglVariable = !(*fTglVariable);
117  fValue=(*fTglVariable);
119  }
120  if (fGetter && fSetter){
123  fState=(!(fValue!=fOnValue));
124  char stringon[20];
125  snprintf(stringon,sizeof(stringon),"%li",fValue);
126  fSetter->Execute(fObject, stringon);
127  }
128  }
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Initializes it to toggle an object's datamember using this object's
133 /// method.
134 
136 {
137  fObject = obj;
138  TDataMember *m = anymethod->FindDataMember();
139  if (!m) {
140  // try to see if the TMethod has a getter associated via the *GETTER=
141  // comment string
142  if (anymethod->GetterMethod()) {
143  fGetter = anymethod->GetterMethod();
144  fSetter = anymethod->SetterMethod();
145  fInitialized = 1;
146  } else
147  Error("SetToggledObject", "cannot determine getter method for %s", anymethod->GetName());
148  } else {
149  fGetter = m->GetterMethod(obj->IsA());
150  fSetter = m->SetterMethod(obj->IsA());
151  fInitialized = 1;
152  }
153 }
m
auto * m
Definition: textangle.C:8
TMethod::GetterMethod
virtual TMethodCall * GetterMethod()
Return call environment for the getter method in case this is a *TOGGLE method (for the context menu)...
Definition: TMethod.cxx:265
snprintf
#define snprintf
Definition: civetweb.c:1540
TToggle::fGetter
TMethodCall * fGetter
Definition: TToggle.h:58
TToggle::GetState
virtual Bool_t GetState()
Returns the state of Toggle according to its current value and fOnValue, returns true if they match.
Definition: TToggle.cxx:69
TToggle::fObject
TObject * fObject
Definition: TToggle.h:57
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TMethod.h
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TDataMember.h
TDataMember
All ROOT classes may have RTTI (run time type identification) support added.
Definition: TDataMember.h:31
TToggle::fValue
Long_t fValue
Definition: TToggle.h:53
TToggle::SetToggledVariable
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:59
bool
TToggle::SetToggledObject
virtual void SetToggledObject(TObject *obj, TMethod *anymethod)
Initializes it to toggle an object's datamember using this object's method.
Definition: TToggle.cxx:135
TToggle
This class defines toggling facility for both - object's method or variables.
Definition: TToggle.h:47
TToggle::SetState
virtual void SetState(Bool_t state)
Sets the value of toggle to fOnValue or fOffValue according to passed argument.
Definition: TToggle.cxx:80
TToggle::fTglVariable
Int_t * fTglVariable
Definition: TToggle.h:61
TToggle::fOnValue
Long_t fOnValue
Definition: TToggle.h:51
TMethod::FindDataMember
virtual TDataMember * FindDataMember()
Tries to guess DataMember from comment string and Method's name <==(only if 1 Argument!...
Definition: TMethod.cxx:134
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TToggle::fOffValue
Long_t fOffValue
Definition: TToggle.h:52
TMethod
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:38
Long_t
long Long_t
Definition: RtypesCore.h:54
TToggle::Toggle
virtual void Toggle()
Toggles the Values and State of this object and connected data!
Definition: TToggle.cxx:112
TToggle.h
TToggle::fState
Bool_t fState
Definition: TToggle.h:50
TToggle::fInitialized
Bool_t fInitialized
Definition: TToggle.h:56
TToggle::SetValue
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:98
TMethodCall::Execute
void Execute(const char *, const char *, int *=0)
Execute method on this object with the given parameter string, e.g.
Definition: TMethodCall.h:64
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TMethod::SetterMethod
virtual TMethodCall * SetterMethod()
Return call environment for this method in case this is a *TOGGLE method which takes a single boolean...
Definition: TMethod.cxx:296
TToggle::fSetter
TMethodCall * fSetter
Definition: TToggle.h:59
TToggle::TToggle
TToggle()
TToggle default constructor.
Definition: TToggle.cxx:42
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
int
TMethodCall.h