CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/Fireworks/Core/src/FWParameterSetterBase.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWParameterSetterBase
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Fri Mar  7 14:16:20 EST 2008
00011 // $Id: FWParameterSetterBase.cc,v 1.15 2012/06/26 22:13:04 wmtan Exp $
00012 //
00013 
00014 // system include files
00015 #include "Reflex/Type.h"
00016 #include "Reflex/Object.h"
00017 
00018 #include <assert.h>
00019 #include <iostream>
00020 #include <boost/bind.hpp>
00021 
00022 // user include files
00023 #include "FWCore/Utilities/interface/TypeID.h"
00024 
00025 #include "Fireworks/Core/interface/FWParameterSetterBase.h"
00026 #include "Fireworks/Core/interface/FWParameterBase.h"
00027 #include "Fireworks/Core/interface/FWParameterSetterEditorBase.h"
00028 #include "Fireworks/Core/interface/fwLog.h"
00029 
00030 //
00031 // constants, enums and typedefs
00032 //
00033 
00034 //
00035 // static data member definitions
00036 //
00037 
00038 //
00039 // constructors and destructor
00040 //
00041 FWParameterSetterBase::FWParameterSetterBase() :
00042    m_frame(0)
00043 {
00044 }
00045 
00046 // FWParameterSetterBase::FWParameterSetterBase(const FWParameterSetterBase& rhs)
00047 // {
00048 //    // do actual copying here;
00049 // }
00050 
00051 FWParameterSetterBase::~FWParameterSetterBase()
00052 {
00053 }
00054 
00055 //
00056 // assignment operators
00057 //
00058 // const FWParameterSetterBase& FWParameterSetterBase::operator=(const FWParameterSetterBase& rhs)
00059 // {
00060 //   //An exception safe implementation is
00061 //   FWParameterSetterBase temp(rhs);
00062 //   swap(rhs);
00063 //
00064 //   return *this;
00065 // }
00066 
00067 //
00068 // member functions
00069 //
00070 
00071 void
00072 FWParameterSetterBase::attach(FWParameterBase* iBase, FWParameterSetterEditorBase* iFrame)
00073 {
00074    m_frame=iFrame;
00075    attach(iBase);
00076 }
00077 
00078 
00079 //
00080 // const member functions
00081 //
00082 
00083 void
00084 FWParameterSetterBase::update() const
00085 {
00086    if (m_frame != 0)
00087       m_frame->updateEditor();
00088 }
00089 
00090 //
00091 // static member functions
00092 //
00093 
00094 boost::shared_ptr<FWParameterSetterBase>
00095 FWParameterSetterBase::makeSetterFor(FWParameterBase* iParam)
00096 {
00097    static std::map<edm::TypeID,Reflex::Type> s_paramToSetterMap;
00098    edm::TypeID paramType( typeid(*iParam) );
00099    std::map<edm::TypeID,Reflex::Type>::iterator itFind = s_paramToSetterMap.find(paramType);
00100    if (itFind == s_paramToSetterMap.end())
00101    {
00102       Reflex::Type paramClass( Reflex::Type::ByTypeInfo(typeid(*iParam)) );
00103       if (paramClass == Reflex::Type())
00104       {
00105          fwLog(fwlog::kError) << " the type "<<typeid(*iParam).name()<< " is not known to REFLEX" <<std::endl;
00106       }
00107       assert(paramClass != Reflex::Type() );
00108 
00109       //the corresponding setter has the same name but with 'Setter' at the end
00110       std::string name = paramClass.Name();
00111       // FIXME: there was a convention between parameter class names and associated
00112       //        setters. The following works around the problem introduced by
00113       //        the generic parameter class but it is clear that a better 
00114       //        way of doing the binding is required. Notice that there are only 5 
00115       //        different type of FW*Parameter.
00116       if (name == "FWGenericParameter<bool>")
00117          name = "FWBoolParameterSetter";
00118       else if (name == "FWGenericParameter<std::string>")
00119          name = "FWStringParameterSetter";
00120       else if (name == "FWGenericParameter<std::basic_string<char> >")
00121          name = "FWStringParameterSetter"; 
00122       else if (name == "FWGenericParameterWithRange<double>")
00123          name = "FWDoubleParameterSetter";
00124       else if (name == "FWGenericParameterWithRange<long int>")
00125          name = "FWLongParameterSetter";
00126       else if (name == "FWGenericParameterWithRange<long>")
00127          name = "FWLongParameterSetter";
00128       else
00129          name += "Setter";
00130 
00131       Reflex::Type setterClass( Reflex::Type::ByName( name ) );
00132       if (setterClass == Reflex::Type())
00133       {
00134          fwLog(fwlog::kError) << " the type "<<name<< " is not known to REFLEX" <<std::endl;
00135       }
00136       assert(setterClass != Reflex::Type());
00137 
00138       s_paramToSetterMap[paramType]=setterClass;
00139       itFind = s_paramToSetterMap.find(paramType);
00140    }
00141    //create the instance we want
00142    //NOTE: for some odd reason Reflex 'Construct' uses 'malloc' to allocate the memory.  This means the object
00143    // can not be deleted using 'delete'!  So we must call Type::Destruct on the object
00144    Reflex::Object setterObj = itFind->second.Construct();
00145 
00146    //make it into the base class
00147    Reflex::Type s_setterBaseType( Reflex::Type::ByTypeInfo( typeid(FWParameterSetterBase) ) );
00148    assert(s_setterBaseType != Reflex::Type());
00149    Reflex::Object castSetterObj = setterObj.CastObject(s_setterBaseType);
00150    boost::shared_ptr<FWParameterSetterBase> ptr(reinterpret_cast<FWParameterSetterBase*>( castSetterObj.Address() ),
00151                                                 boost::bind(&Reflex::Type::Destruct,itFind->second,setterObj.Address(),true));
00152    return ptr;
00153 }
00154 
00155 /* Virtual function which sets widgets enabled state.*/
00156 void
00157 FWParameterSetterBase::setEnabled(bool)
00158 {
00159 }