|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 /* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */ 00003 // 00004 // exporter.h: Rcpp R/C++ interface class library -- identify if a class has a nested iterator typedef 00005 // 00006 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois 00007 // 00008 // This file is part of Rcpp. 00009 // 00010 // Rcpp is free software: you can redistribute it and/or modify it 00011 // under the terms of the GNU General Public License as published by 00012 // the Free Software Foundation, either version 2 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // Rcpp is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU General Public License 00021 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00022 00023 #ifndef Rcpp__traits__exporter__h 00024 #define Rcpp__traits__exporter__h 00025 00026 namespace Rcpp{ 00027 namespace traits{ 00028 00029 template <typename T> class Exporter{ 00030 public: 00031 Exporter( SEXP x ) : t(x){} 00032 inline T get(){ return t ; } 00033 00034 private: 00035 T t ; 00036 } ; 00037 00038 template <typename T> class RangeExporter { 00039 public: 00040 typedef typename T::value_type r_export_type ; 00041 00042 RangeExporter( SEXP x ) : object(x){} 00043 ~RangeExporter(){} 00044 00045 T get(){ 00046 T vec( ::Rf_length(object) ); 00047 ::Rcpp::internal::export_range( object, vec.begin() ) ; 00048 return vec ; 00049 } 00050 00051 private: 00052 SEXP object ; 00053 } ; 00054 00055 template <typename T, typename value_type> class IndexingExporter { 00056 public: 00057 typedef value_type r_export_type ; 00058 00059 IndexingExporter( SEXP x) : object(x){} 00060 ~IndexingExporter(){} 00061 00062 T get(){ 00063 T result( ::Rf_length(object) ) ; 00064 ::Rcpp::internal::export_indexing<T,value_type>( object, result ) ; 00065 return result ; 00066 } 00067 00068 private: 00069 SEXP object ; 00070 } ; 00071 00072 template <typename T, typename value_type> class MatrixExporter { 00073 public: 00074 typedef value_type r_export_type ; 00075 00076 MatrixExporter( SEXP x) : object(x){} 00077 ~MatrixExporter(){} 00078 00079 T get() { 00080 SEXP dims = PROTECT( ::Rf_getAttrib( object, R_DimSymbol ) ) ; 00081 if( dims == R_NilValue || ::Rf_length(dims) != 2 ){ 00082 throw ::Rcpp::not_a_matrix() ; 00083 } 00084 int* dims_ = INTEGER(dims) ; 00085 T result( dims_[0], dims_[1] ) ; 00086 ::Rcpp::internal::export_indexing<T,value_type>( object, result ) ; 00087 UNPROTECT(1) ; 00088 return result ; 00089 } 00090 00091 private: 00092 SEXP object ; 00093 } ; 00094 00095 00096 template <typename T> class Exporter< std::vector<T> > : public RangeExporter< std::vector<T> > { 00097 public: 00098 Exporter(SEXP x) : RangeExporter< std::vector<T> >(x){} 00099 }; 00100 template <typename T> class Exporter< std::deque<T> > : public RangeExporter< std::deque<T> > { 00101 public: 00102 Exporter(SEXP x) : RangeExporter< std::deque<T> >(x){} 00103 }; 00104 template <typename T> class Exporter< std::list<T> > : public RangeExporter< std::list<T> > { 00105 public: 00106 Exporter(SEXP x) : RangeExporter< std::list<T> >(x){} 00107 }; 00108 00109 } // namespace traits 00110 } // namespace Rcpp 00111 #endif