[Rprotobuf-commits] r608 - in pkg: . R src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Dec 27 21:04:30 CET 2013
Author: murray
Date: 2013-12-27 21:04:29 +0100 (Fri, 27 Dec 2013)
New Revision: 608
Removed:
pkg/R/exceptions.R
pkg/src/exceptions.cpp
Modified:
pkg/ChangeLog
pkg/src/extractors.cpp
pkg/src/mutators.cpp
pkg/src/rprotobuf.cpp
pkg/src/wrapper_EnumDescriptor.cpp
pkg/src/wrapper_FieldDescriptor.cpp
pkg/src/wrapper_Message.cpp
pkg/src/wrapper_ServiceDescriptor.cpp
Log:
Remove ancient exception handling code and migrate the codebase to use
the standard exception classes and helper functions from Rcpp.
Some whitespace changes were necessary to clean up the indentation,
which identified additional minor cut-and-paste bugs which have been
corrected.
Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog 2013-12-27 09:11:21 UTC (rev 607)
+++ pkg/ChangeLog 2013-12-27 20:04:29 UTC (rev 608)
@@ -9,6 +9,10 @@
for our handling of setting repeated message fields.
* src/wrapper_Message.cpp: Add const qualifier to
field_desc throughout file.
+ * src/exceptions.cpp: Remove ancient exceptions handling code,
+ migrate codebase to use standard exception classes and helper
+ functions from Rcpp.
+ * R/exceptions.R (throw): idem.
2013-12-26 Murray Stokely <mstokely at google.com>
Deleted: pkg/R/exceptions.R
===================================================================
--- pkg/R/exceptions.R 2013-12-27 09:11:21 UTC (rev 607)
+++ pkg/R/exceptions.R 2013-12-27 20:04:29 UTC (rev 608)
@@ -1,23 +0,0 @@
-
-#' throw a particular protobuf error condition
-#' all conditions have the class "ProtobufError", "error" and "condition"
-#' plus any number of classes as defined by the class argument
-#'
-#' @param message the message of the error
-#' @param class the sub class of the condition
-
-# this is exported at the moment, but I would prefer it not to be
-# so need to find a way to call a namespace private function from the
-# C call
-throw <- function( message = " error", class = NULL ){
-
- callstack <- sys.calls()
- ncalls <- length(callstack)
- call <- if( ncalls > 1L) callstack[[ ncalls - 1L ]] else match.call()
- classes <- c( class, "ProtobufError", "error", "condition" )
- condition <- structure(
- list( message = message, call = call ),
- class = classes )
- stop( condition )
-}
-
Deleted: pkg/src/exceptions.cpp
===================================================================
--- pkg/src/exceptions.cpp 2013-12-27 09:11:21 UTC (rev 607)
+++ pkg/src/exceptions.cpp 2013-12-27 20:04:29 UTC (rev 608)
@@ -1,29 +0,0 @@
-#include "rprotobuf.h"
-
-namespace rprotobuf{
-
-/* FIXME: this has got to disappear */
-
-/**
- * create a call to throw an evaluate it
- *
- * @param message the message
- * @param subclass the subclass to use (to allow direct handlers in tryCatch)
- */
-SEXP throwException( const char* message, const char* subclass ){
-
- SEXP m = PROTECT( Rf_mkString( message ) ) ;
- SEXP s = PROTECT( Rf_mkString( subclass ) ) ;
-
- SEXP call = PROTECT( Rf_lang3( Rf_install("throw"), m, s) ) ;
- UNPROTECT( 2) ; /* m, s */
-
- Rf_eval( call, R_FindNamespace(Rf_mkString("RProtoBuf")) ) ;
-
- /* but this is never actually called since the call eventually calls stop */
- UNPROTECT(1);
- return( R_NilValue ); /* Wall */
-}
-
-} // namespace rprotobuf
-
Modified: pkg/src/extractors.cpp
===================================================================
--- pkg/src/extractors.cpp 2013-12-27 09:11:21 UTC (rev 607)
+++ pkg/src/extractors.cpp 2013-12-27 20:04:29 UTC (rev 608)
@@ -35,18 +35,20 @@
// So, if an option is set, we return as a character string.
template<typename ValueType>
SEXP Int64AsSEXP(ValueType value) {
- if (UseStringsForInt64()) {
- std::stringstream ss;
- if ((ss << value).fail()) {
- // This should not happen, its a bug in the code.
- string message = string("Error converting int64 to string, unset ") +
+ BEGIN_RCPP
+ if (UseStringsForInt64()) {
+ std::stringstream ss;
+ if ((ss << value).fail()) {
+ // This should not happen, its a bug in the code.
+ string message = string("Error converting int64 to string, unset ") +
kIntStringOptionName + " option.";
- throwException(message.c_str(), "ConversionException");
+ Rcpp::throw(message.c_str());
+ }
+ return Rcpp::CharacterVector(ss.str());
+ } else {
+ return Rcpp::wrap(value);
}
- return Rcpp::CharacterVector(ss.str());
- } else {
- return Rcpp::wrap(value);
- }
+ END_RCPP
}
/**
@@ -82,7 +84,7 @@
SEXP extractFieldAsSEXP( const Rcpp::XPtr<GPB::Message>& message,
const GPB::FieldDescriptor* fieldDesc ){
-
+ BEGIN_RCPP
const Reflection * ref = message->GetReflection() ;
if( fieldDesc->is_repeated() ){
@@ -137,15 +139,14 @@
}
return res;
} else {
- throwException( "unknown field type with CPP_TYPE STRING", "ConversionException" ) ;
+ Rcpp::throw("unknown field type with CPP_TYPE STRING");
}
default:
- throwException("Unsupported type", "ConversionException");
+ Rcpp::throw("Unsupported type");
}
} else {
-
switch( GPB::FieldDescriptor::TypeToCppType(fieldDesc->type()) ){
#undef HANDLE_SINGLE_FIELD
@@ -175,7 +176,7 @@
std::string s = ref->GetString(*message, fieldDesc);
return Rcpp::wrap(std::vector<Rbyte>(s.begin(), s.end()));
} else {
- throwException( "unknown field type with CPP_TYPE STRING", "ConversionException" ) ;
+ Rcpp::throw("unknown field type with CPP_TYPE STRING");
}
case CPPTYPE_ENUM :
return Rcpp::wrap( ref->GetEnum( *message, fieldDesc )->number() ) ;
@@ -185,11 +186,11 @@
break ;
default:
- throwException("Unsupported type", "ConversionException");
+ Rcpp::throw("Unsupported type");
}
}
- return R_NilValue ; /* -Wall */
+ END_RCPP
}
} // namespace rprotobuf
Modified: pkg/src/mutators.cpp
===================================================================
--- pkg/src/mutators.cpp 2013-12-27 09:11:21 UTC (rev 607)
+++ pkg/src/mutators.cpp 2013-12-27 20:04:29 UTC (rev 608)
@@ -31,11 +31,12 @@
* @param x some R data
* @param index the index
* @return x[index], as an double
- * @throws CastException if x[index] cannot be converted to double
+ * @throws Rcpp::exception if x[index] cannot be converted to double
*/
/* FIXME: should we convert the NA's */
double GET_double( SEXP x, int index ){
- switch( TYPEOF(x) ){
+ try {
+ switch( TYPEOF(x) ){
case INTSXP:
return( (double)INTEGER(x)[index] ) ;
case REALSXP:
@@ -45,14 +46,18 @@
case RAWSXP:
return( (double)RAW(x)[index] ) ;
default:
- throwException( "cannot cast SEXP to double", "CastException" ) ;
+ Rcpp::throw("cannot cast SEXP to double");
+ }
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
}
return 0.0 ; // -Wall
}
// }}}
float GET_float( SEXP x, int index ){
- switch( TYPEOF(x) ){
+ try {
+ switch( TYPEOF(x) ){
case INTSXP:
return( (float)INTEGER(x)[index] ) ;
case REALSXP:
@@ -62,13 +67,17 @@
case RAWSXP:
return( (float)RAW(x)[index] ) ;
default:
- throwException( "cannot cast SEXP to double", "CastException" ) ;
+ Rcpp::throw("cannot cast SEXP to double");
+ }
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
}
return (float)0.0 ; // -Wall
}
int GET_int( SEXP x, int index ){
- switch( TYPEOF(x) ){
+ try {
+ switch( TYPEOF(x) ){
case INTSXP:
return( INTEGER(x)[index] );
case REALSXP:
@@ -78,37 +87,49 @@
case RAWSXP:
return( (int)RAW(x)[index] ) ;
default:
- throwException( "cannot cast SEXP to int", "CastException" ) ;
+ Rcpp::throw( "cannot cast SEXP to int" );
+ }
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
}
return 0 ; // -Wall, should not happen since we only call this when we know it works
}
template<typename ValueType>
ValueType Int64FromString(const string &value) {
- std::stringstream ss(value);
- ValueType ret;
- if ((ss >> ret).fail() || !(ss>>std::ws).eof()) {
- string message = "Provided character value '" + value +
+ try {
+ std::stringstream ss(value);
+ ValueType ret;
+ if ((ss >> ret).fail() || !(ss>>std::ws).eof()) {
+ string message = "Provided character value '" + value +
"' cannot be cast to 64-bit integer.";
- throwException(message.c_str(), "CastException");
- }
- return ret;
+ Rcpp::throw(message.c_str());
+ }
+ return ret;
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ }
}
template<typename ValueType>
ValueType Int32FromString(const string &value) {
- std::stringstream ss(value);
- ValueType ret;
- if ((ss >> ret).fail() || !(ss>>std::ws).eof()) {
- string message = "Provided character value '" + value +
+ try {
+ std::stringstream ss(value);
+ ValueType ret;
+ if ((ss >> ret).fail() || !(ss>>std::ws).eof()) {
+ string message = "Provided character value '" + value +
"' cannot be cast to 32-bit integer.";
- throwException(message.c_str(), "CastException");
- }
- return ret;
+ Rcpp::throw(message.c_str());
+ }
+ return ret;
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ }
}
int32 GET_int32( SEXP x, int index ){
- switch( TYPEOF(x) ){
+ try {
+ switch( TYPEOF(x) ){
case INTSXP:
return( (int32)INTEGER(x)[index] );
case REALSXP:
@@ -120,13 +141,17 @@
case STRSXP:
return Int32FromString<int32>(CHAR(STRING_ELT(x, index)));
default:
- throwException( "cannot cast SEXP to int32", "CastException" ) ;
+ Rcpp::throw( "cannot cast SEXP to int32");
+ }
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
}
return (int32)0 ; // -Wall, should not happen since we only call this when we know it works
}
int64 GET_int64( SEXP x, int index ){
- switch( TYPEOF(x) ){
+ try {
+ switch( TYPEOF(x) ){
case INTSXP:
return( (int64)INTEGER(x)[index] );
case REALSXP:
@@ -138,13 +163,17 @@
case STRSXP:
return Int64FromString<int64>(CHAR(STRING_ELT(x, index)));
default:
- throwException( "cannot cast SEXP to int64", "CastException" ) ;
+ Rcpp::throw("cannot cast SEXP to int64");
+ }
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
}
return (int64)0 ; // -Wall, should not happen since we only call this when we know it works
}
uint32 GET_uint32( SEXP x, int index ){
- switch( TYPEOF(x) ){
+ try {
+ switch( TYPEOF(x) ){
case INTSXP:
return( (uint32)INTEGER(x)[index] );
case REALSXP:
@@ -156,13 +185,17 @@
case STRSXP:
return Int32FromString<uint32>(CHAR(STRING_ELT(x, index)));
default:
- throwException( "cannot cast SEXP to uint32", "CastException" ) ;
+ Rcpp::throw("cannot cast SEXP to uint32");
+ }
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
}
return (uint32)0 ; // -Wall, should not happen since we only call this when we know it works
}
uint64 GET_uint64( SEXP x, int index ){
- switch( TYPEOF(x) ){
+ try {
+ switch( TYPEOF(x) ){
case INTSXP:
return( (uint64)INTEGER(x)[index] );
case REALSXP:
@@ -174,38 +207,42 @@
case STRSXP:
return Int64FromString<uint64>(CHAR(STRING_ELT(x, index)));
default:
- throwException( "cannot cast SEXP to uint64", "CastException" ) ;
+ Rcpp::throw("cannot cast SEXP to uint64");
+ }
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
}
return (uint64)0 ; // -Wall, should not happen since we only call this when we know it works
}
bool GET_bool( SEXP x, int index ){
- switch( TYPEOF(x) ){
+ try {
+ switch( TYPEOF(x) ){
case INTSXP:
if (INTEGER(x)[index] == R_NaInt) {
- throwException( "NA boolean values can not be stored in "
- "bool protocol buffer fields",
- "CastException" ) ;
+ Rcpp::throw("NA boolean values can not be stored in "
+ "bool protocol buffer fields");
}
return( (bool)INTEGER(x)[index] );
case REALSXP:
if (REAL(x)[index] == R_NaReal) {
- throwException( "NA boolean values can not be stored in "
- "bool protocol buffer fields",
- "CastException" ) ;
+ Rcpp::throw("NA boolean values can not be stored in "
+ "bool protocol buffer fields");
}
return( (bool)REAL(x)[index] );
case LGLSXP:
if (LOGICAL(x)[index] == NA_LOGICAL) {
- throwException( "NA boolean values can not be stored in "
- "bool protocol buffer fields",
- "CastException" ) ;
+ Rcpp::throw("NA boolean values can not be stored in "
+ "bool protocol buffer fields");
}
return( (bool)LOGICAL(x)[index] );
case RAWSXP:
return( (bool)RAW(x)[index] ) ;
default:
- throwException( "cannot cast SEXP to bool", "CastException" ) ;
+ Rcpp::throw("cannot cast SEXP to bool");
+ }
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
}
return (bool)0 ; // -Wall, should not happen since we only call this when we know it works
}
@@ -218,21 +255,25 @@
}
std::string GET_bytes( SEXP x, int index ){
- switch( TYPEOF(x)) {
+ try {
+ switch( TYPEOF(x)) {
case RAWSXP:
if (index == 0) {
return(std::string((const char *) RAW(x), (size_t) LENGTH(x)));
} else {
- throwException( "cannot cast SEXP to bytes", "CastException" ) ;
+ Rcpp::throw("cannot cast SEXP to bytes");
}
case VECSXP:
if (TYPEOF(VECTOR_ELT(x, index)) == RAWSXP) {
return(std::string((const char *) RAW(VECTOR_ELT(x, index)), (size_t) LENGTH(VECTOR_ELT(x, index))));
} else {
- throwException( "cannot cast SEXP to bytes", "CastException" ) ;
+ Rcpp::throw("cannot cast SEXP to bytes");
}
default:
- throwException( "cannot cast SEXP to bytes", "CastException" ) ;
+ Rcpp::throw("cannot cast SEXP to bytes");
+ }
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
}
return "" ; // -Wall, should not happen since we only call this when we know it works
}
@@ -289,7 +330,7 @@
*
*/
void CHECK_values_for_enum( const GPB::FieldDescriptor* field_desc, SEXP value ){
-
+ BEGIN_RCPP
const GPB::EnumDescriptor* enum_desc = field_desc->enum_type() ;
// N.B. n undefined if TYPEOF(value) not a vector, but we catch that below.
int n = LENGTH(value) ;
@@ -318,7 +359,7 @@
}
}
if( !ok ){
- throwException( "wrong value for enum", "WrongEnumValueException" ) ;
+ Rcpp::throw("wrong value for enum");
}
}
@@ -347,28 +388,26 @@
}
}
if( !ok ){
- throwException( "wrong value for enum", "WrongEnumValueException" ) ;
+ Rcpp::throw("wrong value for enum");
}
}
-
break ;
}
// }}}
default:
- throwException( "impossible to convert to a enum" , "ConversionException" ) ;
+ Rcpp::throw("impossible to convert to a enum");
}
-
-
+ VOID_END_RCPP
}
/**
* check that the values are suitable for the field descriptor
*/
void CHECK_messages( const GPB::FieldDescriptor* field_desc, SEXP values ){
-
+ BEGIN_RCPP
if( TYPEOF( values ) != VECSXP ){
- throwException( "expecting a list of messages", "ConversionException" ) ;
+ Rcpp::throw("expecting a list of messages");
}
const char* target = field_desc->message_type()->full_name().c_str() ;
@@ -376,10 +415,10 @@
for( int i=0; i<n; i++){
if( !isMessage( VECTOR_ELT(values, i), target ) ){
/* TODO: include i, target type and actual type in the message */
- throwException( "incorrect type", "IncorrectMessageTypeException" ) ;
+ Rcpp::throw("incorrect type");
}
}
-
+ VOID_END_RCPP
}
/**
@@ -389,6 +428,7 @@
*/
void CHECK_repeated_vals(const GPB::FieldDescriptor* field_desc,
SEXP value, int value_size) {
+ BEGIN_RCPP
switch( field_desc->type() ){
case TYPE_MESSAGE:
case TYPE_GROUP:
@@ -407,14 +447,13 @@
/* check that this is a message of the appropriate type */
if( !isMessage( value,
field_desc->message_type()->full_name().c_str() ) ){
- throwException( "incorrect type", "IncorrectMessageTypeException" ) ;
+ Rcpp::throw("incorrect type");
}
break ;
}
default:
{
- throwException( "impossible to convert to a message" ,
- "ConversionException" ) ;
+ Rcpp::("impossible to convert to a message");
}
}
break ;
@@ -453,7 +492,7 @@
}
}
if( !ok ){
- throwException( "wrong value for enum", "WrongEnumValueException" ) ;
+ Rcpp::throw("wrong value for enum");
}
}
break ;
@@ -481,7 +520,7 @@
}
}
if( !ok ){
- throwException( "wrong value for enum", "WrongEnumValueException" ) ;
+ Rcpp::throw("wrong value for enum");
}
}
@@ -490,7 +529,7 @@
// }}}
default:
- throwException( "impossible to convert to a enum" , "ConversionException" ) ;
+ Rcpp::throw("impossible to convert to a enum");
}
break ;
}
@@ -514,6 +553,7 @@
}
}
// }}}
+ VOID_END_RCPP
}
/**
@@ -524,7 +564,7 @@
* @param field_desc pointer to field descriptor to update
* @param value new value for the field
* @param value_size size of the new value for the field
- *
+ * @throws Rcpp::exception on error (uncaught)
* @return void, the message is modified by reference
*/
void setNonRepeatedMessageField(GPB::Message* message,
@@ -532,8 +572,7 @@
const GPB::FieldDescriptor* field_desc,
SEXP value, int value_size) {
if (value_size > 1) {
- throwException( "cannot set non-repeated field to vector of length > 1",
- "CastException" ) ;
+ Rcpp::throw("cannot set non-repeated field to vector of length > 1");
}
switch( GPB::FieldDescriptor::TypeToCppType( field_desc->type() ) ){
// {{{ simple cases using macro expansion
@@ -552,19 +591,16 @@
// TODO(mstokely): Rcpp should handle this!
if ((TYPEOF(value) == LGLSXP) &&
(LOGICAL(value)[0] == NA_LOGICAL)) {
- throwException("NA boolean values can not be stored in "
- "bool protocol buffer fields",
- "CastException");
+ Rcpp::throw("NA boolean values can not be stored in "
+ "bool protocol buffer fields");
} else if ((TYPEOF(value) == INTSXP) &&
(INTEGER(value)[0] == R_NaInt)) {
- throwException( "NA boolean values can not be stored in "
- "bool protocol buffer fields",
- "CastException");
+ Rcpp::throw("NA boolean values can not be stored in "
+ "bool protocol buffer fields");
} else if ((TYPEOF(value) == REALSXP) &&
(REAL(value)[0] == R_NaReal)) {
- throwException( "NA boolean values can not be stored in "
- "bool protocol buffer fields",
- "CastException");
+ Rcpp::throw("NA boolean values can not be stored in "
+ "bool protocol buffer fields");
}
ref->SetBool(message, field_desc, Rcpp::as<bool>(value));
break ;
@@ -631,7 +667,7 @@
#endif
#undef HANDLE_SINGLE_FIELD
default:
- throwException("Unsupported type", "ConversionException");
+ Rcpp::throw("Unsupported type");
// }}}
// {{{ string
@@ -653,8 +689,7 @@
{
/* check if value is a message */
if( !Rf_inherits( value, "Message" ) ){
- throwException( "Can only convert S4 objects of class 'Message'",
- "ConversionException" ) ;
+ Rcpp::throw("Can only convert S4 objects of class 'Message'");
}
GPB::Message* __mess = GET_MESSAGE_POINTER_FROM_S4(value);
ref->SetString(message, field_desc,
@@ -663,8 +698,7 @@
}
default:
{
- throwException( "Cannot convert to string",
- "ConversionException" ) ;
+ Rcpp::throw("Cannot convert to string");
}
}
break ;
@@ -679,13 +713,12 @@
const char* type = mess->GetDescriptor()->full_name().c_str() ;
const char* target = field_desc->message_type()->full_name().c_str() ;
if( strcmp( type, target ) ){
- throwException( "wrong message type", "WrongMessageException" ) ;
+ Rcpp::throw("wrong message type");
}
GPB::Message* m = ref->MutableMessage( message, field_desc ) ;
m->CopyFrom( *mess ) ;
} else {
- throwException( "type mismatch, expecting a 'Message' object",
- "TypeMismatchException" ) ;
+ Rcpp::throw("type mismatch, expecting a 'Message' object");
}
break ;
}
@@ -704,8 +737,7 @@
int val = Rcpp::as<int>(value) ;
const GPB::EnumValueDescriptor* evd = enum_desc->FindValueByNumber(val) ;
if( !evd ){
- throwException( "wrong value for enum",
- "WrongEnumValueException" ) ;
+ Rcpp::throw("wrong value for enum");
} else {
ref->SetEnum( message, field_desc, evd );
}
@@ -716,7 +748,7 @@
std::string val = Rcpp::as<std::string>( value ) ;
const GPB::EnumValueDescriptor* evd = enum_desc->FindValueByName(val) ;
if( !evd ){
- throwException( "wrong value for enum", "WrongEnumValueException" ) ;
+ Rcpp::throw("wrong value for enum");
} else {
ref->SetEnum( message, field_desc, evd );
}
@@ -724,7 +756,7 @@
}
default:
{
- throwException( "cannot set enum value", "WrongTypeEnumValueException" ) ;
+ Rcpp::throw("cannot set enum value");
}
}
}
@@ -741,7 +773,7 @@
* @param field_desc pointer to field descriptor to update
* @param value new value for the field
* @param value_size size of the new value for the field
- *
+ * @throws Rcpp::exception on error (uncaught)
* @return void, the message is modified by reference
*/
@@ -799,8 +831,7 @@
default:
{
- throwException( "Cannot convert to int32",
- "ConversionException" ) ;
+ Rcpp::throw("Cannot convert to int32");
}
}
break ;
@@ -838,8 +869,7 @@
}
default:
- throwException( "Cannot convert to int64",
- "ConversionException" ) ;
+ Rcpp::throw("Cannot convert to int64");
}
break ;
}
@@ -873,8 +903,7 @@
break ;
}
default:
- throwException( "Cannot convert to uint32",
- "ConversionException" ) ;
+ Rcpp::throw("Cannot convert to uint32");
}
break ;
}
@@ -908,8 +937,7 @@
break ;
}
default:
- throwException( "Cannot convert to int64",
- "ConversionException" ) ;
+ Rcpp::throw("Cannot convert to int64");
}
break ;
}
@@ -940,8 +968,7 @@
break ;
}
default:
- throwException( "Cannot convert to double",
- "ConversionException" ) ;
+ Rcpp::throw("Cannot convert to double");
}
break ;
}
@@ -973,8 +1000,7 @@
break ;
}
default:
- throwException( "Cannot convert to float",
- "ConversionException" ) ;
+ Rcpp::throw("Cannot convert to float");
}
break ;
}
@@ -1006,8 +1032,7 @@
break ;
}
default:
- throwException( "Cannot convert to bool",
- "ConversionException" ) ;
+ Rcpp::throw("Cannot convert to bool");
}
break ;
}
@@ -1058,8 +1083,7 @@
{
/* check if value is a message */
if( !Rf_inherits( value, "Message" ) ){
- throwException( "Can only convert S4 objects of class 'Message' ",
- "ConversionException" ) ;
+ Rcpp::throw("Can only convert S4 objects of class 'Message'");
}
GPB::Message* __mess = GET_MESSAGE_POINTER_FROM_S4( value ) ;
ref->SetRepeatedString(message, field_desc, 0,
@@ -1110,8 +1134,7 @@
break ;
}
default:
- throwException( "Cannot convert to string",
- "ConversionException" ) ;
+ Rcpp::throw("Cannot convert to string");
}
break ;
}
@@ -1156,7 +1179,7 @@
}
}
} else{
- throwException( "type mismatch, expecting a 'Message' object or a list of them", "TypeMismatchException" ) ;
+ Rcpp::throw("type mismatch, expecting a 'Message' object or a list of them");
}
break ;
}
@@ -1220,7 +1243,7 @@
// {{{ default
default:
{
- throwException( "cannot set enum value", "WrongTypeEnumValueException" ) ;
+ Rcpp::throw("cannot set enum value");
}
// }}}
}
@@ -1240,6 +1263,7 @@
* @return allways NULL, the message is modified by reference
*/
SEXP setMessageField( SEXP pointer, SEXP name, SEXP value ){
+ BEGIN_RCPP
// {{{ grab data
#ifdef RPB_DEBUG
Rprintf( "<setMessageField>\n" ) ;
@@ -1249,7 +1273,6 @@
PRINT_DEBUG_INFO( "value", value ) ;
#endif
- try {
/* grab the Message pointer */
GPB::Message* message = GET_MESSAGE_POINTER_FROM_XP(pointer) ;
@@ -1282,7 +1305,7 @@
} else if( TYPEOF(value) == VECSXP && allAreRaws( value ) ){
value_size = LENGTH(value) ;
} else {
- throwException( "cannot convert to string", "ConversionException" ) ;
+ Rcpp::throw("cannot convert to string");
}
}
// }}}
@@ -1295,10 +1318,7 @@
#ifdef RPB_DEBUG
Rprintf( "</setMessageField>\n" ) ;
#endif
- return R_NilValue ;
- } catch(std::exception &ex) {
- forward_exception_to_r(ex);
- }
+ END_RCPP
}
RPB_FUNCTION_VOID_2( update_message, Rcpp::XPtr<GPB::Message> message, Rcpp::List list ){
Modified: pkg/src/rprotobuf.cpp
===================================================================
--- pkg/src/rprotobuf.cpp 2013-12-27 09:11:21 UTC (rev 607)
+++ pkg/src/rprotobuf.cpp 2013-12-27 20:04:29 UTC (rev 608)
@@ -123,7 +123,7 @@
* @param descriptor a "Descriptor" R object
*/
SEXP newProtoMessage( SEXP descriptor ){
-
+ try {
#ifdef RPB_DEBUG
Rprintf( "<newProtoMessage>\n" ) ;
/* FIXME: the message type, we don't really need that*/
@@ -141,13 +141,16 @@
/* grab the Message from the factory */
const GPB::Message* message = PROTOTYPE( desc ) ;
if( !message ){
- throwException( "could not call factory->GetPrototype(desc)->New()", "MessageCreationException" ) ;
+ Rcpp_error("could not call factory->GetPrototype(desc)->New()");
}
#ifdef RPB_DEBUG
Rprintf( "</newProtoMessage>\n" ) ;
#endif
return( S4_Message( message ) ) ;
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ }
}
/**
@@ -221,6 +224,7 @@
GPB::FieldDescriptor* getFieldDescriptor(GPB::Message* message, SEXP name){
+ try {
GPB::FieldDescriptor* field_desc = (GPB::FieldDescriptor*)0;
const GPB::Descriptor* desc = message->GetDescriptor() ;
std::string error_message = "could not get FieldDescriptor for field";
@@ -230,7 +234,7 @@
if (Rf_inherits( name, "FieldDescriptor") ){
field_desc = GET_FIELD_DESCRIPTOR_POINTER_FROM_S4(name);
} else {
- throwException( "S4 class is not a FieldDescriptor", "NoSuchFieldException" ) ;
+ Rcpp::throw("S4 class is not a FieldDescriptor");
}
break ;
}
@@ -254,9 +258,12 @@
}
}
if( !field_desc ){
- throwException( error_message.c_str(), "NoSuchFieldException" ) ;
+ Rcpp::throw(error_message.c_str());
}
return field_desc ;
+ } catch(std::exception &ex) {
+ forward_exception_to_r(ex);
+ }
}
RPB_FUNCTION_VOID_1( check_libprotobuf_version, int minversion ){
Modified: pkg/src/wrapper_EnumDescriptor.cpp
===================================================================
--- pkg/src/wrapper_EnumDescriptor.cpp 2013-12-27 09:11:21 UTC (rev 607)
+++ pkg/src/wrapper_EnumDescriptor.cpp 2013-12-27 20:04:29 UTC (rev 608)
@@ -70,7 +70,7 @@
const GPB::EnumValueDescriptor* evd = d->FindValueByName(name) ;
if( !evd ){
/* or maybe it should just be NA */
- throwException( "cannot get the value", "UnknownEnumValueException" ) ;
+ Rcpp::throw("cannot get the value");
}
return evd->number();
}
Modified: pkg/src/wrapper_FieldDescriptor.cpp
===================================================================
--- pkg/src/wrapper_FieldDescriptor.cpp 2013-12-27 09:11:21 UTC (rev 607)
+++ pkg/src/wrapper_FieldDescriptor.cpp 2013-12-27 20:04:29 UTC (rev 608)
@@ -83,7 +83,7 @@
RPB_FUNCTION_1(S4_EnumDescriptor, METHOD(enum_type), Rcpp::XPtr<GPB::FieldDescriptor> d){
if( d->cpp_type() != CPPTYPE_ENUM ){
- throwException( "not an enum type field", "NotEnumType" );
+ Rcpp::throw("not an enum type field");
}
return S4_EnumDescriptor( d->enum_type() ) ;
}
Modified: pkg/src/wrapper_Message.cpp
===================================================================
--- pkg/src/wrapper_Message.cpp 2013-12-27 09:11:21 UTC (rev 607)
+++ pkg/src/wrapper_Message.cpp 2013-12-27 20:04:29 UTC (rev 608)
@@ -8,53 +8,55 @@
/* helpers */
- /* this is only to be called for repeated fields */
- int MESSAGE_GET_REPEATED_INT( GPB::Message* message, GPB::FieldDescriptor* field_desc, int index ){
+/* this is only to be called for repeated fields */
+int MESSAGE_GET_REPEATED_INT(GPB::Message* message,
+ GPB::FieldDescriptor* field_desc, int index ){
+ BEGIN_RCPP
+ const GPB::Reflection* ref = message->GetReflection() ;
- const GPB::Reflection* ref = message->GetReflection() ;
-
- switch( field_desc->type() ){
- case TYPE_INT32:
- case TYPE_SINT32:
- case TYPE_SFIXED32:
- return (int) ref->GetRepeatedInt32( *message, field_desc, index ) ;
- case TYPE_INT64:
- case TYPE_SINT64:
- case TYPE_SFIXED64:
- return (int) ref->GetRepeatedInt64( *message, field_desc, index ) ;
- case TYPE_UINT32:
- case TYPE_FIXED32:
- return (int) ref->GetRepeatedUInt32( *message, field_desc, index ) ;
- case TYPE_UINT64:
- case TYPE_FIXED64:
- return (int) ref->GetRepeatedUInt64( *message, field_desc, index ) ;
- case TYPE_ENUM:
- return ref->GetRepeatedEnum( *message, field_desc, index )->number() ;
- default:
- throwException( "cannot cast to int", "CastException" ) ;
- }
- return 0 ; // -Wall
+ switch( field_desc->type() ){
+ case TYPE_INT32:
+ case TYPE_SINT32:
+ case TYPE_SFIXED32:
+ return (int) ref->GetRepeatedInt32( *message, field_desc, index ) ;
+ case TYPE_INT64:
+ case TYPE_SINT64:
+ case TYPE_SFIXED64:
+ return (int) ref->GetRepeatedInt64( *message, field_desc, index ) ;
+ case TYPE_UINT32:
+ case TYPE_FIXED32:
+ return (int) ref->GetRepeatedUInt32( *message, field_desc, index ) ;
+ case TYPE_UINT64:
+ case TYPE_FIXED64:
+ return (int) ref->GetRepeatedUInt64( *message, field_desc, index ) ;
+ case TYPE_ENUM:
+ return ref->GetRepeatedEnum( *message, field_desc, index )->number() ;
+ default:
+ Rcpp_error("cannot cast to int");
}
+ VOID_END_RCPP
+ return 0 ; // -Wall
+}
- /* this is only to be called for repeated fields */
- double MESSAGE_GET_REPEATED_DOUBLE( GPB::Message* message, GPB::FieldDescriptor* field_desc, int index ){
+/* this is only to be called for repeated fields */
+double MESSAGE_GET_REPEATED_DOUBLE(GPB::Message* message,
+ GPB::FieldDescriptor* field_desc,
+ int index ){
+ BEGIN_RCPP
+ const GPB::Reflection* ref = message->GetReflection() ;
- const GPB::Reflection* ref = message->GetReflection() ;
-
- switch( field_desc->type() ){
- case TYPE_FLOAT:
- return (double) ref->GetRepeatedFloat( *message, field_desc, index ) ;
- case TYPE_DOUBLE:
- return (double) ref->GetRepeatedDouble( *message, field_desc, index ) ;
- default:
- throwException( "cannot cast to double", "CastException" ) ;
- }
- return 0 ; // -Wall
+ switch( field_desc->type() ){
+ case TYPE_FLOAT:
+ return (double) ref->GetRepeatedFloat( *message, field_desc, index ) ;
+ case TYPE_DOUBLE:
+ return (double) ref->GetRepeatedDouble( *message, field_desc, index ) ;
+ default:
+ Rcpp_error("cannot cast to double");
}
-
+ VOID_END_RCPP
+ return 0; // -Wall
+}
-
-
#undef METHOD
#define METHOD(__NAME__) RCPP_PP_CAT(Message__,__NAME__)
@@ -457,6 +459,7 @@
}
bool identical_messages_( GPB::Message* m1, GPB::Message* m2, double tol ){
+ BEGIN_RCPP
const GPB::Descriptor* d1 = m1->GetDescriptor() ;
const GPB::Descriptor* d2 = m2->GetDescriptor() ;
@@ -563,7 +566,7 @@
break ;
}
default:
- throwException( "unknown type" , "UnknownTypeException" ) ;
+ Rcpp_error("unknown type");
}
} else {
@@ -632,15 +635,14 @@
break ;
}
default:
- throwException( "unknown type" , "UnknownTypeException" ) ;
+ Rcpp_error("unknown type");
}
}
}
-
- /* finally */
- return true ;
-
+ VOID_END_RCPP
+ /* finally */
+ return true ;
}
RPB_FUNCTION_2( bool, identical_messages, Rcpp::XPtr<GPB::Message> m1, Rcpp::XPtr<GPB::Message> m2){
@@ -686,384 +688,379 @@
* @param field field tag number or name
* @param values values to append
*/
-RPB_FUNCTION_VOID_3( METHOD(add_values), Rcpp::XPtr<GPB::Message> message, SEXP field, SEXP values){
- const Reflection * ref = message->GetReflection() ;
- const GPB::FieldDescriptor* field_desc = getFieldDescriptor( message, field );
+RPB_FUNCTION_VOID_3( METHOD(add_values), Rcpp::XPtr<GPB::Message> message,
+ SEXP field, SEXP values){
+ const Reflection * ref = message->GetReflection() ;
+ const GPB::FieldDescriptor* field_desc = getFieldDescriptor( message, field );
- if( values == R_NilValue || LENGTH(values) == 0 ){
- return ;
- }
+ if( values == R_NilValue || LENGTH(values) == 0 ){
+ return ;
+ }
- if( field_desc->is_repeated() ){
- /* first check */
- switch( field_desc->type() ){
- case TYPE_ENUM:
+ if( field_desc->is_repeated() ){
+ /* first check */
+ switch( field_desc->type() ){
+ case TYPE_ENUM:
+ {
+ CHECK_values_for_enum( field_desc, values) ;
+ break ;
+ }
+ case TYPE_MESSAGE:
+ case TYPE_GROUP:
+ {
+ CHECK_messages( field_desc, values ) ;
+ break ;
+ }
+ default:
+ {// nothing
+ }
+ }
+
+ int value_size = LENGTH( values ) ;
+ /* then add the values */
+ switch( field_desc->type() ){
+ // {{{ int32
+ case TYPE_INT32:
+ case TYPE_SINT32:
+ case TYPE_SFIXED32:
+ {
+ switch( TYPEOF( values ) ){
+ case INTSXP:
+ case REALSXP:
+ case LGLSXP:
+ case RAWSXP:
{
- CHECK_values_for_enum( field_desc, values) ;
- break ;
+ for( int i=0; i<value_size; i++){
+ ref->AddInt32( message, field_desc, GET_int32(values,i) ) ;
}
- case TYPE_MESSAGE:
- case TYPE_GROUP:
+ break ;
+ }
+ default:
{
- CHECK_messages( field_desc, values ) ;
- break ;
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/rprotobuf -r 608
More information about the Rprotobuf-commits
mailing list