1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 2 // 3 // RObject.cpp: Rcpp R/C++ interface class library -- RObject unit tests 4 // 5 // Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois 6 // 7 // This file is part of Rcpp. 8 // 9 // Rcpp is free software: you can redistribute it and/or modify it 10 // under the terms of the GNU General Public License as published by 11 // the Free Software Foundation, either version 2 of the License, or 12 // (at your option) any later version. 13 // 14 // Rcpp is distributed in the hope that it will be useful, but 15 // WITHOUT ANY WARRANTY; without even the implied warranty of 16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 // GNU General Public License for more details. 18 // 19 // You should have received a copy of the GNU General Public License 20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 21 22 #include <Rcpp.h> 23 using namespace Rcpp ; 24 25 // [[Rcpp::export]] asDouble(double d)26double asDouble(double d){ 27 return 2*d ; 28 } 29 30 // [[Rcpp::export]] asInt(int i)31int asInt(int i){ 32 return 2*i; 33 } 34 35 // [[Rcpp::export]] asStdString(std::string s)36std::string asStdString(std::string s){ 37 return s+s ; 38 } 39 40 // [[Rcpp::export]] asRaw(Rbyte i)41Rbyte asRaw( Rbyte i ){ 42 return (Rbyte)(2*i) ; 43 } 44 45 // [[Rcpp::export]] asLogical(bool b)46bool asLogical( bool b){ 47 return !b ; 48 } 49 50 // [[Rcpp::export]] asStdVectorInt(SEXP x)51std::vector<int> asStdVectorInt( SEXP x){ 52 std::vector<int> iv = as< std::vector<int> >(x); 53 for (size_t i=0; i<iv.size(); i++) { 54 iv[i] = 2*iv[i]; 55 } 56 return iv ; 57 } 58 59 // [[Rcpp::export]] asStdVectorDouble(SEXP x)60std::vector<double> asStdVectorDouble(SEXP x){ 61 std::vector<double> iv = as< std::vector<double> >( x ); 62 for (size_t i=0; i<iv.size(); i++) { 63 iv[i] = 2*iv[i]; 64 } 65 return iv ; 66 } 67 68 // [[Rcpp::export]] asStdVectorRaw(SEXP x)69std::vector<Rbyte> asStdVectorRaw( SEXP x){ 70 std::vector<Rbyte> iv = as< std::vector<Rbyte> >( x ); 71 for (size_t i=0; i<iv.size(); i++) { 72 iv[i] = 2*iv[i]; 73 } 74 return iv ; 75 } 76 77 78 // [[Rcpp::export]] asStdVectorBool(SEXP x)79std::vector<bool> asStdVectorBool( SEXP x ){ 80 std::vector<bool> bv = as< std::vector<bool> >( x ); 81 for (size_t i=0; i<bv.size(); i++) { 82 bv[i].flip() ; 83 } 84 return bv ; 85 } 86 87 // [[Rcpp::export]] asStdVectorString(SEXP x)88std::vector<std::string> asStdVectorString( SEXP x){ 89 std::vector<std::string> iv = as< std::vector<std::string> >( x ); 90 for (size_t i=0; i<iv.size(); i++) { 91 iv[i] = iv[i] + iv[i]; 92 } 93 return iv ; 94 } 95 96 // [[Rcpp::export]] stdsetint()97std::set<int> stdsetint(){ 98 std::set<int> iv ; 99 iv.insert( 0 ) ; 100 iv.insert( 1 ) ; 101 iv.insert( 0 ) ; 102 return iv ; 103 } 104 105 // [[Rcpp::export]] stdsetdouble()106std::set<double> stdsetdouble(){ 107 std::set<double> ds; 108 ds.insert( 0.0 ); 109 ds.insert( 1.0 ); 110 ds.insert( 0.0 ); 111 return ds ; 112 } 113 114 // [[Rcpp::export]] stdsetraw()115std::set<Rbyte> stdsetraw(){ 116 std::set<Rbyte> bs ; 117 bs.insert( (Rbyte)0 ) ; 118 bs.insert( (Rbyte)1 ) ; 119 bs.insert( (Rbyte)0 ) ; 120 return bs ; 121 } 122 123 // [[Rcpp::export]] stdsetstring()124std::set<std::string> stdsetstring(){ 125 std::set<std::string> ss ; 126 ss.insert( "foo" ) ; 127 ss.insert( "bar" ) ; 128 ss.insert( "foo" ) ; 129 return ss ; 130 } 131 132 // [[Rcpp::export]] attributeNames(DataFrame x)133std::vector<std::string> attributeNames(DataFrame x){ 134 return x.attributeNames() ; 135 } 136 137 // [[Rcpp::export]] hasAttribute(DataFrame x)138bool hasAttribute( DataFrame x){ 139 bool has_class = x.hasAttribute( "class" ) ; 140 return has_class ; 141 } 142 143 // [[Rcpp::export]] attr_(DataFrame x)144SEXP attr_( DataFrame x){ 145 return x.attr( "row.names" ) ; 146 } 147 148 // [[Rcpp::export]] attr_set()149RObject attr_set(){ 150 RObject y = wrap("blabla") ; 151 y.attr("foo") = 10 ; 152 return y ; 153 } 154 155 // [[Rcpp::export]] isNULL(RObject x)156bool isNULL(RObject x){ 157 return x.isNULL() ; 158 } 159 160 // [[Rcpp::export]] inherits_(RObject xx)161bool inherits_( RObject xx){ 162 return xx.inherits( "foo" ) ; 163 } 164 165