Update of /cvsroot/quantlibaddin/QuantLibAddin/qlo
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv17283/qlo
Modified Files:
calendarfactory.cpp typefactory.hpp
Log Message:
calendar factory
Index: calendarfactory.cpp
===================================================================
RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/calendarfactory.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** calendarfactory.cpp 7 Sep 2006 21:27:37 -0000 1.1
--- calendarfactory.cpp 8 Sep 2006 10:40:53 -0000 1.2
***************
*** 17,27 ****
#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
--- 17,59 ----
#include <oh/objhandlerdefines.hpp>
#include <boost/regex.hpp>
#include <qlo/typefactory.hpp>
+ #include <set>
namespace QuantLibAddin {
+ /* Calendar factory - accept a string, and return either a
+ QuantLib::Calendar or a QuantLib::JointCalendar as appropriate
+ */
+
+ QuantLib::Calendar Create<QuantLib::Calendar>::operator()(const std::string &id) {
+ // Is this an ID for a Calendar or a JointCalendar?
+ if (parseID(id)) {
+ // It's a JointCalendar. Does the requested JointCalendar already exist?
+ if (checkType(fullID)) {
+ // It does - return it.
+ return *(static_cast<QuantLib::Calendar*>(this->getType(fullID)));
+ } else {
+ // It doesn't - create it, add it to the registry, and return it.
+ QuantLib::Calendar *jointCalendar = makeJointCalendar();
+ registerType(fullID, jointCalendar);
+ return *jointCalendar;
+ }
+ } else {
+ // the ID is for a Calendar - return it
+ return *(static_cast<QuantLib::Calendar*>(this->getType(id)));
+ }
+ }
+
+ /*
+ Accept a string and test whether it's the ID of a joint calendar.
+ A JointCalendar ID is in a format such as
+ UnitedKingdom::Exchange^UnitedStates::Settlement
+ - there may be 2, 3 or 4 calendars
+ - a calendar name may consist of letters and colons (:)
+ - the calendar names are delimited by one of two characters:
+ ^ - QuantLib::JoinHolidays
+ + - QuantLib::JoinBusinessDays
+ */
bool Create<QuantLib::Calendar>::parseID(const std::string &id) {
// strip out whitespace
***************
*** 29,33 ****
std::string idStrip = boost::regex_replace(id, regex_whitespace, "");
! // parse the ID
static boost::regex jointCalendarID(
"([\\w:]+)(\\+|\\^)([\\w:]+)(?:\\2([\\w:]+))?(?:\\2([\\w:]+))?");
--- 61,65 ----
std::string idStrip = boost::regex_replace(id, regex_whitespace, "");
! // parse the ID.
static boost::regex jointCalendarID(
"([\\w:]+)(\\+|\\^)([\\w:]+)(?:\\2([\\w:]+))?(?:\\2([\\w:]+))?");
***************
*** 36,54 ****
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 <<
--- 68,97 ----
return false;
! // Derive the inputs to the JointCalendar constructor.
!
! // Add the calendar IDs to a set where they will be uniquely sorted.
! std::set<std::string> calendarIdSet;
!
! // Given that the regex succeeded, we are guaranteed :-) to have
! // m[1] - the ID of the first calendar
! // m[2] - the delimiter
! // m[3] - the ID of the second calendar
! calendarIdSet.insert(m[1]);
! if (m[2] == "^")
jointCalendarRule = QuantLib::JointCalendarRule(QuantLib::JoinHolidays);
! else // "+"
! jointCalendarRule = QuantLib::JointCalendarRule(QuantLib::JoinBusinessDays);
! calendarIdSet.insert(m[3]);
+ // we may have a 3rd or a 3rd & 4th calendar
if (m[5].matched) {
! calendarIdSet.insert(m[4]);
! calendarIdSet.insert(m[5]);
} else if (m[4].matched) {
! calendarIdSet.insert(m[4]);
} else if (m[3].matched) {
;
+ // given that the regex succeeded, we can't arrive here
+ // so the test below is just a paranoid sanity check
} else {
QL_FAIL("the string '" << id <<
***************
*** 56,74 ****
}
! // 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 =
--- 99,137 ----
}
! // set the key "fullID" equal to the list of sorted, delimited IDs
! // and transfer the IDs from the set to a vector
! std::set<std::string>::const_iterator i = calendarIdSet.begin();
std::ostringstream s;
! s << *i;
! calendarIDs.push_back(*i);
! i++;
! while (i != calendarIdSet.end()) {
! calendarIDs.push_back(*i);
! s << m[2] << *i;
i++;
}
fullID = s.str();
+ // if the list of calendars contained duplicates,
+ // we may end up with just one value, which is invalid
+ QL_REQUIRE(calendarIDs.size() > 1, "the string '" << id <<
+ "' is not a valid joint calendar identifier");
+
return true;
}
+ QuantLib::Calendar *Create<QuantLib::Calendar>::makeJointCalendar() {
+ switch (calendarIDs.size()) {
+ case 2:
+ return makeJointCalendar2();
+ case 3:
+ return makeJointCalendar3();
+ case 4:
+ return makeJointCalendar4();
+ default:
+ QL_FAIL("error creating calendar");
+ }
+ }
+
QuantLib::Calendar *Create<QuantLib::Calendar>::makeJointCalendar2() {
QuantLib::Calendar *calendar1 =
***************
*** 113,150 ****
}
- 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)));
- }
- }
-
}
--- 176,179 ----
Index: typefactory.hpp
===================================================================
RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/typefactory.hpp,v
retrieving revision 1.36
retrieving revision 1.37
diff -C2 -d -r1.36 -r1.37
*** typefactory.hpp 8 Sep 2006 09:02:02 -0000 1.36
--- typefactory.hpp 8 Sep 2006 10:40:53 -0000 1.37
***************
*** 121,126 ****
QuantLib::JointCalendarRule jointCalendarRule;
std::string fullID;
- void resetParameters();
bool parseID(const std::string &id);
QuantLib::Calendar *makeJointCalendar2();
QuantLib::Calendar *makeJointCalendar3();
--- 121,126 ----
QuantLib::JointCalendarRule jointCalendarRule;
std::string fullID;
bool parseID(const std::string &id);
+ QuantLib::Calendar *makeJointCalendar();
QuantLib::Calendar *makeJointCalendar2();
QuantLib::Calendar *makeJointCalendar3();
|