Repository navigation
Expand file tree
/
Copy pathdot-net-wrapper.R
More file actions
92 lines (91 loc) · 2.58 KB
/
Copy pathdot-net-wrapper.R
File metadata and controls
92 lines (91 loc) · 2.58 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
#' @title Wrapper class for `.NET` objects
#' @docType class
#'
#' @examples
#'
#' sim <- loadSimulation(system.file("extdata", "simple.pkml", package = "ospsuite"))
#'
#' # looking at a reference to `.NET` simulation object
#' sim$pointer
#'
#' # create a new instance of `DotNetWrapper` class using this reference
#' DotNetWrapper$new(sim)
#'
#' @export
DotNetWrapper <- R6::R6Class(
"DotNetWrapper",
inherit = NetObject,
cloneable = FALSE,
private = list(
.wrapExtensionMethod = function(typename, methodName, propertyName, value) {
if (missing(value)) {
rSharp::callStatic(typename, methodName, self)
} else {
private$.throwPropertyIsReadonly(propertyName)
}
},
.wrapExtensionMethodCached = function(
typename,
methodName,
propertyName,
cachedValue,
value
) {
if (missing(value)) {
if (is.null(cachedValue)) {
return(rSharp::callStatic(typename, methodName, self))
}
return(cachedValue)
} else {
private$.throwPropertyIsReadonly(propertyName)
}
},
.readOnlyProperty = function(propertyName, value, returnValue) {
if (missing(value)) {
returnValue
} else {
private$.throwPropertyIsReadonly(propertyName)
}
},
# Special method needed because Index are 0-based in `.NET` but 1-based in R
.wrapIndexProperty = function(propertyName, value) {
if (missing(value)) {
self$get(propertyName) + 1
} else {
self$set(propertyName, value - 1, asInteger = TRUE)
}
},
# There is no concept of a scalar in R. A single value is still a vector of
# length 1, which can trip up `.NET`, which *does* distinguish between a
# scalar and an array.
#
# This method makes sure that the correct `.NET` method is called depending
# on whether R vector (for `value`) is of length 1 or higher.
.wrapVectorProperty = function(
propertyNameSingular,
propertyNamePlural,
value,
returnPropertyName
) {
if (missing(value)) {
self$get(returnPropertyName)
} else {
if (length(value) == 1L) {
self$set(propertyNameSingular, value)
} else {
self$set(propertyNamePlural, value)
}
}
}
),
public = list(
#' @description
#' Initialize a new instance of the class
#' @param netObject An `rSharp::NetObject` object.
#' @return A new `Molecule` object.
initialize = function(netObject) {
validateIsOfType(netObject, "NetObject")
super$initialize(netObject$pointer)
}
)
)