TToggle.cxx

Go to the documentation of this file.
00001 // @(#)root/meta:$Id: TToggle.cxx 35682 2010-09-23 16:07:59Z pcanal $
00002 // Author: Piotr Golonka   30/07/97
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TToggle                                                              //
00015 //                                                                      //
00016 // This class defines toggling facility for both - object's method or   //
00017 // variables.                                                           //
00018 // Assume that user provides an object with a two-state field , and     //
00019 // methods to Get/Set value of this field. This object enables to switch//
00020 // values via this method when the only thing you know about the field  //
00021 // is the name of the method (or method itself) which sets the field.   //
00022 // This facility is required in context Pop-Up menu, when the only      //
00023 // information about how to toggle a field is a name of methhod which   //
00024 // sets it.                                                             //
00025 // This class may be also used for toggling an integer variable,        //
00026 // which may be important while building universal objects...           //
00027 // When user provides a "set-method" of name SetXXX this object tries   //
00028 // automaticaly find a matching "get-method" by lookin for a method     //
00029 // with name GetXXX, IsXXX or HasXXX for given object.                  //
00030 //                                                                      //
00031 //////////////////////////////////////////////////////////////////////////
00032 
00033 #include "TMethod.h"
00034 #include "TToggle.h"
00035 #include "TDataMember.h"
00036 
00037 
00038 ClassImp(TToggle)
00039 
00040 //______________________________________________________________________________
00041 TToggle::TToggle()
00042 {
00043    // TToggle default constructor. You have to initialize it before using
00044    // by making a call to SetToggledVariable() or SetToggledObject().
00045 
00046    fState       =  kFALSE;
00047    fValue       = -1;
00048    fOnValue     =  1;
00049    fOffValue    =  0;
00050    fInitialized =  0;
00051    fObject      =  0;
00052    fGetter      =  0;
00053    fSetter      =  0;
00054    fTglVariable =  0;
00055 }
00056 
00057 //______________________________________________________________________________
00058 void TToggle::SetToggledVariable(Int_t &var)
00059 {
00060    // Initializes object for use with a variable - you pass it via reference
00061    // so it will be modified by Toggle.
00062 
00063    fTglVariable=&var;
00064    fValue=var;
00065 }
00066 
00067 //______________________________________________________________________________
00068 Bool_t TToggle::GetState()
00069 {
00070    // Returns the state of Toggle according to its current value and
00071    // fOnValue, returns true if they match.
00072 
00073    if (fInitialized)
00074       if (fGetter) fGetter->Execute(fObject, fValue);
00075    return (fState = (fValue == fOnValue));
00076 }
00077 
00078 //______________________________________________________________________________
00079 void TToggle::SetState(Bool_t state)
00080 {
00081    // Sets the value of toggle to fOnValue or fOffValue according to passed
00082    // argument.
00083 
00084    if (fInitialized) {
00085       char stringon[20];
00086       char stringoff[20];
00087       snprintf(stringon,sizeof(stringon),"%li",fOnValue);
00088       snprintf(stringoff,sizeof(stringoff),"%li",fOffValue);
00089 
00090       fSetter->Execute(fObject, state ? stringon:stringoff);
00091       fState=state;
00092       fValue= ( state ? fOnValue : fOffValue);
00093    }
00094 }
00095 
00096 //______________________________________________________________________________
00097 void TToggle::SetValue(Long_t val)
00098 {
00099    // Sets the value of toggle and modifies its state according to whether
00100    // the value is equal to fOnValue.
00101 
00102    if (fInitialized) {
00103       char stringval[20];
00104       snprintf(stringval,sizeof(stringval),"%li",val);
00105       fSetter->Execute(fObject, stringval);
00106       fState=(val==fOnValue);
00107       fValue= val;
00108    }
00109 }
00110 
00111 //______________________________________________________________________________
00112 void TToggle::Toggle()
00113 {
00114    // Toggles the Values and State of this object and connected data!
00115 
00116    if (fInitialized){
00117       if (fTglVariable){
00118          *fTglVariable = !(*fTglVariable);
00119          fValue=(*fTglVariable);
00120          fState=*fTglVariable;
00121       }
00122       if (fGetter && fSetter){
00123          fGetter->Execute(fObject,fValue);
00124          fValue=( (fValue==fOnValue) ? fOffValue:fOnValue);
00125          fState=(!(fValue!=fOnValue));
00126          char stringon[20];
00127          snprintf(stringon,sizeof(stringon),"%li",fValue);
00128          fSetter->Execute(fObject, stringon);
00129       }
00130    }
00131 }
00132 
00133 //______________________________________________________________________________
00134 void TToggle::SetToggledObject(TObject *obj, TMethod *anymethod)
00135 {
00136    // Initializes it to toggle an object's datamember using this object's
00137    // method.
00138 
00139    fObject = obj;
00140    TDataMember *m = anymethod->FindDataMember();
00141    if (!m) {
00142       // try to see if the TMethod has a getter associated via the *GETTER=
00143       // comment string
00144       if (anymethod->GetterMethod()) {
00145          fGetter = anymethod->GetterMethod();
00146          fSetter = anymethod->SetterMethod();
00147          fInitialized = 1;
00148       } else
00149          Error("SetToggledObject", "cannot determine getter method for %s", anymethod->GetName());
00150    } else {
00151       fGetter = m->GetterMethod(obj->IsA());
00152       fSetter = m->SetterMethod(obj->IsA());
00153       fInitialized = 1;
00154    }
00155 }

Generated on Tue Jul 5 14:11:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1