00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <RcppSexp.h>
00023
00024 RcppSexp::RcppSexp(const double & v) {
00025 logTxt("RcppSexp from double\n");
00026 m_sexp = Rf_allocVector(REALSXP, 1);
00027 R_PreserveObject(m_sexp);
00028 REAL(m_sexp)[0] = v;
00029 }
00030
00031 RcppSexp::RcppSexp(const int & v) {
00032 logTxt("RcppSexp from int\n");
00033 m_sexp = Rf_allocVector(INTSXP, 1);
00034 R_PreserveObject(m_sexp);
00035 INTEGER(m_sexp)[0] = v;
00036 }
00037
00038 RcppSexp::RcppSexp(const std::string & v) {
00039 logTxt("RcppSexp from std::string\n");
00040 m_sexp = Rf_allocVector(STRSXP, 1);
00041 R_PreserveObject(m_sexp);
00042 SET_STRING_ELT(m_sexp, 0, Rf_mkChar(v.c_str()));
00043 }
00044
00045 RcppSexp::RcppSexp(const std::vector<int> & v) {
00046 logTxt("RcppSexp from int vector\n");
00047 int n = v.size();
00048 m_sexp = Rf_allocVector(INTSXP, n);
00049 R_PreserveObject(m_sexp);
00050 for (int i = 0; i < n; i++) {
00051 INTEGER(m_sexp)[i] = v[i];
00052 }
00053 }
00054
00055 RcppSexp::RcppSexp(const std::vector<double> & v) {
00056 logTxt("RcppSexp from double vector\n");
00057 int n = v.size();
00058 m_sexp = Rf_allocVector(REALSXP, n);
00059 R_PreserveObject(m_sexp);
00060 for (int i = 0; i < n; i++) {
00061 REAL(m_sexp)[i] = v[i];
00062 }
00063 }
00064
00065 RcppSexp::RcppSexp(const std::vector<std::string> & v) {
00066 logTxt("RcppSexp from std::string vector\n");
00067 int n = v.size();
00068 m_sexp = Rf_allocVector(STRSXP, n);
00069 R_PreserveObject(m_sexp);
00070 for (int i = 0; i < n; i++) {
00071 SET_STRING_ELT(m_sexp, i, Rf_mkChar(v[i].c_str()));
00072 }
00073 }
00074
00075 RcppSexp::~RcppSexp() {
00076 logTxt("dtor");
00077 R_ReleaseObject(m_sexp);
00078 }
00079
00080 double RcppSexp::asDouble() const {
00081 if (Rf_length(m_sexp) != 1) {
00082 throw std::range_error("RcppSexp::asDouble expects single value");
00083 }
00084 if (!Rf_isNumeric(m_sexp)) {
00085 throw std::range_error("RcppSexp::asDouble expect numeric type");
00086 }
00087 if (Rf_isInteger(m_sexp)) {
00088 return (double)INTEGER(m_sexp)[0];
00089 } else if (Rf_isReal(m_sexp)) {
00090 return REAL(m_sexp)[0];
00091 } else {
00092 throw std::range_error("RcppSexp::asDouble invalid type");
00093 }
00094 return 0;
00095 }
00096
00097 int RcppSexp::asInt() const {
00098 if (Rf_length(m_sexp) != 1) {
00099 throw std::range_error("RcppSexp::asInt expects single value");
00100 }
00101 if (!Rf_isNumeric(m_sexp)) {
00102 throw std::range_error("RcppSexp::asInt expects numeric type");
00103 }
00104 if (Rf_isInteger(m_sexp)) {
00105 return INTEGER(m_sexp)[0];
00106 } else if (Rf_isReal(m_sexp)) {
00107 return (int)REAL(m_sexp)[0];
00108 } else {
00109 std::string mesg = "RcppParams::asInt unknown type";
00110 }
00111 return 0;
00112 }
00113
00114 std::string RcppSexp::asStdString() const {
00115 if (Rf_length(m_sexp) != 1) {
00116 throw std::range_error("RcppSexp::asStdString expects single value");
00117 }
00118 if (!Rf_isString(m_sexp)) {
00119 throw std::range_error("RcppSexp::asStdString expects string");
00120 }
00121 return std::string(CHAR(STRING_ELT(m_sexp,0)));
00122 }
00123
00124 SEXP RcppSexp::asSexp() const {
00125 return m_sexp;
00126 }
00127
00128 std::vector<int> RcppSexp::asStdVectorInt() const {
00129 int n = Rf_length(m_sexp);
00130 std::vector<int> v(n);
00131 if (Rf_isInteger(m_sexp)) {
00132 for (int i = 0; i < n; i++) {
00133 v[i] = INTEGER(m_sexp)[i];
00134 }
00135 } else if (Rf_isReal(m_sexp)) {
00136 for (int i = 0; i < n; i++) {
00137 v[i] = (int)REAL(m_sexp)[i];
00138 }
00139 }
00140 return v;
00141 }
00142
00143
00144 std::vector<double> RcppSexp::asStdVectorDouble() const {
00145 int n = Rf_length(m_sexp);
00146 std::vector<double> v(n);
00147 if (Rf_isInteger(m_sexp)) {
00148 for (int i = 0; i < n; i++) {
00149 v[i] = (double)INTEGER(m_sexp)[i];
00150 }
00151 } else if (Rf_isReal(m_sexp)) {
00152 for (int i = 0; i < n; i++) {
00153 v[i] = REAL(m_sexp)[i];
00154 }
00155 }
00156 return v;
00157 }
00158
00159
00160 std::vector<std::string> RcppSexp::asStdVectorString() const {
00161 int n = Rf_length(m_sexp);
00162 std::vector<std::string> v(n);
00163 if (!Rf_isString(m_sexp)) {
00164 throw std::range_error("RcppSexp::asStdVectorString expects string");
00165 }
00166 for (int i = 0; i < n; i++) {
00167 v[i] = std::string(CHAR(STRING_ELT(m_sexp,i)));
00168 }
00169 return v;
00170 }
00171