[Rprotobuf-commits] r327 - in pkg: R src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 14 19:05:31 CEST 2010


Author: romain
Date: 2010-07-14 19:05:31 +0200 (Wed, 14 Jul 2010)
New Revision: 327

Added:
   pkg/src/S4_classes.h
   pkg/src/wrapper_ArrayInputStream.cpp
   pkg/src/wrapper_ArrayOutputStream.cpp
   pkg/src/wrapper_ZeroCopyInputStream.cpp
Modified:
   pkg/R/wrapper_ZeroCopyInputStream.R
   pkg/src/RconnectionCopyingInputStream.cpp
   pkg/src/RconnectionCopyingInputStream.h
   pkg/src/rprotobuf.h
   pkg/src/streams.cpp
Log:
commiting some code I did not commit ages ago (still not working)

Modified: pkg/R/wrapper_ZeroCopyInputStream.R
===================================================================
--- pkg/R/wrapper_ZeroCopyInputStream.R	2010-07-14 00:12:54 UTC (rev 326)
+++ pkg/R/wrapper_ZeroCopyInputStream.R	2010-07-14 17:05:31 UTC (rev 327)
@@ -64,15 +64,15 @@
 } )
 setMethod( "ArrayInputStream", c( payload = "raw", block_size = "missing" ) , 
 function(payload, block_size){
-	.Call( "ArrayInputStream_new", payload, -1L, PACKAGE = "RProtoBuf" )
+	.Call( "ArrayInputStream__new", payload, -1L, PACKAGE = "RProtoBuf" )
 } )
 setMethod( "ArrayInputStream", c( payload = "raw", block_size = "integer" ) , 
 function(payload, block_size){
-	.Call( "ArrayInputStream_new", payload, block_size, PACKAGE = "RProtoBuf" )
+	.Call( "ArrayInputStream__new", payload, block_size, PACKAGE = "RProtoBuf" )
 } )
 setMethod( "ArrayInputStream", c( payload = "raw", block_size = "numeric" ) , 
 function(payload, block_size){
-	.Call( "ArrayInputStream_new", payload, as.integer(block_size), PACKAGE = "RProtoBuf" )
+	.Call( "ArrayInputStream__new", payload, as.integer(block_size), PACKAGE = "RProtoBuf" )
 } )
 # }}}
 
@@ -81,22 +81,22 @@
 	standardGeneric( "ArrayOutputStream" )
 } )
 setMethod( "ArrayOutputStream", signature( size = "integer", block_size = "missing" ), function(size, block_size){
-	.Call( "ArrayOutputStream_new", size, -1L, PACKAGE = "RProtoBuf" ) 
+	.Call( "ArrayOutputStream__new", size, -1L, PACKAGE = "RProtoBuf" ) 
 } )
 setMethod( "ArrayOutputStream", signature( size = "integer", block_size = "integer" ), function(size, block_size){
-	.Call( "ArrayOutputStream_new", size, block_size, PACKAGE = "RProtoBuf" ) 
+	.Call( "ArrayOutputStream__new", size, block_size, PACKAGE = "RProtoBuf" ) 
 } )
 setMethod( "ArrayOutputStream", signature( size = "integer", block_size = "numeric" ), function(size, block_size){
-	.Call( "ArrayOutputStream_new", size, as.integer(block_size) , PACKAGE = "RProtoBuf" ) 
+	.Call( "ArrayOutputStream__new", size, as.integer(block_size) , PACKAGE = "RProtoBuf" ) 
 } )
 setMethod( "ArrayOutputStream", signature( size = "numeric", block_size = "missing" ), function(size, block_size){
-	.Call( "ArrayOutputStream_new", as.integer(size), -1L, PACKAGE = "RProtoBuf" ) 
+	.Call( "ArrayOutputStream__new", as.integer(size), -1L, PACKAGE = "RProtoBuf" ) 
 } )
 setMethod( "ArrayOutputStream", signature( size = "numeric", block_size = "integer" ),function(size, block_size){
-	.Call( "ArrayOutputStream_new", as.integer(size), block_size, PACKAGE = "RProtoBuf" ) 
+	.Call( "ArrayOutputStream__new", as.integer(size), block_size, PACKAGE = "RProtoBuf" ) 
 } )
 setMethod( "ArrayOutputStream", signature( size = "numeric", block_size = "numeric" ), function(size, block_size){
-	.Call( "ArrayOutputStream_new", as.integer(size), as.integer(block_size) , PACKAGE = "RProtoBuf" ) 
+	.Call( "ArrayOutputStream__new", as.integer(size), as.integer(block_size) , PACKAGE = "RProtoBuf" ) 
 } )
 # }}}
 

