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
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 = nullptr;
49 fGetter = nullptr;
50 fSetter = nullptr;
51 fTglVariable = nullptr;
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
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);
86
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);
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),"%zi",(size_t)fValue);
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}
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define snprintf
Definition civetweb.c:1579
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
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:1071
virtual TClass * IsA() const
Definition TObject.h:246
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: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:97
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:111
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:134
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: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
TMarker m
Definition textangle.C:8