// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- // // wrap.cpp: Rcpp R/C++ interface class library -- wrap unit tests // // Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois // // This file is part of Rcpp. // // Rcpp is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 2 of the License, or // (at your option) any later version. // // Rcpp is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Rcpp. If not, see . // Testing 'wrap', 'as' with custom class -- see // https://github.com/RcppCore/Rcpp/issues/165 template class Bar { public: Bar(T t) : t(t) {} ~Bar() {} T t; }; #include namespace Rcpp { template SEXP wrap(const Bar &b); } #include template SEXP Rcpp::wrap(const Bar &b) { return Rcpp::wrap(b.t); } // [[Rcpp::export]] Bar test_wrap_custom_class() { Bar b(42); return b; } using namespace Rcpp; // [[Rcpp::export]] IntegerVector map_string_int(){ std::map< std::string, int > m ; m["b"] = 100; m["a"] = 200; m["c"] = 300; return wrap(m); } // [[Rcpp::export]] NumericVector map_string_double(){ std::map m ; m["b"] = 100; m["a"] = 200; m["c"] = 300; return wrap(m); } // [[Rcpp::export]] LogicalVector map_string_bool(){ std::map m ; m["b"] = true; m["a"] = false; m["c"] = true; return wrap(m); } // [[Rcpp::export]] RawVector map_string_Rbyte(){ std::map m ; m["b"] = (Rbyte)0; m["a"] = (Rbyte)1; m["c"] = (Rbyte)2; return wrap(m); } // [[Rcpp::export]] CharacterVector map_string_string(){ std::map m ; m["b"] = "foo" ; m["a"] = "bar" ; m["c"] = "bling" ; return wrap(m); } // [[Rcpp::export]] List map_string_generic(){ std::map< std::string,std::vector > m ; std::vector b; b.push_back(1); b.push_back(2); m["b"] = b; std::vector a; a.push_back(1); a.push_back(2); a.push_back(2); m["a"] = a; std::vector c; c.push_back(1); c.push_back(2); c.push_back(2); c.push_back(2); m["c"] = c; return wrap(m); } // [[Rcpp::export]] IntegerVector multimap_string_int(){ std::multimap< std::string, int > m; m.insert( std::pair("b", 100)); m.insert( std::pair("a", 200)); m.insert( std::pair("c", 300)); return wrap(m); } // [[Rcpp::export]] NumericVector multimap_string_double(){ std::multimap m ; m.insert( std::pair("b", 100) ); m.insert( std::pair("a", 200) ); m.insert( std::pair("c", 300) ); return wrap(m); } // [[Rcpp::export]] LogicalVector multimap_string_bool(){ std::multimap m ; m.insert( std::pair("b", true ) ) ; m.insert( std::pair("a", false) ) ; m.insert( std::pair("c", true ) ) ; return wrap(m); } // [[Rcpp::export]] RawVector multimap_string_Rbyte(){ std::multimap m ; m.insert( std::pair("b", (Rbyte)0) ); m.insert( std::pair("a", (Rbyte)1) ); m.insert( std::pair("c", (Rbyte)2) ); return wrap(m); } // [[Rcpp::export]] CharacterVector multimap_string_string(){ std::multimap m ; m.insert( std::pair( "b", "foo" ) ) ; m.insert( std::pair( "a", "bar" ) ) ; m.insert( std::pair( "c", "bling") ) ; return wrap(m); } // [[Rcpp::export]] List multimap_string_generic(){ typedef std::pair > _pair ; std::multimap< std::string,std::vector > m ; std::vector b ; b.push_back(1) ; b.push_back(2) ; m.insert( _pair("b", b) ); std::vector a ; a.push_back(1) ; a.push_back(2) ; a.push_back(2) ; m.insert( _pair("a", a) ); std::vector c ; c.push_back(1) ; c.push_back(2) ; c.push_back(2) ; c.push_back(2) ; m.insert( _pair("c", c) ); return wrap(m); } // [[Rcpp::export]] SEXP nonnull_const_char(){ const char *p = "foo"; return wrap(p) ; } // [[Rcpp::export]] IntegerVector unordered_map_string_int(){ RCPP_UNORDERED_MAP< std::string, int > m ; m["b"] = 100; m["a"] = 200; m["c"] = 300; return wrap(m); } // [[Rcpp::export]] NumericVector unordered_map_string_double(){ RCPP_UNORDERED_MAP m ; m["b"] = 100; m["a"] = 200; m["c"] = 300; return wrap(m); } // [[Rcpp::export]] LogicalVector unordered_map_string_bool(){ RCPP_UNORDERED_MAP m ; m["b"] = true; m["a"] = false; m["c"] = true; return wrap(m) ; } // [[Rcpp::export]] RawVector unordered_map_string_Rbyte(){ RCPP_UNORDERED_MAP m ; m["b"] = (Rbyte)0; m["a"] = (Rbyte)1; m["c"] = (Rbyte)2; return wrap(m); } // [[Rcpp::export]] CharacterVector unordered_map_string_string(){ RCPP_UNORDERED_MAP m ; m["b"] = "foo" ; m["a"] = "bar" ; m["c"] = "bling" ; return wrap(m) ; } // [[Rcpp::export]] List unordered_map_string_generic(){ RCPP_UNORDERED_MAP< std::string,std::vector > m ; std::vector b; b.push_back(1); b.push_back(2); m["b"] = b ; std::vector a; a.push_back(1); a.push_back(2); a.push_back(2); m["a"] = a; std::vector c; c.push_back(1); c.push_back(2); c.push_back(2); c.push_back(2); m["c"] = c; return wrap(m); } // [[Rcpp::export]] SEXP map_int_double(){ std::map map ; map[0] = 2.0 ; map[-1] = 3.0 ; return wrap( map ) ; } // [[Rcpp::export]] SEXP map_double_double(){ std::map map ; map[0.0] = 2.0 ; map[1.2] = 3.0 ; return wrap( map ) ; } // [[Rcpp::export]] SEXP map_int_vector_double(){ std::map > map ; map[0].push_back( 1.0 ) ; map[0].push_back( 2.0 ) ; map[1].push_back( 2.0 ) ; map[1].push_back( 3.0 ) ; return wrap( map ) ; } RCPP_EXPOSED_CLASS(Foo) class Foo{ public: Foo() : x(0.0){} Foo( double x_ ) : x(x_){} double get() { return x ; } private: double x ; } ; RCPP_MODULE(mod){ class_("Foo") .constructor() .method( "get", &Foo::get ) ; } // [[Rcpp::export]] SEXP map_int_Foo(){ std::map map ; map[0] = Foo( 2 ) ; map[1] = Foo( 3 ) ; return wrap( map ) ; } // [[Rcpp::export]] SEXP vector_Foo(){ std::vector vec(2) ; vec[0] = Foo( 2 ) ; vec[1] = Foo( 3 ) ; return wrap(vec) ; }