File: isGenericS3.R

package info (click to toggle)
r-cran-r.methodss3 1.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 300 kB
  • sloc: sh: 12; makefile: 2
file content (181 lines) | stat: -rw-r--r-- 4,960 bytes parent folder | download | duplicates (2)
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
###########################################################################/**
# @RdocDefault isGenericS3
#
# @title "Checks if a function is a S3 generic function"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{fcn}{A @function or a @character string.}
#   \item{envir}{If argument \code{fcn} is a @character, this is the
#      @environment from which the search for the @function is done.}
#   \item{...}{Not used.}
# }
#
# \details{
#   A function is considered to be a generic S3/UseMethod function if
#   its name matches one of the known S3 generic functions, or if it
#   calls \code{UseMethod()}.
# }
#
# \value{
#  Returns @TRUE if a generic S3/UseMethod function, otherwise @FALSE.
# }
#
# @author
#
# @keyword programming
# @keyword methods
#*/###########################################################################
isGenericS3.default <- function(fcn, envir=parent.frame(), ...) {
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Local functions
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  knownInternalGenericS3 <- function(fcn, which=1:4, ...) {
    knownGenerics <- NULL

    # Get the name of all known S3 generic functions
    if (any(which == 1L)) {
      knownGenerics <- c(knownGenerics, names(.knownS3Generics))
    }

    if (any(which == 2L)) {
      knownGenerics <- c(knownGenerics, .S3PrimitiveGenerics)
    }

    # tools:::.get_internal_S3_generics() if available
    if (any(which == 3L)) {
      ns <- getNamespace("tools")
      if (exists(".get_internal_S3_generics", envir=ns, inherits=FALSE)) {
        names <- get(".get_internal_S3_generics", envir=ns, inherits=FALSE)()
        knownGenerics <- c(knownGenerics, names)
      }
    }

    # Manually added, cf. ?cbind
    if (any(which == 4L)) {
      names <- c("cbind", "rbind")
      knownGenerics <- c(knownGenerics, names)
    }

    # Is it one of the known S3 generic functions?
    knownGenerics <- unique(knownGenerics)

    knownGenerics
  } # knownInternalGenericS3()

  isNameInternalGenericS3 <- function(fcn, ...) {
    is.element(fcn, knownInternalGenericS3())
  } # isNameInternalGenericS3()

  isPrimitive <- function(fcn, ...) {
    switch(typeof(fcn), special=TRUE, builtin=TRUE, FALSE)
  } # isPrimitive()


  if (is.character(fcn)) {
    if (isNameInternalGenericS3(fcn)) return(TRUE)

    # Get the function
    fcn <- .findFunction(fcn, envir=envir, inherits=TRUE)$fcn

    # Does it even exist?
    if (is.null(fcn)) {
      return(FALSE)
    }
  }

  # Check with codetools::findGlobals(), otherwise scan the body
  res <- tryCatch({
    fcns <- codetools::findGlobals(fcn, merge=FALSE)$functions
    is.element("UseMethod", fcns)
  }, error = function(ex) {
    # Scan the body of the function
    body <- body(fcn)
    if (is.call(body))
      body <- deparse(body)
    body <- as.character(body)
    (length(grep("UseMethod[(]", body)) > 0L)
  })
  if (isTRUE(res)) return(TRUE)

  # Check primitive functions
  if (isPrimitive(fcn)) {
    # Scan the body of the function
    body <- deparse(fcn)
    call <- grep(".Primitive[(]", body, value=TRUE)
    call <- gsub(".Primitive[(]\"", "", call)
    call <- gsub("\"[)].*", "", call)
    if (is.element(call, knownInternalGenericS3(2L))) return(TRUE)
  }

  # Finally, compare to all known internal generics
  for (name in knownInternalGenericS3()) {
    if (exists(name, mode="function", inherits=TRUE)) {
      generic <- get(name, mode="function", inherits=TRUE)
      if (identical(fcn, generic)) return(TRUE)
    }
  }

  FALSE
}
S3class(isGenericS3.default) <- "default"
export(isGenericS3.default) <- FALSE

setGenericS3("isGenericS3")



###########################################################################/**
# @RdocDefault isGenericS4
#
# @title "Checks if a function is a S4 generic function"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{fcn}{A @function or a @character string.}
#   \item{...}{Not used.}
# }
#
# \details{
#   A function is considered to be a generic S4 function if its
#   body, that is the source code, contains the regular pattern
#   \code{"standardGeneric"}.
# }
#
# \value{
#  Returns @TRUE if a generic S4 function, otherwise @FALSE.
# }
#
# @author
#
# @keyword "programming"
# @keyword "methods"
# @keyword "internal"
#*/###########################################################################
isGenericS4.default <- function(fcn, envir=parent.frame(), ...) {
  if (is.character(fcn)) {
    if (!exists(fcn, mode="function", envir=envir, inherits=TRUE)) {
      return(FALSE)
    }
    fcn <- get(fcn, mode="function", envir=envir, inherits=TRUE)
  }
  body <- body(fcn)
  if (is.call(body))
    body <- deparse(body)
  body <- as.character(body)
  return(length(grep("standardGeneric", body)) > 0)
}
S3class(isGenericS4.default) <- "default"
export(isGenericS4.default) <- FALSE

setGenericS3("isGenericS4")