The R Project SVN R

Rev

Rev 83769 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/methods/man/RClassUtils.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
75638 maechler 3
% Copyright 1995-2018 R Core Team
4
% Copyright 2008-2018 The R Foundation
42333 ripley 5
% Distributed under GPL 2 or later
6
 
20092 jmc 7
\name{RClassUtils}
29714 ripley 8
%% NOTE: This is {internal} for a good reason
44586 maechler 9
 
29714 ripley 10
%% FIXME the following are undocumented utilities (Sep 23, 2002)
21345 jmc 11
\alias{completeSubclasses}
12
\alias{newClassRepresentation}
13
\alias{print.classRepresentation}
14
\alias{setExtendsMetaData}
15
\alias{setSubclassMetaData}
16
\alias{subclassesMetaName}
17
\alias{extendsMetaName}
18
\alias{classPrototypeDef-class}
19
%%
57849 maechler 20
\alias{.classEnv}
56335 jmc 21
\alias{classLabel}
15357 jmc 22
\alias{testVirtual}
23
\alias{makePrototypeFromClassDef}
24
\alias{newEmptyObject}
25
\alias{completeClassDefinition}
26
\alias{getAllSuperClasses}
16764 jmc 27
\alias{superClassDepth}
15357 jmc 28
\alias{isVirtualClass}
29
\alias{assignClassDef}
30
\alias{newBasic}
31
\alias{makeExtends}
15431 jmc 32
\alias{reconcilePropertiesAndPrototype}
15357 jmc 33
\alias{tryNew}
15431 jmc 34
\alias{empty.dump}
15357 jmc 35
\alias{showClass}
36
\alias{showExtends}
20330 jmc 37
\alias{possibleExtends}
15357 jmc 38
\alias{completeExtends}
15431 jmc 39
\alias{classMetaName}
20330 jmc 40
\alias{methodsPackageMetaName}
20092 jmc 41
\alias{metaNameUndo}
15431 jmc 42
\alias{requireMethods}
61820 jmc 43
\alias{checkAtAssignment}
17000 jmc 44
\alias{checkSlotAssignment}
17265 jmc 45
\alias{defaultPrototype}
17454 jmc 46
\alias{isClassDef}
18042 jmc 47
\alias{validSlotNames}
20092 jmc 48
\alias{getDataPart}
49
\alias{setDataPart}
15822 jmc 50
%% not explicitly documented here
51
\alias{.BasicClasses}
52
\alias{.BasicVectorClasses}
53
\alias{.InitBasicClasses}
54
\alias{.InitMethodsListClass}
16471 jmc 55
\alias{.setCoerceGeneric}
47803 jmc 56
\alias{conditionalExtension-class}
15822 jmc 57
 
15357 jmc 58
\title{Utilities for Managing Class Definitions}
18994 hornik 59
\description{
60
  These are various functions to support the definition and use of
61
  formal classes.  Most of them are rarely suitable to be called
62
  directly.
29714 ripley 63
 
64
  Others are somewhat experimental and/or partially implemented only. Do
65
  refer to \code{\link{setClass}} for normal code development.
18994 hornik 66
}
67
 
