-
Notifications
You must be signed in to change notification settings - Fork 13
/
copy.R
49 lines (48 loc) · 1.84 KB
/
copy.R
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
if (!isGeneric("Copy")) {
setGeneric("Copy", function(object, ...) {
standardGeneric("Copy")
})
}
#' Copy for simList class objects
#'
#' Because a simList works with an environment to hold all objects,
#' all objects within that slot are pass-by-reference. That means
#' it is not possible to simply copy an object with an assignment operator:
#' the two objects will share the same objects. As one simList object changes
#' so will the other. when this is not the desired behaviour, use this function.
#' NOTE: use capital C, to limit confusion with \code{data.table::copy()}
#' See \code{\link[reproducible]{Copy}}.
#'
#' @inheritParams reproducible::Copy
#' @param objects Whether the objects contained within the simList environment
#' should be copied. Default \code{TRUE}, which may be slow.
#' @param queues Logical. Should the events queues (\code{events},
#' \code{current}, \code{completed}) be deep copied via
#' \code{data.table::copy}
#'
#' @author Eliot McIntire
#' @exportMethod Copy
#' @export
#' @importFrom reproducible Copy
#' @importMethodsFrom reproducible Copy
#' @include simList-class.R
#' @rdname Copy
#' @seealso \code{\link[reproducible]{Copy}}
setMethod("Copy",
signature(object = "simList"),
definition = function(object, objects, queues) {
if (missing(objects)) objects <- TRUE
if (missing(queues)) queues <- TRUE
sim_ <- object
if (queues) {
sim_@events <- object@events
sim_@current <- object@current
}
if (objects) {
sim_@.xData <- Copy(sim_@.xData, filebackedDir = cachePath(object))
} else {
sim_@.xData <- new.env(parent = asNamespace("SpaDES.core"))
}
sim_@.envir <- sim_@.xData
return(sim_)
})