[Rprotobuf-commits] r635 - pkg/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Dec 30 21:30:29 CET 2013


Author: murray
Date: 2013-12-30 21:30:29 +0100 (Mon, 30 Dec 2013)
New Revision: 635

Modified:
   pkg/src/wrapper_Message.cpp
Log:
Whitespace change only from clang-format (See STYLE file).

* Consistently wrap lines at 80 chars.
* Consistently avoid space inside '(' for argument lists.
* Consistenlty place '{' on same line as case statement.



Modified: pkg/src/wrapper_Message.cpp
===================================================================
--- pkg/src/wrapper_Message.cpp	2013-12-30 20:27:29 UTC (rev 634)
+++ pkg/src/wrapper_Message.cpp	2013-12-30 20:30:29 UTC (rev 635)
@@ -2,63 +2,64 @@
 #include "fieldtypes.h"
 #include "RcppMacros.h"
 
-#define SAME(x,y,tol) ( (tol==0.0 && x == y ) ||  ( ( (x-y)*(x-y) < tol*tol ) ? 1 : 0 ) ) 
+#define SAME(x, y, tol) \
+    ((tol == 0.0 && x == y) || (((x - y) * (x - y) < tol* tol) ? 1 : 0))
 
-namespace rprotobuf{
+namespace rprotobuf {
 
 /* helpers */
 
 /* 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() ; 
-		
-	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
+                             GPB::FieldDescriptor* field_desc, int index) {
+    BEGIN_RCPP
+    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:
+            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 ){
-	BEGIN_RCPP
-	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:
-		Rcpp_error("cannot cast to double");
-	}
-	VOID_END_RCPP
-	return 0; // -Wall
+                                   GPB::FieldDescriptor* field_desc,
+                                   int index) {
+    BEGIN_RCPP
+    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:
+            Rcpp_error("cannot cast to double");
+    }
+    VOID_END_RCPP
+    return 0;  // -Wall
 }
 
 #undef METHOD
-#define METHOD(__NAME__) RCPP_PP_CAT(Message__,__NAME__)
+#define METHOD(__NAME__) RCPP_PP_CAT(Message__, __NAME__)
 
 /**
  * clone a message
@@ -66,12 +67,12 @@
  * @param xp external pointer to a message
  * @return a new message, clone of the input message
  */
-RPB_FUNCTION_1( S4_Message, METHOD(clone) , Rcpp::XPtr<GPB::Message> message ){
-	/* cloning message as sheep */
-	GPB::Message* sheep = message->New() ;
-	sheep->CopyFrom( *message );
-	
-	return S4_Message( sheep ) ;
+RPB_FUNCTION_1(S4_Message, METHOD(clone), Rcpp::XPtr<GPB::Message> message) {
+    /* cloning message as sheep */
+    GPB::Message* sheep = message->New();
+    sheep->CopyFrom(*message);
+
+    return S4_Message(sheep);
 }
 
 /**
@@ -82,10 +83,11 @@
  * @param xp external pointer to the Message
  * @param name name of the field
  */
-RPB_FUNCTION_2(bool, METHOD(field_exists), Rcpp::XPtr<GPB::Message> message, std::string name ){
-	const GPB::Descriptor* desc = message->GetDescriptor(); 
-	const GPB::FieldDescriptor* field_desc = desc->FindFieldByName( name ) ;
-	return (field_desc != NULL);
+RPB_FUNCTION_2(bool, METHOD(field_exists), Rcpp::XPtr<GPB::Message> message,
+               std::string name) {
+    const GPB::Descriptor* desc = message->GetDescriptor();
+    const GPB::FieldDescriptor* field_desc = desc->FindFieldByName(name);
+    return (field_desc != NULL);
 }
 
 /**
@@ -95,72 +97,72 @@
  * @param xp external pointer to the Message
  * @param name name of the field
  */
-RPB_FUNCTION_2(bool, METHOD(has_field), Rcpp::XPtr<GPB::Message> message, std::string name ){
+RPB_FUNCTION_2(bool, METHOD(has_field), Rcpp::XPtr<GPB::Message> message,
+               std::string name) {
 
-	const GPB::Descriptor* desc = message->GetDescriptor(); 
-	const GPB::FieldDescriptor* field_desc = desc->FindFieldByName( name ) ;
-	
-	bool res = false ;
-	if( field_desc ){
-		const GPB::Reflection * ref = message->GetReflection() ;
-		if( field_desc->is_repeated() ){
-			res = ref->FieldSize( *message, field_desc ) > 0 ;
-		} else{
-			res = ref->HasField( *message, field_desc ) ;
-		}
-	}
-	return res ;
+    const GPB::Descriptor* desc = message->GetDescriptor();
+    const GPB::FieldDescriptor* field_desc = desc->FindFieldByName(name);
+
+    bool res = false;
+    if (field_desc) {
+        const GPB::Reflection* ref = message->GetReflection();
+        if (field_desc->is_repeated()) {
+            res = ref->FieldSize(*message, field_desc) > 0;
+        } else {
+            res = ref->HasField(*message, field_desc);
+        }
+    }
+    return res;
 }
 
-
 /**
  * Check if all required fields are set
  *
  * @param xp external pointer to the Message
  */
-RPB_FUNCTION_1( bool, METHOD(is_initialized), Rcpp::XPtr<GPB::Message> message){
-	return message->IsInitialized() ;
+RPB_FUNCTION_1(bool, METHOD(is_initialized), Rcpp::XPtr<GPB::Message> message) {
+    return message->IsInitialized();
 }
 
-
-/** 
+/**
  * serialize a message to a file
  *
  * @param xp external pointer to a GPB::Message*
  * @param filename file name where to serialize
  */
-RPB_FUNCTION_VOID_2( METHOD(serialize_to_file) , Rcpp::XPtr<GPB::Message> message, const char* filename){
-	
-	/* open the file in binary mode to write */
-	/* we make sure in the R side that filename is the full path of the file */
-	int file = open( filename, 
-		O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); 
-	
-	/* using partial to allow partially filled messages */
-	message->SerializePartialToFileDescriptor( file ) ;
-	
-	close( file ) ; 
+RPB_FUNCTION_VOID_2(METHOD(serialize_to_file), Rcpp::XPtr<GPB::Message> message,
+                    const char* filename) {
+
+    /* open the file in binary mode to write */
+    /* we make sure in the R side that filename is the full path of the file */
+    int file = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
+
+    /* using partial to allow partially filled messages */
+    message->SerializePartialToFileDescriptor(file);
+
+    close(file);
 }
 
 /**
- * create a raw vector that contains the content of the serialized 
+ * create a raw vector that contains the content of the serialized
  * message
  *
  * @param xp xternal pointer to the message
  */
-RPB_FUNCTION_1( Rcpp::RawVector, METHOD(get_payload), Rcpp::XPtr<GPB::Message> message ){
+RPB_FUNCTION_1(Rcpp::RawVector, METHOD(get_payload),
+               Rcpp::XPtr<GPB::Message> message) {
 
-	/* create a raw vector of the appropriate size */
-	int size = message->ByteSize() ;
-	Rcpp::RawVector payload( size ) ;
-	
-	/* fill the array */
-	message->SerializePartialToArray( payload.begin(), size ); 
-	
-	return( payload ) ;
+    /* create a raw vector of the appropriate size */
+    int size = message->ByteSize();
+    Rcpp::RawVector payload(size);
+
+    /* fill the array */
+    message->SerializePartialToArray(payload.begin(), size);
+
+    return (payload);
 }
 
-RPB_XP_METHOD_VOID_0(METHOD(clear), GPB::Message, Clear )
+RPB_XP_METHOD_VOID_0(METHOD(clear), GPB::Message, Clear)
 
 /**
  * Clear a field of a message
@@ -168,63 +170,64 @@
  * @param xp (GPB::Message*) external pointer
  * @param field name or tag of the field
  */
-RPB_FUNCTION_VOID_2(METHOD(clear_field), Rcpp::XPtr<GPB::Message> m, SEXP field ){
-	const GPB::FieldDescriptor* field_desc = getFieldDescriptor( m, field ) ;
-	const GPB::Reflection* ref = m->GetReflection(); 
-	ref->ClearField( m, field_desc ) ;
+RPB_FUNCTION_VOID_2(METHOD(clear_field), Rcpp::XPtr<GPB::Message> m,
+                    SEXP field) {
+    const GPB::FieldDescriptor* field_desc = getFieldDescriptor(m, field);
+    const GPB::Reflection* ref = m->GetReflection();
+    ref->ClearField(m, field_desc);
 }
 
-
 /**
  * @param xp external pointer to a Message
  * @return the message as an R list
  */
-RPB_FUNCTION_1( Rcpp::List, METHOD(as_list), Rcpp::XPtr<GPB::Message> message ){
-    
-	const GPB::Descriptor* desc = message->GetDescriptor() ;
-	int nf = desc->field_count() ;
-	
-	Rcpp::CharacterVector fieldNames(nf) ;
-	Rcpp::List val( nf ) ;
-	/* TODO: not use getMessageField */
-	for( int i=0; i<nf; i++){
-		const GPB::FieldDescriptor* fd = desc->field(i) ;
-		val[i] = 
-			getMessageField( message, Rcpp::CharacterVector::create( fd->name() ) ) ;
-		fieldNames[i] = fd->name() ;
-	}
-	val.names() = fieldNames ;
-	return val ;
+RPB_FUNCTION_1(Rcpp::List, METHOD(as_list), Rcpp::XPtr<GPB::Message> message) {
+
+    const GPB::Descriptor* desc = message->GetDescriptor();
+    int nf = desc->field_count();
+
+    Rcpp::CharacterVector fieldNames(nf);
+    Rcpp::List val(nf);
+    /* TODO: not use getMessageField */
+    for (int i = 0; i < nf; i++) {
+        const GPB::FieldDescriptor* fd = desc->field(i);
+        val[i] =
+            getMessageField(message, Rcpp::CharacterVector::create(fd->name()));
+        fieldNames[i] = fd->name();
+    }
+    val.names() = fieldNames;
+    return val;
 }
 
 /**
- * The number of fields the message has. A field counts in these two situations :
+ * The number of fields the message has. A field counts in these two situations
+ *:
  * - it is repeated and the array size is greater than 0
  * - it is not repeated and the message has it
  *
  * @param xp external pointer to the Message
  */
-RPB_FUNCTION_1(int, METHOD(length), Rcpp::XPtr<GPB::Message> message){
-	const GPB::Descriptor* desc = message->GetDescriptor(); 
-	const GPB::Reflection * ref = message->GetReflection() ;
-	
-	int nfields = desc->field_count() ;
-	
-	int res = 0; 
-	
-	for( int i=0; i<nfields; i++){
-		const GPB::FieldDescriptor* field_desc = desc->field( i ) ;
-		if( field_desc->is_repeated() ){
-			if( ref->FieldSize( *message, field_desc ) > 0 ){
-				res++ ;
-			}
-		} else{
-			if( ref->HasField( *message, field_desc ) ){
-				res++ ;
-			}
-		}
-	}
-	return res ;
+RPB_FUNCTION_1(int, METHOD(length), Rcpp::XPtr<GPB::Message> message) {
+    const GPB::Descriptor* desc = message->GetDescriptor();
+    const GPB::Reflection* ref = message->GetReflection();
+
+    int nfields = desc->field_count();
+
+    int res = 0;
+
+    for (int i = 0; i < nfields; i++) {
+        const GPB::FieldDescriptor* field_desc = desc->field(i);
+        if (field_desc->is_repeated()) {
+            if (ref->FieldSize(*message, field_desc) > 0) {
+                res++;
+            }
+        } else {
+            if (ref->HasField(*message, field_desc)) {
+                res++;
+            }
+        }
+    }
+    return res;
 }
 
 /**
@@ -232,212 +235,197 @@
  *
  * @param xp external pointer to the Message
  */
-RPB_FUNCTION_1(int, METHOD(num_extensions), Rcpp::XPtr<GPB::Message> message){
-	const GPB::Reflection * ref = message->GetReflection() ;
-	int nexts = 0;
-	vector<const FieldDescriptor*> fields;
-	ref->ListFields(*message, &fields);
-	for (int i = 0; i < fields.size(); i++) {
-	  if (fields[i]->is_extension()) {
-	    nexts++;
-	  }
-	}
-	return nexts ;
+RPB_FUNCTION_1(int, METHOD(num_extensions), Rcpp::XPtr<GPB::Message> message) {
+    const GPB::Reflection* ref = message->GetReflection();
+    int nexts = 0;
+    vector<const FieldDescriptor*> fields;
+    ref->ListFields(*message, &fields);
+    for (int i = 0; i < fields.size(); i++) {
+        if (fields[i]->is_extension()) {
+            nexts++;
+        }
+    }
+    return nexts;
 }
 
 /**
  * Get the message descriptor of a Message
- * 
+ *
  * @param xp (GPB::Message*) external pointer
  * @return the descriptor, as a Descriptor R S4 object
  */
-RPB_FUNCTION_1(S4_Descriptor, METHOD(descriptor), Rcpp::XPtr<GPB::Message> message ){
-	return( message->GetDescriptor() ) ;
+RPB_FUNCTION_1(S4_Descriptor, METHOD(descriptor),
+               Rcpp::XPtr<GPB::Message> message) {
+    return (message->GetDescriptor());
 }
 
-RPB_XP_METHOD_0( METHOD(as_character) , GPB::Message, DebugString) 
-RPB_XP_METHOD_0( METHOD(bytesize), GPB::Message, ByteSize )
+RPB_XP_METHOD_0(METHOD(as_character), GPB::Message, DebugString)
+RPB_XP_METHOD_0(METHOD(bytesize), GPB::Message, ByteSize)
 
-RPB_FUNCTION_2( int, METHOD(field_size), Rcpp::XPtr<GPB::Message> message, SEXP field  ){
-	
-	const GPB::FieldDescriptor* field_desc = getFieldDescriptor( message, field ) ;
-	
-	int res = 0 ;
-	if( field_desc->is_repeated() ){
-		res = message->GetReflection()->FieldSize(*message, field_desc ) ;
-	} else{
-		res = message->GetReflection()->HasField(*message, field_desc) ? 1: 0 ;
-	}
-	return res ;
+RPB_FUNCTION_2(int, METHOD(field_size), Rcpp::XPtr<GPB::Message> message,
+               SEXP field) {
+
+    const GPB::FieldDescriptor* field_desc = getFieldDescriptor(message, field);
+
+    int res = 0;
+    if (field_desc->is_repeated()) {
+        res = message->GetReflection()->FieldSize(*message, field_desc);
+    } else {
+        res = message->GetReflection()->HasField(*message, field_desc) ? 1 : 0;
+    }
+    return res;
 }
 
-RPB_FUNCTION_1( S4_FileDescriptor, METHOD(fileDescriptor), Rcpp::XPtr<GPB::Message> message ){
-	return S4_FileDescriptor( message->GetDescriptor()->file() ) ; 
+RPB_FUNCTION_1(S4_FileDescriptor, METHOD(fileDescriptor),
+               Rcpp::XPtr<GPB::Message> message) {
+    return S4_FileDescriptor(message->GetDescriptor()->file());
 }
 
+RPB_FUNCTION_VOID_3(METHOD(set_field_size), Rcpp::XPtr<GPB::Message> message,
+                    SEXP field, int target) {
 
-RPB_FUNCTION_VOID_3( METHOD(set_field_size), Rcpp::XPtr<GPB::Message> message, SEXP field, int target){
-	
-	const GPB::FieldDescriptor* field_desc = getFieldDescriptor( message, field ) ;
-	const GPB::Reflection* ref = message->GetReflection() ;
-	
-	if( field_desc->is_repeated() ){
-		int current = ref->FieldSize(*message, field_desc ) ;
-		
-		if( target == 0){
-			ref->ClearField( message, field_desc );
-		} else if( current > target ){
-			while( current != target ){
-				ref->RemoveLast( message, field_desc ) ;
-				current-- ;
-			}
-		} else if( current == target ) {
-			/* nothing to do */
-		} else { /* current < target */
-			
-			while( current != target ){
-			
-				switch( field_desc->type() ){
-						case TYPE_INT32:
-    					case TYPE_SINT32:
-    					case TYPE_SFIXED32:
-    				        {
-    				        	ref->AddInt32(message, field_desc, (int32)0 ) ;
-    				        	break ;
-    				        }
-    					case TYPE_INT64:
-						case TYPE_SINT64:
-						case TYPE_SFIXED64:
-    				        {
-    				        	ref->AddInt64(message, field_desc, (int64)0 ) ;
-    				        	break ;
-    				        }
-		    			case TYPE_UINT32:
-    					case TYPE_FIXED32:
-    						{
-    							ref->AddUInt32(message, field_desc, (uint32)0 ) ;
-    				        	break ;
-    				        }
-    					case TYPE_UINT64:
-    					case TYPE_FIXED64:
-    						{
-    							ref->AddUInt32(message, field_desc, (uint64)0 ) ;
-    				        	break ;
-    						}
-    				    case TYPE_DOUBLE:
-    				    	{
-    				    		ref->AddDouble(message, field_desc, (double)0.0 ) ;
-    				    		break ;
-    				    	}
-    				    case TYPE_FLOAT:
-    				    	{
-    				    		ref->AddFloat(message, field_desc, (float)0.0 ) ;
-    				    		break ;
-    				    	}
-    				    case TYPE_BOOL:
-    				    	{
-    				    		ref->AddBool(message, field_desc, (bool)0 ) ;
-    				    		break ;
-    				    	}
-    				    case TYPE_STRING:
-    				    case TYPE_BYTES:
-    				    	{
-    				    		ref->AddString(message, field_desc, "" ) ;
-    							break ;
-    				    	}
-    				    case TYPE_MESSAGE:
-    				    case TYPE_GROUP:
-    				    	{
-    				    		/* fill with the prototype for that message type */
-    				    		Rf_error( "growing repeated messages not implemented yet, patches welcome" ) ;
-    				    		break ;
-    				    	}
-    				    case TYPE_ENUM:
-    				    	{ 
-    				    		/* fill with the prototype for that message type */
-    				    		Rf_error( "growing repeated enum not implemented yet, patches welcome" ) ;
-    				    		break ;
-    				    	}
-    				} /* switch */
-    			
-    			current++ ;
-    		} /* while */
-			
-			
-		}
-		
-	} else{
-		if( target == 0 ){
-			if( ref->HasField( *message, field_desc ) ){
-				ref->ClearField( message, field_desc );
-			}
-		} else {
-			if( !ref->HasField( *message, field_desc ) ){
-				switch( field_desc->type() ){
-					case TYPE_INT32:
-    				case TYPE_SINT32:
-    				case TYPE_SFIXED32:
-    			        {
-    			        	ref->SetInt32(message, field_desc, (int32)0 ) ;
-    			        	break ;
-    			        }
-    				case TYPE_INT64:
-					case TYPE_SINT64:
-					case TYPE_SFIXED64:
-    			        {
-    			        	ref->SetInt64(message, field_desc, (int64)0 ) ;
-    			        	break ;
-    			        }
-		    		case TYPE_UINT32:
-    				case TYPE_FIXED32:
-    					{
-    						ref->SetUInt32(message, field_desc, (uint32)0 ) ;
-    			        	break ;
-    			        }
-    				case TYPE_UINT64:
-    				case TYPE_FIXED64:
-    					{
-    						ref->SetUInt32(message, field_desc, (uint64)0 ) ;
-    			        	break ;
-    					}
-    			    case TYPE_DOUBLE:
-    			    	{
-    			    		ref->SetDouble(message, field_desc, (double)0.0 ) ;
-    			    		break ;
-    			    	}
-    			    case TYPE_FLOAT:
-    			    	{
-    			    		ref->SetFloat(message, field_desc, (float)0.0 ) ;
-    			    		break ;
-    			    	}
-    			    case TYPE_BOOL:
-    			    	{
-    			    		ref->SetBool(message, field_desc, (bool)0 ) ;
-    			    		break ;
-    			    	}
-    			    case TYPE_STRING:
-    			    case TYPE_BYTES:
-    			    	{
-    			    		ref->SetString(message, field_desc, "" ) ;
-    						break ;
-    			    	}
-    			    case TYPE_MESSAGE:
-    			    case TYPE_GROUP:
-    			    	{
-    			    		/* fill with the prototype for that message type */
-    			    		Rf_error( "not implemented yet, patches welcome" ) ;
-    			    		break ;
-    			    	}
-    			    case TYPE_ENUM:
-    			    	{ 
-    			    		/* fill with the prototype for that message type */
-    			    		Rf_error( "not implemented yet, patches welcome" ) ;
-    			    		break ;
-    			    	}
-    				}
-    			}
-		}
-	}
+    const GPB::FieldDescriptor* field_desc = getFieldDescriptor(message, field);
+    const GPB::Reflection* ref = message->GetReflection();
+
+    if (field_desc->is_repeated()) {
+        int current = ref->FieldSize(*message, field_desc);
+
+        if (target == 0) {
+            ref->ClearField(message, field_desc);
+        } else if (current > target) {
+            while (current != target) {
+                ref->RemoveLast(message, field_desc);
+                current--;
+            }
+        } else if (current == target) {
+            /* nothing to do */
+        } else {/* current < target */
+
+            while (current != target) {
+
+                switch (field_desc->type()) {
+                    case TYPE_INT32:
+                    case TYPE_SINT32:
+                    case TYPE_SFIXED32: {
+                        ref->AddInt32(message, field_desc, (int32)0);
+                        break;
+                    }
+                    case TYPE_INT64:
+                    case TYPE_SINT64:
+                    case TYPE_SFIXED64: {
+                        ref->AddInt64(message, field_desc, (int64)0);
+                        break;
+                    }
+                    case TYPE_UINT32:
+                    case TYPE_FIXED32: {
+                        ref->AddUInt32(message, field_desc, (uint32)0);
+                        break;
+                    }
+                    case TYPE_UINT64:
+                    case TYPE_FIXED64: {
+                        ref->AddUInt32(message, field_desc, (uint64)0);
+                        break;
+                    }
+                    case TYPE_DOUBLE: {
+                        ref->AddDouble(message, field_desc, (double)0.0);
+                        break;
+                    }
+                    case TYPE_FLOAT: {
+                        ref->AddFloat(message, field_desc, (float)0.0);
+                        break;
+                    }
+                    case TYPE_BOOL: {
+                        ref->AddBool(message, field_desc, (bool)0);
+                        break;
+                    }
+                    case TYPE_STRING:
+                    case TYPE_BYTES: {
+                        ref->AddString(message, field_desc, "");
+                        break;
+                    }
+                    case TYPE_MESSAGE:
+                    case TYPE_GROUP: {
+                        /* fill with the prototype for that message type */
+                        Rf_error(
+                            "growing repeated messages not implemented yet, "
+                            "patches welcome");
+                        break;
+                    }
+                    case TYPE_ENUM: {
+                        /* fill with the prototype for that message type */
+                        Rf_error(
+                            "growing repeated enum not implemented yet, "
+                            "patches welcome");
+                        break;
+                    }
+                } /* switch */
+
+                current++;
+            } /* while */
+        }
+
+    } else {
+        if (target == 0) {
+            if (ref->HasField(*message, field_desc)) {
+                ref->ClearField(message, field_desc);
+            }
+        } else {
+            if (!ref->HasField(*message, field_desc)) {
+                switch (field_desc->type()) {
+                    case TYPE_INT32:
+                    case TYPE_SINT32:
+                    case TYPE_SFIXED32: {
+                        ref->SetInt32(message, field_desc, (int32)0);
+                        break;
+                    }
+                    case TYPE_INT64:
+                    case TYPE_SINT64:
+                    case TYPE_SFIXED64: {
+                        ref->SetInt64(message, field_desc, (int64)0);
+                        break;
+                    }
+                    case TYPE_UINT32:
+                    case TYPE_FIXED32: {
+                        ref->SetUInt32(message, field_desc, (uint32)0);
+                        break;
+                    }
+                    case TYPE_UINT64:
+                    case TYPE_FIXED64: {
+                        ref->SetUInt32(message, field_desc, (uint64)0);
+                        break;
+                    }
+                    case TYPE_DOUBLE: {
+                        ref->SetDouble(message, field_desc, (double)0.0);
+                        break;
+                    }
+                    case TYPE_FLOAT: {
+                        ref->SetFloat(message, field_desc, (float)0.0);
+                        break;
+                    }
+                    case TYPE_BOOL: {
+                        ref->SetBool(message, field_desc, (bool)0);
+                        break;
+                    }
+                    case TYPE_STRING:
+                    case TYPE_BYTES: {
+                        ref->SetString(message, field_desc, "");
+                        break;
+                    }
+                    case TYPE_MESSAGE:
+                    case TYPE_GROUP: {
+                        /* fill with the prototype for that message type */
+                        Rf_error("not implemented yet, patches welcome");
+                        break;
+                    }
+                    case TYPE_ENUM: {
+                        /* fill with the prototype for that message type */
+                        Rf_error("not implemented yet, patches welcome");
+                        break;
+                    }
+                }
+            }
+        }
+    }
 }
 
 /**
@@ -447,222 +435,247 @@
  *
  * @return field names, as an R character vector (STRSXP)
  */
-RPB_FUNCTION_1( Rcpp::CharacterVector, METHOD(fieldNames), Rcpp::XPtr<GPB::Message> message){
-	const GPB::Descriptor* desc = message->GetDescriptor() ;
-	
-	int nfields = desc->field_count() ; 
-	Rcpp::CharacterVector res(nfields) ;
-	for(int i=0; i<nfields; i++){
-		res[i] = desc->field(i)->name() ;
-	}
-	return( res );
+RPB_FUNCTION_1(Rcpp::CharacterVector, METHOD(fieldNames),
+               Rcpp::XPtr<GPB::Message> message) {
+    const GPB::Descriptor* desc = message->GetDescriptor();
+
+    int nfields = desc->field_count();
+    Rcpp::CharacterVector res(nfields);
+    for (int i = 0; i < nfields; i++) {
+        res[i] = desc->field(i)->name();
+    }
+    return (res);
 }
 
-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() ;
-	
-	/* first of all, check if this is the same message type */
-	if( d1 != d2 ){
-		return false ;
-	}
-	
-	const GPB::Reflection* ref = m2->GetReflection() ;
-	
-	/* iterate field descriptors */
-	int nf = d1->field_count() ;
-	for( int i=0; i<nf; i++){
-		const GPB::FieldDescriptor* field_desc = d1->field( i ) ;
-		
-		if( field_desc->is_repeated() ){
-			
-			/* test if the size differs */
-			int fs = ref->FieldSize( *m1, field_desc) ;
-			if( fs != ref->FieldSize( *m2, field_desc) ) return false ;
-			
-			/* test all items */
-			switch( field_desc->type() ){
-				case TYPE_INT32:
-				case TYPE_SINT32:
-				case TYPE_SFIXED32:
-					{
-						for( int j=0; j<fs; j++){
-							if( ref->GetRepeatedInt32( *m1, field_desc, j ) != ref->GetRepeatedInt32( *m2, field_desc, j ) ) return false ; 
-						}
-						break ;
-					}
-				case TYPE_INT64:
-				case TYPE_SINT64:
-				case TYPE_SFIXED64:
-					{
-						for( int j=0; j<fs; j++){
-							if( ref->GetRepeatedInt64( *m1, field_desc, j ) != ref->GetRepeatedInt64( *m2, field_desc, j ) ) return false ; 
-						}
-						break ;
-					}
-    			case TYPE_UINT32:
-    			case TYPE_FIXED32:
-    				{
-    					for( int j=0; j<fs; j++){
-							if( ref->GetRepeatedUInt32( *m1, field_desc, j ) != ref->GetRepeatedUInt32( *m2, field_desc, j ) ) return false ; 
-						}
-						break ;
-    				}
-    			case TYPE_UINT64:
-    			case TYPE_FIXED64:
-    				{
-    					for( int j=0; j<fs; j++){
-							if( ref->GetRepeatedUInt64( *m1, field_desc, j ) != ref->GetRepeatedUInt64( *m2, field_desc, j ) ) return false ; 
-						}
-						break ;	    					
-    				}
-    			case TYPE_DOUBLE:
-    				{
-    					for( int j=0; j<fs; j++){
-							if( !SAME( ref->GetRepeatedDouble( *m1, field_desc, j ), ref->GetRepeatedDouble( *m2, field_desc, j ), tol) ) return false ; 
-						}
-						break ;	    					
-    				}
-    			case TYPE_FLOAT:
-    				{
-    					for( int j=0; j<fs; j++){
-							if( !SAME( ref->GetRepeatedFloat( *m1, field_desc, j ), ref->GetRepeatedFloat( *m2, field_desc, j ), tol) ) return false ; 
-						}
-						break ;	    					
-    				}
-    			case TYPE_BOOL:
-    				{
-    					for( int j=0; j<fs; j++){
-							if( ref->GetRepeatedBool( *m1, field_desc, j ) != ref->GetRepeatedBool( *m2, field_desc, j ) ) return false ; 
-						}
-						break ;	    					
-    				}
-				case TYPE_STRING:
-    			case TYPE_BYTES:
-    				{
-    					for( int j=0; j<fs; j++){
-							if( ref->GetRepeatedString( *m1, field_desc, j ) != ref->GetRepeatedString( *m2, field_desc, j ) ) return false ; 
-						}
-						break ;	    					
-    				}
-    			case TYPE_ENUM :
-    				{
-    					for( int j=0; j<fs; j++){
-							if( ref->GetRepeatedEnum( *m1, field_desc, j ) != ref->GetRepeatedEnum( *m2, field_desc, j ) ) return false ; 
-						}
-						break ;
-    				}
-    			case TYPE_MESSAGE:
-    			case TYPE_GROUP:
-    				{
-    					for( int j=0; j<fs; j++){
-							const GPB::Message* mm1 = &ref->GetRepeatedMessage( *m1, field_desc, j ) ;
-							const GPB::Message* mm2 = &ref->GetRepeatedMessage( *m2, field_desc, j ) ;
-    						if( !identical_messages_( (GPB::Message*)mm1, (GPB::Message*)mm2, tol ) ){
-    							return false ;
-    						}
-						}
-						break ;
-    				}
-    			default:
-				Rcpp_error("unknown type");
-			}
-			
-		} else {
-			
-			switch( field_desc->type() ){
-				case TYPE_INT32:
-				case TYPE_SINT32:
-				case TYPE_SFIXED32:
-					{
-						if( ref->GetInt32( *m1, field_desc) != ref->GetInt32( *m2, field_desc ) ) return false ; 
-						break ;
-					}
-				case TYPE_INT64:
-				case TYPE_SINT64:
-				case TYPE_SFIXED64:
-					{
-						if( ref->GetInt64( *m1, field_desc) != ref->GetInt64( *m2, field_desc) ) return false ; 
-						break ;
-					}
-    			case TYPE_UINT32:
-    			case TYPE_FIXED32:
-    				{
-    					if( ref->GetUInt32( *m1, field_desc ) != ref->GetUInt32( *m2, field_desc ) ) return false ; 
-						break ;
-    				}
-    			case TYPE_UINT64:
-    			case TYPE_FIXED64:
-    				{
-    					if( ref->GetUInt64( *m1, field_desc ) != ref->GetUInt64( *m2, field_desc ) ) return false ; 
-						break ;	    					
-    				}
-    			case TYPE_DOUBLE:
-    				{
-    					if( ref->GetDouble( *m1, field_desc ) != ref->GetDouble( *m2, field_desc ) ) return false ; 
-						break ;	    					
-    				}
-    			case TYPE_FLOAT:
-    				{
-    					if( ref->GetFloat( *m1, field_desc ) != ref->GetFloat( *m2, field_desc ) ) return false ; 
-						break ;	    					
-    				}
-    			case TYPE_BOOL:
-    				{
-    					if( ref->GetBool( *m1, field_desc ) != ref->GetBool( *m2, field_desc ) ) return false ; 
-						break ;	    					
-    				}
-				case TYPE_STRING:
-    			case TYPE_BYTES:
-    				{
-    					if( ref->GetString( *m1, field_desc ) != ref->GetString( *m2, field_desc ) ) return false ; 
-						break ;	    					
-    				}
-    			case TYPE_ENUM :
-    				{
-    					if( ref->GetEnum( *m1, field_desc) != ref->GetEnum( *m2, field_desc ) ) return false ; 
-						break ;
-    				}
-    			case TYPE_MESSAGE:
-    			case TYPE_GROUP:
-    				{
-    					const GPB::Message* mm1 = &ref->GetMessage( *m1, field_desc ) ;
-						const GPB::Message* mm2 = &ref->GetMessage( *m2, field_desc ) ;
-    					if( !identical_messages_( (GPB::Message*)mm1, (GPB::Message*)mm2, tol ) ){
-    						return false ;
-    					}
-						break ;
-    				}
-    			default:
-				Rcpp_error("unknown type");
-			}
-			
-		}
-	}
-  VOID_END_RCPP
-  /* finally */
-  return true ;
+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();
+
+    /* first of all, check if this is the same message type */
+    if (d1 != d2) {
+        return false;
+    }
+
+    const GPB::Reflection* ref = m2->GetReflection();
+
+    /* iterate field descriptors */
+    int nf = d1->field_count();
+    for (int i = 0; i < nf; i++) {
+        const GPB::FieldDescriptor* field_desc = d1->field(i);
+
+        if (field_desc->is_repeated()) {
+
+            /* test if the size differs */
+            int fs = ref->FieldSize(*m1, field_desc);
+            if (fs != ref->FieldSize(*m2, field_desc)) return false;
+
+            /* test all items */
+            switch (field_desc->type()) {
+                case TYPE_INT32:
+                case TYPE_SINT32:
+                case TYPE_SFIXED32: {
+                    for (int j = 0; j < fs; j++) {
+                        if (ref->GetRepeatedInt32(*m1, field_desc, j) !=
+                            ref->GetRepeatedInt32(*m2, field_desc, j))
+                            return false;
+                    }
+                    break;
+                }
+                case TYPE_INT64:
+                case TYPE_SINT64:
+                case TYPE_SFIXED64: {
+                    for (int j = 0; j < fs; j++) {
+                        if (ref->GetRepeatedInt64(*m1, field_desc, j) !=
+                            ref->GetRepeatedInt64(*m2, field_desc, j))
+                            return false;
+                    }
+                    break;
+                }
+                case TYPE_UINT32:
+                case TYPE_FIXED32: {
+                    for (int j = 0; j < fs; j++) {
+                        if (ref->GetRepeatedUInt32(*m1, field_desc, j) !=
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/rprotobuf -r 635


More information about the Rprotobuf-commits mailing list