Modified: pkg/src/RconnectionCopyingInputStream.cpp
===================================================================
--- pkg/src/RconnectionCopyingInputStream.cpp	2010-07-14 00:12:54 UTC (rev 326)
+++ pkg/src/RconnectionCopyingInputStream.cpp	2010-07-14 17:05:31 UTC (rev 327)
@@ -17,26 +17,18 @@
 	 */
 	int	RconnectionCopyingInputStream::Read(void * buffer, int size){
 		
-		SEXP call = PROTECT( getReadBinCall( size ) ) ;
-		SEXP res = PROTECT( Rf_eval( call, R_GlobalEnv ) ); 
+		Rcpp::Language call( "readBin", connection_id, Rcpp::RawVector(0), size ) ;
+		Rcpp::RawVector res ;
+		try{
+			res = call.run(); 
+		}  catch( ... ){
+			return 0 ;
+		}
 		
-		int len = LENGTH( res ) ;
-		memcpy( buffer, RAW(res), len ) ;
-		
-		UNPROTECT( 2 ) ; /* res, call */ 
+		int len = res.size() ;
+		memcpy( buffer, res.begin(), lenlen) ;
 		return len ;
 	}
 	
-	/* makes the call : readBin( con, raw(0), size ) */
-	SEXP RconnectionCopyingInputStream::getReadBinCall( int size ){
-		SEXP con = PROTECT( Rf_ScalarInteger(connection_id) );
-		SEXP what = PROTECT( Rf_allocVector(RAWSXP, 0) ) ;
-		SEXP n = PROTECT( Rf_ScalarInteger( size ) );
-		SEXP call = PROTECT( Rf_lang4( Rf_install( "readBin" ), con, what, n) ) ; 
-		UNPROTECT(4) ; /* call, n, what, con */
-		return call ;
-	}
-	
-	
 }
 

Modified: pkg/src/RconnectionCopyingInputStream.h
===================================================================
--- pkg/src/RconnectionCopyingInputStream.h	2010-07-14 00:12:54 UTC (rev 326)
+++ pkg/src/RconnectionCopyingInputStream.h	2010-07-14 17:05:31 UTC (rev 327)
@@ -11,8 +11,6 @@
 	
 		private: 
 			int connection_id ;
-			
-			SEXP getReadBinCall(int size) ;
 } ;
 
 } // namespace rprotobuf

