-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolid.R
More file actions
138 lines (124 loc) · 4.35 KB
/
solid.R
File metadata and controls
138 lines (124 loc) · 4.35 KB
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
# Copyright (c) 2021, Janko Thyson, Iñaki Úcar
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# global repo for interfaces
if (is.null(getOption("solidr.repo")))
options(solidr.repo=new.env())
#' Register specialized type for an interface
#'
#' @param cls specialized type
#' @param as interface name
#' @param inst optional instance
#' @param singleton whether to keep a single instance
#' @param overwrite whether to force registration
#' @param repo repository environment
#'
registerType <- function(cls, as, inst = NULL, singleton = FALSE,
overwrite = !is.null(inst), repo = getOption("solidr.repo")) {
if (exists(as, envir=repo, inherits=FALSE) && !overwrite)
stop(sprintf("Already registered: %s as %s", get(as, repo)$classname, as))
if (is.character(cls))
cls <- get(cls, parent.frame())
if (!is.null(iface <- get0(as, parent.frame())))
.checkInterfaceMatch(cls, iface)
if (!singleton) {
assign(as, cls, repo)
} else {
if (is.null(inst)) {
attributes(cls)$singleton <- TRUE
assign(as, cls, repo)
} else {
attributes(inst)$singleton <- TRUE
assign(as, inst, repo)
}
}
}
# TODO: inherits = TRUE/FALSE? Some other restrictions necessary
# (e.g. `as.environment("package:xyz")`)?
.checkInterfaceMatch <- function(impl, ifac) {
for (method in names(ifac$public_methods)) {
if (method %in% c("clone", "initialize"))
next
# implements method
if(!method %in% names(impl$public_methods))
stop(sprintf("Method %s from interface %s is not implemented in %s",
method, ifac$classname, impl$classname))
# method has same signature
fmls_ifac <- formals(ifac$public_methods[[method]])
fmls_impl <- formals(impl$public_methods[[method]])
if(!identical(fmls_ifac, fmls_impl))
stop(sprintf("Formals in method %s from %s don't match the interface %s",
method, impl$classname, ifac$classname))
}
}
#' Resolve specialized type for an interface
#'
#' @param as interface name
#' @param repo repository environment
#'
resolveType <- function(as, repo = getOption("solidr.repo")) {
gen <- repo[[as]]
if (is.null(gen) || !inherits(gen, "R6ClassGenerator"))
return(gen)
gen <- .resolveTypeInner(gen, "public_fields", repo)
gen <- .resolveTypeInner(gen, "private_fields", repo)
inst <- gen$new()
# initialize singleton
if (isTRUE(attributes(gen)$singleton))
registerType(gen, as, inst = inst, singleton = TRUE, repo = repo)
inst
}
.resolveTypeInner <- function(gen, what, repo) {
x <- gen[[what]]
for (field in 1:length(x)) {
value <- x[[field]]
if (!inherits(value, "R6"))
next
if (!is.null(this <- resolveType(name = class(value)[1], repo = repo)))
gen[[what]][[field]] <- this
}
gen
}
#' Define interface
#'
#' @param as interface name
#' @param methods list of methods
#' @param inherit another base class to inherit from
#' @param envir assignment environment
#'
defineType <- function(as, methods, inherit=NULL, envir=parent.frame()) {
cls <- R6::R6Class(
as,
inherit = R6::R6Class(
"IR6",
inherit = inherit,
private = list(
model = NULL,
service = list()
)
),
public = lapply(methods, function(args) {
method <- function() stop("I'm an abstract interface method")
formals(method) <- args
method
})
)
assign(as, cls, envir)
}