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 "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 = nullptr;
50 fGetter = nullptr;
51 fSetter = nullptr;
52 fTglVariable = nullptr;
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),"%zi",(size_t)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}
long Long_t
Definition RtypesCore.h:54
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
#define ClassImp(name)
Definition Rtypes.h:377
#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 *=nullptr) override
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:134
virtual TMethodCall * SetterMethod()
Return call environment for this method in case this is a *TOGGLE method which takes a single boolean...
Definition TMethod.cxx:295
virtual TMethodCall * GetterMethod()
Return call environment for the getter method in case this is a *TOGGLE method (for the context menu)...
Definition TMethod.cxx:264
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:970
virtual TClass * IsA() const
Definition TObject.h:245
This class defines toggling facility for both - object's method or variables.
Definition TToggle.h:47
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:59
Int_t * fTglVariable
Definition TToggle.h:61
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
TObject * fObject
Definition TToggle.h:57
Long_t fOffValue
Definition TToggle.h:52
virtual void Toggle()
Toggles the Values and State of this object and connected data!
Definition TToggle.cxx:112
TMethodCall * fSetter
Definition TToggle.h:59
Longptr_t fValue
Definition TToggle.h:53
TMethodCall * fGetter
Definition TToggle.h:58
Long_t fOnValue
Definition TToggle.h:51
Bool_t fInitialized
Definition TToggle.h:56
virtual void SetToggledObject(TObject *obj, TMethod *anymethod)
Initializes it to toggle an object's datamember using this object's method.
Definition TToggle.cxx:135
Bool_t fState
Definition TToggle.h:50
virtual void SetState(Bool_t state)
Sets the value of toggle to fOnValue or fOffValue according to passed argument.
Definition TToggle.cxx:80
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
TMarker m
Definition textangle.C:8