Added: pkg/src/S4_classes.h
===================================================================
--- pkg/src/S4_classes.h	                        (rev 0)
+++ pkg/src/S4_classes.h	2010-07-14 17:05:31 UTC (rev 327)
@@ -0,0 +1,203 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// rprotobuf.h: R/C++ interface class library
+//
+// Copyright (C) 2010 Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of RProtoBuf.
+//
+// RProtoBuf is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// RProtoBuf is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with RProtoBuf.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef RPROTOBUF_S4CLASSES_H
+#define RPROTOBUF_S4CLASSES_H
+
+namespace rprotobuf {
+
+	class S4_EnumValueDescriptor : public Rcpp::S4 {
+	public:
+		S4_EnumValueDescriptor( const GPB::EnumValueDescriptor* d) : S4("EnumValueDescriptor"){
+		
+			if( d ){
+				slot( "pointer" ) = Rcpp::XPtr<GPB::EnumValueDescriptor>( 
+					const_cast<GPB::EnumValueDescriptor*>(d), false) ;
+			} else{
+				setSEXP( R_NilValue ); 
+			}
+		}
+		
+		S4_EnumValueDescriptor( const S4_EnumValueDescriptor& other) : S4(){
+			setSEXP( other.asSexp() );
+		}
+		S4_EnumValueDescriptor& operator=( const S4_EnumValueDescriptor& other){
+			setSEXP( other.asSexp() );
+			return *this ;
+		}
+		
+	} ;
+	
+	
+	class S4_Descriptor : public Rcpp::S4 {
+	public:
+		S4_Descriptor( const GPB::Descriptor* d) : S4( "Descriptor" ){
+			slot( "pointer" ) = Rcpp::XPtr<GPB::Descriptor>( 
+				const_cast<GPB::Descriptor*>(d), false) ;
+			slot( "type" )    = d->full_name() ;
+		}
+		
+		S4_Descriptor( const S4_Descriptor& other) : S4(){
+			setSEXP( other.asSexp() );
+		}
+		S4_Descriptor& operator=( const S4_Descriptor& other){
+			setSEXP( other.asSexp() );
+			return *this ;
+		}
+	} ;
+	
+	class S4_FieldDescriptor : public Rcpp::S4 {
+	public:
+		S4_FieldDescriptor( const GPB::FieldDescriptor* d) : S4( "FieldDescriptor" ){
+			slot( "pointer" ) = Rcpp::XPtr<GPB::FieldDescriptor>( 
+				const_cast<GPB::FieldDescriptor*>(d), false) ;
+			slot( "name" )      = d->name() ;
+			slot( "full_name" ) = d->full_name() ;
+			slot( "type" )      = d->containing_type()->full_name() ;
+		}
+		
+		S4_FieldDescriptor( const S4_FieldDescriptor& other) : S4(){
+			setSEXP( other.asSexp() );
+		}
+		S4_FieldDescriptor& operator=( const S4_FieldDescriptor& other){
+			setSEXP( other.asSexp() );
+			return *this ;
+		}
+	} ;
+	
+
+	class S4_ServiceDescriptor : public Rcpp::S4 {
+	public:
+		S4_ServiceDescriptor( const GPB::ServiceDescriptor* d) : S4( "ServiceDescriptor" ){
+			slot( "pointer" ) = Rcpp::XPtr<GPB::ServiceDescriptor>( 
+				const_cast<GPB::ServiceDescriptor*>(d), false) ;
+		}
+		
+		S4_ServiceDescriptor( const S4_ServiceDescriptor& other) : S4(){
+			setSEXP( other.asSexp() );
+		}
+		S4_ServiceDescriptor& operator=( const S4_ServiceDescriptor& other){
+			setSEXP( other.asSexp() );
+			return *this ;
+		}
+	} ;
+	
+	class S4_MethodDescriptor : public Rcpp::S4 {
+	public:
+		S4_MethodDescriptor( const GPB::MethodDescriptor* d) : S4( "MethodDescriptor" ){
+			slot( "pointer" ) = Rcpp::XPtr<GPB::MethodDescriptor>( 
+				const_cast<GPB::MethodDescriptor*>(d), false) ;
+		}
+		
+		S4_MethodDescriptor( const S4_MethodDescriptor& other) : S4(){
+			setSEXP( other.asSexp() );
+		}
+		S4_MethodDescriptor& operator=( const S4_MethodDescriptor& other){
+			setSEXP( other.asSexp() );
+			return *this ;
+		}
+	} ;
+	
+	class S4_EnumDescriptor : public Rcpp::S4 {
+	public:
+		S4_EnumDescriptor( const GPB::EnumDescriptor* d) : S4( "EnumDescriptor" ){
+			slot( "pointer" ) = Rcpp::XPtr<GPB::EnumDescriptor>( 
+				const_cast<GPB::EnumDescriptor*>(d), false) ;
+			slot( "name" )     = d->name() ;
+			slot( "full_name") = d->full_name() ;
+			const GPB::Descriptor *type_desc = d->containing_type() ;
+			if( type_desc ){
+				slot( "type" ) = type_desc->full_name()  ;
+			} else{
+				slot( "type" ) = Rcpp::StringVector(0) ;
+			}
+			
+		}
+		
+		S4_EnumDescriptor( const S4_EnumDescriptor& other) : S4(){
+			setSEXP( other.asSexp() );
+		}
+		S4_EnumDescriptor& operator=( const S4_EnumDescriptor& other){
+			setSEXP( other.asSexp() );
+			return *this ;
+		}
+	} ;
+
+	class S4_Message : public Rcpp::S4 {
+	public:
+		S4_Message( const GPB::Message* d) : S4( "Message" ){
+			slot( "pointer" ) = Rcpp::XPtr<GPB::Message>( 
+				const_cast<GPB::Message*>(d), true) ;
+			slot( "type" ) = d->GetDescriptor()->full_name() ;
+		}
+		
+		S4_Message( const S4_Message& other) : S4(){
+			setSEXP( other.asSexp() );
+		}
+		S4_Message& operator=( const S4_Message& other){
+			setSEXP( other.asSexp() );
+			return *this ;
+		}
+	} ;
+
+	class S4_ArrayOutputStream : public Rcpp::S4 {
+		
+	public:
+		S4_ArrayOutputStream( int size, int block_size ) : S4( "ArrayOutputStream" ) {
+			
+			Rcpp::RawVector payload(size) ;
+			GPB::io::ArrayOutputStream* stream = 
+				new GPB::io::ArrayOutputStream( payload.begin(), size, block_size ) ;
+		
+			Rcpp::XPtr<ZeroCopyOutputStreamWrapper> wrapper( 
+				new ZeroCopyOutputStreamWrapper(stream), true  ) ;
+			SETCDR( wrapper, payload ); /* TODO: update the API of Rcpp::XPtr */
+			slot( "pointer" ) = wrapper ;
+		}
+		
+		S4_ArrayOutputStream( const S4_ArrayOutputStream& other ){
+			setSEXP( other.asSexp() );
+		}
+		S4_ArrayOutputStream& operator=( const S4_ArrayOutputStream& other){
+			setSEXP( other.asSexp() );
+			return *this ;
+		}
+		
+	} ;
+	
+	class S4_ArrayInputStream : public Rcpp::S4 {
+	public:
+		S4_ArrayInputStream( Rcpp::RawVector payload, int block_size ) : S4( "ArrayInputStream" ){
+			GPB::io::ArrayInputStream* stream = 
+				new GPB::io::ArrayInputStream( payload.begin(), payload.size() , block_size ) ;
+			Rcpp::XPtr<ZeroCopyInputStreamWrapper> wrapper( 
+				new ZeroCopyInputStreamWrapper(stream), true ) ;
+			SETCDR( wrapper, payload ) ; /* TODO: update the API of Rcpp::XPtr */
+			slot("pointer") = wrapper ;
+		}
+	}
+	
+	
+	
+} // namespace rprotobuf
+
+
+#endif

