[Rprotobuf-commits] r722 - in pkg: . src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jan 5 04:26:06 CET 2014


Author: murray
Date: 2014-01-05 04:26:03 +0100 (Sun, 05 Jan 2014)
New Revision: 722

Modified:
   pkg/ChangeLog
   pkg/src/streams.cpp
Log:
Add missing BEGIN/END_RCPP and gracefully catch exceptions rather than long jumping without freeing resources with Rf_error.

Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2014-01-05 03:11:26 UTC (rev 721)
+++ pkg/ChangeLog	2014-01-05 03:26:03 UTC (rev 722)
@@ -13,6 +13,7 @@
 	* src/wrapper_ZeroCopyInputStream.cpp (rprotobuf): Add
 	  BEGIN/END_RCPP macros to gracefully catch exceptions and return
 	  them as R language errors.
+	* src/streams.cpp (rprotobuf): Idem.
 
 2014-01-04  Dirk Eddelbuettel  <edd at debian.org>
 

Modified: pkg/src/streams.cpp
===================================================================
--- pkg/src/streams.cpp	2014-01-05 03:11:26 UTC (rev 721)
+++ pkg/src/streams.cpp	2014-01-05 03:26:03 UTC (rev 722)
@@ -14,7 +14,7 @@
 
 // {{{ FileInputStream
 SEXP FileInputStream_new(SEXP filename, SEXP block_size, SEXP close_on_delete) {
-
+    BEGIN_RCPP
     NEW_S4_OBJECT("FileInputStream");
     int fd = open(CHAR(STRING_ELT(filename, 0)), O_RDONLY | O_BINARY);
 
@@ -28,25 +28,33 @@
 
     UNPROTECT(2); /* oo, ptr */
     return oo;
+    END_RCPP
 }
 SEXP FileInputStream_GetErrno(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::FileInputStream* stream = GET_FIS(xp);
     return Rf_ScalarInteger(stream->GetErrno());
+    END_RCPP
 }
 SEXP FileInputStream_SetCloseOnDelete(SEXP xp, SEXP close) {
+    BEGIN_RCPP
     GPB::io::FileInputStream* stream = GET_FIS(xp);
     stream->SetCloseOnDelete(LOGICAL(close));
     return R_NilValue;
+    END_RCPP
 }
 
 SEXP FileInputStream_Close(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::FileInputStream* stream = GET_FIS(xp);
     bool res = stream->Close();
     return Rf_ScalarLogical(res ? _TRUE_ : _FALSE_);
+    END_RCPP
 }
 // }}}
 // {{{ ConnectionInputStream
 SEXP ConnectionInputStream_new(SEXP con, SEXP was_open) {
+    BEGIN_RCPP
     NEW_S4_OBJECT("ConnectionInputStream");
     ConnectionInputStream* stream = new ConnectionInputStream(con, (bool)LOGICAL(was_open)[0]);
     ZeroCopyInputStreamWrapper* wrapper = new ZeroCopyInputStreamWrapper(stream);
@@ -56,6 +64,7 @@
 
     UNPROTECT(2); /* oo, ptr */
     return oo;
+    END_RCPP
 }
 // }}}
 // }}}
