[Rcpp-commits] r328 - in pkg/src: . Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jan 9 22:41:23 CET 2010


Author: romain
Date: 2010-01-09 22:41:22 +0100 (Sat, 09 Jan 2010)
New Revision: 328

Modified:
   pkg/src/Function.cpp
   pkg/src/Rcpp/Function.h
Log:
throw an exception on Function::operator() if an R error occurs

Modified: pkg/src/Function.cpp
===================================================================
--- pkg/src/Function.cpp	2010-01-08 22:53:13 UTC (rev 327)
+++ pkg/src/Function.cpp	2010-01-09 21:41:22 UTC (rev 328)
@@ -24,6 +24,7 @@
 #include <Rcpp/Language.h>
 #include <Rcpp/Pairlist.h>
 #include <RcppCommon.h>
+#include <Rcpp/as.h>
 
 namespace Rcpp {
 	
@@ -52,4 +53,18 @@
 		return Environment( CLOENV(m_sexp) ) ;
 	}
 	
+	Function::eval_error::eval_error(const RObject& err) throw() : message(){
+		if( err.isNULL() ) {
+			message = "unknown error" ;
+		} else{
+			message = as<std::string>( Rf_eval( 
+				Rf_lang2( Rf_install("conditionCall"), err), 
+				R_GlobalEnv ) );
+		}
+	}
+	Function::eval_error::~eval_error() throw(){}
+	const char* Function::eval_error::what() throw() {
+		return message.c_str() ;
+	}
+	
 } // namespace Rcpp

Modified: pkg/src/Rcpp/Function.h
===================================================================
--- pkg/src/Rcpp/Function.h	2010-01-08 22:53:13 UTC (rev 327)
+++ pkg/src/Rcpp/Function.h	2010-01-09 21:41:22 UTC (rev 328)
@@ -47,6 +47,18 @@
 	} ;
 	
 	/**
+	 * exception generated when a function calls generates an R error
+	 */
+	class eval_error : public std::exception{
+	public:
+		eval_error(const RObject& err) throw() ;
+		~eval_error() throw() ;
+		const char* what() throw() ;
+	private: 
+		std::string message ;
+	} ;
+	
+	/**
 	 * Attempts to convert the SEXP to a pair list
 	 *
 	 * @throw not_compatible if the SEXP could not be converted
@@ -65,7 +77,7 @@
 	 */
 #ifdef HAS_VARIADIC_TEMPLATES
 template<typename... Args> 
-	SEXP operator()( const Args&... args) {
+	SEXP operator()( const Args&... args) throw(eval_error){
 		
 		/* FIXME: we should use applyClosure instead */
 		Evaluator evaluator( Rf_lcons( m_sexp, pairlist(args...) ) ) ; 
@@ -73,9 +85,7 @@
 		if( evaluator.successfull() ){
 			return evaluator.getResult() ;
 		} else{
-			/* FIXME: need some strategy about error handling */
-			/* throw an exception ? */
-			return evaluator.getError() ;
+			throw eval_error( evaluator.getError() );
 		}
 	}
 #endif



More information about the Rcpp-commits mailing list