15357 jmc 68
\usage{
56335 jmc 69
classLabel(Class)
57849 maechler 70
.classEnv(Class, default = .requirePackage("methods"), mustFind = TRUE)
56335 jmc 71
 
31085 jmc 72
testVirtual(properties, extends, prototype, where)
15357 jmc 73
 
30842 jmc 74
makePrototypeFromClassDef(slots, ClassDef, extends, where)
15357 jmc 75
 
76
newEmptyObject()
77
 
25351 jmc 78
completeClassDefinition(Class, ClassDef, where, doExtends)
15357 jmc 79
 
25351 jmc 80
getAllSuperClasses(ClassDef, simpleOnly = TRUE)
15357 jmc 81
 
25351 jmc 82
superClassDepth(ClassDef, soFar, simpleOnly = TRUE)
16764 jmc 83
 
26093 jmc 84
isVirtualClass(Class, where)
15357 jmc 85
 
20092 jmc 86
newBasic(Class, ...)
15357 jmc 87
 
77801 lawrence 88
makeExtends(Class, coerce, test, replace, by, package, slots,
75638 maechler 89
            classDef1, classDef2)
15357 jmc 90
 
26530 maechler 91
reconcilePropertiesAndPrototype(name, properties, prototype,
75638 maechler 92
                                superClasses, where)
30922 jmc 93
tryNew(Class, where)
15357 jmc 94
 
15431 jmc 95
empty.dump()
96
 
26530 maechler 97
showClass(Class, complete=TRUE, propertiesAreCalled="Slots")
15357 jmc 98
 
17000 jmc 99
showExtends(ext, printTo = stdout())
15357 jmc 100
 
75638 maechler 101
possibleExtends(class1, class2,
102
                ClassDef1 = getClassDef(class1),
103
                ClassDef2 = getClassDef(class2, where = .classEnv(ClassDef1)))
15357 jmc 104
 
26093 jmc 105
completeExtends(ClassDef, class2, extensionDef, where)
15357 jmc 106
 
15431 jmc 107
classMetaName(name)
15357 jmc 108
 
44805 jmc 109
methodsPackageMetaName(prefix, name, package = "")
15431 jmc 110
 
45630 maechler 111
metaNameUndo(strings, prefix, searchForm = FALSE)
20092 jmc 112
 
27836 jmc 113
requireMethods(functions, signature, message, where)
15431 jmc 114
 
61820 jmc 115
checkAtAssignment(cl, name, valueClass)
17000 jmc 116
checkSlotAssignment(obj, name, value)
117
 
17265 jmc 118
defaultPrototype()
119
 
17454 jmc 120
isClassDef(object)
121
 
18042 jmc 122
validSlotNames(names)
123
 
75638 maechler 124
getDataPart(object, NULL.for.none = FALSE)
46531 jmc 125
setDataPart(object, value, check = TRUE)
83769 hornik 126
 
127
assignClassDef(Class, def, where = .GlobalEnv, force = FALSE,
128
               doSubclasses = is(def, "ClassUnionRepresentation"))
129
 
130
completeSubclasses(classDef, class2, extensionDef, where,
131
                   classDef2 = getClassDef(class2, where))
132
 
133
newClassRepresentation(\dots)
15357 jmc 134
}
15432 jmc 135
 
