1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
# Copyright (C) 2013 - 2016 John Chambers, Dirk Eddelbuettel and Romain Francois
#
# This file is part of Rcpp.
#
# Rcpp 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.
#
# Rcpp 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 Rcpp. If not, see <http://www.gnu.org/licenses/>.
.stdHeader <- c( # #nocov start
"#include <Rcpp.h>",
"using namespace Rcpp ;"
)
.asString <- function(what) if(is.character(what)) what else deparse(what)
.strings <- function(expr) {
if(is.call(expr) && ! identical(expr[[1]], quote(`::`)))
lapply(as.list(expr)[-1], .strings)
else
.asString(expr)
}
.specifyItems <- function(what) {
what <- as.list(what)
wn <- allNames(what)
simple <- !nzchar(wn)
## todo: error checking here that unnamed elements are single strings
wn[simple] <- as.character(what[simple])
names(what) <- wn
what[simple] <- list(character())
what
}
.writeFieldFunction <- function(fldi, typei, CppClass, readOnly, ns, con){
rootName <- paste0("field_", fldi)
writeLines(sprintf(" %s %s_get(%s *obj) { return obj->%s; }\n",
typei, rootName, CppClass, fldi), con)
value <- "_get"
if(!readOnly) {
writeLines(sprintf(" void %s_set(%s *obj, %s value) { obj->%s = value; }\n",
rootName, CppClass, typei, fldi), con)
value <- c(value, "_set")
}
paste0(ns, "::field_", fldi, value)
}
.writeMethodFunction <- function(mdi, sigi, CppClass, ns, con) {
mName <- paste0("method_", mdi)
if(length(sigi) < 1)
stop(gettextf("The type signature for method %s for class %s was of length 0: Must at least include the return type",
mdi, CppClass))
rtnType <- sigi[[1]]
sigi <- sigi[-1]
if(length(sigi)) {
argNames <- paste0("a", seq_along(sigi))
args <- paste(" ,", paste(sigi, argNames, collapse = ", "))
}
else argNames <- args <- ""
writeLines(sprintf(" %s %s(%s *obj%s){ return obj->%s(%s); }\n",
rtnType, mName, CppClass, args, mdi, argNames), con)
paste0(ns, "::",mName)
}
exposeClass <- function(class, constructors, fields, methods,
file = paste0(CppClass, "Module.cpp"),
header = character(),
module = paste0("class_",class), CppClass = class,
readOnly = character(), rename = character(),
Rfile = TRUE) {
## some argument checks
## TODO: checks on constructors, fields, methods
if(length(readOnly)) {
readOnly <- as.character(readOnly)
if(!all(nzchar(readOnly)))
stop("argument readOnly should be a vector of non-empty strings")
}
newnames <- allNames(rename)
if(length(rename)) {
if(!all(sapply(rename, function(x) is.character(x) && length(x) == 1 && nzchar(x))))
stop("argument rename should be a vector of single, non-empty strings")
if(!all(nzchar(newnames)))
stop("all the elements of argument rename should be non-empty strings")
}
if(is.character(file)) {
## are we in a package directory? Writable, searchable src subdirectory:
if(file.access("src",3)==0 && (basename(file) == file))
cfile <- file.path("src", file)
else
cfile <- file
con <- file(cfile, "w")
on.exit({message(sprintf("Wrote C++ file \"%s\"", cfile)); close(con)})
}
else
con <- file
## and for the R code:
if(identical(Rfile, FALSE)) {}
else {
if(identical(Rfile, TRUE))
Rfile <- sprintf("%sClass.R",class)
if(is.character(Rfile)) {
if(file.access("R",3)==0 && (basename(file) == file)) # in a package directory
Rfile <- file.path("R", Rfile)
Rcon <- file(Rfile, "w")
msg <- sprintf("Wrote R file \"%s\"",Rfile)
on.exit({message(msg); close(Rcon)}, add = TRUE)
}
else
Rcon <- Rfile
Rfile <- TRUE
}
mfile <- tempfile()
mcon <- file(mfile, "w")
writeLines(.stdHeader, con)
if(length(header))
writeLines(header, con)
writeLines(c("", sprintf("RCPP_MODULE(%s) {\n",module), ""), mcon)
writeLines(sprintf(" class_<%s>(\"%s\")\n", CppClass, class), mcon)
## the constructors argument defines a list of vectors of types
for( cons in constructors) {
if(length(cons) > 1 ||
(length(cons) == 1 && nzchar(cons) && !identical(cons, "void")))
cons <- paste0("<", paste(cons, collapse = ","),">")
else
cons = ""
writeLines(paste0(" .constructor",cons,"()"),mcon)
}
writeLines("", mcon)
flds <- .specifyItems(fields)
nm <- fnm <- names(flds)
rdOnly <- nm %in% readOnly
macros <- ifelse(rdOnly, ".field_readonly", ".field")
test <- nm %in% rename
if(any(test))
nm[test] <- newnames[match(nm[test], rename)]
ns <- NULL
for(i in seq_along(nm)) {
typei <- flds[[i]]
fldi <- fnm[i]
nmi <- nm[[i]]
macroi <- macros[[i]]
if(!length(typei) || identical(typei, "")) ## direct field
writeLines(sprintf(" %s(\"%s\", &%s::%s)",
macroi, nmi, CppClass, fldi), mcon)
else { # create a free function, e.g. for an inherited field
if(is.null(ns)) { # enclose in a namespace
ns <- paste("module",class,"NS", sep = "_")
writeLines(sprintf("\nnamespace %s {\n", ns),
con)
}
fldFuns <- .writeFieldFunction(fldi, typei, CppClass, rdOnly[[i]], ns, con)
if(rdOnly[[i]])
## NOTE: string 3rd arg. required by problem w. module parsing 10/3/13
writeLines(sprintf(" .property(\"%s\", &%s, \"read-only field\")",
nmi, fldFuns[[1]]), mcon)
else
writeLines(sprintf(" .property(\"%s\", &%s, &%s)",
nmi, fldFuns[[1]], fldFuns[[2]]), mcon)
}
}
writeLines("", mcon)
sigs <- .specifyItems(methods)
nm <- mds <- names(sigs)
test <- nm %in% rename
if(any(test))
nm[test] <- newnames[match(nm[test], rename)]
for(i in seq_along(nm)) {
sigi <- sigs[[i]]
nmi <- nm[[i]]
mdi <- mds[[i]]
if(!length(sigi) || identical(sigi, "")) # direct method
writeLines(sprintf(" .method(\"%s\", &%s::%s)",
nmi, CppClass, mdi), mcon)
else { # create a free function, e.g. for an inherited method
if(is.null(ns)) { # enclose in a namespace
ns <- paste("module",class,"NS", sep = "_")
writeLines(sprintf("\nnamespace %s {\n", ns),
con)
}
mFun <- .writeMethodFunction(mdi, sigi, CppClass, ns, con)
writeLines(sprintf(" .method(\"%s\", &%s)",
nmi, mFun), mcon)
}
}
writeLines(" ;\n}", mcon)
close(mcon)
if(!is.null(ns))
writeLines(sprintf("} // %s", ns), con) # close namespace
writeLines(readLines(mfile), con)
if(Rfile) {
if(missing(CppClass))
CppString <- ""
else
CppString <- paste0(", \"",CppClass, "\"")
if(missing(module))
ModString <- ""
else
ModString <- paste0(", module = \"", module, "\"")
writeLines(sprintf("%s <- setRcppClass(\"%s\"%s%s)",
class, class, CppString,ModString), Rcon)
}
} # #nocov end
|