The OdRxObjectImpl template class is used to create classes and their instances that count the references when a program operates using smart pointers to the instances. This template creates the reference counter and standard implementation of the addRef(), numRefs(), and release() virtual methods of the base class. You can use this template as a wrapper for your classes when you create the instances in automatic or static memory, allocate the dynamic memory for instances, or derive a new class from the specified class.
If you want to create your own class for static or dynamic instances, derive your class from the OdRxObject base class using the following class definition:
class MyObj : public OdRxObject
{
public:
MyObj();
~MyObj();
};
You do not need to add the ODRX_HEAP_OPERATORS macro to the class definition because the standard implementation of the new and delete operators is defined in the OdRxObjectImpl class. Together with the class definition, you should declare the smart pointer type to your class using the OdSmartPtr template:
typedef OdSmartPtr<MyObj> MyObjPtr;
To create an instance in automatic or static memory, use the OdRxObjectImpl template as the wrapper of your class when you declare the variables:
OdRxObjectImpl<MyObj> my;
To create an instance in dynamic memory, declare a smart pointer of your class type in the code and use the createObject() method together with the OdRxObjectImpl template without the (new) operator:
MyObjPtr pSmart = OdRxObjectImpl<MyObj>::createObject();
The createObject() method is the pseudo-constructor which is defined in the OdRxObjectImpl template. The pseudo-constructor creates an instance of the defined class using the (new) operator, creates the smart pointer to it, correctly initializes the reference counter, and returns the smart pointer to the instance. The pseudo-constructor has the following standard implementation:
static OdSmartPtr<TInterface> createObject()
{
return OdSmartPtr<TInterface>(static_cast<TInterface*>(new OdRxObjectImpl<T, TInterface>), kOdRxObjAttach);
}
The pseudo-constructor uses the OdSmartPtr template to create the smart pointer and the kOdRxObjAttach option to suppress incrementing of the reference counter when the instance is being created. This is necessary because the reference counter is set to one by default and the first assignment increments it.
If you want to insert the smart pointer functionality in all classes derived from your own class, derive your class directly from the OdRxObjectImpl template and redefine the pseudo-constructor in the class definition:
class MyObj : public OdRxObjectImpl<OdRxObject>
{
public:
static OdSmartPtr<MyObj> createObject()
{
return OdSmartPtr<MyObj>(new MyObj, kOdRxObjAttach);
}
public:
MyObj();
~MyObj();
};
To create an instance in automatic or static memory, declare a variable of your class type without the template:
MyObj my;
To create an instance in the dynamic memory, declare a smart pointer of your class type and use the pseudo-constructor without the template and new operation:
MyObjPtr pSmart = MyObj::createObject();
You can declare a smart pointer and assign to it the address of the static instance:
MyObjPtr pSmart = &my;
You do not need to use the delete operator because smart pointers control the memory and destroy dynamic instances when their reference counters become zero. The static instances are automatically destroyed when they abandon the program scope.
Overview of Smart Pointer Classes
Using a Standard Implementation of the Reference Counter
Example of Counting References
Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.
|