Rcpp Version 0.9.10
as.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 //
00003 // as.h: Rcpp R/C++ interface class library -- convert SEXP to C++ objects
00004 //
00005 // Copyright (C) 2009 - 2011    Dirk Eddelbuettel and Romain Francois
00006 //
00007 // This file is part of Rcpp.
00008 //
00009 // Rcpp is free software: you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 2 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // Rcpp is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 #ifndef Rcpp__as__h
00023 #define Rcpp__as__h
00024 
00025 namespace Rcpp{
00026 
00027     namespace internal{
00028         
00029         template <typename T> T as( SEXP x, ::Rcpp::traits::r_type_primitive_tag ) {
00030             if( ::Rf_length(x) != 1 ) throw ::Rcpp::not_compatible( "expecting a single value" ) ;
00031             const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype ;
00032             SEXP y = PROTECT( r_cast<RTYPE>(x) );
00033             typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE;
00034             T res = caster<STORAGE,T>( *r_vector_start<RTYPE,STORAGE>( y ) ) ;
00035             UNPROTECT(1) ;
00036             return res ; 
00037         }
00038         
00039         template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_string_tag ) {
00040             if( ! ::Rf_isString(x) ){
00041                 throw ::Rcpp::not_compatible( "expecting a string" ) ;
00042             }
00043             if (Rf_length(x) != 1) {
00044                 throw ::Rcpp::not_compatible( "expecting a single value");
00045             }
00046             return T( CHAR( STRING_ELT( ::Rcpp::r_cast<STRSXP>(x) ,0 ) ) ) ;
00047         }
00048         
00049         template <typename T> T as(SEXP x, ::Rcpp::traits::r_type_generic_tag ) {
00050             RCPP_DEBUG_1( "as(SEXP = <%p>, r_type_generic_tag )", x ) ;
00051             ::Rcpp::traits::Exporter<T> exporter(x);
00052             RCPP_DEBUG_1( "exporter type = %s", DEMANGLE(exporter) ) ;
00053             return exporter.get() ;
00054         }
00055         
00056     }
00057         
00058         
00074     template <typename T> T as( SEXP m_sexp) {
00075         return internal::as<T>( m_sexp, typename traits::r_type_traits<T>::r_category() ) ;
00076     }
00077 
00078     template<> inline SEXP as(SEXP m_sexp) { return m_sexp ; }
00079 
00080 } // Rcpp 
00081 
00082 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines