Smart Pointers
[Data Types]
Detailed Description
If one object keeps a pointer to another object, and the pointed-to object goes away, the first object is left with an invalid object pointer. An nRef creates a wire between the 2 objects, if the referenced object goes away, the nRef will be invalidated and the object holding the reference will be notified.When creating a nRef you need to specify the type of object it will be referencing.
nRef<nSkinAnimator> refSkinAnimator;
Here a nRef is checked for validity and assigned to as if it were a pointer.
if (!refSkinAnimator.isvalid()) { refSkinAnimator = kernelServer->New("nskinanimator", "/usr/scene/node/animator"); }
A nRef can be dereferenced just like a pointer.
this->refSkinAnimator->Animate(this, renderContext);
Use nRef::get() to get a pointer to the object
nSkinAnimator* skinAnimator = refSkinAnimator.get();
nAutoRef builds on nRef and adds the ability to revalidate itself by looking up the referenced object by its path name. It is up to the nAutoRef holder to ensure that the path name string stays in scope.
// nAutoRef needs access to the kernel server // in order to find objects by path name nAutoRef<nGfxServer> refGfxServer(kernelServer, "/sys/servers/gfx");
or:
// nAutoRef needs access to the kernel server // in order to find objects by path name nAutoRef<nGfxServer2> refGfxServer(kernelServer); refGfxServer = "/sys/servers/gfx";
An nDynAutoRef is the same as an nAutoRef except that it maintains its own copy of the name string.
Technically, it's a class template implementing a smart pointer.
See also nHardRef & nHardRefServer.
Classes | |
| class | nAutoRef< TYPE > |
| class | nDynAutoRef< TYPE > |
| class | nHardRef< TYPE > |
| Implements reference to moveable target object. More... | |
| class | nHardRefServer |
| Validates nHardRef objects as soon as target object is created. More... | |
| class | nRef< TYPE > |