Rcpp Version 0.9.10
preprocessor.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
00002 //
00003 // preprocessor.h: Rcpp R/C++ interface class library -- pre processor help
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__PREPROCESSOR_h
00023 #define Rcpp__PREPROCESSOR_h
00024 
00025 #define RCPP_DECORATE(__FUN__) __FUN__##__rcpp__wrapper__
00026 
00027 namespace Rcpp{
00028 namespace internal{     
00029         class converter {
00030         public:
00031                 converter( ) : x(R_NilValue){} ;
00032                 converter( SEXP x_) : x(x_){} ;
00033                 converter( const converter& other) : x(other.x){}
00034                 converter& operator=( const converter& other){
00035                         x = other.x ;
00036                         return *this ;
00037                 }
00038                 ~converter(){}
00039         
00040                 template <typename T> operator T(){
00041                         return ::Rcpp::as<T>( x ) ;     
00042                 }
00043         private:
00044                 SEXP x ;
00045                 
00046         } ;
00047 } // namespace internal
00048 } // namespace Rcpp
00049 
00050 #ifndef BEGIN_RCPP
00051 #define BEGIN_RCPP try{ 
00052 #endif 
00053 
00054 #ifndef VOID_END_RCPP
00055 #define VOID_END_RCPP } catch( std::exception& __ex__ ){ forward_exception_to_r( __ex__ ) ; } catch(...){ ::Rf_error( "c++ exception (unknown reason)" ) ; }
00056 #endif
00057 
00058 #ifndef END_RCPP
00059 #define END_RCPP VOID_END_RCPP return R_NilValue;
00060 #endif
00061 
00062 #include <Rcpp/preprocessor_generated.h>
00063 // from boost preprocessor library
00064 #include <Rcpp/preprocessor/cat.hpp>
00065 
00066 #define RCPP_XP_FIELD_GET(__NAME__,__CLASS__,__FIELD__)        \
00067 extern "C" SEXP RCPP_PP_CAT(__NAME__,__rcpp_info__)(){         \
00068         using Rcpp::_ ;                                            \
00069         Rcpp::List info = Rcpp::List::create(                      \
00070         _["class"]  = #__CLASS__  ,                            \
00071         _["field"]  = #__FIELD__                              \
00072         )   ;                                                  \
00073     info.attr( "class" ) = "rcppxpfieldgetinfo" ;              \
00074     return info   ;                                            \
00075 }                                                              \
00076 extern "C" SEXP __NAME__( SEXP xp ){                           \
00077         SEXP res = R_NilValue ;                                    \
00078         BEGIN_RCPP                                                 \
00079                 ::Rcpp::XPtr< __CLASS__ > ptr(xp) ;                    \
00080                 res = ::Rcpp::wrap( ptr->__FIELD__ ) ;                 \
00081         return res ;                                               \
00082         END_RCPP                                                   \
00083 }
00084 
00085 #define RCPP_XP_FIELD_SET(__NAME__,__CLASS__,__FIELD__)        \
00086 extern "C" SEXP RCPP_PP_CAT(__NAME__,__rcpp_info__)(){         \
00087         using Rcpp::_ ;                                            \
00088         Rcpp::List info = Rcpp::List::create(                      \
00089         _["class"]  = #__CLASS__  ,                            \
00090         _["field"]  = #__FIELD__                              \
00091         )   ;                                                  \
00092     info.attr( "class" ) = "rcppxpfieldsetinfo" ;              \
00093     return info   ;                                            \
00094 }                                                              \
00095 extern "C" SEXP __NAME__( SEXP xp, SEXP value ){               \
00096         BEGIN_RCPP                                                 \
00097                 ::Rcpp::XPtr< __CLASS__ > ptr(xp) ;                    \
00098                 ptr->__FIELD__ = ::Rcpp::internal::converter(value) ;  \
00099         END_RCPP                                                   \
00100 }
00101 
00102 #define RCPP_XP_FIELD(__PREFIX__,__CLASS__,__FIELD__)          \
00103 RCPP_XP_FIELD_GET( RCPP_PP_CAT(__PREFIX__,_get), __CLASS__, __FIELD__ )    \
00104 RCPP_XP_FIELD_SET( RCPP_PP_CAT(__PREFIX__,_set), __CLASS__, __FIELD__ )    
00105 
00106 
00107 #define RCPP_TRAITS(__CLASS__,__SEXPTYPE__)                     \
00108 namespace Rcpp{ namespace traits {                                \
00109 template<> struct r_type_traits< __CLASS__ >{                       \
00110         typedef r_type_primitive_tag r_category ;                     \
00111 } ;                                                               \
00112 template<> struct r_type_traits< std::pair< std::string , __CLASS__ > >{   \
00113         typedef r_type_pairstring_primitive_tag r_category ;          \
00114 } ;                                                               \
00115 template<> struct wrap_type_traits< __CLASS__ >{                    \
00116         typedef wrap_type_primitive_tag wrap_category ;               \
00117 } ;                                                               \
00118 template<> struct r_sexptype_traits< __CLASS__ >{                   \
00119         enum{ rtype = __SEXPTYPE__ } ;                                \
00120 } ;                                                               \
00121 } }
00122 #define RCPP_ENUM_TRAITS(__ENUM__) RCPP_TRAITS(__ENUM__,INTSXP)
00123 
00124 
00125 #define ___RCPP_HANDLE_CASE___( ___RTYPE___ , ___FUN___ , ___OBJECT___ , ___RCPPTYPE___ )       \
00126         case ___RTYPE___ :                                                                                                                                      \
00127                 return ___FUN___( ::Rcpp::___RCPPTYPE___< ___RTYPE___ >( ___OBJECT___ ) ) ;     
00128                          
00129 #define ___RCPP_RETURN___( __FUN__, __SEXP__ , __RCPPTYPE__ )                                           \
00130         SEXP __TMP__ = __SEXP__ ;                                                                                                                       \
00131         switch( TYPEOF( __TMP__ ) ){                                                                                                            \
00132                 ___RCPP_HANDLE_CASE___( INTSXP  , __FUN__ , __TMP__ , __RCPPTYPE__ )                    \
00133                 ___RCPP_HANDLE_CASE___( REALSXP , __FUN__ , __TMP__ , __RCPPTYPE__ )                    \
00134                 ___RCPP_HANDLE_CASE___( RAWSXP  , __FUN__ , __TMP__ , __RCPPTYPE__ )                    \
00135                 ___RCPP_HANDLE_CASE___( LGLSXP  , __FUN__ , __TMP__ , __RCPPTYPE__ )                    \
00136                 ___RCPP_HANDLE_CASE___( CPLXSXP , __FUN__ , __TMP__ , __RCPPTYPE__ )                    \
00137                 ___RCPP_HANDLE_CASE___( STRSXP  , __FUN__ , __TMP__ , __RCPPTYPE__ )                    \
00138                 ___RCPP_HANDLE_CASE___( VECSXP  , __FUN__ , __TMP__ , __RCPPTYPE__ )                    \
00139                 ___RCPP_HANDLE_CASE___( EXPRSXP , __FUN__ , __TMP__ , __RCPPTYPE__ )                    \
00140         default:                                                                                                                                                        \
00141                 throw std::range_error( "not a vector" ) ;                                                                      \
00142         }
00143 
00144 #define RCPP_RETURN_VECTOR( _FUN_, _SEXP_ )  ___RCPP_RETURN___( _FUN_, _SEXP_ , Vector ) 
00145 #define RCPP_RETURN_MATRIX( _FUN_, _SEXP_ )  ___RCPP_RETURN___( _FUN_, _SEXP_ , Matrix )
00146 
00147 
00148 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines