[Rcpp-commits] r1101 - in pkg/Rcpp: inst/include inst/include/Rcpp src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Apr 22 00:25:10 CEST 2010
Author: romain
Date: 2010-04-22 00:25:09 +0200 (Thu, 22 Apr 2010)
New Revision: 1101
Modified:
pkg/Rcpp/inst/include/Rcpp/RObject.h
pkg/Rcpp/inst/include/Rcpp/Vector.h
pkg/Rcpp/inst/include/Rcpp/exceptions.h
pkg/Rcpp/inst/include/Rcpp/r_cast.h
pkg/Rcpp/inst/include/RcppCommon.h
pkg/Rcpp/src/Evaluator.cpp
pkg/Rcpp/src/RObject.cpp
pkg/Rcpp/src/r_cast.cpp
Log:
move not_compatible out of RObject
Modified: pkg/Rcpp/inst/include/Rcpp/RObject.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/RObject.h 2010-04-21 21:24:57 UTC (rev 1100)
+++ pkg/Rcpp/inst/include/Rcpp/RObject.h 2010-04-21 22:25:09 UTC (rev 1101)
@@ -29,21 +29,10 @@
class RObject {
public:
+
/**
* Exception thrown when attempting to convert a SEXP
*/
- class not_compatible: public std::exception{
- public:
- not_compatible(const std::string& message) throw() : message(message){};
- virtual ~not_compatible() throw(){} ;
- virtual const char* what() const throw() ;
- private:
- std::string message ;
- } ;
-
- /**
- * Exception thrown when attempting to convert a SEXP
- */
class not_s4: public std::exception{
public:
not_s4() throw(){};
Modified: pkg/Rcpp/inst/include/Rcpp/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Vector.h 2010-04-21 21:24:57 UTC (rev 1100)
+++ pkg/Rcpp/inst/include/Rcpp/Vector.h 2010-04-21 22:25:09 UTC (rev 1101)
@@ -2265,14 +2265,14 @@
Matrix() : VECTOR() {}
- Matrix(SEXP x) throw(RObject::not_compatible) : VECTOR(){
- if( ! ::Rf_isMatrix(x) ) throw RObject::not_compatible("not a matrix") ;
+ Matrix(SEXP x) throw(not_compatible) : VECTOR(){
+ if( ! ::Rf_isMatrix(x) ) throw not_compatible("not a matrix") ;
SEXP y = r_cast<RTYPE>( x ) ;
VECTOR::setSEXP( y );
}
- Matrix( const Dimension& dims) : VECTOR() {
- if( dims.size() != 2 ) throw RObject::not_compatible("not a matrix") ;
+ Matrix( const Dimension& dims) throw(not_compatible) : VECTOR() {
+ if( dims.size() != 2 ) throw not_compatible("not a matrix") ;
VECTOR::setSEXP( Rf_allocVector( RTYPE, dims.prod() ) ) ;
VECTOR::init() ;
VECTOR::attr( "dim" ) = dims ;
@@ -2281,15 +2281,15 @@
Matrix( const int& nrows, const int& ncols) : VECTOR( Dimension( nrows, ncols ) ) {}
Matrix( const int& n) : VECTOR( Dimension( n, n ) ) {}
- Matrix( const Matrix& other) : VECTOR() {
+ Matrix( const Matrix& other) throw(not_compatible) : VECTOR() {
SEXP x = other.asSexp() ;
- if( ! ::Rf_isMatrix(x) ) throw RObject::not_compatible("not a matrix") ;
+ if( ! ::Rf_isMatrix(x) ) throw not_compatible("not a matrix") ;
VECTOR::setSEXP( x ) ;
}
- Matrix& operator=(const Matrix& other) {
+ Matrix& operator=(const Matrix& other) throw(not_compatible) {
SEXP x = other.asSexp() ;
- if( ! ::Rf_isMatrix(x) ) throw RObject::not_compatible("not a matrix") ;
+ if( ! ::Rf_isMatrix(x) ) not_compatible("not a matrix") ;
VECTOR::setSEXP( x ) ;
return *this ;
}
Modified: pkg/Rcpp/inst/include/Rcpp/exceptions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/exceptions.h 2010-04-21 21:24:57 UTC (rev 1100)
+++ pkg/Rcpp/inst/include/Rcpp/exceptions.h 2010-04-21 22:25:09 UTC (rev 1101)
@@ -24,6 +24,18 @@
namespace Rcpp{
+/**
+ * Exception thrown when attempting to convert a SEXP
+ */
+class not_compatible: public std::exception{
+ public:
+ not_compatible(const std::string& message) throw() : message(message){};
+ virtual ~not_compatible() throw(){} ;
+ virtual const char* what() const throw(){ return message.c_str() ; } ;
+ private:
+ std::string message ;
+} ;
+
class not_a_matrix : public std::exception{
public:
not_a_matrix(){} ;
@@ -64,12 +76,6 @@
class no_such_binding: public std::exception{
public:
- no_such_binding() throw() : message(){}
- no_such_binding(const no_such_binding& other) throw() : message(other.message){} ;
- no_such_binding& operator=( const no_such_binding& other ){
- message = other.message ;
- return *this ;
- }
/**
* @param binding name of the binding
*/
Modified: pkg/Rcpp/inst/include/Rcpp/r_cast.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/r_cast.h 2010-04-21 21:24:57 UTC (rev 1100)
+++ pkg/Rcpp/inst/include/Rcpp/r_cast.h 2010-04-21 22:25:09 UTC (rev 1101)
@@ -22,30 +22,32 @@
#ifndef Rcpp_rcast_h
#define Rcpp_rcast_h
+#include <Rcpp/exceptions.h>
+
namespace Rcpp{
namespace internal {
// /* r_true_cast is only meant to be used when the target SEXP type
// is different from the SEXP type of x */
template <int TARGET>
-SEXP r_true_cast( SEXP x){
- throw std::exception( "not compatible" ) ;
+SEXP r_true_cast( SEXP x) throw(not_compatible) {
+ throw not_compatible( "not compatible" ) ;
}
-template<> SEXP r_true_cast<INTSXP>(SEXP x);
-template<> SEXP r_true_cast<REALSXP>(SEXP x);
-template<> SEXP r_true_cast<RAWSXP>(SEXP x);
-template<> SEXP r_true_cast<CPLXSXP>(SEXP x);
-template<> SEXP r_true_cast<LGLSXP>(SEXP x);
-template<> SEXP r_true_cast<STRSXP>(SEXP x);
-template<> SEXP r_true_cast<VECSXP>(SEXP x);
-template<> SEXP r_true_cast<EXPRSXP>(SEXP x);
-template<> SEXP r_true_cast<LISTSXP>(SEXP x);
-template<> SEXP r_true_cast<LANGSXP>(SEXP x);
+template<> SEXP r_true_cast<INTSXP>(SEXP x) throw(not_compatible) ;
+template<> SEXP r_true_cast<REALSXP>(SEXP x) throw(not_compatible) ;
+template<> SEXP r_true_cast<RAWSXP>(SEXP x) throw(not_compatible) ;
+template<> SEXP r_true_cast<CPLXSXP>(SEXP x) throw(not_compatible) ;
+template<> SEXP r_true_cast<LGLSXP>(SEXP x) throw(not_compatible) ;
+template<> SEXP r_true_cast<STRSXP>(SEXP x) throw(not_compatible) ;
+template<> SEXP r_true_cast<VECSXP>(SEXP x) throw(not_compatible) ;
+template<> SEXP r_true_cast<EXPRSXP>(SEXP x) throw(not_compatible) ;
+template<> SEXP r_true_cast<LISTSXP>(SEXP x) throw(not_compatible) ;
+template<> SEXP r_true_cast<LANGSXP>(SEXP x) throw(not_compatible) ;
} // namespace internal
-template <int TARGET> SEXP r_cast( SEXP x){
+template <int TARGET> SEXP r_cast( SEXP x) throw(not_compatible) {
return (TYPEOF(x)== TARGET) ? x : internal::r_true_cast<TARGET>(x) ;
}
Modified: pkg/Rcpp/inst/include/RcppCommon.h
===================================================================
--- pkg/Rcpp/inst/include/RcppCommon.h 2010-04-21 21:24:57 UTC (rev 1100)
+++ pkg/Rcpp/inst/include/RcppCommon.h 2010-04-21 22:25:09 UTC (rev 1101)
@@ -224,6 +224,7 @@
#include <Rcpp/internal/caster.h>
#include <Rcpp/internal/r_vector.h>
+#include <Rcpp/exceptions.h>
#include <Rcpp/r_cast.h>
#include <Rcpp/internal/export.h>
Modified: pkg/Rcpp/src/Evaluator.cpp
===================================================================
--- pkg/Rcpp/src/Evaluator.cpp 2010-04-21 21:24:57 UTC (rev 1100)
+++ pkg/Rcpp/src/Evaluator.cpp 2010-04-21 22:25:09 UTC (rev 1101)
@@ -59,8 +59,14 @@
namespace internal{
/* this is defined here because we need to be sure that Evaluator is
defined */
- SEXP convert_using_rfunction(SEXP x, const char* const fun){
- return Evaluator::run( Rf_lcons( Rf_install(fun), Rf_cons(x, R_NilValue) ) ) ;
+ SEXP convert_using_rfunction(SEXP x, const char* const fun) {
+ SEXP res = R_NilValue ;
+ try{
+ res = Evaluator::run( Rf_lcons( Rf_install(fun), Rf_cons(x, R_NilValue) ) ) ;
+ } catch( Evaluator::eval_error& e){
+ throw not_compatible( std::string("could not convert using R function : ") + fun ) ;
+ }
+ return res;
}
SEXP try_catch( SEXP expr, SEXP env ){
Modified: pkg/Rcpp/src/RObject.cpp
===================================================================
--- pkg/Rcpp/src/RObject.cpp 2010-04-21 21:24:57 UTC (rev 1100)
+++ pkg/Rcpp/src/RObject.cpp 2010-04-21 22:25:09 UTC (rev 1101)
@@ -150,10 +150,6 @@
return SlotProxy( *this, name ) ;
}
-
-const char* RObject::not_compatible::what( ) const throw() {
- return message.c_str() ;
-}
const char* RObject::not_s4::what( ) const throw() {
return "not an S4 object" ;
}
Modified: pkg/Rcpp/src/r_cast.cpp
===================================================================
--- pkg/Rcpp/src/r_cast.cpp 2010-04-21 21:24:57 UTC (rev 1100)
+++ pkg/Rcpp/src/r_cast.cpp 2010-04-21 22:25:09 UTC (rev 1101)
@@ -25,7 +25,7 @@
namespace Rcpp{
namespace internal{
-template<> SEXP r_true_cast<INTSXP>(SEXP x){
+template<> SEXP r_true_cast<INTSXP>(SEXP x) throw(not_compatible){
switch( TYPEOF(x) ){
case REALSXP:
case RAWSXP:
@@ -33,12 +33,12 @@
case CPLXSXP:
return Rf_coerceVector( x, INTSXP) ;
default:
- throw RObject::not_compatible( "not compatible with INTSXP" ) ;
+ throw not_compatible( "not compatible with INTSXP" ) ;
}
return R_NilValue ; /* -Wall */
}
-template<> SEXP r_true_cast<REALSXP>( SEXP x){
+template<> SEXP r_true_cast<REALSXP>( SEXP x) throw(not_compatible){
switch( TYPEOF( x ) ){
case INTSXP:
case LGLSXP:
@@ -46,12 +46,12 @@
case RAWSXP:
return Rf_coerceVector( x, REALSXP );
default:
- throw RObject::not_compatible( "not compatible with INTSXP" ) ;
+ throw not_compatible( "not compatible with INTSXP" ) ;
}
return R_NilValue ; /* -Wall */
}
-template<> SEXP r_true_cast<LGLSXP>( SEXP x){
+template<> SEXP r_true_cast<LGLSXP>( SEXP x) throw(not_compatible){
switch( TYPEOF( x ) ){
case REALSXP:
case INTSXP:
@@ -59,12 +59,12 @@
case RAWSXP:
return Rf_coerceVector( x, LGLSXP );
default:
- throw RObject::not_compatible( "not compatible with LGLSXP" ) ;
+ throw not_compatible( "not compatible with LGLSXP" ) ;
}
return R_NilValue ; /* -Wall */
}
-template<> SEXP r_true_cast<RAWSXP>( SEXP x){
+template<> SEXP r_true_cast<RAWSXP>( SEXP x) throw(not_compatible){
switch( TYPEOF( x ) ){
case LGLSXP:
case REALSXP:
@@ -72,13 +72,13 @@
case CPLXSXP:
return Rf_coerceVector( x, RAWSXP );
default:
- throw RObject::not_compatible( "not compatible with RAWSXP" ) ;
+ throw not_compatible( "not compatible with RAWSXP" ) ;
}
return R_NilValue ; /* -Wall */
}
-template<> SEXP r_true_cast<CPLXSXP>( SEXP x){
+template<> SEXP r_true_cast<CPLXSXP>( SEXP x) throw(not_compatible){
switch( TYPEOF( x ) ){
case RAWSXP:
case LGLSXP:
@@ -86,12 +86,12 @@
case INTSXP:
return Rf_coerceVector( x, CPLXSXP );
default:
- throw RObject::not_compatible( "not compatible with CPLXSXP" ) ;
+ throw not_compatible( "not compatible with CPLXSXP" ) ;
}
return R_NilValue ; /* -Wall */
}
-template<> SEXP r_true_cast<STRSXP>( SEXP x){
+template<> SEXP r_true_cast<STRSXP>( SEXP x) throw(not_compatible){
switch( TYPEOF( x ) ){
case CPLXSXP:
case RAWSXP:
@@ -104,20 +104,20 @@
case SYMSXP:
return Rf_ScalarString( PRINTNAME( x ) ) ;
default:
- throw RObject::not_compatible( "not compatible with STRSXP" ) ;
+ throw not_compatible( "not compatible with STRSXP" ) ;
}
return R_NilValue ; /* -Wall */
}
-template<> SEXP r_true_cast<VECSXP>(SEXP x){
+template<> SEXP r_true_cast<VECSXP>(SEXP x) throw(not_compatible){
return convert_using_rfunction(x, "as.list" ) ;
}
-template<> SEXP r_true_cast<EXPRSXP>(SEXP x){
+template<> SEXP r_true_cast<EXPRSXP>(SEXP x) throw(not_compatible){
return convert_using_rfunction(x, "as.expression" ) ;
}
-template<> SEXP r_true_cast<LISTSXP>(SEXP x){
+template<> SEXP r_true_cast<LISTSXP>(SEXP x) throw(not_compatible){
switch( TYPEOF(x) ){
case LANGSXP:
{
@@ -133,7 +133,7 @@
}
-template<> SEXP r_true_cast<LANGSXP>(SEXP x){
+template<> SEXP r_true_cast<LANGSXP>(SEXP x) throw(not_compatible) {
return convert_using_rfunction(x, "as.call" ) ;
}
More information about the Rcpp-commits
mailing list