[QuantLibAddin-cvs] QuantLibAddin/qlo typefactory.hpp,1.12,1.13
Brought to you by:
ericehlers,
nando
|
From: Eric E. <eri...@us...> - 2006-06-30 18:07:00
|
Update of /cvsroot/quantlibaddin/QuantLibAddin/qlo In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv30161/qlo Modified Files: typefactory.hpp Log Message: refactor getType() function Index: typefactory.hpp =================================================================== RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/typefactory.hpp,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** typefactory.hpp 30 Jun 2006 17:29:34 -0000 1.12 --- typefactory.hpp 30 Jun 2006 18:06:57 -0000 1.13 *************** *** 31,51 **** class RegistryManager { protected: ! void* getType(const std::string& id) { ! TypeMapPtr type_map = getTypeMap(); ! std::string idUpper = QuantLib::uppercase(id); ! for (TypeMap::iterator i = type_map->begin(); i != type_map->end(); i++) ! if (QuantLib::uppercase(i->first) == idUpper) return i->second; ! throw ObjHandler::Exception("Unknown id for Type: " + id); ! } ! ! // alternative to getType - on failure return null pointer ! // instead of throwing exception ! T *checkType(const std::string& id) { TypeMapPtr type_map = getTypeMap(); std::string idUpper = QuantLib::uppercase(id); for (TypeMap::iterator i = type_map->begin(); i != type_map->end(); i++) if (QuantLib::uppercase(i->first) == idUpper) ! return static_cast<T*>(i->second); ! return 0; } private: --- 31,45 ---- class RegistryManager { protected: ! template<typename ConstructorSignature> ! ConstructorSignature getType(const std::string& id, bool failIfNotFound = true) { TypeMapPtr type_map = getTypeMap(); std::string idUpper = QuantLib::uppercase(id); for (TypeMap::iterator i = type_map->begin(); i != type_map->end(); i++) if (QuantLib::uppercase(i->first) == idUpper) ! return static_cast<ConstructorSignature>(i->second); ! if (failIfNotFound) ! throw ObjHandler::Exception("Unknown id for Type: " + id); ! else ! return 0; } private: *************** *** 64,76 **** }; template<typename T> class Create : private RegistryManager<T, EnumTypeRegistry> { public: T operator()(const std::string& id) { ! return *(static_cast<T*>(this->getType(id))); } ! using RegistryManager<T, EnumTypeRegistry>::checkType; }; template<> class Create<boost::shared_ptr<QuantLib::StrikedTypePayoff> > : --- 58,79 ---- }; + /* *** Enumerated Types *** */ template<typename T> class Create : private RegistryManager<T, EnumTypeRegistry> { public: T operator()(const std::string& id) { ! return *(getType<T*>(id)); } ! using RegistryManager<T, EnumTypeRegistry>::getType; }; + /* *** Enumerated Classes *** */ + + /* *** StrikedTypePayoff *** */ + typedef boost::shared_ptr<QuantLib::StrikedTypePayoff>(*StrikedTypePayoffConstructor1)( + const QuantLib::Option::Type&, const double&); + typedef boost::shared_ptr<QuantLib::StrikedTypePayoff>(*StrikedTypePayoffConstructor2)( + const QuantLib::Option::Type&, const double&, const double&); + template<> class Create<boost::shared_ptr<QuantLib::StrikedTypePayoff> > : *************** *** 81,88 **** const QuantLib::Option::Type& optionType, const double& strike) { ! boost::shared_ptr<QuantLib::StrikedTypePayoff>(*ctor)(const QuantLib::Option::Type&, const double&) = ! (boost::shared_ptr<QuantLib::StrikedTypePayoff>(*)(const QuantLib::Option::Type&, const double&)) ! getType(payoffID); ! return ctor(optionType, strike); } boost::shared_ptr<QuantLib::StrikedTypePayoff> operator()( --- 84,90 ---- const QuantLib::Option::Type& optionType, const double& strike) { ! StrikedTypePayoffConstructor1 strikedTypePayoffConstructor = ! getType<StrikedTypePayoffConstructor1>(payoffID); ! return strikedTypePayoffConstructor(optionType, strike); } boost::shared_ptr<QuantLib::StrikedTypePayoff> operator()( *************** *** 91,123 **** const double& strike, const double& strikeIncrement) { ! boost::shared_ptr<QuantLib::StrikedTypePayoff>(*ctor)(const QuantLib::Option::Type&, const double&, const double&) = ! (boost::shared_ptr<QuantLib::StrikedTypePayoff>(*)(const QuantLib::Option::Type&, const double&, const double&)) ! getType(payoffID); ! return ctor(optionType, strike, strikeIncrement); } }; template<> class Create<boost::shared_ptr<QuantLib::PricingEngine> > : private RegistryManager<QuantLib::PricingEngine, EnumClassRegistry> { public: boost::shared_ptr<QuantLib::PricingEngine> operator()(const std::string& engineID, const long& timeSteps) { QL_REQUIRE(timeSteps>0, "timeSteps must be positive"); // FIXME move this validation into QL ! boost::shared_ptr<QuantLib::PricingEngine>(*ctor)(const long&) = ! (boost::shared_ptr<QuantLib::PricingEngine>(*)(const long&)) getType(engineID); ! return ctor(timeSteps); ! } ! boost::shared_ptr<QuantLib::PricingEngine> operator()(const std::string& engineID) { ! boost::shared_ptr<QuantLib::PricingEngine>(*ctor)() = ! (boost::shared_ptr<QuantLib::PricingEngine>(*)()) getType(engineID); ! return ctor(); } }; typedef const std::vector<double>::const_iterator dbl_itr; ! typedef boost::shared_ptr<QuantLib::Extrapolator>(*ExtrapolatorConstructor1D)( dbl_itr&, dbl_itr&, dbl_itr&); ! typedef boost::shared_ptr<QuantLib::Extrapolator>(*ExtrapolatorConstructor2D)( dbl_itr&, dbl_itr&, dbl_itr&, dbl_itr&, const QuantLib::Matrix&); --- 93,130 ---- const double& strike, const double& strikeIncrement) { ! StrikedTypePayoffConstructor2 strikedTypePayoffConstructor = ! getType<StrikedTypePayoffConstructor2>(payoffID); ! return strikedTypePayoffConstructor(optionType, strike, strikeIncrement); } }; + /* *** PricingEngine *** */ + typedef boost::shared_ptr<QuantLib::PricingEngine>(*PricingEngineConstructor1)(); + typedef boost::shared_ptr<QuantLib::PricingEngine>(*PricingEngineConstructor2)( + const long&); + template<> class Create<boost::shared_ptr<QuantLib::PricingEngine> > : private RegistryManager<QuantLib::PricingEngine, EnumClassRegistry> { public: + boost::shared_ptr<QuantLib::PricingEngine> operator()(const std::string& engineID) { + PricingEngineConstructor1 pricingEngineConstructor = + getType<PricingEngineConstructor1>(engineID); + return pricingEngineConstructor(); + } boost::shared_ptr<QuantLib::PricingEngine> operator()(const std::string& engineID, const long& timeSteps) { QL_REQUIRE(timeSteps>0, "timeSteps must be positive"); // FIXME move this validation into QL ! PricingEngineConstructor2 pricingEngineConstructor = ! getType<PricingEngineConstructor2>(engineID); ! return pricingEngineConstructor(timeSteps); } }; + /* *** Extrapolator *** */ typedef const std::vector<double>::const_iterator dbl_itr; ! typedef boost::shared_ptr<QuantLib::Extrapolator>(*ExtrapolatorConstructor1)( dbl_itr&, dbl_itr&, dbl_itr&); ! typedef boost::shared_ptr<QuantLib::Extrapolator>(*ExtrapolatorConstructor2)( dbl_itr&, dbl_itr&, dbl_itr&, dbl_itr&, const QuantLib::Matrix&); *************** *** 129,134 **** const std::string& extrapolatorID, dbl_itr& xBegin, dbl_itr& xEnd, dbl_itr& yBegin) { ! ExtrapolatorConstructor1D extrapolatorConstructor = ! static_cast<ExtrapolatorConstructor1D>(getType(extrapolatorID)); return extrapolatorConstructor(xBegin, xEnd, yBegin); } --- 136,141 ---- const std::string& extrapolatorID, dbl_itr& xBegin, dbl_itr& xEnd, dbl_itr& yBegin) { ! ExtrapolatorConstructor1 extrapolatorConstructor = ! getType<ExtrapolatorConstructor1>(extrapolatorID); return extrapolatorConstructor(xBegin, xEnd, yBegin); } *************** *** 137,142 **** dbl_itr& xBegin, dbl_itr& xEnd, dbl_itr& yBegin, dbl_itr& yEnd, const QuantLib::Matrix& zData) { ! ExtrapolatorConstructor2D extrapolatorConstructor = ! static_cast<ExtrapolatorConstructor2D>(getType(extrapolatorID)); return extrapolatorConstructor(xBegin, xEnd, yBegin, yEnd, zData); } --- 144,149 ---- dbl_itr& xBegin, dbl_itr& xEnd, dbl_itr& yBegin, dbl_itr& yEnd, const QuantLib::Matrix& zData) { ! ExtrapolatorConstructor2 extrapolatorConstructor = ! getType<ExtrapolatorConstructor2>(extrapolatorID); return extrapolatorConstructor(xBegin, xEnd, yBegin, yEnd, zData); } |