Rcpp Version 0.9.10
grow.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
00002 //
00003 // grow.h: Rcpp R/C++ interface class library -- grow a pairlist
00004 //
00005 // Copyright (C) 2010 - 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_grow_h
00023 #define Rcpp_grow_h
00024 
00025 #include <RcppCommon.h>
00026 #include <Rcpp/Named.h>
00027 
00028 namespace Rcpp {
00029 
00030     inline SEXP pairlist() { return R_NilValue ; }
00031 
00032     namespace internal {
00033 
00034         template <typename T>
00035         SEXP grow__dispatch( ::Rcpp::traits::false_type, const T& head, SEXP tail ){
00036             SEXP x = PROTECT( wrap( head ) ) ;
00037             SEXP res = PROTECT( Rf_cons( x, tail ) ) ;
00038             UNPROTECT(2) ;
00039             return res ;
00040         }
00041 
00042         template <typename T>
00043         SEXP grow__dispatch( ::Rcpp::traits::true_type, const T& head, SEXP tail ){
00044             SEXP y = PROTECT( wrap( head.object) ) ;
00045             SEXP x = PROTECT( Rf_cons( y , tail) ) ;
00046             SEXP headNameSym = ::Rf_install( head.name.c_str() ); // cannot be gc()ed once in symbol table
00047             SET_TAG( x, headNameSym ); 
00048             UNPROTECT(2); 
00049             return x;   
00050         }
00051 
00052     } // namespace internal
00053 
00054 
00059     template <typename T>
00060     SEXP grow(const T& head, SEXP tail) {
00061         return internal::grow__dispatch( typename traits::is_named<T>::type(), head, tail );
00062     }
00063 
00064 #ifdef HAS_VARIADIC_TEMPLATES
00065 
00066     /* end of the recursion, wrap first to make the CAR and use R_NilValue as the CDR of the list */
00067     template<typename T>
00068     SEXP pairlist( const T& first){
00069         return grow(first, R_NilValue ); 
00070     }
00071 
00072     template<typename T, typename... Args>
00073     SEXP pairlist( const T& first, const Args&... args ){
00074         return grow(first, pairlist(args...) );
00075     }
00076 
00077 #else
00078 
00079 #include <Rcpp/generated/grow__pairlist.h>
00080 
00081 #endif
00082 
00083 } // namespace Rcpp
00084 
00085 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines