Skip to content

Commit d0077bf

Browse files
added sizeof R function
1 parent 5a83842 commit d0077bf

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

ChangeLog

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
* include/Rcpp/traits/r_sexptype_traits.h : unsigned int wrapped as REALSXP
44
long standing feature request from Murray.
5-
* R/Attributes.R : Added the helper demangle function
6-
* man/demangle.Rd : Documentation for demangle
5+
* R/Attributes.R : Added the helper demangle and sizeof functions
6+
* man/demangle.Rd : Documentation for demangle and sizeof
77

88
2013-09-18 JJ Allaire <jj@rstudio.org>
99

NAMESPACE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ export(Module,
2525
compileAttributes,
2626
registerPlugin,
2727
RcppLdFlags,
28-
demangle
28+
demangle, sizeof
2929
)
30-
30+
S3method( print, bytes )
3131
exportClass(RcppClass)
3232

3333

R/Attributes.R

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,30 @@ cppFunction <- function(code,
252252
}
253253
}
254254

255-
# demangling a type
256-
demangle <- function( type = "int", ... ){
257-
code <- sprintf( '
258-
String demangle_this_type(){
259-
typedef %s type ;
260-
return DEMANGLE(type) ;
261-
}', type )
262-
dots <- list( code, ... )
263-
dots[["env"]] <- environment()
264-
do.call( cppFunction, dots )
265-
demangle_this_type()
255+
.type_manipulate <- function( what = "DEMANGLE", class = NULL ) {
256+
function( type = "int", ... ){
257+
code <- sprintf( '
258+
SEXP manipulate_this_type(){
259+
typedef %s type ;
260+
return wrap( %s(type) ) ;
261+
}', type, what )
262+
dots <- list( code, ... )
263+
dots[["env"]] <- environment()
264+
manipulate_this_type <- do.call( cppFunction, dots )
265+
res <- manipulate_this_type()
266+
if( ! is.null(class) ){
267+
class(res) <- class
268+
}
269+
res
270+
}
271+
}
272+
273+
demangle <- .type_manipulate( "DEMANGLE" )
274+
sizeof <- .type_manipulate( "sizeof", "bytes" )
275+
276+
print.bytes <- function( x, ...){
277+
writeLines( sprintf( "%d bytes (%d bits)", x, 8 * x ) )
278+
invisible( x )
266279
}
267280

268281
# Evaluate a simple c++ expression

inst/NEWS.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
\itemize{
77
\item Changes in R code:
88
\itemize{
9-
\item New R function \code{demangle} that calls the \code{DEMANGLE} macro.
9+
\item New R function \code{demangle} that calls the \code{DEMANGLE} macro.
10+
\item New R function \code{sizeof} to query the byte size of a type. This
11+
returns an object of S3 class \code{bytes} that has a \code{print} method
12+
showing bytes and bits.
1013
}
1114
\item Changes in Rcpp API:
1215
\itemize{

man/demangle.Rd

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
\name{demangle}
22
\alias{demangle}
3+
\alias{sizeof}
4+
\alias{print.bytes}
35
\title{
4-
demanging c++ types
6+
c++ type information
57
}
68
\description{
7-
This uses the compiler functionality (if available) to demangle type. If the compiler
8-
cannot demangle, then it will return its argument as is
9+
\code{demangle} gives the demangled type, \code{sizeof} its size (in bytes).
910
}
1011
\usage{
1112
demangle(type = "int", ...)
13+
sizeof(type = "int", ...)
1214
}
1315
\arguments{
1416
\item{type}{The type we want to demangle}
@@ -18,10 +20,15 @@ demanging c++ types
1820
The following function is compiled and invoked:
1921

2022
\preformatted{%
21-
String demangle_this_type(){
23+
SEXP demangle_this_type(){
2224
typedef %s type ;
23-
return DEMANGLE(type) ;
24-
}
25+
return wrap( DEMANGLE(type) ) ;
26+
}
27+
28+
SEXP sizeof_this_type(){
29+
typedef %s type ;
30+
return wrap( sizeof(type) ) ;
31+
}
2532
}
2633

2734
\code{DEMANGLE} is a macro in \samp{Rcpp} that does the work.
@@ -50,6 +57,10 @@ demanging c++ types
5057

5158
demangle( "NumericVector" )
5259
demangle( "std::map<std::string,double>" )
60+
61+
sizeof( "long" )
62+
sizeof( "long long" )
63+
5364
}
5465
}
5566
\keyword{programming}

0 commit comments

Comments
 (0)