00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <RcppList.h>
00023
00024 RcppList::RcppList(void) : listArg(R_NilValue),
00025 listSize(0),
00026 currListPosn(0),
00027 numProtected(0) {
00028 };
00029
00030 RcppList::~RcppList(void) {
00031 UNPROTECT(numProtected);
00032 }
00033
00034 void RcppList::setSize(int n) {
00035 listSize = n;
00036 listArg = PROTECT(Rf_allocVector(VECSXP, n));
00037 numProtected++;
00038 }
00039
00040 void RcppList::append(std::string name, double value) {
00041 if (currListPosn < 0 || currListPosn >= listSize)
00042 throw std::range_error("RcppList::append(double): list posn out of range");
00043 SEXP valsxp = PROTECT(Rf_allocVector(REALSXP,1));
00044 numProtected++;
00045 REAL(valsxp)[0] = value;
00046 SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
00047 names.push_back(name);
00048 }
00049
00050 void RcppList::append(std::string name, int value) {
00051 if (currListPosn < 0 || currListPosn >= listSize)
00052 throw std::range_error("RcppList::append(int): posn out of range");
00053 SEXP valsxp = PROTECT(Rf_allocVector(INTSXP,1));
00054 numProtected++;
00055 INTEGER(valsxp)[0] = value;
00056 SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
00057 names.push_back(name);
00058 }
00059
00060 void RcppList::append(std::string name, std::string value) {
00061 if (currListPosn < 0 || currListPosn >= listSize)
00062 throw std::range_error("RcppList::append(string): posn out of range");
00063 SEXP valsxp = PROTECT(Rf_allocVector(STRSXP,1));
00064 numProtected++;
00065 SET_STRING_ELT(valsxp, 0, Rf_mkChar(value.c_str()));
00066 SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
00067 names.push_back(name);
00068 }
00069
00070 void RcppList::append(std::string name, SEXP sexp) {
00071 if (currListPosn < 0 || currListPosn >= listSize)
00072 throw std::range_error("RcppList::append(sexp): posn out of range");
00073 SET_VECTOR_ELT(listArg, currListPosn++, sexp);
00074 names.push_back(name);
00075 }
00076
00077 void RcppList::clearProtectionStack() {
00078 UNPROTECT(numProtected);
00079 numProtected = 0;
00080 }
00081
00082 SEXP RcppList::getList(void) const {
00083 SEXP li = PROTECT(Rf_allocVector(VECSXP, listSize));
00084 SEXP nm = PROTECT(Rf_allocVector(STRSXP, listSize));
00085 for (int i=0; i<listSize; i++) {
00086 SET_VECTOR_ELT(li, i, VECTOR_ELT(listArg, i));
00087 SET_STRING_ELT(nm, i, Rf_mkChar(names[i].c_str()));
00088 }
00089 Rf_setAttrib(li, R_NamesSymbol, nm);
00090 UNPROTECT(2);
00091 return li;
00092 }
00093
00094