A SERVICE OF

logo

4
Chapter 1: Getting Started with Cosmo 3D
Example 1-1 demonstrates how reference counts are incremented and decremented.
Example 1-1 Objects and Reference Counts
csAppearance *appearanceA, *appearanceC;
csGeoSet *gset;
csShape *shape;
shape->setGeometry(0, gset);
/* Attach appearanceC to gset. Reference count of appearanceC
* is incremented. */
shape->setAppearance(appearanceC);
/* Attach appearanceA to gset, replacing appearanceC. Reference
* count of appearanceC is decremented and that of appearanceA
* is incremented. */
shape->setAppearance(appearanceA);
When the reference count of an existing csObject becomes 0, the object is assumed not to
be referenced by any other object and is deleted. An object that has nothing above itself
in the scene hierarchy is removed because it is no longer part of the scene graph.
This automatic reference counting is usually all you ever need to use. However, the
routines csObject::Ref(), csObject::Unref(), and csObject::GetRefCount() allow you to
increment, decrement, and retrieve the reference count of a csObject should you wish to
do so.
Runtime Typing
Each csObject knows what type it is. Applications can nd out the class object of an
instance by querying the object with getClassType(), as in the following example:
// csContainer *ctr
if(ctr->getType() == csMaterial::getClassType())
printf(“It’s a csMaterial!\n”);
else
printf(“It’s not a csMaterial!\n”);
You need to know the runtime type of an object so you can invoke the right code to
manipulate an object.
For checking the derivation of a type, use csObject::isOfType().