|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*- 00002 // 00003 // cache.cpp: Rcpp R/C++ interface class library -- Rcpp cache 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 #include <Rcpp.h> 00023 00024 static SEXP Rcpp_cache = R_NilValue ; 00025 static bool Rcpp_cache_ready = false ; 00026 00027 void maybe_init() { 00028 if( ! Rcpp_cache_ready ) init_Rcpp_cache() ; 00029 } 00030 00031 namespace Rcpp { 00032 namespace internal { 00033 SEXP get_Rcpp_namespace(){ 00034 maybe_init() ; return VECTOR_ELT( Rcpp_cache , 0 ) ; 00035 } 00036 SEXP get_rcpptrycatch(){ 00037 // maybe_init() ; return VECTOR_ELT( Rcpp_cache, 4 ) ; 00038 return Rf_install("rcpp_tryCatch") ; // maybe not worth assigning to SEXP 00039 } 00040 SEXP get_evalq(){ 00041 // maybe_init() ; return VECTOR_ELT( Rcpp_cache, 5 ) ; 00042 return Rf_install("evalq"); // maybe not worth assigning to SEXP 00043 } 00044 } 00045 } 00046 00047 // only used for debugging 00048 SEXP get_rcpp_cache() { return Rcpp_cache ; } 00049 00050 SEXP init_Rcpp_cache(){ 00051 Rcpp_cache = PROTECT( Rf_allocVector( VECSXP, 10 ) ); 00052 00053 // the Rcpp namespace 00054 SEXP getNamespaceSym = Rf_install("getNamespace"); // cannot be gc()'ed once in symbol table 00055 SEXP RCPP = PROTECT( Rf_eval(Rf_lang2( getNamespaceSym, Rf_mkString("Rcpp") ), R_GlobalEnv) ) ; 00056 SET_VECTOR_ELT( Rcpp_cache, 0, RCPP ) ; 00057 reset_current_error() ; 00058 // SET_VECTOR_ELT( Rcpp_cache, 4, Rf_install("rcpp_tryCatch") ) ; 00059 // SET_VECTOR_ELT( Rcpp_cache, 5, Rf_install("evalq") ) ; 00060 00061 R_PreserveObject( Rcpp_cache ) ; 00062 UNPROTECT(2) ; 00063 Rcpp_cache_ready = true ; 00064 return Rcpp_cache ; 00065 } 00066 00067 SEXP reset_current_error(){ 00068 00069 // error occured 00070 SET_VECTOR_ELT( Rcpp_cache, 1, Rf_ScalarLogical(FALSE) ) ; 00071 00072 // current error 00073 SET_VECTOR_ELT( Rcpp_cache, 2, R_NilValue ) ; 00074 00075 // stack trace 00076 SET_VECTOR_ELT( Rcpp_cache, 3, R_NilValue ) ; 00077 00078 return R_NilValue ; 00079 } 00080 00081 SEXP rcpp_error_recorder(SEXP e){ 00082 maybe_init() ; 00083 00084 // error occured 00085 SET_VECTOR_ELT( Rcpp_cache, 1, Rf_ScalarLogical(TRUE) ) ; 00086 00087 // current error 00088 rcpp_set_current_error(e ) ; 00089 00090 return R_NilValue ; 00091 00092 } 00093 00094 SEXP rcpp_set_current_error(SEXP e){ 00095 SET_VECTOR_ELT( Rcpp_cache, 2, e ) ; 00096 return R_NilValue ; 00097 } 00098 00099 SEXP rcpp_get_current_error(){ 00100 return VECTOR_ELT( Rcpp_cache, 2 ) ; 00101 } 00102 00103 SEXP rcpp_set_error_occured(SEXP e){ 00104 SET_VECTOR_ELT( Rcpp_cache, 1, e ) ; 00105 return R_NilValue ; 00106 } 00107 00108 SEXP rcpp_get_error_occured(){ 00109 return VECTOR_ELT( Rcpp_cache, 1 ) ; 00110 } 00111 00112 SEXP rcpp_set_stack_trace(SEXP e){ 00113 SET_VECTOR_ELT( Rcpp_cache, 3, e ) ; 00114 return R_NilValue ; 00115 } 00116 00117 SEXP rcpp_get_stack_trace(){ 00118 return VECTOR_ELT( Rcpp_cache, 3 ) ; 00119 } 00120