[Rprotobuf-commits] r659 - pkg/src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Dec 31 02:38:54 CET 2013
Author: murray
Date: 2013-12-31 02:38:53 +0100 (Tue, 31 Dec 2013)
New Revision: 659
Modified:
pkg/src/extractors.cpp
pkg/src/mutators.cpp
Log:
clang-format the last 2 major C++ files now that the content changes
here have been reviewed, integrated, and tested against my private
branch.
Modified: pkg/src/extractors.cpp
===================================================================
--- pkg/src/extractors.cpp 2013-12-30 23:07:10 UTC (rev 658)
+++ pkg/src/extractors.cpp 2013-12-31 01:38:53 UTC (rev 659)
@@ -19,9 +19,9 @@
#include "fieldtypes.h"
#include "Rcppsupport.h"
-namespace rprotobuf{
+namespace rprotobuf {
-const char * kIntStringOptionName = "RProtoBuf.int64AsString";
+const char* kIntStringOptionName = "RProtoBuf.int64AsString";
bool UseStringsForInt64() {
static const SEXP option_name = Rf_install(kIntStringOptionName);
return (Rf_asLogical(Rf_GetOption1(option_name)));
@@ -30,22 +30,22 @@
// Rcpp::wrap silently coerces 64-bit integers to numerics
// which drop precision for values between 2^53 - 2^64.
// So, if an option is set, we return as a character string.
-template<typename ValueType>
+template <typename ValueType>
SEXP Int64AsSEXP(ValueType value) {
- 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.";
- Rcpp::stop(message.c_str());
- }
- return Rcpp::CharacterVector(ss.str());
- } else {
- return Rcpp::wrap(value);
- }
- END_RCPP
+ 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.";
+ Rcpp::stop(message.c_str());
+ }
+ return Rcpp::CharacterVector(ss.str());
+ } else {
+ return Rcpp::wrap(value);
+ }
+ END_RCPP
}
/**
@@ -54,148 +54,141 @@
* @param pointer external pointer to a message
* @param name name of the field
*
- * @return the field called "name" of the message if the
+ * @return the field called "name" of the message if the
* message has the field, otherwise an error is generated
*/
-SEXP getMessageField( SEXP pointer, SEXP name ){
-
+SEXP getMessageField(SEXP pointer, SEXP name) {
+
#ifdef RPB_DEBUG
-Rprintf( "<getMessageField>\n" ) ;
+ Rprintf("<getMessageField>\n");
-PRINT_DEBUG_INFO( "pointer", pointer ) ;
-PRINT_DEBUG_INFO( "name", name ) ;
+ PRINT_DEBUG_INFO("pointer", pointer);
+ PRINT_DEBUG_INFO("name", name);
#endif
- /* grab the Message pointer */
- Rcpp::XPtr<GPB::Message> message(pointer) ;
+ /* grab the Message pointer */
+ Rcpp::XPtr<GPB::Message> message(pointer);
- GPB::FieldDescriptor* field_desc = getFieldDescriptor( message, name ) ;
-
+ GPB::FieldDescriptor* field_desc = getFieldDescriptor(message, name);
+
#ifdef RPB_DEBUG
-Rprintf( "</getMessageField>\n" ) ;
+ Rprintf("</getMessageField>\n");
#endif
- return( extractFieldAsSEXP(message, field_desc) ) ;
-
+ return (extractFieldAsSEXP(message, field_desc));
}
-SEXP extractFieldAsSEXP( const Rcpp::XPtr<GPB::Message>& message,
- const GPB::FieldDescriptor* fieldDesc ){
- BEGIN_RCPP
- const Reflection * ref = message->GetReflection() ;
-
- if( fieldDesc->is_repeated() ){
-
- switch( GPB::FieldDescriptor::TypeToCppType(fieldDesc->type()) ){
+SEXP extractFieldAsSEXP(const Rcpp::XPtr<GPB::Message>& message,
+ const GPB::FieldDescriptor* fieldDesc) {
+ BEGIN_RCPP
+ const Reflection* ref = message->GetReflection();
+ if (fieldDesc->is_repeated()) {
+
+ switch (GPB::FieldDescriptor::TypeToCppType(fieldDesc->type())) {
+
#undef HANDLE_REPEATED_FIELD
-#define HANDLE_REPEATED_FIELD(TYPE,DATATYPE) \
- case TYPE : \
- return Rcpp::wrap( RepeatedFieldImporter<DATATYPE>(ref, *message, fieldDesc) ) ; \
+#define HANDLE_REPEATED_FIELD(TYPE, DATATYPE) \
+ case TYPE: \
+ return Rcpp::wrap(RepeatedFieldImporter<DATATYPE>(ref, *message, fieldDesc));
- HANDLE_REPEATED_FIELD(CPPTYPE_INT32, GPB::int32) ;
- HANDLE_REPEATED_FIELD(CPPTYPE_DOUBLE, double) ;
- HANDLE_REPEATED_FIELD(CPPTYPE_FLOAT, float) ;
- HANDLE_REPEATED_FIELD(CPPTYPE_BOOL, bool) ;
- HANDLE_REPEATED_FIELD(CPPTYPE_ENUM, enum_field ) ;
- HANDLE_REPEATED_FIELD(CPPTYPE_MESSAGE, message_field ) ;
- // TODO(mstokely): Rcpp doesn't handle uint32 properly as of 2013/12
- // See https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1360&group_id=155&atid=637
- case CPPTYPE_UINT32:
- return Rcpp::wrap(
- UInt32RepeatedFieldImporter(ref, *message,
- fieldDesc));
+ HANDLE_REPEATED_FIELD(CPPTYPE_INT32, GPB::int32);
+ HANDLE_REPEATED_FIELD(CPPTYPE_DOUBLE, double);
+ HANDLE_REPEATED_FIELD(CPPTYPE_FLOAT, float);
+ HANDLE_REPEATED_FIELD(CPPTYPE_BOOL, bool);
+ HANDLE_REPEATED_FIELD(CPPTYPE_ENUM, enum_field);
+ HANDLE_REPEATED_FIELD(CPPTYPE_MESSAGE, message_field);
+ // TODO(mstokely): Rcpp doesn't handle uint32 properly as of 2013/12
+ // See
+ // https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1360&group_id=155&atid=637
+ case CPPTYPE_UINT32:
+ return Rcpp::wrap(UInt32RepeatedFieldImporter(ref, *message, fieldDesc));
#ifdef RCPP_HAS_LONG_LONG_TYPES
// We can't handle these the same way, because Rcpp::wrap silently
// casts int64s to doubles which may cause us to lose precision.
case CPPTYPE_INT64:
if (UseStringsForInt64()) {
- return Rcpp::wrap(
- Int64AsStringRepeatedFieldImporter(ref, *message,
- fieldDesc));
+ return Rcpp::wrap(Int64AsStringRepeatedFieldImporter(ref, *message, fieldDesc));
} else {
- return Rcpp::wrap(
- RepeatedFieldImporter<int64>(ref, *message, fieldDesc));
+ return Rcpp::wrap(RepeatedFieldImporter<int64>(ref, *message, fieldDesc));
}
case CPPTYPE_UINT64:
if (UseStringsForInt64()) {
return Rcpp::wrap(
- UInt64AsStringRepeatedFieldImporter(ref, *message,
- fieldDesc));
+ UInt64AsStringRepeatedFieldImporter(ref, *message, fieldDesc));
} else {
- return Rcpp::wrap(
- RepeatedFieldImporter<uint64>(ref, *message, fieldDesc));
+ return Rcpp::wrap(RepeatedFieldImporter<uint64>(ref, *message, fieldDesc));
}
#endif
#undef HANDLE_REPEATED_FIELD
- case CPPTYPE_STRING:
- if (fieldDesc->type() == TYPE_STRING) {
- return Rcpp::wrap( RepeatedFieldImporter<std::string>(ref, *message, fieldDesc) ) ;
- } else if (fieldDesc->type() == TYPE_BYTES) {
- int field_size = ref->FieldSize( *message, fieldDesc ) ;
- Rcpp::List res(field_size);
- for (int i=0; i<field_size; i++) {
- std::string s = ref->GetRepeatedString(*message, fieldDesc, i);
- res[i] = Rcpp::wrap(std::vector<Rbyte>(s.begin(), s.end()));
- }
- return res;
- } else {
- Rcpp::stop("unknown field type with CPP_TYPE STRING");
- }
-
- default:
- Rcpp::stop("Unsupported type");
- }
-
+ case CPPTYPE_STRING:
+ if (fieldDesc->type() == TYPE_STRING) {
+ return Rcpp::wrap(RepeatedFieldImporter<std::string>(ref, *message, fieldDesc));
+ } else if (fieldDesc->type() == TYPE_BYTES) {
+ int field_size = ref->FieldSize(*message, fieldDesc);
+ Rcpp::List res(field_size);
+ for (int i = 0; i < field_size; i++) {
+ std::string s = ref->GetRepeatedString(*message, fieldDesc, i);
+ res[i] = Rcpp::wrap(std::vector<Rbyte>(s.begin(), s.end()));
+ }
+ return res;
+ } else {
+ Rcpp::stop("unknown field type with CPP_TYPE STRING");
+ }
+
+ default:
+ Rcpp::stop("Unsupported type");
+ }
+
} else {
- switch( GPB::FieldDescriptor::TypeToCppType(fieldDesc->type()) ){
+ switch (GPB::FieldDescriptor::TypeToCppType(fieldDesc->type())) {
#undef HANDLE_SINGLE_FIELD
-#define HANDLE_SINGLE_FIELD(CPPTYPE,SUFFIX) \
- case CPPTYPE: \
- return Rcpp::wrap( ref->Get##SUFFIX(*message, fieldDesc ) ) ;
+#define HANDLE_SINGLE_FIELD(CPPTYPE, SUFFIX) \
+ case CPPTYPE: \
+ return Rcpp::wrap(ref->Get##SUFFIX(*message, fieldDesc));
- HANDLE_SINGLE_FIELD( CPPTYPE_INT32, Int32 );
- HANDLE_SINGLE_FIELD( CPPTYPE_DOUBLE, Double );
- HANDLE_SINGLE_FIELD( CPPTYPE_FLOAT, Float );
- HANDLE_SINGLE_FIELD( CPPTYPE_BOOL, Bool );
- // TODO(mstokely): Rcpp doesn't handle uint32 properly as of 2013/12
- // See https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1360&group_id=155&atid=637
- case CPPTYPE_UINT32:
- return Rcpp::wrap( double(ref->GetUInt32(*message, fieldDesc)));
+ HANDLE_SINGLE_FIELD(CPPTYPE_INT32, Int32);
+ HANDLE_SINGLE_FIELD(CPPTYPE_DOUBLE, Double);
+ HANDLE_SINGLE_FIELD(CPPTYPE_FLOAT, Float);
+ HANDLE_SINGLE_FIELD(CPPTYPE_BOOL, Bool);
+ // TODO(mstokely): Rcpp doesn't handle uint32 properly as of 2013/12
+ // See
+ // https://r-forge.r-project.org/tracker/index.php?func=detail&aid=1360&group_id=155&atid=637
+ case CPPTYPE_UINT32:
+ return Rcpp::wrap(double(ref->GetUInt32(*message, fieldDesc)));
#ifdef RCPP_HAS_LONG_LONG_TYPES
- // Handle these types separately since Rcpp::wrap doesn't
- // do the right thing.
- case CPPTYPE_INT64:
- return Int64AsSEXP<int64>(ref->GetInt64(*message, fieldDesc));
- case CPPTYPE_UINT64:
- return Int64AsSEXP<uint64>(ref->GetUInt64(*message, fieldDesc));
+ // Handle these types separately since Rcpp::wrap doesn't
+ // do the right thing.
+ case CPPTYPE_INT64:
+ return Int64AsSEXP<int64>(ref->GetInt64(*message, fieldDesc));
+ case CPPTYPE_UINT64:
+ return Int64AsSEXP<uint64>(ref->GetUInt64(*message, fieldDesc));
#endif
#undef HANDLE_SINGLE_FIELD
- case CPPTYPE_STRING:
- if (fieldDesc->type() == TYPE_STRING) {
- return Rcpp::wrap( ref->GetString(*message, fieldDesc) );
- } else if (fieldDesc->type() == TYPE_BYTES) {
- std::string s = ref->GetString(*message, fieldDesc);
- return Rcpp::wrap(std::vector<Rbyte>(s.begin(), s.end()));
- } else {
- Rcpp::stop("unknown field type with CPP_TYPE STRING");
- }
- case CPPTYPE_ENUM :
- return Rcpp::wrap( ref->GetEnum( *message, fieldDesc )->number() ) ;
-
- case CPPTYPE_MESSAGE:
- return S4_Message( CLONE( &ref->GetMessage( *message, fieldDesc ) ) ) ;
- break ;
+ case CPPTYPE_STRING:
+ if (fieldDesc->type() == TYPE_STRING) {
+ return Rcpp::wrap(ref->GetString(*message, fieldDesc));
+ } else if (fieldDesc->type() == TYPE_BYTES) {
+ std::string s = ref->GetString(*message, fieldDesc);
+ return Rcpp::wrap(std::vector<Rbyte>(s.begin(), s.end()));
+ } else {
+ Rcpp::stop("unknown field type with CPP_TYPE STRING");
+ }
+ case CPPTYPE_ENUM:
+ return Rcpp::wrap(ref->GetEnum(*message, fieldDesc)->number());
- default:
- Rcpp::stop("Unsupported type");
- }
+ case CPPTYPE_MESSAGE:
+ return S4_Message(CLONE(&ref->GetMessage(*message, fieldDesc)));
+ break;
+ default:
+ Rcpp::stop("Unsupported type");
+ }
}
- END_RCPP
+ END_RCPP
}
-} // namespace rprotobuf
+} // namespace rprotobuf
Modified: pkg/src/mutators.cpp
===================================================================
--- pkg/src/mutators.cpp 2013-12-30 23:07:10 UTC (rev 658)
+++ pkg/src/mutators.cpp 2013-12-31 01:38:53 UTC (rev 659)
@@ -18,11 +18,11 @@
// You should have received a copy of the GNU General Public License
// along with RProtoBuf. If not, see <http://www.gnu.org/licenses/>.
-#include "rprotobuf.h"
-#include "fieldtypes.h"
+#include "rprotobuf.h"
+#include "fieldtypes.h"
#include "RcppMacros.h"
-namespace rprotobuf{
+namespace rprotobuf {
// {{{ GETDOUBLE
/**
@@ -34,327 +34,329 @@
* @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) ){
- case INTSXP:
- return( (double)INTEGER(x)[index] ) ;
- case REALSXP:
- return( REAL(x)[index] ) ;
- case LGLSXP:
- return( (double)LOGICAL(x)[index] );
- case RAWSXP:
- return( (double)RAW(x)[index] ) ;
- default:
- Rcpp::stop("cannot cast SEXP to double");
- }
- return 0.0 ; // -Wall
+double GET_double(SEXP x, int index) {
+ switch (TYPEOF(x)) {
+ case INTSXP:
+ return ((double)INTEGER(x)[index]);
+ case REALSXP:
+ return (REAL(x)[index]);
+ case LGLSXP:
+ return ((double)LOGICAL(x)[index]);
+ case RAWSXP:
+ return ((double)RAW(x)[index]);
+ default:
+ Rcpp::stop("cannot cast SEXP to double");
+ }
+ return 0.0; // -Wall
}
// }}}
-float GET_float( SEXP x, int index ){
- switch( TYPEOF(x) ){
- case INTSXP:
- return( (float)INTEGER(x)[index] ) ;
- case REALSXP:
- return( (float)REAL(x)[index] ) ;
- case LGLSXP:
- return( (float)LOGICAL(x)[index] );
- case RAWSXP:
- return( (float)RAW(x)[index] ) ;
- default:
- Rcpp::stop("cannot cast SEXP to double");
- }
- return (float)0.0 ; // -Wall
+float GET_float(SEXP x, int index) {
+ switch (TYPEOF(x)) {
+ case INTSXP:
+ return ((float)INTEGER(x)[index]);
+ case REALSXP:
+ return ((float)REAL(x)[index]);
+ case LGLSXP:
+ return ((float)LOGICAL(x)[index]);
+ case RAWSXP:
+ return ((float)RAW(x)[index]);
+ default:
+ Rcpp::stop("cannot cast SEXP to double");
+ }
+ return (float)0.0; // -Wall
}
-int GET_int( SEXP x, int index ){
- switch( TYPEOF(x) ){
- case INTSXP:
- return( INTEGER(x)[index] );
- case REALSXP:
- return( (int)REAL(x)[index] );
- case LGLSXP:
- return( (int)LOGICAL(x)[index] );
- case RAWSXP:
- return( (int)RAW(x)[index] ) ;
- default:
- Rcpp::stop( "cannot cast SEXP to int" );
- }
- return 0 ; // -Wall, should not happen since we only call this when we know it works
+int GET_int(SEXP x, int index) {
+ switch (TYPEOF(x)) {
+ case INTSXP:
+ return (INTEGER(x)[index]);
+ case REALSXP:
+ return ((int)REAL(x)[index]);
+ case LGLSXP:
+ return ((int)LOGICAL(x)[index]);
+ case RAWSXP:
+ return ((int)RAW(x)[index]);
+ default:
+ Rcpp::stop("cannot cast SEXP to int");
+ }
+ 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 +
- "' cannot be cast to 64-bit integer.";
- Rcpp::stop(message.c_str());
- }
- return ret;
+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 + "' cannot be cast to 64-bit integer.";
+ Rcpp::stop(message.c_str());
+ }
+ return ret;
}
-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 +
- "' cannot be cast to 32-bit integer.";
- Rcpp::stop(message.c_str());
- }
- return ret;
+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 + "' cannot be cast to 32-bit integer.";
+ Rcpp::stop(message.c_str());
+ }
+ return ret;
}
-int32 GET_int32( SEXP x, int index ){
- switch( TYPEOF(x) ){
- case INTSXP:
- return( (int32)INTEGER(x)[index] );
- case REALSXP:
- return( (int32)REAL(x)[index] );
- case LGLSXP:
- return( (int32)LOGICAL(x)[index] );
- case RAWSXP:
- return( (int32)RAW(x)[index] ) ;
- case STRSXP:
- return Int32FromString<int32>(CHAR(STRING_ELT(x, index)));
- default:
- Rcpp::stop( "cannot cast SEXP to int32");
- }
- return (int32)0 ; // -Wall, should not happen since we only call this when we know it works
+int32 GET_int32(SEXP x, int index) {
+ switch (TYPEOF(x)) {
+ case INTSXP:
+ return ((int32)INTEGER(x)[index]);
+ case REALSXP:
+ return ((int32)REAL(x)[index]);
+ case LGLSXP:
+ return ((int32)LOGICAL(x)[index]);
+ case RAWSXP:
+ return ((int32)RAW(x)[index]);
+ case STRSXP:
+ return Int32FromString<int32>(CHAR(STRING_ELT(x, index)));
+ default:
+ Rcpp::stop("cannot cast SEXP to int32");
+ }
+ 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) ){
- case INTSXP:
- return( (int64)INTEGER(x)[index] );
- case REALSXP:
- return( (int64)REAL(x)[index] );
- case LGLSXP:
- return( (int64)LOGICAL(x)[index] );
- case RAWSXP:
- return( (int64)RAW(x)[index] ) ;
- case STRSXP:
+int64 GET_int64(SEXP x, int index) {
+ switch (TYPEOF(x)) {
+ case INTSXP:
+ return ((int64)INTEGER(x)[index]);
+ case REALSXP:
+ return ((int64)REAL(x)[index]);
+ case LGLSXP:
+ return ((int64)LOGICAL(x)[index]);
+ case RAWSXP:
+ return ((int64)RAW(x)[index]);
+ case STRSXP:
return Int64FromString<int64>(CHAR(STRING_ELT(x, index)));
- default:
- Rcpp::stop("cannot cast SEXP to int64");
- }
- return (int64)0 ; // -Wall, should not happen since we only call this when we know it works
+ default:
+ Rcpp::stop("cannot cast SEXP to int64");
+ }
+ 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) ){
- case INTSXP:
- return( (uint32)INTEGER(x)[index] );
- case REALSXP:
- return( (uint32)REAL(x)[index] );
- case LGLSXP:
- return( (uint32)LOGICAL(x)[index] );
- case RAWSXP:
- return( (uint32)RAW(x)[index] ) ;
- case STRSXP:
+uint32 GET_uint32(SEXP x, int index) {
+ switch (TYPEOF(x)) {
+ case INTSXP:
+ return ((uint32)INTEGER(x)[index]);
+ case REALSXP:
+ return ((uint32)REAL(x)[index]);
+ case LGLSXP:
+ return ((uint32)LOGICAL(x)[index]);
+ case RAWSXP:
+ return ((uint32)RAW(x)[index]);
+ case STRSXP:
return Int32FromString<uint32>(CHAR(STRING_ELT(x, index)));
- default:
- Rcpp::stop("cannot cast SEXP to uint32");
- }
- return (uint32)0 ; // -Wall, should not happen since we only call this when we know it works
+ default:
+ Rcpp::stop("cannot cast SEXP to uint32");
+ }
+ 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) ){
- case INTSXP:
- return( (uint64)INTEGER(x)[index] );
- case REALSXP:
- return( (uint64)REAL(x)[index] );
- case LGLSXP:
- return( (uint64)LOGICAL(x)[index] );
- case RAWSXP:
- return( (uint64)RAW(x)[index] ) ;
- case STRSXP:
+uint64 GET_uint64(SEXP x, int index) {
+ switch (TYPEOF(x)) {
+ case INTSXP:
+ return ((uint64)INTEGER(x)[index]);
+ case REALSXP:
+ return ((uint64)REAL(x)[index]);
+ case LGLSXP:
+ return ((uint64)LOGICAL(x)[index]);
+ case RAWSXP:
+ return ((uint64)RAW(x)[index]);
+ case STRSXP:
return Int64FromString<uint64>(CHAR(STRING_ELT(x, index)));
- default:
- Rcpp::stop("cannot cast SEXP to uint64");
- }
- return (uint64)0 ; // -Wall, should not happen since we only call this when we know it works
+ default:
+ Rcpp::stop("cannot cast SEXP to uint64");
+ }
+ 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) ){
- case INTSXP:
+bool GET_bool(SEXP x, int index) {
+ switch (TYPEOF(x)) {
+ case INTSXP:
if (INTEGER(x)[index] == R_NaInt) {
- Rcpp::stop("NA boolean values can not be stored in "
- "bool protocol buffer fields");
+ Rcpp::stop(
+ "NA boolean values can not be stored in "
+ "bool protocol buffer fields");
}
- return( (bool)INTEGER(x)[index] );
- case REALSXP:
+ return ((bool)INTEGER(x)[index]);
+ case REALSXP:
if (REAL(x)[index] == R_NaReal) {
- Rcpp::stop("NA boolean values can not be stored in "
- "bool protocol buffer fields");
+ Rcpp::stop(
+ "NA boolean values can not be stored in "
+ "bool protocol buffer fields");
}
- return( (bool)REAL(x)[index] );
- case LGLSXP:
+ return ((bool)REAL(x)[index]);
+ case LGLSXP:
if (LOGICAL(x)[index] == NA_LOGICAL) {
- Rcpp::stop("NA boolean values can not be stored in "
- "bool protocol buffer fields");
+ Rcpp::stop(
+ "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:
- Rcpp::stop("cannot cast SEXP to bool");
- }
- return (bool)0 ; // -Wall, should not happen since we only call this when we know it works
+ return ((bool)LOGICAL(x)[index]);
+ case RAWSXP:
+ return ((bool)RAW(x)[index]);
+ default:
+ Rcpp::stop("cannot cast SEXP to bool");
+ }
+ return (bool)0; // -Wall, should not happen since we only call this when we know it works
}
-std::string GET_stdstring( SEXP x, int index ){
- if( TYPEOF(x) == STRSXP){
- return( CHAR(STRING_ELT(x, index)) );
- }
- return "" ; // -Wall, should not happen since we only call this when we know it works
+std::string GET_stdstring(SEXP x, int index) {
+ if (TYPEOF(x) == STRSXP) {
+ return (CHAR(STRING_ELT(x, index)));
+ }
+ return ""; // -Wall, should not happen since we only call this when we know it works
}
-std::string GET_bytes( SEXP x, int index ){
- switch( TYPEOF(x)) {
- case RAWSXP:
- if (index == 0) {
- return(std::string((const char *) RAW(x), (size_t) LENGTH(x)));
- } else {
- Rcpp::stop("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 {
- Rcpp::stop("cannot cast SEXP to bytes");
- }
- default:
- Rcpp::stop("cannot cast SEXP to bytes");
- }
- return "" ; // -Wall, should not happen since we only call this when we know it works
+std::string GET_bytes(SEXP x, int index) {
+ switch (TYPEOF(x)) {
+ case RAWSXP:
+ if (index == 0) {
+ return (std::string((const char*)RAW(x), (size_t)LENGTH(x)));
+ } else {
+ Rcpp::stop("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 {
+ Rcpp::stop("cannot cast SEXP to bytes");
+ }
+ default:
+ Rcpp::stop("cannot cast SEXP to bytes");
+ }
+ return ""; // -Wall, should not happen since we only call this when we know it works
}
/**
* indicates if this is a list of messages
- *
+ *
* @param x a list (VECSXP)
* @return TRUE if all objects are instances of Message class
*/
-Rboolean allAreMessages( SEXP x) {
-
- if( TYPEOF(x) != VECSXP ) return _FALSE_ ;
-
- int n = LENGTH(x) ;
- SEXP current ;
- for( int i=0; i<n; i++){
- current = VECTOR_ELT( x, i) ;
- /* not an S4 object */
- if( TYPEOF(current) != S4SXP ) return _FALSE_ ;
-
- /* not a Message object */
- if( !Rf_inherits( current, "Message" ) ) return _FALSE_ ;
- }
- return _TRUE_ ;
+Rboolean allAreMessages(SEXP x) {
+
+ if (TYPEOF(x) != VECSXP) return _FALSE_;
+
+ int n = LENGTH(x);
+ SEXP current;
+ for (int i = 0; i < n; i++) {
+ current = VECTOR_ELT(x, i);
+ /* not an S4 object */
+ if (TYPEOF(current) != S4SXP) return _FALSE_;
+
+ /* not a Message object */
+ if (!Rf_inherits(current, "Message")) return _FALSE_;
+ }
+ return _TRUE_;
}
/**
* indicates if this is a list of raws
- *
+ *
* @param x a list (VECSXP)
* @return TRUE if all objects are instances of RAWSXP
*/
-Rboolean allAreRaws( SEXP x) {
-
- if( TYPEOF(x) != VECSXP ) return _FALSE_ ;
-
- int n = LENGTH(x) ;
- SEXP current ;
- for( int i=0; i<n; i++){
- current = VECTOR_ELT( x, i) ;
- /* not a RAWSXP */
- if( TYPEOF(current) != RAWSXP ) return _FALSE_ ;
- }
- return _TRUE_ ;
+Rboolean allAreRaws(SEXP x) {
+
+ if (TYPEOF(x) != VECSXP) return _FALSE_;
+
+ int n = LENGTH(x);
+ SEXP current;
+ for (int i = 0; i < n; i++) {
+ current = VECTOR_ELT(x, i);
+ /* not a RAWSXP */
+ if (TYPEOF(current) != RAWSXP) return _FALSE_;
+ }
+ return _TRUE_;
}
/**
- * check that all the values contained in value are suitable for the
- * enum type
+ * check that all the values contained in value are suitable for the
+ * enum type
*
* @param field_desc field descriptor of an enum type
* @param value value to potentially fill the enum
*
*/
-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) ;
-
- switch( TYPEOF( value ) ){
- // {{{ numbers
- case INTSXP:
- case REALSXP:
- case LGLSXP:
- case RAWSXP:
- {
- int nenums = enum_desc->value_count() ;
- std::vector<int> possibles( nenums ) ;
- for( int i=0; i< nenums; i++){
- possibles[i] = enum_desc->value(i)->number();
- }
-
- /* loop around the numbers to see if they are in the possibles */
- for( int i=0; i<n; i++){
- int val = GET_int(value, i );
- int ok = 0;
- for( int j=0; j<nenums; j++){
- if( val == possibles[j] ){
- ok = 1;
- break ;
- }
- }
- if( !ok ){
- Rcpp::stop("wrong value for enum");
- }
- }
-
- break ;
- }
- // }}}
-
- // {{{ STRSXP
- case STRSXP:
- {
- int nenums = enum_desc->value_count() ;
- std::vector<std::string> possibles( nenums ) ;
- for( int i=0; i< nenums; i++){
- possibles[i] = enum_desc->value(i)->name() ;
- }
-
- /* loop around the numbers to see if they are in the possibles */
- for( int i=0; i<n; i++){
- const char* val = CHAR( STRING_ELT(value, i )) ;
- int ok = 0;
- /* FIXME: there is probably something more efficient */
- for( int j=0; j<nenums; j++){
- if( ! possibles[j].compare( val) ){
- ok = 1;
- break ;
- }
- }
- if( !ok ){
- Rcpp::stop("wrong value for enum");
- }
- }
- break ;
- }
- // }}}
-
- default:
- Rcpp::stop("impossible to convert to a enum");
+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);
+
+ switch (TYPEOF(value)) {
+ // {{{ numbers
+ case INTSXP:
+ case REALSXP:
+ case LGLSXP:
+ case RAWSXP: {
+ int nenums = enum_desc->value_count();
+ std::vector<int> possibles(nenums);
+ for (int i = 0; i < nenums; i++) {
+ possibles[i] = enum_desc->value(i)->number();
+ }
+
+ /* loop around the numbers to see if they are in the possibles */
+ for (int i = 0; i < n; i++) {
+ int val = GET_int(value, i);
+ int ok = 0;
+ for (int j = 0; j < nenums; j++) {
+ if (val == possibles[j]) {
+ ok = 1;
+ break;
+ }
+ }
+ if (!ok) {
+ Rcpp::stop("wrong value for enum");
+ }
+ }
+
+ break;
+ }
+ // }}}
+
+ // {{{ STRSXP
+ case STRSXP: {
+ int nenums = enum_desc->value_count();
+ std::vector<std::string> possibles(nenums);
+ for (int i = 0; i < nenums; i++) {
+ possibles[i] = enum_desc->value(i)->name();
+ }
+
+ /* loop around the numbers to see if they are in the possibles */
+ for (int i = 0; i < n; i++) {
+ const char* val = CHAR(STRING_ELT(value, i));
+ int ok = 0;
+ /* FIXME: there is probably something more efficient */
+ for (int j = 0; j < nenums; j++) {
+ if (!possibles[j].compare(val)) {
+ ok = 1;
+ break;
+ }
+ }
+ if (!ok) {
+ Rcpp::stop("wrong value for enum");
+ }
+ }
+ break;
+ }
+ // }}}
+
+ default:
+ Rcpp::stop("impossible to convert to a enum");
}
- VOID_END_RCPP
+ VOID_END_RCPP
}
/**
@@ -362,28 +364,28 @@
*
* @throws Rcpp::exception on error (uncaught)
*/
-void CHECK_messages( const GPB::FieldDescriptor* field_desc, SEXP values ){
- if( TYPEOF( values ) != VECSXP ){
- Rcpp::stop("expecting a list of messages");
- }
-
- const char* target = field_desc->message_type()->full_name().c_str() ;
- int n = LENGTH(values) ;
- for( int i=0; i<n; i++){
- if( !isMessage( VECTOR_ELT(values, i), target ) ){
- // TODO(mstokely): When we have C++11 CXX11, use
- // std::to_string(i)
- // {{{
- std::string s;
- std::stringstream out;
- out << i;
- s = out.str();
- // }}}
- string message = "List element " + s + " is not a message " +
- "of the appropriate type ('" + target + "')";
- Rcpp::stop(message.c_str());
- }
- }
+void CHECK_messages(const GPB::FieldDescriptor* field_desc, SEXP values) {
+ if (TYPEOF(values) != VECSXP) {
+ Rcpp::stop("expecting a list of messages");
+ }
+
+ const char* target = field_desc->message_type()->full_name().c_str();
+ int n = LENGTH(values);
+ for (int i = 0; i < n; i++) {
+ if (!isMessage(VECTOR_ELT(values, i), target)) {
+ // TODO(mstokely): When we have C++11 CXX11, use
+ // std::to_string(i)
+ // {{{
+ std::string s;
+ std::stringstream out;
+ out << i;
+ s = out.str();
+ // }}}
+ string message = "List element " + s + " is not a message " +
+ "of the appropriate type ('" + target + "')";
+ Rcpp::stop(message.c_str());
+ }
+ }
}
/**
@@ -391,133 +393,121 @@
* otherwise this could lead to modifying a few values then failing in
* an inconsistent state.
*/
-void CHECK_repeated_vals(const GPB::FieldDescriptor* field_desc,
- SEXP value, int value_size) {
- switch( field_desc->type() ){
- case TYPE_MESSAGE:
- case TYPE_GROUP:
- {
- switch( TYPEOF( value ) ){
- case VECSXP :
- {
+void CHECK_repeated_vals(const GPB::FieldDescriptor* field_desc, SEXP value, int value_size) {
+ switch (field_desc->type()) {
+ case TYPE_MESSAGE:
+ case TYPE_GROUP: {
+ switch (TYPEOF(value)) {
+ case VECSXP: {
- /* check that it is a list of Messages of the
- appropriate type */
- CHECK_messages(field_desc, value);
- break ;
- }
- case S4SXP:
- {
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/rprotobuf -r 659
More information about the Rprotobuf-commits
mailing list