17211 jmc 136
\section{Summary of Functions}{
18994 hornik 137
  \describe{
26530 maechler 138
    \item{\code{testVirtual}:}{
18994 hornik 139
      Test for a Virtual Class.
140
      Figures out, as well as possible, whether the class with these
141
      properties, extension, and prototype is a virtual class.
29714 ripley 142
      Can be forced to be virtual by extending \code{"VIRTUAL"}.
26530 maechler 143
 
18994 hornik 144
      Otherwise, a class is virtual only if it has no slots, extends no
145
      non-virtual classes, and has a \code{NULL} Prototype.
146
    }
26530 maechler 147
 
148
    \item{\code{makePrototypeFromClassDef}:}{
18994 hornik 149
      Makes the prototype implied by the class definition.
15357 jmc 150
 
18994 hornik 151
      The following three rules are applied in this order.
29714 ripley 152
      \enumerate{
30842 jmc 153
        \item If the class has slots, then the prototype for each slot
154
          is used by default, but a corresponding element in the
155
          explicitly supplied prototype in \code{ClassDef}, if there is
156
          one, is used instead (but it must be coercible to the class of
157
          the slot). This includes the data part (\code{".Data"} slot) if
158
          there is one.
15357 jmc 159
 
30842 jmc 160
        \item If there are no slots but a non-null prototype was
161
          specified, this is returned.
15431 jmc 162
 
30842 jmc 163
        \item If there is a non-virtual superclass (a class in the
164
          extends list), then its prototype is used.  The data part is
165
          extracted if needed (it is allowed to have two superclasses
166
          with a data part; the first is used and a warning issued on
167
          any others).
168
        }
169
        If all three of the above fail, the prototype is \code{NULL}.
18994 hornik 170
    }
15431 jmc 171
 
18994 hornik 172
    \item{\code{newEmptyObject}:}{
173
      Utility function to create an empty object into which slots can be
174
      set.
26530 maechler 175
 
19029 hornik 176
      Currently just creates an empty list with class \code{"NULL"}.
15357 jmc 177
 
18994 hornik 178
      Later version should create a special object reference that marks
179
      an object currently with no slots and no data.
180
    }
15357 jmc 181
 
26530 maechler 182
    \item{\code{completeClassDefinition}:}{
25351 jmc 183
      Completes the definition of \code{Class}, relative to the class
184
      definitions visible from environment \code{where}.  If
185
      \code{doExtends} is \code{TRUE}, complete the super- and
186
      sub-class information.
15357 jmc 187
 
25351 jmc 188
      This function is called when a class is defined or re-defined.
18994 hornik 189
    }
17454 jmc 190
 
26530 maechler 191
    \item{\code{getFromClassDef}:}{
18994 hornik 192
      Extracts one of the intrinsically defined class definition
19029 hornik 193
      properties (".Properties", etc.)  Strictly a utility function.
18994 hornik 194
    }
29714 ripley 195
%%
18994 hornik 196
    \item{\code{getSlots}:}{
197
      Returns a named character vector.  The names are the names of the
27836 jmc 198
      slots, the values are the classes of the corresponding slots.
199
      The argument \code{x} can either be the name of
200
      a class or the class definition object.
18994 hornik 201
    }
15357 jmc 202
 
26530 maechler 203
    \item{\code{getAllSuperClasses}, \code{superClassDepth}:}{
18994 hornik 204
      Get the names of all the classes that this class definition
205
      extends.
15357 jmc 206
 
18994 hornik 207
      \code{getAllSuperClasses} is a utility function used to complete a
208
      class definition.  It returns all the superclasses reachable from
209
      this class, in breadth-first order (which is the order used for
210
      matching methods); that is, the first direct superclass followed
211
      by all its superclasses, then the next, etc.  (The order is
212
      relevant only in the case that some of the superclasses have
213
      multiple inheritance.)
15357 jmc 214
 
18994 hornik 215
      \code{superClassDepth}, which is called from
216
      \code{getAllSuperClasses}, returns the same information, but as a
217
      list with components \code{label} and \code{depth}, the latter for
218
      the number of generations back each class is in the inheritance
219
      tree.  The argument \code{soFar} is used to avoid loops in the
220
      network of class relationships.
221
    }
15357 jmc 222
 
26530 maechler 223
    \item{\code{isVirtualClass}:}{
224
      Is the named class a virtual class?
225
 
18994 hornik 226
      A class is virtual if explicitly declared to be, and also if the
227
      class is not formally defined.
228
    }
29714 ripley 229
%%
26530 maechler 230
    \item{\code{assignClassDef}:}{
18994 hornik 231
      assign the definition of the class to the specially named object
232
    }
26530 maechler 233
 
18994 hornik 234
    \item{\code{newBasic}:}{
235
      the implementation of the function \code{new} for basic classes
26530 maechler 236
      that don't have a formal definition.
237
 
18994 hornik 238
      Any of these could have a formal definition, except for
19029 hornik 239
      \code{Class="NULL"} (disallowed because \code{NULL} can't have
240
      attributes).  For all cases except \code{"NULL"}, the class of the
241
      result will be set to \code{Class}.
15357 jmc 242
 
18994 hornik 243
      See \code{new} for the interpretation of the arguments.
244
    }
15431 jmc 245
 
26530 maechler 246
    \item{\code{makeExtends}:}{
85925 hornik 247
      Construct an \code{"SClassExtension"} object representing the relationship
77801 lawrence 248
      from \code{Class} to the class defined by \code{classDef2}. 
18994 hornik 249
    }
26530 maechler 250
 
251
    \item{\code{reconcilePropertiesAndPrototype}:}{
18994 hornik 252
      makes a list or a structure look like a prototype for the given
253
      class.
26530 maechler 254
 
18994 hornik 255
      Specifically, returns a structure with attributes corresponding to
256
      the slot names in properties and values taken from prototype if
257
      they exist there, from \code{new(classi)} for the class,
258
      \code{classi} of the slot if that succeeds, and \code{NULL}
259
      otherwise.
26530 maechler 260
 
18994 hornik 261
      The prototype may imply slots not in the properties list, since
262
      properties does not include inherited slots (these are left
263
      unresolved until the class is used in a session).
264
    }
26530 maechler 265
 
266
    \item{\code{tryNew}:}{
18994 hornik 267
      Tries to generate a new element from this class, but if the attempt
268
      fails (as, e.g., when the class is undefined or virtual) just
26530 maechler 269
      returns \code{NULL}.
270
 
18994 hornik 271
      This is inefficient and also not a good idea when actually
272
      generating objects, but is useful in the initial definition of
273
      classes.
274
    }
26530 maechler 275
 
276
    \item{\code{showClass}:}{
277
      Print the information about a class definition.
278
 
19029 hornik 279
      If \code{complete} is \code{TRUE}, include the indirect
280
      information about extensions.
44586 maechler 281
 
282
      It is the utility called from \code{\link{show}(\link{getClass}(.))},
283
      and the user should typically use \code{getClass(.)} for looking
284
      at class definitions.
18994 hornik 285
    }
26530 maechler 286
 
287
    \item{\code{showExtends}:}{
35206 maechler 288
      Print the elements of the list of extensions; for \code{printTo = FALSE},
289
      returns a list with components \code{what} and \code{how}; this is
290
      used e.g., by \code{\link{promptClass}()}.
18994 hornik 291
    }
26530 maechler 292
 
293
    \item{\code{possibleExtends}:}{
18994 hornik 294
      Find the information that says whether class1 extends class2,
295
      directly or indirectly.
26530 maechler 296
 
20330 jmc 297
      This can be either a logical value or an object of class
44751 maechler 298
      \code{\linkS4class{SClassExtension}} containing various
18994 hornik 299
      functions to test and/or coerce the relationship.
300
    }
26530 maechler 301
 
57849 maechler 302
    \item{\code{classLabel}: }{
56335 jmc 303
        Returns an informative character string identifying the class
304
        and, if appropriate, the package from which the class came.
305
    }
306
 
68129 maechler 307
    \item{\code{.classEnv}: }{% is called from C level  R_check_class_etc()
57849 maechler 308
        Returns the environment, typically a namespace, in which the
68129 maechler 309
        \code{Class} has been defined.  \code{Class} should typically be
310
        the result of \code{\link{class}()} (and hence contain a
311
        \code{"package"} attribute) or \code{\link{getClass}} (or
312
        \code{\link{getClassDef}}).
57849 maechler 313
    }
314
 
26530 maechler 315
    \item{\code{completeExtends}:}{
18994 hornik 316
      complete the extends information in the class definition, by
317
      following transitive chains.
26530 maechler 318
 
25351 jmc 319
      If \code{class2} and \code{extensionDef} are included, this
320
      class relation is to be added.  Otherwise just use the current
321
      \code{ClassDef}.
322
 
323
      Both the \code{contains} and \code{subclasses} slots are
324
      completed with any indirect relations visible.
18994 hornik 325
    }
26530 maechler 326
 
327
    \item{\code{classMetaName}:}{
18994 hornik 328
      a name for the object storing this class's definition
329
    }
26530 maechler 330
 
331
    \item{\code{methodsPackageMetaName}:}{
20330 jmc 332
      a name mangling device to hide metadata defining method and
333
      class information.
18994 hornik 334
    }
27569 ripley 335
    \item{\code{metaNameUndo}}{
336
      As its name implies, this function undoes the
20092 jmc 337
      name-mangling used to produce meta-data object names, and
44751 maechler 338
      returns a object of class \code{\linkS4class{ObjectsWithPackage}}.
27569 ripley 339
    }
26530 maechler 340
 
18994 hornik 341
    \item{\code{requireMethods}:}{
342
      Require a subclass to implement methods for the generic functions,
343
      for this signature.
26530 maechler 344
 
18994 hornik 345
      For each generic, \code{setMethod} will be called to define a
346
      method that throws an error, with the supplied message.
26530 maechler 347
 
18994 hornik 348
      The \code{requireMethods} function allows virtual classes to
349
      require actual classes that extend them to implement methods for
350
      certain functions, in effect creating an API for the virtual class.
26530 maechler 351
 
18994 hornik 352
      Otherwise, default methods for the corresponding function would be
353
      called, resulting in less helpful error messages or (worse still)
354
      silently incorrect results.
355
    }
26530 maechler 356
 
62069 hornik 357
    \item{\code{checkSlotAssignment}, \code{checkAtAssignment}:}{
18994 hornik 358
      Check that the value provided is allowed for this slot, by
359
      consulting the definition of the class.  Called from the C code
360
      that assigns slots.
26530 maechler 361
 
40281 ripley 362
      For privileged slots (those that can only be set by accessor
18994 hornik 363
      functions defined along with the class itself), the class designer
364
      may choose to improve efficiency by validating the value to be
365
      assigned in the accessor function and then calling \code{slot<-}
366
      with the argument \code{check=FALSE}, to prevent the call to
367
      \code{checkSlotAssignment}.
368
    }
26530 maechler 369
 
18994 hornik 370
    \item{\code{defaultPrototype}:}{
26530 maechler 371
      The prototype for a class which will have slots, is not a virtual
44586 maechler 372
      class, and does not extend one of the basic classes. Both its
373
      \code{\link{class}} and its (\R internal) type,
374
      \code{\link{typeof}()}, are \code{"S4"}.
18994 hornik 375
    }
26530 maechler 376
 
19115 ripley 377
    \item{\code{.InitBasicClasses}, \code{.InitMethodsListClass}, \code{.setCoerceGeneric}:}{
18994 hornik 378
      These functions perform part of the initialization of classes and
56382 murdoch 379
      methods, and are called (only!) from \code{.onLoad}.
18994 hornik 380
    }
26530 maechler 381
 
18994 hornik 382
    \item{\code{isClassDef}:}{
383
      Is \code{object} a representation of a class?
384
    }
26530 maechler 385
 
18994 hornik 386
    \item{\code{validSlotNames}:}{
387
      Returns \code{names} unless one of the names is reserved, in which
388
      case there is an error.  (As of writing, \code{"class"} is the
389
      only reserved slot name.)
390
    }
29714 ripley 391
    \item{\code{getDataPart}, \code{setDataPart}:}{
46531 jmc 392
      Utilities called to implement
393
      \code{object@.Data}.  Calls to \code{setDataPart} are also used
394
      to merge the data part of a superclass prototype.
29714 ripley 395
    }
26530 maechler 396
 
57849 maechler 397
  }%end{ describe }
15357 jmc 398
}
44586 maechler 399
\examples{
400
typeof(defaultPrototype()) #-> "S4"
57849 maechler 401
 
402
## .classEnv()
403
meth.ns <- asNamespace("methods")
404
if(get4 <- !any("package:stats4" == search()))
405
   require("stats4")
406
stopifnot(TRUE
407
 , identical(.classEnv("data.frame"), meth.ns)
408
 , identical(.classEnv(class(new("data.frame"))), meth.ns)
68129 maechler 409
 , identical(.classEnv(     "mle"       ), meth.ns) # <- *not* 'stats4'
57849 maechler 410
 , identical(.classEnv(class(new("mle"))), asNamespace("stats4"))
68129 maechler 411
 , identical(.classEnv(getClass ("mle") ), asNamespace("stats4"))
57849 maechler 412
 )
413
if(get4) detach("package:stats4")
44586 maechler 414
}
16471 jmc 415
\keyword{internal}