The R Project SVN R

Rev

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