[Rcpp-commits] r4520 - in pkg/Rcpp: . R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Sep 19 11:08:54 CEST 2013


Author: romain
Date: 2013-09-19 11:08:54 +0200 (Thu, 19 Sep 2013)
New Revision: 4520

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/NAMESPACE
   pkg/Rcpp/R/Attributes.R
   pkg/Rcpp/inst/NEWS.Rd
   pkg/Rcpp/man/demangle.Rd
Log:
added sizeof R function

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-09-19 08:01:51 UTC (rev 4519)
+++ pkg/Rcpp/ChangeLog	2013-09-19 09:08:54 UTC (rev 4520)
@@ -2,8 +2,8 @@
 
         * include/Rcpp/traits/r_sexptype_traits.h : unsigned int wrapped as REALSXP
         long standing feature request from Murray. 
-        * R/Attributes.R : Added the helper demangle function
-        * man/demangle.Rd : Documentation for demangle
+        * R/Attributes.R : Added the helper demangle and sizeof functions
+        * man/demangle.Rd : Documentation for demangle and sizeof
 
 2013-09-18  JJ Allaire  <jj at rstudio.org>
 

Modified: pkg/Rcpp/NAMESPACE
===================================================================
--- pkg/Rcpp/NAMESPACE	2013-09-19 08:01:51 UTC (rev 4519)
+++ pkg/Rcpp/NAMESPACE	2013-09-19 09:08:54 UTC (rev 4520)
@@ -25,9 +25,9 @@
        compileAttributes,
        registerPlugin,
        RcppLdFlags, 
-       demangle
+       demangle, sizeof
        )
-
+S3method( print, bytes )
 exportClass(RcppClass)
 
 

Modified: pkg/Rcpp/R/Attributes.R
===================================================================
--- pkg/Rcpp/R/Attributes.R	2013-09-19 08:01:51 UTC (rev 4519)
+++ pkg/Rcpp/R/Attributes.R	2013-09-19 09:08:54 UTC (rev 4520)
@@ -252,19 +252,32 @@
     }
 }
 
-# demangling a type
-demangle <- function( type = "int", ... ){
-    code <- sprintf( '
-    String demangle_this_type(){
-        typedef %s type ;
-        return DEMANGLE(type) ;
-    }', type )
-    dots <- list( code, ... )
-    dots[["env"]] <- environment()
-    do.call( cppFunction, dots )
-    demangle_this_type()
+.type_manipulate <- function( what = "DEMANGLE", class = NULL ) {
+    function( type = "int", ... ){
+        code <- sprintf( '
+        SEXP manipulate_this_type(){
+            typedef %s type ;
+            return wrap( %s(type) ) ;
+        }', type, what )
+        dots <- list( code, ... )
+        dots[["env"]] <- environment()
+        manipulate_this_type <- do.call( cppFunction, dots )
+        res <- manipulate_this_type()
+        if( ! is.null(class) ){
+            class(res) <- class    
+        }
+        res
+    }
 }
 
+demangle <- .type_manipulate( "DEMANGLE" )
+sizeof   <- .type_manipulate( "sizeof", "bytes" )
+
+print.bytes <- function( x, ...){
+    writeLines( sprintf( "%d bytes (%d bits)", x, 8 * x ) )
+    invisible( x )    
+}
+
 # Evaluate a simple c++ expression
 evalCpp <- function(code,
                     depends = character(),

Modified: pkg/Rcpp/inst/NEWS.Rd
===================================================================
--- pkg/Rcpp/inst/NEWS.Rd	2013-09-19 08:01:51 UTC (rev 4519)
+++ pkg/Rcpp/inst/NEWS.Rd	2013-09-19 09:08:54 UTC (rev 4520)
@@ -6,7 +6,10 @@
   \itemize{
     \item Changes in R code:
     \itemize{
-      \item New R function \code{demangle} that calls the \code{DEMANGLE} macro.     
+      \item New R function \code{demangle} that calls the \code{DEMANGLE} macro.  
+      \item New R function \code{sizeof} to query the byte size of a type. This
+      returns an object of S3 class \code{bytes} that has a \code{print} method
+      showing bytes and bits. 
     }
     \item Changes in Rcpp API:
     \itemize{

Modified: pkg/Rcpp/man/demangle.Rd
===================================================================
--- pkg/Rcpp/man/demangle.Rd	2013-09-19 08:01:51 UTC (rev 4519)
+++ pkg/Rcpp/man/demangle.Rd	2013-09-19 09:08:54 UTC (rev 4520)
@@ -1,14 +1,16 @@
 \name{demangle}
 \alias{demangle}
+\alias{sizeof}
+\alias{print.bytes}
 \title{
-demanging c++ types 
+c++ type information 
 }
 \description{
-    This uses the compiler functionality (if available) to demangle type. If the compiler
-    cannot demangle, then it will return its argument as is
+    \code{demangle} gives the demangled type, \code{sizeof} its size (in bytes). 
 }
 \usage{
     demangle(type = "int", ...)
+    sizeof(type = "int", ...)
 }
 \arguments{
     \item{type}{The type we want to demangle}
@@ -18,10 +20,15 @@
     The following function is compiled and invoked: 
     
     \preformatted{%
-        String demangle_this_type(){
+        SEXP demangle_this_type(){
             typedef %s type ;
-            return DEMANGLE(type) ;
-        }         
+            return wrap( DEMANGLE(type) ) ;
+        }
+        
+        SEXP sizeof_this_type(){
+            typedef %s type ;
+            return wrap( sizeof(type) ) ;
+        }
     }    
     
     \code{DEMANGLE} is a macro in \samp{Rcpp} that does the work. 
@@ -50,6 +57,10 @@
 
     demangle( "NumericVector" )
     demangle( "std::map<std::string,double>" )
+    
+    sizeof( "long" )
+    sizeof( "long long" )
+    
 }
 }
 \keyword{programming}



More information about the Rcpp-commits mailing list