[Rcpp-commits] r1520 - in pkg/Rcpp: inst/include src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Jun 12 02:48:48 CEST 2010
Author: edd
Date: 2010-06-12 02:48:48 +0200 (Sat, 12 Jun 2010)
New Revision: 1520
Modified:
pkg/Rcpp/inst/include/RcppResultSet.h
pkg/Rcpp/src/RcppResultSet.cpp
Log:
carved out four new Rcpp::wrap() instances for RcppDate(time)?(Vector)? by
splitting the existing RcppResultSet.add() into these Rcpp::wrap() instanced
and a call each
Modified: pkg/Rcpp/inst/include/RcppResultSet.h
===================================================================
--- pkg/Rcpp/inst/include/RcppResultSet.h 2010-06-11 13:16:43 UTC (rev 1519)
+++ pkg/Rcpp/inst/include/RcppResultSet.h 2010-06-12 00:48:48 UTC (rev 1520)
@@ -4,6 +4,7 @@
//
// Copyright (C) 2005 - 2006 Dominick Samperi
// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -37,6 +38,13 @@
#include <RcppStringVector.h>
#include <RcppVector.h>
+namespace Rcpp {
+ SEXP wrap(RcppDate &date);
+ SEXP wrap(RcppDatetime &date);
+ SEXP wrap(RcppDateVector &datevec);
+ SEXP wrap(RcppDatetimeVector &dtvec);
+}
+
class RcppResultSet {
public:
RcppResultSet();
Modified: pkg/Rcpp/src/RcppResultSet.cpp
===================================================================
--- pkg/Rcpp/src/RcppResultSet.cpp 2010-06-11 13:16:43 UTC (rev 1519)
+++ pkg/Rcpp/src/RcppResultSet.cpp 2010-06-12 00:48:48 UTC (rev 1520)
@@ -4,6 +4,7 @@
//
// Copyright (C) 2005 - 2006 Dominick Samperi
// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -24,29 +25,43 @@
RcppResultSet::RcppResultSet() : numProtected(0) { }
-void RcppResultSet::add(std::string name, RcppDate& date) {
+namespace Rcpp {
+SEXP wrap(RcppDate &date) {
SEXP value = PROTECT(Rf_allocVector(REALSXP, 1));
- numProtected++;
+ //numProtected++;
REAL(value)[0] = date.getJDN() - RcppDate::Jan1970Offset;
SEXP dateclass = PROTECT(Rf_allocVector(STRSXP,1));
- numProtected++;
+ //numProtected++;
SET_STRING_ELT(dateclass, 0, Rf_mkChar("Date"));
Rf_setAttrib(value, R_ClassSymbol, dateclass);
- values.push_back(make_pair(name, value));
+ UNPROTECT(2);
+ return value;
}
+}
-void RcppResultSet::add(std::string name, RcppDatetime& datetime) {
+void RcppResultSet::add(std::string name, RcppDate& date) {
+ values.push_back(make_pair(name, Rcpp::wrap(date)));
+}
+
+namespace Rcpp {
+SEXP wrap(RcppDatetime &datetime) {
SEXP value = PROTECT(Rf_allocVector(REALSXP, 1));
- numProtected++;
+ //numProtected++;
REAL(value)[0] = datetime.getFractionalTimestamp();
SEXP datetimeclass = PROTECT(Rf_allocVector(STRSXP,2));
- numProtected++;
+ //numProtected++;
SET_STRING_ELT(datetimeclass, 0, Rf_mkChar("POSIXt"));
SET_STRING_ELT(datetimeclass, 1, Rf_mkChar("POSIXct"));
Rf_setAttrib(value, R_ClassSymbol, datetimeclass);
- values.push_back(make_pair(name, value));
+ UNPROTECT(2);
+ return value;
}
+}
+void RcppResultSet::add(std::string name, RcppDatetime& datetime) {
+ values.push_back(make_pair(name, Rcpp::wrap(datetime)));
+}
+
void RcppResultSet::add(std::string name, double x) {
SEXP value = PROTECT(Rf_allocVector(REALSXP, 1));
numProtected++;
@@ -78,33 +93,47 @@
values.push_back(make_pair(name, value));
}
-void RcppResultSet::add(std::string name, RcppDateVector& datevec) {
+namespace Rcpp {
+SEXP wrap(RcppDateVector& datevec) {
SEXP value = PROTECT(Rf_allocVector(REALSXP, datevec.size()));
- numProtected++;
+ //numProtected++;
for (int i = 0; i < datevec.size(); i++) {
REAL(value)[i] = datevec(i).getJDN() - RcppDate::Jan1970Offset;
}
SEXP dateclass = PROTECT(Rf_allocVector(STRSXP,1));
- numProtected++;
+ //numProtected++;
SET_STRING_ELT(dateclass, 0, Rf_mkChar("Date"));
Rf_setAttrib(value, R_ClassSymbol, dateclass);
- values.push_back(make_pair(name, value));
+ UNPROTECT(2);
+ return value;
}
+}
-void RcppResultSet::add(std::string name, RcppDatetimeVector &dtvec) {
+void RcppResultSet::add(std::string name, RcppDateVector& datevec) {
+ values.push_back(make_pair(name, Rcpp::wrap(datevec)));
+}
+
+namespace Rcpp {
+SEXP wrap(RcppDatetimeVector &dtvec) {
SEXP value = PROTECT(Rf_allocVector(REALSXP, dtvec.size()));
- numProtected++;
+ //numProtected++;
for (int i = 0; i < dtvec.size(); i++) {
REAL(value)[i] = dtvec(i).getFractionalTimestamp();
}
SEXP datetimeclass = PROTECT(Rf_allocVector(STRSXP,2));
- numProtected++;
+ //numProtected++;
SET_STRING_ELT(datetimeclass, 0, Rf_mkChar("POSIXt"));
SET_STRING_ELT(datetimeclass, 1, Rf_mkChar("POSIXct"));
Rf_setAttrib(value, R_ClassSymbol, datetimeclass);
- values.push_back(make_pair(name, value));
+ UNPROTECT(2);
+ return value;
}
+}
+void RcppResultSet::add(std::string name, RcppDatetimeVector &dtvec) {
+ values.push_back(make_pair(name, Rcpp::wrap(dtvec)));
+}
+
void RcppResultSet::add(std::string name, RcppStringVector& stringvec) {
int len = (int)stringvec.size();
SEXP value = PROTECT(Rf_allocVector(STRSXP, len));
More information about the Rcpp-commits
mailing list