Update of /cvsroot/quantlibaddin/QuantLibAddin/qlo
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv13364/qlo
Modified Files:
Makefile.am typefactory.hpp
Added Files:
calendarfactory.cpp
Removed Files:
calendar.cpp calendar.hpp
Log Message:
calendar factory
--- NEW FILE: calendarfactory.cpp ---
/*
Copyright (C) 2006 The QuantLib Group
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <oh/objhandlerdefines.hpp>
#include <algorithm>
#include <vector>
#include <boost/regex.hpp>
#include <qlo/typefactory.hpp>
namespace QuantLibAddin {
bool Create<QuantLib::Calendar>::parseID(const std::string &id) {
// strip out whitespace
static boost::regex regex_whitespace("\\s");
std::string idStrip = boost::regex_replace(id, regex_whitespace, "");
// parse the ID
static boost::regex jointCalendarID(
"([\\w:]+)(\\+|\\^)([\\w:]+)(?:\\2([\\w:]+))?(?:\\2([\\w:]+))?");
boost::smatch m;
if (!boost::regex_match(idStrip, m, jointCalendarID))
return false;
// derive the inputs to the JointCalendar constructor
calendarIDs.push_back(m[1]);
if (m[2] == "+")
jointCalendarRule = QuantLib::JointCalendarRule(QuantLib::JoinBusinessDays);
else // "^"
jointCalendarRule = QuantLib::JointCalendarRule(QuantLib::JoinHolidays);
calendarIDs.push_back(m[3]);
if (m[5].matched) {
calendarIDs.push_back(m[4]);
calendarIDs.push_back(m[5]);
} else if (m[4].matched) {
calendarIDs.push_back(m[4]);
} else if (m[3].matched) {
;
} else {
QL_FAIL("the string '" << id <<
"' is not a valid joint calendar identifier");
}
// set the key equal to the list of sorted, delimited IDs
std::sort(calendarIDs.begin(),
calendarIDs.end());
std::ostringstream s;
s << calendarIDs[0];
unsigned int i = 1;
while (i < calendarIDs.size()) {
s << m[2] << calendarIDs[i];
i++;
}
fullID = s.str();
return true;
}
QuantLib::Calendar *Create<QuantLib::Calendar>::makeJointCalendar2() {
QuantLib::Calendar *calendar1 =
static_cast<QuantLib::Calendar*>(this->getType(calendarIDs[0]));
QuantLib::Calendar *calendar2 =
static_cast<QuantLib::Calendar*>(this->getType(calendarIDs[1]));
return new QuantLib::JointCalendar(
*calendar1,
*calendar2,
jointCalendarRule);
}
QuantLib::Calendar *Create<QuantLib::Calendar>::makeJointCalendar3() {
QuantLib::Calendar *calendar1 =
static_cast<QuantLib::Calendar*>(this->getType(calendarIDs[0]));
QuantLib::Calendar *calendar2 =
static_cast<QuantLib::Calendar*>(this->getType(calendarIDs[1]));
QuantLib::Calendar *calendar3 =
static_cast<QuantLib::Calendar*>(this->getType(calendarIDs[2]));
return new QuantLib::JointCalendar(
*calendar1,
*calendar2,
*calendar3,
jointCalendarRule);
}
QuantLib::Calendar *Create<QuantLib::Calendar>::makeJointCalendar4() {
QuantLib::Calendar *calendar1 =
static_cast<QuantLib::Calendar*>(this->getType(calendarIDs[0]));
QuantLib::Calendar *calendar2 =
static_cast<QuantLib::Calendar*>(this->getType(calendarIDs[1]));
QuantLib::Calendar *calendar3 =
static_cast<QuantLib::Calendar*>(this->getType(calendarIDs[2]));
QuantLib::Calendar *calendar4 =
static_cast<QuantLib::Calendar*>(this->getType(calendarIDs[3]));
return new QuantLib::JointCalendar(
*calendar1,
*calendar2,
*calendar3,
*calendar4,
jointCalendarRule);
}
void Create<QuantLib::Calendar>::resetParameters() {
calendarIDs.clear();
fullID = "";
jointCalendarRule = QuantLib::JointCalendarRule();
}
QuantLib::Calendar Create<QuantLib::Calendar>::operator()(const std::string &id) {
resetParameters();
if (parseID(id)) {
if (checkType(fullID)) {
return *(static_cast<QuantLib::Calendar*>(this->getType(fullID)));
} else {
QuantLib::Calendar *jointCalendar;
switch (calendarIDs.size()) {
case 2:
jointCalendar = makeJointCalendar2();
break;
case 3:
jointCalendar = makeJointCalendar3();
break;
case 4:
jointCalendar = makeJointCalendar4();
break;
default:
QL_FAIL("error creating calendar");
}
registerType(fullID, jointCalendar);
return *jointCalendar;
}
} else {
return *(static_cast<QuantLib::Calendar*>(this->getType(id)));
}
}
}
Index: typefactory.hpp
===================================================================
RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/typefactory.hpp,v
retrieving revision 1.34
retrieving revision 1.35
diff -C2 -d -r1.34 -r1.35
*** typefactory.hpp 7 Sep 2006 15:40:57 -0000 1.34
--- typefactory.hpp 7 Sep 2006 21:27:37 -0000 1.35
***************
*** 32,35 ****
--- 32,36 ----
#include <ql/CashFlows/cmscoupon.hpp>
#include <ql/CashFlows/conundrumpricer.hpp>
+ #include <ql/Calendars/jointcalendar.hpp>
#include <oh/exception.hpp>
***************
*** 78,81 ****
--- 79,87 ----
return false;
}
+
+ void registerType(const std::string& id, void *type) {
+ typename RegistryClass::TypeMapPtr type_map = getTypeMap();
+ (*type_map)[id] = type;
+ }
private:
const typename RegistryClass::TypeMapPtr &getTypeMap() {
***************
*** 94,97 ****
--- 100,105 ----
/* *** Enumerated Types *** */
+
+ /* *** Generic *** */
template<typename T>
class Create : private RegistryManager<T, EnumTypeRegistry> {
***************
*** 103,106 ****
--- 111,132 ----
};
+ /* *** Calendar *** */
+ template<>
+ class Create<QuantLib::Calendar> :
+ private RegistryManager<QuantLib::Calendar, EnumTypeRegistry> {
+ public:
+ QuantLib::Calendar operator()(const std::string& id);
+ using RegistryManager<QuantLib::Calendar, EnumTypeRegistry>::checkType;
+ private:
+ std::vector<std::string> calendarIDs;
+ QuantLib::JointCalendarRule jointCalendarRule;
+ std::string fullID;
+ void resetParameters();
+ bool parseID(const std::string &id);
+ QuantLib::Calendar *makeJointCalendar2();
+ QuantLib::Calendar *makeJointCalendar3();
+ QuantLib::Calendar *makeJointCalendar4();
+ };
+
/* *** Enumerated Classes *** */
***************
*** 244,248 ****
QuantLib::Handle<QuantLib::YieldTermStructure> handleYieldTermStructure_;
};
! // a singleton to store the Handle<YieldTermStructure>
// shared by all enumerated Euribor365 classes
class Euribor365Handle : public QuantLib::Singleton<Euribor365Handle> {
--- 270,274 ----
QuantLib::Handle<QuantLib::YieldTermStructure> handleYieldTermStructure_;
};
! // a singleton to store the Handle<YieldTermStructure>
// shared by all enumerated Euribor365 classes
class Euribor365Handle : public QuantLib::Singleton<Euribor365Handle> {
--- calendar.hpp DELETED ---
Index: Makefile.am
===================================================================
RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/Makefile.am,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** Makefile.am 28 Aug 2006 10:05:25 -0000 1.3
--- Makefile.am 7 Sep 2006 21:27:37 -0000 1.4
***************
*** 14,18 ****
baseinstruments.hpp \
bonds.hpp \
! calendar.hpp \
capfloor.hpp \
capletvolstructure.hpp \
--- 14,18 ----
baseinstruments.hpp \
bonds.hpp \
! calendarfactory.cpp \
capfloor.hpp \
capletvolstructure.hpp \
***************
*** 58,62 ****
vcconfig.hpp \
vo_bonds.hpp \
- vo_calendar.hpp \
vo_capfloor.hpp \
vo_capletvolstructure.hpp \
--- 58,61 ----
***************
*** 88,92 ****
barrieroption.cpp \
bonds.cpp \
- calendar.cpp \
capfloor.cpp \
capletvolstructure.cpp \
--- 87,90 ----
***************
*** 125,129 ****
vanillaswap.cpp \
vo_bonds.cpp \
- vo_calendar.cpp \
vo_capfloor.cpp \
vo_capletvolstructure.cpp \
--- 123,126 ----
--- calendar.cpp DELETED ---
|