Rev 58549 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/RClassUtils.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2011 R Core Team% Copyright 2008-2011 The R Foundation% Distributed under GPL 2 or later\name{RClassUtils}%% NOTE: This is {internal} for a good reason%% FIXME the following are undocumented utilities (Sep 23, 2002)\alias{completeSubclasses}\alias{newClassRepresentation}\alias{print.classRepresentation}\alias{setExtendsMetaData}\alias{setSubclassMetaData}\alias{subclassesMetaName}\alias{extendsMetaName}\alias{classPrototypeDef-class}%%\alias{.classEnv}\alias{classLabel}\alias{testVirtual}\alias{makePrototypeFromClassDef}\alias{newEmptyObject}\alias{completeClassDefinition}\alias{getAllSuperClasses}\alias{superClassDepth}\alias{isVirtualClass}\alias{assignClassDef}\alias{newBasic}\alias{makeExtends}\alias{reconcilePropertiesAndPrototype}\alias{tryNew}\alias{empty.dump}\alias{showClass}\alias{showExtends}\alias{possibleExtends}\alias{completeExtends}\alias{classMetaName}\alias{methodsPackageMetaName}\alias{metaNameUndo}\alias{requireMethods}\alias{checkSlotAssignment}\alias{defaultPrototype}\alias{isClassDef}\alias{validSlotNames}\alias{getDataPart}\alias{setDataPart}%% not explicitly documented here\alias{.BasicClasses}\alias{.BasicVectorClasses}\alias{.InitBasicClasses}\alias{.InitMethodsListClass}\alias{.setCoerceGeneric}\alias{conditionalExtension-class}\title{Utilities for Managing Class Definitions}\description{These are various functions to support the definition and use offormal classes. Most of them are rarely suitable to be calleddirectly.Others are somewhat experimental and/or partially implemented only. Dorefer to \code{\link{setClass}} for normal code development.}\usage{classLabel(Class).classEnv(Class, default = .requirePackage("methods"), mustFind = TRUE)testVirtual(properties, extends, prototype, where)makePrototypeFromClassDef(slots, ClassDef, extends, where)newEmptyObject()completeClassDefinition(Class, ClassDef, where, doExtends)getAllSuperClasses(ClassDef, simpleOnly = TRUE)superClassDepth(ClassDef, soFar, simpleOnly = TRUE)isVirtualClass(Class, where)newBasic(Class, ...)makeExtends(Class, to, coerce, test, replace, by, package, slots,classDef1, classDef2)reconcilePropertiesAndPrototype(name, properties, prototype,superClasses, where)tryNew(Class, where)empty.dump()showClass(Class, complete=TRUE, propertiesAreCalled="Slots")showExtends(ext, printTo = stdout())possibleExtends(class1, class2, ClassDef1 = getClassDef(class1),ClassDef2 = getClassDef(class2, where =.classEnv(ClassDef1)))completeExtends(ClassDef, class2, extensionDef, where)classMetaName(name)methodsPackageMetaName(prefix, name, package = "")metaNameUndo(strings, prefix, searchForm = FALSE)requireMethods(functions, signature, message, where)checkSlotAssignment(obj, name, value)defaultPrototype()isClassDef(object)validSlotNames(names)getDataPart(object)setDataPart(object, value, check = TRUE)}\section{Summary of Functions}{\describe{\item{\code{testVirtual}:}{Test for a Virtual Class.Figures out, as well as possible, whether the class with theseproperties, extension, and prototype is a virtual class.Can be forced to be virtual by extending \code{"VIRTUAL"}.Otherwise, a class is virtual only if it has no slots, extends nonon-virtual classes, and has a \code{NULL} Prototype.}\item{\code{makePrototypeFromClassDef}:}{Makes the prototype implied by the class definition.The following three rules are applied in this order.\enumerate{\item If the class has slots, then the prototype for each slotis used by default, but a corresponding element in theexplicitly supplied prototype in \code{ClassDef}, if there isone, is used instead (but it must be coercible to the class ofthe slot). This includes the data part (\code{".Data"} slot) ifthere is one.\item If there are no slots but a non-null prototype wasspecified, this is returned.\item If there is a non-virtual superclass (a class in theextends list), then its prototype is used. The data part isextracted if needed (it is allowed to have two superclasseswith a data part; the first is used and a warning issued onany others).}If all three of the above fail, the prototype is \code{NULL}.}\item{\code{newEmptyObject}:}{Utility function to create an empty object into which slots can beset.Currently just creates an empty list with class \code{"NULL"}.Later version should create a special object reference that marksan object currently with no slots and no data.}\item{\code{completeClassDefinition}:}{Completes the definition of \code{Class}, relative to the classdefinitions visible from environment \code{where}. If\code{doExtends} is \code{TRUE}, complete the super- andsub-class information.This function is called when a class is defined or re-defined.}\item{\code{getFromClassDef}:}{Extracts one of the intrinsically defined class definitionproperties (".Properties", etc.) Strictly a utility function.}%%\item{\code{getSlots}:}{Returns a named character vector. The names are the names of theslots, the values are the classes of the corresponding slots.The argument \code{x} can either be the name ofa class or the class definition object.}\item{\code{getAllSuperClasses}, \code{superClassDepth}:}{Get the names of all the classes that this class definitionextends.\code{getAllSuperClasses} is a utility function used to complete aclass definition. It returns all the superclasses reachable fromthis class, in breadth-first order (which is the order used formatching methods); that is, the first direct superclass followedby all its superclasses, then the next, etc. (The order isrelevant only in the case that some of the superclasses havemultiple inheritance.)\code{superClassDepth}, which is called from\code{getAllSuperClasses}, returns the same information, but as alist with components \code{label} and \code{depth}, the latter forthe number of generations back each class is in the inheritancetree. The argument \code{soFar} is used to avoid loops in thenetwork of class relationships.}\item{\code{isVirtualClass}:}{Is the named class a virtual class?A class is virtual if explicitly declared to be, and also if theclass is not formally defined.}%%\item{\code{assignClassDef}:}{assign the definition of the class to the specially named object}\item{\code{newBasic}:}{the implementation of the function \code{new} for basic classesthat don't have a formal definition.Any of these could have a formal definition, except for\code{Class="NULL"} (disallowed because \code{NULL} can't haveattributes). For all cases except \code{"NULL"}, the class of theresult will be set to \code{Class}.See \code{new} for the interpretation of the arguments.}\item{\code{makeExtends}:}{convert the argument to a list defining the extension mechanism.}\item{\code{reconcilePropertiesAndPrototype}:}{makes a list or a structure look like a prototype for the givenclass.Specifically, returns a structure with attributes corresponding tothe slot names in properties and values taken from prototype ifthey exist there, from \code{new(classi)} for the class,\code{classi} of the slot if that succeeds, and \code{NULL}otherwise.The prototype may imply slots not in the properties list, sinceproperties does not include inherited slots (these are leftunresolved until the class is used in a session).}\item{\code{tryNew}:}{Tries to generate a new element from this class, but if the attemptfails (as, e.g., when the class is undefined or virtual) justreturns \code{NULL}.This is inefficient and also not a good idea when actuallygenerating objects, but is useful in the initial definition ofclasses.}\item{\code{showClass}:}{Print the information about a class definition.If \code{complete} is \code{TRUE}, include the indirectinformation about extensions.It is the utility called from \code{\link{show}(\link{getClass}(.))},and the user should typically use \code{getClass(.)} for lookingat class definitions.}\item{\code{showExtends}:}{Print the elements of the list of extensions; for \code{printTo = FALSE},returns a list with components \code{what} and \code{how}; this isused e.g., by \code{\link{promptClass}()}.}\item{\code{possibleExtends}:}{Find the information that says whether class1 extends class2,directly or indirectly.This can be either a logical value or an object of class\code{\linkS4class{SClassExtension}} containing variousfunctions to test and/or coerce the relationship.}\item{\code{classLabel}: }{Returns an informative character string identifying the classand, if appropriate, the package from which the class came.}\item{\code{.classEnv}: }{Returns the environment, typically a namespace, in which the\code{Class} has been defined.%% is called from C level R_check_class_etc()}\item{\code{completeExtends}:}{complete the extends information in the class definition, byfollowing transitive chains.If \code{class2} and \code{extensionDef} are included, thisclass relation is to be added. Otherwise just use the current\code{ClassDef}.Both the \code{contains} and \code{subclasses} slots arecompleted with any indirect relations visible.}\item{\code{classMetaName}:}{a name for the object storing this class's definition}\item{\code{methodsPackageMetaName}:}{a name mangling device to hide metadata defining method andclass information.}\item{\code{metaNameUndo}}{As its name implies, this function undoes thename-mangling used to produce meta-data object names, andreturns a object of class \code{\linkS4class{ObjectsWithPackage}}.}\item{\code{requireMethods}:}{Require a subclass to implement methods for the generic functions,for this signature.For each generic, \code{setMethod} will be called to define amethod that throws an error, with the supplied message.The \code{requireMethods} function allows virtual classes torequire actual classes that extend them to implement methods forcertain functions, in effect creating an API for the virtual class.Otherwise, default methods for the corresponding function would becalled, resulting in less helpful error messages or (worse still)silently incorrect results.}\item{\code{checkSlotAssignment}:}{Check that the value provided is allowed for this slot, byconsulting the definition of the class. Called from the C codethat assigns slots.For privileged slots (those that can only be set by accessorfunctions defined along with the class itself), the class designermay choose to improve efficiency by validating the value to beassigned in the accessor function and then calling \code{slot<-}with the argument \code{check=FALSE}, to prevent the call to\code{checkSlotAssignment}.}\item{\code{defaultPrototype}:}{The prototype for a class which will have slots, is not a virtualclass, and does not extend one of the basic classes. Both its\code{\link{class}} and its (\R internal) type,\code{\link{typeof}()}, are \code{"S4"}.}\item{\code{.InitBasicClasses}, \code{.InitMethodsListClass}, \code{.setCoerceGeneric}:}{These functions perform part of the initialization of classes andmethods, and are called (only!) from \code{.onLoad}.}\item{\code{isClassDef}:}{Is \code{object} a representation of a class?}\item{\code{validSlotNames}:}{Returns \code{names} unless one of the names is reserved, in whichcase there is an error. (As of writing, \code{"class"} is theonly reserved slot name.)}\item{\code{getDataPart}, \code{setDataPart}:}{Utilities called to implement\code{object@.Data}. Calls to \code{setDataPart} are also usedto merge the data part of a superclass prototype.}}%end{ describe }}\examples{typeof(defaultPrototype()) #-> "S4"## .classEnv()meth.ns <- asNamespace("methods")if(get4 <- !any("package:stats4" == search()))require("stats4")stopifnot(TRUE, identical(.classEnv("data.frame"), meth.ns), identical(.classEnv(class(new("data.frame"))), meth.ns), identical(.classEnv(class(new("mle"))), asNamespace("stats4")))if(get4) detach("package:stats4")}\keyword{internal}