@@ -63,33 +72,40 @@
 // {{{ output streams
 // {{{ ZeroCopyOutputStream
 SEXP ZeroCopyOutputStream_Next(SEXP xp, SEXP payload) {
+    BEGIN_RCPP
     GPB::io::ZeroCopyOutputStream* stream = GET_ZCOS(xp);
     void* out;
     int s = LENGTH(payload);
     bool res = stream->Next(&out, &s);
     if (!res) {
-        Rf_error("cannot write to stream");
+        Rcpp_error("cannot write to stream");
     }
     memcpy(out, RAW(payload), s);
     return Rf_ScalarInteger(s);
+    END_RCPP
 }
 
 SEXP ZeroCopyOutputStream_ByteCount(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::ZeroCopyOutputStream* stream = GET_ZCOS(xp);
     return (Rf_ScalarReal((double)stream->ByteCount()));
+    END_RCPP
 }
 
 SEXP ZeroCopyOutputStream_BackUp(SEXP xp, SEXP count) {
+    BEGIN_RCPP
     GPB::io::ZeroCopyOutputStream* stream = GET_ZCOS(xp);
     int s = GET_int(count, 0);
     stream->BackUp(s);
     return R_NilValue;
+    END_RCPP
 }
 // }}}
 // {{{ ArrayOutputStream
 // }}}
 // {{{ FileOutputStream
 SEXP FileOutputStream_new(SEXP filename, SEXP block_size, SEXP close_on_delete) {
+    BEGIN_RCPP
     NEW_S4_OBJECT("FileOutputStream");
     int fd = open(CHAR(STRING_ELT(filename, 0)), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
 
@@ -103,29 +119,39 @@
 
     UNPROTECT(2); /* oo, ptr */
     return oo;
+    END_RCPP
 }
 SEXP FileOutputStream_Flush(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::FileOutputStream* stream = GET_FOS(xp);
     bool res = stream->Flush();
     return Rf_ScalarLogical(res ? _TRUE_ : _FALSE_);
+    END_RCPP
 }
 SEXP FileOutputStream_Close(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::FileOutputStream* stream = GET_FOS(xp);
     bool res = stream->Close();
     return Rf_ScalarLogical(res ? _TRUE_ : _FALSE_);
+    END_RCPP
 }
 SEXP FileOutputStream_GetErrno(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::FileOutputStream* stream = GET_FOS(xp);
     return Rf_ScalarInteger(stream->GetErrno());
+    END_RCPP
 }
 SEXP FileOutputStream_SetCloseOnDelete(SEXP xp, SEXP close) {
+    BEGIN_RCPP
     GPB::io::FileOutputStream* stream = GET_FOS(xp);
     stream->SetCloseOnDelete(LOGICAL(close));
     return R_NilValue;
+    END_RCPP
 }
 // }}}
 // {{{ ConnectionOutputStream
 SEXP ConnectionOutputStream_new(SEXP con, SEXP was_open) {
+    BEGIN_RCPP
     NEW_S4_OBJECT("ConnectionOutputStream");
     ConnectionOutputStream* stream = new ConnectionOutputStream(con, (bool)LOGICAL(was_open)[0]);
     ZeroCopyOutputStreamWrapper* wrapper = new ZeroCopyOutputStreamWrapper(stream);
@@ -137,6 +163,7 @@
 
     UNPROTECT(2); /* oo, ptr */
     return oo;
+    END_RCPP
 }
 // }}}
 
@@ -144,92 +171,116 @@
 
 // {{{ Read*** functions using CodedInputStream
 SEXP ZeroCopyInputStream_ReadRaw(SEXP xp, SEXP size) {
+    BEGIN_RCPP
     GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
     int s = INTEGER(size)[0];
     SEXP payload = PROTECT(Rf_allocVector(RAWSXP, s));
-    if (!coded_stream->ReadRaw(RAW(payload), s)) Rf_error("error reading raw bytes");
+    if (!coded_stream->ReadRaw(RAW(payload), s)) Rcpp_error("error reading raw bytes");
     UNPROTECT(1); /* payload */
     return payload;
+    END_RCPP
 }
 
 SEXP ZeroCopyInputStream_ReadString(SEXP xp, SEXP size) {
+    BEGIN_RCPP
     GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
     int s = INTEGER(size)[0];
     std::string buffer("");
-    if (!coded_stream->ReadString(&buffer, s)) Rf_error("error reading string");
+    if (!coded_stream->ReadString(&buffer, s)) Rcpp_error("error reading string");
 
     return Rf_mkString(buffer.c_str());
+    END_RCPP
 }
 
 SEXP ZeroCopyInputStream_ReadVarint32(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
     uint32 res = 0;
-    if (!coded_stream->ReadVarint32(&res)) Rf_error("error reading varint32");
+    if (!coded_stream->ReadVarint32(&res)) Rcpp_error("error reading varint32");
     return Rf_ScalarInteger(res);
+    END_RCPP
 }
 
 SEXP ZeroCopyInputStream_ReadLittleEndian32(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
     uint32 res = 0;
-    if (!coded_stream->ReadVarint32(&res)) Rf_error("error reading little endian int32");
+    if (!coded_stream->ReadVarint32(&res)) Rcpp_error("error reading little endian int32");
     return Rf_ScalarInteger(res);
+    END_RCPP
 }
 
 SEXP ZeroCopyInputStream_ReadLittleEndian64(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
     uint64 res = 0;
-    if (!coded_stream->ReadVarint64(&res)) Rf_error("error reading little endian int32");
+    if (!coded_stream->ReadVarint64(&res)) Rcpp_error("error reading little endian int32");
     return Rf_ScalarReal((double)res);
+    END_RCPP
 }
 
 SEXP ZeroCopyInputStream_ReadVarint64(SEXP xp) {
+    BEGIN_RCPP
     GPB::io::CodedInputStream* coded_stream = GET_CIS(xp);
     uint64 res = 0;
-    if (!coded_stream->ReadVarint64(&res)) Rf_error("error reading varint64");
+    if (!coded_stream->ReadVarint64(&res)) Rcpp_error("error reading varint64");
     return Rf_ScalarReal((double)res);
+    END_RCPP
 }
 // }}}
 
 // {{{ Write*** functions using CodedOuputStream
 SEXP ZeroCopyOutputStream_WriteRaw(SEXP xp, SEXP payload) {
+    BEGIN_RCPP
     GPB::io::CodedOutputStream* stream = GET_COS(xp);
     stream->WriteRaw(RAW(payload), LENGTH(payload));
     return R_NilValue;
+    END_RCPP
 }
 SEXP ZeroCopyOutputStream_WriteString(SEXP xp, SEXP payload) {
+    BEGIN_RCPP
     if (LENGTH(payload) > 1) {
         Rf_warning("only the first element is used");
     }
     if (LENGTH(payload) == 0) {
-        Rf_error("need at least one element");
+        Rcpp_error("need at least one element");
     }
     GPB::io::CodedOutputStream* stream = GET_COS(xp);
     stream->WriteString(CHAR(STRING_ELT(payload, 0)));
     return R_NilValue;
+    END_RCPP
 }
 
 SEXP ZeroCopyOutputStream_WriteLittleEndian32(SEXP xp, SEXP payload) {
+    BEGIN_RCPP
     GPB::io::CodedOutputStream* stream = GET_COS(xp);
     stream->WriteLittleEndian32(GET_int32(payload, 0));
     return R_NilValue;
+    END_RCPP
 }
 
 SEXP ZeroCopyOutputStream_WriteLittleEndian64(SEXP xp, SEXP payload) {
+    BEGIN_RCPP
     GPB::io::CodedOutputStream* stream = GET_COS(xp);
     stream->WriteLittleEndian64(GET_int64(payload, 0));
     return R_NilValue;
+    END_RCPP
 }
 
 SEXP ZeroCopyOutputStream_WriteVarint32(SEXP xp, SEXP payload) {
+    BEGIN_RCPP
     GPB::io::CodedOutputStream* stream = GET_COS(xp);
     stream->WriteVarint32(GET_int32(payload, 0));
     return R_NilValue;
+    END_RCPP
 }
 
 SEXP ZeroCopyOutputStream_WriteVarint64(SEXP xp, SEXP payload) {
+    BEGIN_RCPP
     GPB::io::CodedOutputStream* stream = GET_COS(xp);
     stream->WriteVarint64(GET_int64(payload, 0));
     return R_NilValue;
+    END_RCPP
 }
 // }}}
 



More information about the Rprotobuf-commits mailing list