The OdSmartPtr template class and OdRxObjectPtr class are used for creating and declaring the instances of smart pointers for classes derived from the OdRxObject class. Smart pointers store the address of the instance, modify the reference counter of the instance, and perform memory control. To control the instances, the class must have an implementation of the numRefs(), addRef(), and release() methods. You can use the OdRxObjectImpl template to create a standard implementation or the OdStaticRxObject template to create an empty implementation of the reference counting functionality.
The OdRxObjectPtr class is a ready-to-use non-typified smart pointer that can control instances of any classes derived from the OdRxObject base class. It does not verify the class type of the stored object when it performs operations with pointers.
The OdSmartPtr class is the template that creates the implementation of smart pointer functionality typified for the specified class. It checks the class type when it performs the operations with pointers. You must use the (typedef) statement to create the smart pointer type for your class. For example, if you have the MyObj class derived in any way from the OdRxObject base class, you should use the following definition to declare the smart pointer type for your class:
typedef OdSmartPtr<MyObj> MyObjPtr;
Your defined type must be: <ClassName> ClassNamePtr. The suffix "Ptr" of the type name is required.
To create an instance of the smart pointer, use its type name before a variable:
MyObjPtr pSmart;
OdRxObjectPtr pSmartRx;
You can also use the OdSmartPtr template in the code before the variable name:
OdSmartPtr<MyObj> pSmart;
When you use the pseudo-constructor to create an instance in dynamic memory, it returns the smart pointer and you should save it in the user-defined variable:
MyObjPtr pSmart = OdRxObjectImpl<MyObj>::createObject();
You can assign the address of the static instance when you declare the smart pointer:
OdRxObjectImpl<MyObj> my1;
OdStaticRxObject<MyObj> my2;
MyObjPtr pSmart1 = &my1;
MyObjPtr pSmart2 = &my2;
OdRxObjectPtr pSmartRx1 = &my1;
OdRxObjectPtr pSmartRx2 = &my2;
You can assign the raw pointer to the dynamic instance when you declare the smart pointer:
MyObj* pMy = new OdStaticRxObject<MyObj>;
MyObjPtr pSmart = pMy
OdRxObjectPtr pSmartRx = pMy;
You can initialize the smart pointer using another smart pointer when you declare it:
MyObjPtr pSmart0 = pSmart;
OdRxObjectPtr pSmartRx0 = pSmart;
You can use the constructor and pass to it the address of the instance when you declare the smart pointer:
MyObjPtr pSmart(pMy, kOdRxObjAttach);
OdRxObjectPtr pSmartRx(pMy, kOdRxObjAttach);
The kOdRxObjAttach option suppresses incrementing of the reference counter.
You can declare the smart pointer as a member of a structure or class. For example:
struct MyStructure {
long idem;
MyObjPtr pMySmart;
OdRxObjectPtr pMySmartRx;
};
To initialize an instance of this structure:
struct MyStructure myStr;
myStr.idem = 1;
myStr.pMySmart = &my1;
myStr.pMySmartRx = &my2;
You can declare the array of smart pointers and work with it as an ordinary array of pointers. For example:
OdRxObjectImpl<MyObj> Obj1;
OdRxObjectImpl<MyObj> Obj2;
OdRxObjectImpl<MyObj> Obj3;
OdRxObjectImpl<MyObj> Obj4;
MyObjPtr arrPtr[4];
arrPtr[0] = &Obj1;
arrPtr[1] = &Obj2;
arrPtr[2] = &Obj3;
arrPtr[3] = &Obj4;
for(int i=0 ; i < 4 ; i++)
odPrintConsoleString(L"\nAddress=%x Refs=%d", arrPtr[i], arrPtr[i]–>numRefs());
OdRxObjectPtr arrPtrRx[4];
for(int j=0 ; j < 4 ; j++) arrPtrRx[j] = arrPtr[3 – j];
Overview of Smart Pointer Classes
Functionality of Smart Pointers
Example of Assigning Smart Pointers
Example of a Typified Smart Pointer
Example of a Non-Typified Smart Pointer
Example of Counting References
Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.
|