[Rquantlib-commits] r241 - in pkg/RQuantLib: inst src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Jun 12 04:33:49 CEST 2010
Author: edd
Date: 2010-06-12 04:33:48 +0200 (Sat, 12 Jun 2010)
New Revision: 241
Modified:
pkg/RQuantLib/inst/ChangeLog
pkg/RQuantLib/src/calendars.cpp
Log:
some simplification in the code as we move to new Rcpp API
(but note that some of the date/datetime wrap'ing will currently require Rcpp from SVN)
Modified: pkg/RQuantLib/inst/ChangeLog
===================================================================
--- pkg/RQuantLib/inst/ChangeLog 2010-06-10 21:57:05 UTC (rev 240)
+++ pkg/RQuantLib/inst/ChangeLog 2010-06-12 02:33:48 UTC (rev 241)
@@ -1,3 +1,8 @@
+2010-06-11 Dirk Eddelbuettel <edd at debian.org>
+
+ * src/calendars.cpp: Simplified code by using more of Rcpp's new API
+ and some of this will depend on Rcpp (> 0.8.2) ie SVN as of now
+
2010-06-09 Dirk Eddelbuettel <edd at debian.org>
* man/DiscountCurve.Rd: Uncomment futures entries as there are
Modified: pkg/RQuantLib/src/calendars.cpp
===================================================================
--- pkg/RQuantLib/src/calendars.cpp 2010-06-10 21:57:05 UTC (rev 240)
+++ pkg/RQuantLib/src/calendars.cpp 2010-06-12 02:33:48 UTC (rev 241)
@@ -2,7 +2,8 @@
//
// RQuantLib -- R interface to the QuantLib libraries
//
-// Copyright (C) 2002 - 2009 Dirk Eddelbuettel <edd at debian.org>
+// Copyright (C) 2002 - 2009 Dirk Eddelbuettel
+// Copyright (C) 2010 Dirk Eddelbuettel and Khanh Nguyen
//
// $Id$
//
@@ -24,14 +25,8 @@
#include "rquantlib.hpp"
-
-
-
RcppExport SEXP QL_isBusinessDay(SEXP calParameters, SEXP dateSexp){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
RcppParams rparam(calParameters);
@@ -100,31 +95,22 @@
QuantLib::Date day( dateFromR(dates(i)) );
bizdays[i] = pcal->isBusinessDay(day);
}
-
delete pcal;
- RcppResultSet rs;
- rs.add("bizdays", bizdays);
- rl = rs.getReturnList();
+ return Rcpp::List::create(Rcpp::Named("bizday") = bizdays);
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+
+ return R_NilValue;
}
RcppExport SEXP QL_isHoliday(SEXP calParameters, SEXP dateSexp){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
Calendar* pcal = getCalendar(calParameters);
@@ -136,24 +122,16 @@
QuantLib::Date day( dateFromR(dates(i)) );
hdays[i] = pcal->isHoliday(day);
}
-
-
delete pcal;
- RcppResultSet rs;
- rs.add("holidays", hdays);
- rl = rs.getReturnList();
+ return Rcpp::List::create(Rcpp::Named("holidays") = hdays);
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+ return R_NilValue;
}
@@ -161,9 +139,6 @@
RcppExport SEXP QL_isWeekend(SEXP calParameters, SEXP dateSexp){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
Calendar* pcal = getCalendar(calParameters);
@@ -175,32 +150,22 @@
QuantLib::Date day( dateFromR(dates(i)) );
weekends[i] = pcal->isWeekend(day.weekday());
}
-
-
delete pcal;
- RcppResultSet rs;
- rs.add("weekend", weekends);
- rl = rs.getReturnList();
+ return Rcpp::List::create(Rcpp::Named("weekend") = weekends);
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+
+ return R_NilValue;
}
RcppExport SEXP QL_isEndOfMonth(SEXP calParameters, SEXP dateSexp){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
Calendar* pcal = getCalendar(calParameters);
@@ -212,32 +177,22 @@
QuantLib::Date day( dateFromR(dates(i)) );
eom[i] = pcal->isEndOfMonth(day);
}
-
-
delete pcal;
- RcppResultSet rs;
- rs.add("End.Of.Month", eom);
- rl = rs.getReturnList();
+ return Rcpp::List::create(Rcpp::Named("End.Of.Month") = eom);
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+
+ return R_NilValue;
}
RcppExport SEXP QL_endOfMonth(SEXP calParameters, SEXP dateSexp){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
Calendar* pcal = getCalendar(calParameters);
@@ -245,41 +200,28 @@
int n = dates.size();
std::vector<QuantLib::Date> eom(n);
- RcppDateVector ret(dateSexp);
- RcppResultSet rs;
-
for (int i=0; i<n; i++) {
QuantLib::Date day( dateFromR(dates(i)) );
eom[i] = pcal->endOfMonth(day);
- ret(i) = RcppDate(eom[i].month(), eom[i].dayOfMonth(), eom[i].year());
+ dates(i) = RcppDate(eom[i].month(), eom[i].dayOfMonth(), eom[i].year());
}
-
- rs.add("ret", ret);
delete pcal;
-
-
- rl = rs.getReturnList();
+ return Rcpp::List::create(Rcpp::Named("ret") = Rcpp::wrap(dates));
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+
+ return R_NilValue;
}
RcppExport SEXP QL_adjust(SEXP calParameters, SEXP dateSexp){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
Calendar* pcal = getCalendar(calParameters);
RcppParams rparam(calParameters);
@@ -289,40 +231,29 @@
int n = dates.size();
std::vector<QuantLib::Date> adjusted(n);
- RcppDateVector ret(dateSexp);
- RcppResultSet rs;
-
for (int i=0; i<n; i++) {
QuantLib::Date day( dateFromR(dates(i)) );
adjusted[i] = pcal->adjust(day, bdc);
- ret(i) = RcppDate(adjusted[i].month(),
- adjusted[i].dayOfMonth(),
- adjusted[i].year());
+ dates(i) = RcppDate(adjusted[i].month(),
+ adjusted[i].dayOfMonth(),
+ adjusted[i].year());
}
-
- rs.add("ret", ret);
delete pcal;
-
- rl = rs.getReturnList();
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ return Rcpp::List::create(Rcpp::Named("ret") = Rcpp::wrap(dates));
+
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+
+ return R_NilValue;
}
RcppExport SEXP QL_advance1(SEXP calParameters, SEXP dateSexp){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
Calendar* pcal = getCalendar(calParameters);
RcppParams rparam(calParameters);
@@ -336,40 +267,29 @@
int n = dates.size();
std::vector<QuantLib::Date> advance(n);
- RcppDateVector ret(dateSexp);
- RcppResultSet rs;
-
for (int i=0; i<n; i++) {
QuantLib::Date day( dateFromR(dates(i)) );
advance[i] = pcal->advance(day, amount,getTimeUnit(unit), bdc, (emr == 1)?true:false );
- ret(i) = RcppDate(advance[i].month(),
- advance[i].dayOfMonth(),
- advance[i].year());
+ dates(i) = RcppDate(advance[i].month(),
+ advance[i].dayOfMonth(),
+ advance[i].year());
}
-
- rs.add("ret", ret);
delete pcal;
-
- rl = rs.getReturnList();
+
+ return Rcpp::List::create(Rcpp::Named("ret") = Rcpp::wrap(dates));
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+
+ return R_NilValue;
}
RcppExport SEXP QL_advance2(SEXP calParameters, SEXP dateSexp){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
Calendar* pcal = getCalendar(calParameters);
RcppParams rparam(calParameters);
@@ -382,40 +302,30 @@
int n = dates.size();
std::vector<QuantLib::Date> advance(n);
- RcppDateVector ret(dateSexp);
- RcppResultSet rs;
-
for (int i=0; i<n; i++) {
QuantLib::Date day( dateFromR(dates(i)) );
advance[i] = pcal->advance(day, Period(getFrequency(period)),
bdc, (emr == 1)?true:false );
- ret(i) = RcppDate(advance[i].month(),
- advance[i].dayOfMonth(),
- advance[i].year());
+ dates(i) = RcppDate(advance[i].month(),
+ advance[i].dayOfMonth(),
+ advance[i].year());
}
-
- rs.add("ret", ret);
delete pcal;
-
- rl = rs.getReturnList();
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ return Rcpp::List::create(Rcpp::Named("ret") = Rcpp::wrap(dates));
+
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+
+ return R_NilValue;
}
+
RcppExport SEXP QL_businessDaysBetween(SEXP calParameters,
SEXP from, SEXP to){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
Calendar* pcal = getCalendar(calParameters);
RcppParams rparam(calParameters);
@@ -428,9 +338,6 @@
int n = dates1.size();
std::vector<double> between(n);
-
- RcppResultSet rs;
-
for (int i=0; i<n; i++) {
QuantLib::Date day1( dateFromR(dates1(i)) );
QuantLib::Date day2( dateFromR(dates2(i)) );
@@ -438,22 +345,17 @@
(ifirst == 1)?true:false,
(ilast ==1)?true:false);
}
-
- rs.add("ret", between);
delete pcal;
- rl = rs.getReturnList();
+ return Rcpp::List::create(Rcpp::Named("ret") = Rcpp::wrap(between));
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+
+ return R_NilValue;
}
@@ -461,9 +363,6 @@
SEXP from,
SEXP to){
- SEXP rl=R_NilValue;
- char* exceptionMesg=NULL;
-
try {
Calendar* pcal = getCalendar(calParameters);
RcppParams rparam(calParameters);
@@ -491,25 +390,22 @@
frame.addRow(row);
}
-
delete pcal;
+
RcppResultSet rs;
if (holidays.size() > 0)
rs.add("ret", frame);
else
rs.add("ret", -1);
- rl = rs.getReturnList();
+ return rs.getReturnList();
- } catch(std::exception& ex) {
- exceptionMesg = copyMessageToR(ex.what());
- } catch(...) {
- exceptionMesg = copyMessageToR("unknown reason");
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ } catch(...) {
+ ::Rf_error("c++ exception (unknown reason)");
}
-
- if(exceptionMesg != NULL)
- Rf_error(exceptionMesg);
-
- return rl;
+
+ return R_NilValue;
}
More information about the Rquantlib-commits
mailing list