Modified: pkg/src/rprotobuf.h
===================================================================
--- pkg/src/rprotobuf.h	2010-07-14 00:12:54 UTC (rev 326)
+++ pkg/src/rprotobuf.h	2010-07-14 17:05:31 UTC (rev 327)
@@ -99,151 +99,15 @@
 #define NEW_S4_OBJECT(CLAZZ) SEXP oo = PROTECT( NEW_OBJECT(MAKE_CLASS(CLAZZ)) ); \
   		if (!Rf_inherits(oo, CLAZZ)) throwException(CLAZZ, "CannotCreateObjectException" );
 
+#include "S4_classes.h"  		
+  		
 namespace rprotobuf{
 
 typedef GPB::int32  int32  ;
 typedef GPB::uint32 uint32 ;
 typedef GPB::int64  int64  ;
 typedef GPB::uint64 uint64 ;
-
-} // namespace rprotobuf
-
-namespace rprotobuf {
-
-	class S4_EnumValueDescriptor : public Rcpp::S4 {
-	public:
-		S4_EnumValueDescriptor( const GPB::EnumValueDescriptor* d) : S4("EnumValueDescriptor"){
-		
-			if( d ){
-				slot( "pointer" ) = Rcpp::XPtr<GPB::EnumValueDescriptor>( 
-					const_cast<GPB::EnumValueDescriptor*>(d), false) ;
-			} else{
-				setSEXP( R_NilValue ); 
-			}
-		}
-		
-		S4_EnumValueDescriptor( const S4_EnumValueDescriptor& other) : S4(){
-			setSEXP( other.asSexp() );
-		}
-		S4_EnumValueDescriptor& operator=( const S4_EnumValueDescriptor& other){
-			setSEXP( other.asSexp() );
-			return *this ;
-		}
-		
-	} ;
 	
-	
-	class S4_Descriptor : public Rcpp::S4 {
-	public:
-		S4_Descriptor( const GPB::Descriptor* d) : S4( "Descriptor" ){
-			slot( "pointer" ) = Rcpp::XPtr<GPB::Descriptor>( 
-				const_cast<GPB::Descriptor*>(d), false) ;
-			slot( "type" )    = d->full_name() ;
-		}
-		
-		S4_Descriptor( const S4_Descriptor& other) : S4(){
-			setSEXP( other.asSexp() );
-		}
-		S4_Descriptor& operator=( const S4_Descriptor& other){
-			setSEXP( other.asSexp() );
-			return *this ;
-		}
-	} ;
-	
-	class S4_FieldDescriptor : public Rcpp::S4 {
-	public:
-		S4_FieldDescriptor( const GPB::FieldDescriptor* d) : S4( "FieldDescriptor" ){
-			slot( "pointer" ) = Rcpp::XPtr<GPB::FieldDescriptor>( 
-				const_cast<GPB::FieldDescriptor*>(d), false) ;
-			slot( "name" )      = d->name() ;
-			slot( "full_name" ) = d->full_name() ;
-			slot( "type" )      = d->containing_type()->full_name() ;
-		}
-		
-		S4_FieldDescriptor( const S4_FieldDescriptor& other) : S4(){
-			setSEXP( other.asSexp() );
-		}
-		S4_FieldDescriptor& operator=( const S4_FieldDescriptor& other){
-			setSEXP( other.asSexp() );
-			return *this ;
-		}
-	} ;
-	
-
-	class S4_ServiceDescriptor : public Rcpp::S4 {
-	public:
-		S4_ServiceDescriptor( const GPB::ServiceDescriptor* d) : S4( "ServiceDescriptor" ){
-			slot( "pointer" ) = Rcpp::XPtr<GPB::ServiceDescriptor>( 
-				const_cast<GPB::ServiceDescriptor*>(d), false) ;
-		}
-		
-		S4_ServiceDescriptor( const S4_ServiceDescriptor& other) : S4(){
-			setSEXP( other.asSexp() );
-		}
-		S4_ServiceDescriptor& operator=( const S4_ServiceDescriptor& other){
-			setSEXP( other.asSexp() );
-			return *this ;
-		}
-	} ;
-	
-	class S4_MethodDescriptor : public Rcpp::S4 {
-	public:
-		S4_MethodDescriptor( const GPB::MethodDescriptor* d) : S4( "MethodDescriptor" ){
-			slot( "pointer" ) = Rcpp::XPtr<GPB::MethodDescriptor>( 
-				const_cast<GPB::MethodDescriptor*>(d), false) ;
-		}
-		
-		S4_MethodDescriptor( const S4_MethodDescriptor& other) : S4(){
-			setSEXP( other.asSexp() );
-		}
-		S4_MethodDescriptor& operator=( const S4_MethodDescriptor& other){
-			setSEXP( other.asSexp() );
-			return *this ;
-		}
-	} ;
-	
-	class S4_EnumDescriptor : public Rcpp::S4 {
-	public:
-		S4_EnumDescriptor( const GPB::EnumDescriptor* d) : S4( "EnumDescriptor" ){
-			slot( "pointer" ) = Rcpp::XPtr<GPB::EnumDescriptor>( 
-				const_cast<GPB::EnumDescriptor*>(d), false) ;
-			slot( "name" )     = d->name() ;
-			slot( "full_name") = d->full_name() ;
-			const GPB::Descriptor *type_desc = d->containing_type() ;
-			if( type_desc ){
-				slot( "type" ) = type_desc->full_name()  ;
-			} else{
-				slot( "type" ) = Rcpp::StringVector(0) ;
-			}
-			
-		}
-		
-		S4_EnumDescriptor( const S4_EnumDescriptor& other) : S4(){
-			setSEXP( other.asSexp() );
-		}
-		S4_EnumDescriptor& operator=( const S4_EnumDescriptor& other){
-			setSEXP( other.asSexp() );
-			return *this ;
-		}
-	} ;
-
-	class S4_Message : public Rcpp::S4 {
-	public:
-		S4_Message( const GPB::Message* d) : S4( "Message" ){
-			slot( "pointer" ) = Rcpp::XPtr<GPB::Message>( 
-				const_cast<GPB::Message*>(d), true) ;
-			slot( "type" ) = d->GetDescriptor()->full_name() ;
-		}
-		
-		S4_Message( const S4_Message& other) : S4(){
-			setSEXP( other.asSexp() );
-		}
-		S4_Message& operator=( const S4_Message& other){
-			setSEXP( other.asSexp() );
-			return *this ;
-		}
-	} ;
-	
 /* in rprotobuf.cpp */
 GPB::Message* PROTOTYPE( const GPB::Descriptor*) ;
 GPB::Message* CLONE(const GPB::Message*) ;
@@ -299,10 +163,6 @@
 RcppExport SEXP ZeroCopyInputStream_ReadLittleEndian32( SEXP ) ;
 RcppExport SEXP ZeroCopyInputStream_ReadLittleEndian64( SEXP ) ;
 
-RcppExport SEXP ArrayInputStream_new( SEXP, SEXP ) ;
-
-RcppExport SEXP ArrayOutputStream_new( SEXP, SEXP ) ;
-
 RcppExport SEXP ZeroCopyOutputStream_Next(SEXP, SEXP) ;
 RcppExport SEXP ZeroCopyOutputStream_BackUp(SEXP, SEXP) ;
 RcppExport SEXP ZeroCopyOutputStream_ByteCount(SEXP) ;

Modified: pkg/src/streams.cpp
===================================================================
--- pkg/src/streams.cpp	2010-07-14 00:12:54 UTC (rev 326)
+++ pkg/src/streams.cpp	2010-07-14 17:05:31 UTC (rev 327)
@@ -17,90 +17,8 @@
 
 namespace rprotobuf{
 	
-	// {{{ finalizer
-	void ZeroCopyInputStreamWrapper_finalizer( SEXP xp){
-		if (TYPEOF(xp)==EXTPTRSXP) {
-			ZeroCopyInputStreamWrapper* stream = (ZeroCopyInputStreamWrapper*)XPP(xp) ;
-			FIN_DBG( stream, "ZeroCopyInputStreamWrapper" ) ;
-			delete stream;
-		}
-	}
-	void ZeroCopyOutputStreamWrapper_finalizer( SEXP xp){
-		if (TYPEOF(xp)==EXTPTRSXP) {
-			ZeroCopyOutputStreamWrapper* stream = (ZeroCopyOutputStreamWrapper*)XPP(xp) ;
-			FIN_DBG( stream, "ZeroCopyOutputStreamWrapper" ) ;
-			delete stream;
-		}
-	}
-	// }}}
-	
 	// {{{ input streams
-	// {{{ ZeroCopyInputStream
-	SEXP ZeroCopyInputStream_Next( SEXP xp ){
-		GPB::io::ZeroCopyInputStream* stream = GET_ZCIS(xp) ;
-		int s = 0 ;
-		SEXP result = R_NilValue ; // -Wall 
-		const void* in ;
-		bool res = stream->Next( &in, &s );
-		if( !res ){
-			Rf_error( "cannot read from stream" ) ;
-		} else{
-			result = PROTECT( Rf_allocVector(RAWSXP, s) ) ;
-			memcpy( RAW(result), in, s ) ;
-			UNPROTECT(1) ; /* result */
-		}
-		return result ;
-	}
 	
-	SEXP ZeroCopyInputStream_BackUp(SEXP xp, SEXP size){
-		GPB::io::ZeroCopyInputStream* stream = GET_ZCIS(xp);
-		int s = GET_int(size, 0) ;
-		if( s <= 0 ){
-			Rf_error( "can only BackUp with positive numbers" ) ;
-		}
-		stream->BackUp( s ) ;
-		return R_NilValue ;
-	}
-	
-	
-	SEXP ZeroCopyInputStream_Skip(SEXP xp, SEXP size){
-		GPB::io::ZeroCopyInputStream* stream = GET_ZCIS(xp);
-		int s = GET_int(size, 0) ;
-		bool res = stream->Skip(s) ;
-		return( Rf_ScalarLogical( res ? _TRUE_ : _FALSE_ ) ) ;
-	}
-	
-	SEXP ZeroCopyInputStream_ByteCount(SEXP xp){
-		GPB::io::ZeroCopyInputStream* stream = GET_ZCIS(xp);
-		return( Rf_ScalarReal((double)stream->ByteCount())) ;
-	}
-	// }}}
-	// {{{ ArrayInputStream
-	SEXP ArrayInputStream_new( SEXP payload, SEXP block_size){
-		if( TYPEOF(payload) != RAWSXP ){
-			Rf_error( "expecting a raw vector" );  
-		}
-		
-		int bs = INTEGER(block_size)[0];
-		
-		NEW_S4_OBJECT( "ArrayInputStream" ) ;
-		
-		GPB::io::ArrayInputStream* stream = 
-			new GPB::io::ArrayInputStream( RAW(payload), LENGTH(payload), bs ) ;
-		ZeroCopyInputStreamWrapper* wrapper = new ZeroCopyInputStreamWrapper(stream) ; 
-		
-		/* we keep the payload protected from GC */
-		SEXP ptr = PROTECT( 
-			R_MakeExternalPtr( (void*)wrapper, R_NilValue, payload));
-		
-		/* delete the stream when the xp is GC'ed*/
-		R_RegisterCFinalizerEx( ptr, ZeroCopyInputStreamWrapper_finalizer , _FALSE_ ) ;
-		SET_SLOT( oo, Rf_install("pointer"), ptr ) ;
-		
-		UNPROTECT(2); /* oo, ptr */
-		return oo ;
-	}
-	// }}}
 	// {{{ FileInputStream
 	SEXP FileInputStream_new( SEXP filename, SEXP block_size, SEXP close_on_delete){
 		
@@ -180,27 +98,6 @@
 	}
 	// }}}
 	// {{{ ArrayOutputStream
-	SEXP ArrayOutputStream_new( SEXP size, SEXP block_size){
-		NEW_S4_OBJECT( "ArrayOutputStream" );
-		
-  	  	int s = INTEGER(size)[0]; 
-		int bs = INTEGER(block_size)[0];
-		
-  	  	SEXP payload = Rf_allocVector( RAWSXP, s ) ; 
-		GPB::io::ArrayOutputStream* stream = 
-			new GPB::io::ArrayOutputStream( RAW(payload), s, bs ) ;
-		ZeroCopyOutputStreamWrapper* wrapper = new ZeroCopyOutputStreamWrapper(stream) ;
-		
-		/* we keep the payload protected from GC */
-		SEXP ptr = PROTECT( 
-			R_MakeExternalPtr( (void*)wrapper, R_NilValue, payload));
-		/* delete the stream when the xp is GC'ed*/
-		R_RegisterCFinalizerEx( ptr, ZeroCopyOutputStreamWrapper_finalizer , _FALSE_ ) ;
-		SET_SLOT( oo, Rf_install("pointer"), ptr ) ;
-		
-		UNPROTECT(2); /* oo, ptr */
-		return oo ;
-	}
 	// }}}
 	// {{{ FileOutputStream
 	SEXP FileOutputStream_new( SEXP filename, SEXP block_size, SEXP close_on_delete){

Added: pkg/src/wrapper_ArrayInputStream.cpp
===================================================================
--- pkg/src/wrapper_ArrayInputStream.cpp	                        (rev 0)
+++ pkg/src/wrapper_ArrayInputStream.cpp	2010-07-14 17:05:31 UTC (rev 327)
@@ -0,0 +1,11 @@
+
+#include "rprotobuf.h"
+
+namespace rprotobuf{
+
+	RCPP_FUNCTION_2( S4_ArrayInputStream, ArrayInputStream__new, Rcpp::RawVector payload, int block_size){
+		return S4_ArrayInputStream( payload, block_size ); 
+	}
+
+}
+

Added: pkg/src/wrapper_ArrayOutputStream.cpp
===================================================================
--- pkg/src/wrapper_ArrayOutputStream.cpp	                        (rev 0)
+++ pkg/src/wrapper_ArrayOutputStream.cpp	2010-07-14 17:05:31 UTC (rev 327)
@@ -0,0 +1,11 @@
+
+#include "rprotobuf.h"
+
+namespace rprotobuf{
+
+	RCPP_FUNCTION_2( S4_ArrayOutputStream, ArrayOutputStream__new, int size, int block_size){
+		return S4_ArrayOutputStream( size, block_size ) ;
+	}
+
+}
+

Added: pkg/src/wrapper_ZeroCopyInputStream.cpp
===================================================================
--- pkg/src/wrapper_ZeroCopyInputStream.cpp	                        (rev 0)
+++ pkg/src/wrapper_ZeroCopyInputStream.cpp	2010-07-14 17:05:31 UTC (rev 327)
@@ -0,0 +1,43 @@
+#include "rprotobuf.h"
+
+namespace rprotobuf{
+
+	
+	SEXP ZeroCopyInputStream_Next( SEXP xp ){
+		GPB::io::ZeroCopyInputStream* stream = GET_ZCIS(xp) ;
+		int s = 0 ;
+		const void* in ;
+		bool res = stream->Next( &in, &s );
+		Rcpp::RawVector result ;
+		if( !res ){
+			throw std::range_error( "cannot read from stream" ) ;
+		} else{
+			result.assign( reinterpret_cast<Rbyte*>(in), reinterpret_cast<Rbyte*>(in) + s ) ;
+		}
+		return result ;
+	}
+	
+	SEXP ZeroCopyInputStream_BackUp(SEXP xp, SEXP size){
+		GPB::io::ZeroCopyInputStream* stream = GET_ZCIS(xp);
+		int s = GET_int(size, 0) ;
+		if( s <= 0 ){
+			Rf_error( "can only BackUp with positive numbers" ) ;
+		}
+		stream->BackUp( s ) ;
+		return R_NilValue ;
+	}
+	
+	
+	SEXP ZeroCopyInputStream_Skip(SEXP xp, SEXP size){
+		GPB::io::ZeroCopyInputStream* stream = GET_ZCIS(xp);
+		int s = GET_int(size, 0) ;
+		bool res = stream->Skip(s) ;
+		return( Rf_ScalarLogical( res ? _TRUE_ : _FALSE_ ) ) ;
+	}
+	
+	SEXP ZeroCopyInputStream_ByteCount(SEXP xp){
+		GPB::io::ZeroCopyInputStream* stream = GET_ZCIS(xp);
+		return( Rf_ScalarReal((double)stream->ByteCount())) ;
+	}
+
+}



More information about the Rprotobuf-commits mailing list