forked from RConsortium/S7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzzz.R
145 lines (130 loc) · 3.24 KB
/
zzz.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
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
#' Base S7 class
#'
#' The base class from which all S7 classes eventually inherit from.
#'
#' @keywords internal
#' @export
#' @return The base S7 object.
#' @examples
#'
#' S7_object
S7_object <- new_class(
name = "S7_object",
package = NULL,
parent = NULL,
constructor = function() {
.Call(S7_object_)
},
validator = function(self) {
if (!is_S7_type(self)) {
"Underlying data is corrupt"
}
}
)
methods::setOldClass("S7_object")
.S7_type <- NULL
# Defined onLoad because it depends on R version
on_load_define_S7_type <- function() {
.S7_type <<- typeof(.Call(S7_object_))
}
is_S7_type <- function(x) {
typeof(x) == .S7_type
}
#' @export
`$.S7_object` <- function(x, name) {
if (typeof(x) %in% c("list", "environment")) {
NextMethod()
} else {
msg <- sprintf(
"Can't get S7 properties with `$`. Did you mean `%s@%s`?",
deparse1(substitute(x)),
name
)
stop(msg, call. = FALSE)
}
}
#' @export
`$<-.S7_object` <- function(x, name, value) {
if (typeof(x) %in% c("list", "environment")) {
NextMethod()
} else {
msg <- sprintf(
"Can't set S7 properties with `$`. Did you mean `...@%s <- %s`?",
name,
deparse1(substitute(value))
)
stop(msg, call. = FALSE)
}
}
#' @export
`[.S7_object` <- function(x, ..., drop = TRUE) {
check_subsettable(x)
NextMethod()
}
#' @export
`[<-.S7_object` <- function(x, ..., value) {
check_subsettable(x)
NextMethod()
}
#' @export
`[[.S7_object` <- function(x, ...) {
check_subsettable(x, allow_env = TRUE)
NextMethod()
}
#' @export
`[[<-.S7_object` <- function(x, ..., value) {
check_subsettable(x, allow_env = TRUE)
NextMethod()
}
check_subsettable <- function(x, allow_env = FALSE) {
allowed_types <- c("list", "language", "pairlist", if (allow_env) "environment")
if (!typeof(x) %in% allowed_types) {
stop("S7 objects are not subsettable.")
}
invisible(TRUE)
}
S7_generic <- NULL
on_load_define_S7_generic <- function() {
# we do this in .onLoad() because dynlib `prop_` symbol
# is not available at pkg build time, and new_class()
# errors if `@` is not usable.
S7_generic <<- new_class(
name = "S7_generic",
package = NULL,
properties = list(
name = class_character,
methods = class_environment,
dispatch_args = class_character
),
parent = class_function
)
}
methods::setOldClass(c("S7_generic", "function", "S7_object"))
is_S7_generic <- function(x) inherits(x, "S7_generic")
S7_method <- NULL
on_load_define_S7_method <- function() {
S7_method <<- new_class(
"S7_method",
package = NULL,
parent = class_function,
properties = list(generic = S7_generic, signature = class_list)
)
}
methods::setOldClass(c("S7_method", "function", "S7_object"))
# hooks -------------------------------------------------------------------
.onAttach <- function(libname, pkgname) {
env <- as.environment(paste0("package:", pkgname))
if (getRversion() < "4.3.0") {
env[[".conflicts.OK"]] <- TRUE
}
}
.onLoad <- function(...) {
activate_backward_compatiblility()
on_load_define_S7_generic()
on_load_define_S7_method()
on_load_make_convert_generic()
on_load_define_ops()
on_load_define_or_methods()
on_load_define_S7_type()
on_load_define_union_classes()
}