|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 // 00003 // Language.h: Rcpp R/C++ interface class library -- language objects (calls) 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_Language_h 00023 #define Rcpp_Language_h 00024 00025 #include <RcppCommon.h> 00026 #include <Rcpp/DottedPair.h> 00027 #include <Rcpp/Symbol.h> 00028 #include <Rcpp/Function.h> 00029 #include <Rcpp/grow.h> 00030 #include <Rcpp/r_cast.h> 00031 00032 namespace Rcpp{ 00033 00039 class Language : public DottedPair { 00040 public: 00041 00042 Language() ; 00043 00050 Language(SEXP lang) ; 00051 00052 Language(const Language& other) ; 00053 Language& operator=(const Language& other) ; 00054 00064 explicit Language( const std::string& symbol ); 00065 00074 explicit Language( const Symbol& symbol ); 00075 00081 explicit Language( const Function& function) ; 00082 00100 #ifdef HAS_VARIADIC_TEMPLATES 00101 template<typename... Args> 00102 Language( const std::string& symbol, const Args&... args) : DottedPair(Rf_install(symbol.c_str()), args...) { 00103 update() ; 00104 } 00105 template<typename... Args> 00106 Language( const Function& function, const Args&... args) : DottedPair(function, args...) { 00107 update() ; 00108 } 00109 #else 00110 00111 #include <Rcpp/generated/Language__ctors.h> 00112 00113 #endif 00114 00118 void setSymbol( const std::string& symbol); 00119 00123 void setSymbol( const Symbol& symbol ) ; 00124 00128 void setFunction( const Function& function) ; 00129 00133 SEXP eval() ; 00134 00138 SEXP eval(SEXP env) ; 00139 00140 ~Language() ; 00141 00142 private: 00143 virtual void update() ; 00144 00145 }; 00146 00147 template <typename OUT=SEXP> 00148 class fixed_call { 00149 public: 00150 typedef OUT result_type ; 00151 00152 fixed_call( Language call_ ) : call(call_){} 00153 fixed_call( Function fun ) : call(fun){} 00154 00155 OUT operator()(){ 00156 return as<OUT>( call.eval() ) ; 00157 } 00158 00159 private: 00160 Language call ; 00161 } ; 00162 00163 template <typename T, typename OUT = SEXP> 00164 class unary_call : public std::unary_function<T,OUT> { 00165 public: 00166 unary_call( Language call_ ) : call(call_), proxy(call_,1) {} 00167 unary_call( Language call_, int index ) : call(call_), proxy(call_,index){} 00168 unary_call( Function fun ) : call( fun, R_NilValue), proxy(call,1) {} 00169 00170 OUT operator()( const T& object ){ 00171 proxy = object ; 00172 return as<OUT>( call.eval() ) ; 00173 } 00174 00175 private: 00176 Language call ; 00177 Language::Proxy proxy ; 00178 } ; 00179 00180 template <typename T1, typename T2, typename OUT = SEXP> 00181 class binary_call : public std::binary_function<T1,T2,OUT> { 00182 public: 00183 binary_call( Language call_ ) : call(call_), proxy1(call_,1), proxy2(call_,2) {} 00184 binary_call( Language call_, int index1, int index2 ) : call(call_), proxy1(call_,index1), proxy2(call_,index2){} 00185 binary_call( Function fun) : call(fun, R_NilValue, R_NilValue), proxy1(call,1), proxy2(call,2){} 00186 00187 OUT operator()( const T1& o1, const T2& o2 ){ 00188 proxy1 = o1 ; 00189 proxy2 = o2 ; 00190 return as<OUT>( call.eval() ) ; 00191 } 00192 00193 private: 00194 Language call ; 00195 Language::Proxy proxy1 ; 00196 Language::Proxy proxy2 ; 00197 } ; 00198 00199 } // namespace Rcpp 00200 00201 #endif