From noreply at r-forge.r-project.org Fri Feb 22 04:53:51 2013 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 22 Feb 2013 04:53:51 +0100 (CET) Subject: [Rprotobuf-commits] r491 - in pkg: . R inst/unitTests man src Message-ID: <20130222035351.562A818496E@r-forge.r-project.org> Author: murray Date: 2013-02-22 04:53:51 +0100 (Fri, 22 Feb 2013) New Revision: 491 Modified: pkg/ChangeLog pkg/R/00classes.R pkg/inst/unitTests/runit.addressbook.R pkg/man/FileDescriptor-class.Rd pkg/src/S4_classes.h Log: Add more user-friendly show() methods for Message and FileDescriptor objects to provide more context to the user about what they are looking at. For Messages, also print the number of fields that are currently set in the message. For FileDescriptors, print the package and filename. Modified: pkg/ChangeLog =================================================================== --- pkg/ChangeLog 2012-10-06 03:40:58 UTC (rev 490) +++ pkg/ChangeLog 2013-02-22 03:53:51 UTC (rev 491) @@ -1,3 +1,17 @@ +2013-02-21 Murray Stokely + + * R/00classes.R: add filename and package slots to FileDescriptor + S4 class and provide more user-friendly show() methods for + Messages and FileDescriptors that provide more context about the + contained data. + * src/S4_classes.h: populate package and filename slots when a + FileDescriptor is created based on fields from the underlying + C++ object. + * inst/unitTests/runit.addressbook.R: add a basic test for + FileDescriptors. + * man/FileDescriptor-class.Rd: document the new slots in + FileDescriptor and add examples. + 2012-10-03 Murray Stokely * src/mutators.cpp (rprotobuf): Fix bug where LENGTH() is used on Modified: pkg/R/00classes.R =================================================================== --- pkg/R/00classes.R 2012-10-06 03:40:58 UTC (rev 490) +++ pkg/R/00classes.R 2013-02-22 03:53:51 UTC (rev 491) @@ -50,8 +50,10 @@ ), prototype = list( pointer = NULL, name = character(0), service = character(0) ) ) setClass( "FileDescriptor", representation( - pointer = "externalptr" # pointer to a google::protobuf::FileDescriptor c++ object -), prototype = list( pointer = NULL ) ) + pointer = "externalptr", # pointer to a google::protobuf::FileDescriptor c++ object + filename = "character", # filename + package = "character" # the package +), prototype = list( pointer = NULL, filename = character(0), package = character(0) ) ) setClass( "EnumValueDescriptor", representation( pointer = "externalptr", # pointer to a google::protobuf::EnumValueDescriptor c++ object @@ -125,7 +127,8 @@ # {{{ show setMethod( "show", c( "Message" ), function(object){ - show( sprintf( " message of type '%s' ", object at type ) ) + show( sprintf( "message of type '%s' with %d field%s set", object at type, + length(object), if (length(object) == 1) "" else "s" )) } ) setMethod( "show", c( "Descriptor" ), function(object){ show( sprintf( "descriptor for type '%s' ", object at type ) ) @@ -140,7 +143,8 @@ show( sprintf( "service descriptor <%s>", object at name ) ) } ) setMethod( "show", c( "FileDescriptor" ), function(object){ - show( sprintf( "file descriptor" ) ) + show( sprintf( "file descriptor for package %s (%s)", object at package, + object at filename) ) } ) setMethod( "show", c( "EnumValueDescriptor" ), function(object){ show( sprintf( "enum value descriptor" ) ) @@ -274,7 +278,8 @@ "toString" = function(...) toString(x, ...) , "asMessage" = function() asMessage(x), "as.list" = function() as.list(x), - + "name" = function() x at filename, + "package" = function() x at package, invisible(NULL) ) Modified: pkg/inst/unitTests/runit.addressbook.R =================================================================== --- pkg/inst/unitTests/runit.addressbook.R 2012-10-06 03:40:58 UTC (rev 490) +++ pkg/inst/unitTests/runit.addressbook.R 2013-02-22 03:53:51 UTC (rev 491) @@ -16,6 +16,10 @@ checkEquals(bytesize(book$person[[1]]), 60, msg="Bytes of first person message") } +test.fileDescriptor <- function() { + checkEquals(name(book$fileDescriptor()), "addressbook.proto") +} + test.personOne <- function() { checkEquals(book$person[[1]]$name, "Romain Francois", msg="First person name") checkEquals(book$person[[2]]$name, "Dirk Eddelbuettel", msg="First person name") Modified: pkg/man/FileDescriptor-class.Rd =================================================================== --- pkg/man/FileDescriptor-class.Rd 2012-10-06 03:40:58 UTC (rev 490) +++ pkg/man/FileDescriptor-class.Rd 2013-02-22 03:53:51 UTC (rev 491) @@ -16,6 +16,8 @@ \section{Slots}{ \describe{ \item{\code{pointer}:}{external pointer to a \code{google::protobuf::FileDescriptor} C++ object } + \item{\code{package}:}{the package name defined in the file, e.g. 'tutorial'.} + \item{\code{filename}:}{the filename of this FileDescriptor} } } \section{Methods}{ @@ -31,4 +33,18 @@ \references{ The \url{http://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.descriptor.html#FileDescriptor} } \author{ Romain Francois } \keyword{classes} +\seealso{ + \linkS4class{Descriptor} +} +\examples{ +\dontrun{ +# example proto file supplied with this package +desc <- P("tutorial.Person") +person <- new(desc) +person$fileDescriptor() +name(person$fileDescriptor()) +# [1] "addressbook.proto" +as.character(person$fileDescriptor()) +} +} \ No newline at end of file Modified: pkg/src/S4_classes.h =================================================================== --- pkg/src/S4_classes.h 2012-10-06 03:40:58 UTC (rev 490) +++ pkg/src/S4_classes.h 2013-02-22 03:53:51 UTC (rev 491) @@ -73,9 +73,15 @@ class S4_FileDescriptor : public Rcpp::S4 { public: S4_FileDescriptor( const GPB::FileDescriptor* d) : S4( "FileDescriptor" ){ - slot( "pointer" ) = Rcpp::XPtr( + slot( "pointer" ) = Rcpp::XPtr( const_cast(d), false) ; - // slot( "type" ) = d->full_name() ; + if (!d) { + slot ( "package" ) = Rcpp::StringVector(0); + slot ( "filename" ) = Rcpp::StringVector(0); + } else { + slot ( "package" ) = d->package(); + slot ( "filename" ) = d->name(); + } } S4_FileDescriptor( const S4_FileDescriptor& other) : S4(){