Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:42

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWParameterSetterBase
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Fri Mar  7 14:16:20 EST 2008
0011 //
0012 
0013 // system include files
0014 #include "FWCore/Reflection/interface/TypeWithDict.h"
0015 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0016 
0017 #include <cassert>
0018 #include <iostream>
0019 #include <functional>
0020 
0021 // user include files
0022 #include "FWCore/Utilities/interface/TypeID.h"
0023 
0024 #include "Fireworks/Core/interface/FWParameterSetterBase.h"
0025 #include "Fireworks/Core/interface/FWParameterBase.h"
0026 #include "Fireworks/Core/interface/FWParameterSetterEditorBase.h"
0027 #include "Fireworks/Core/interface/fwLog.h"
0028 
0029 //
0030 // constants, enums and typedefs
0031 //
0032 
0033 //
0034 // static data member definitions
0035 //
0036 
0037 //
0038 // constructors and destructor
0039 //
0040 FWParameterSetterBase::FWParameterSetterBase() : m_frame(nullptr) {}
0041 
0042 // FWParameterSetterBase::FWParameterSetterBase(const FWParameterSetterBase& rhs)
0043 // {
0044 //    // do actual copying here;
0045 // }
0046 
0047 FWParameterSetterBase::~FWParameterSetterBase() {}
0048 
0049 //
0050 // assignment operators
0051 //
0052 // const FWParameterSetterBase& FWParameterSetterBase::operator=(const FWParameterSetterBase& rhs)
0053 // {
0054 //   //An exception safe implementation is
0055 //   FWParameterSetterBase temp(rhs);
0056 //   swap(rhs);
0057 //
0058 //   return *this;
0059 // }
0060 
0061 //
0062 // member functions
0063 //
0064 
0065 void FWParameterSetterBase::attach(FWParameterBase* iBase, FWParameterSetterEditorBase* iFrame) {
0066   m_frame = iFrame;
0067   attach(iBase);
0068 }
0069 
0070 //
0071 // const member functions
0072 //
0073 
0074 void FWParameterSetterBase::update() const {
0075   if (m_frame != nullptr)
0076     m_frame->updateEditor();
0077 }
0078 
0079 //
0080 // static member functions
0081 //
0082 
0083 std::shared_ptr<FWParameterSetterBase> FWParameterSetterBase::makeSetterFor(FWParameterBase* iParam) {
0084   static std::map<edm::TypeID, edm::TypeWithDict> s_paramToSetterMap;
0085   edm::TypeID paramType(typeid(*iParam));
0086   std::map<edm::TypeID, edm::TypeWithDict>::iterator itFind = s_paramToSetterMap.find(paramType);
0087   if (itFind == s_paramToSetterMap.end()) {
0088     edm::TypeWithDict paramClass(typeid(*iParam));
0089     if (paramClass == edm::TypeWithDict()) {
0090       fwLog(fwlog::kError) << " the type " << typeid(*iParam).name() << " is not known to Root" << std::endl;
0091     }
0092     assert(paramClass != edm::TypeWithDict());
0093 
0094     //the corresponding setter has the same name but with 'Setter' at the end
0095     std::string name = paramClass.name();
0096     // FIXME: there was a convention between parameter class names and associated
0097     //        setters. The following works around the problem introduced by
0098     //        the generic parameter class but it is clear that a better
0099     //        way of doing the binding is required. Notice that there are only 5
0100     //        different type of FW*Parameter.
0101     if (name == "FWGenericParameter<bool>")
0102       name = "FWBoolParameterSetter";
0103     else if (name == "FWGenericParameterWithRange<double>")
0104       name = "FWDoubleParameterSetter";
0105     else if (name == "FWGenericParameterWithRange<long int>")
0106       name = "FWLongParameterSetter";
0107     else if (name == "FWGenericParameterWithRange<long>")
0108       name = "FWLongParameterSetter";
0109     else if (paramClass.friendlyClassName() == "StringFWGenericParameter")
0110       name = "FWStringParameterSetter";
0111     else {
0112       name += "Setter";
0113       fwLog(fwlog::kWarning) << "can't find setter type for " << iParam->name() << ", guessing to " << name << "\n";
0114     }
0115     edm::TypeWithDict setterClass(edm::TypeWithDict::byName(name));
0116 
0117     if (setterClass == edm::TypeWithDict()) {
0118       fwLog(fwlog::kError) << " the type " << name << " has no dictionary" << std::endl;
0119     }
0120     assert(setterClass != edm::TypeWithDict());
0121 
0122     s_paramToSetterMap[paramType] = setterClass;
0123     itFind = s_paramToSetterMap.find(paramType);
0124   }
0125   //create the instance we want
0126   //NOTE: 'construct' will use 'malloc' to allocate the memory if the object is not of class type.
0127   // This means the object cannot be deleted using 'delete'!  So we must call destruct on the object.
0128   edm::ObjectWithDict setterObj = itFind->second.construct();
0129 
0130   //make it into the base class
0131   FWParameterSetterBase* p = static_cast<FWParameterSetterBase*>(setterObj.address());
0132   //Make a shared pointer to the base class that uses a destructor for the derived class, in order to match the above construct call.
0133   std::shared_ptr<FWParameterSetterBase> ptr(
0134       p, std::bind(&edm::TypeWithDict::destruct, itFind->second, setterObj.address(), true));
0135   return ptr;
0136 }
0137 
0138 /* Virtual function which sets widgets enabled state.*/
0139 void FWParameterSetterBase::setEnabled(bool) {}