Using the OdStaticRxObject template in the example of reference counting involves implementing the TestStaticInstance() and TestDynamicInstance() functions.
To work with smart pointers, you must derive your class from the OdRxObject class when you define it and define the smart pointer type using the OdSmartPtr template:
class MyObj : public OdRxObject
{
public:
ODRX_HEAP_OPERATORS();
public:
MyObj();
~MyObj();
};
typedef OdSmartPtr<MyObj> MyObjPtr;
To add the empty smart pointer implementation in an instance of your own class, you must use the OdStaticRxObject template when you declare the variables in the code. The test functions have the following implementation:
void TestStaticInstance()
{
OdStaticRxObject<MyObj> my;
RefCountTest(&my);
odPrintConsoleString(L"\nReturn from RefCountTest()\n Reference counter: %d\n", my.numRefs());
getch();
}
void TestDynamicInstance()
{
MyObj* pMyObj = new OdStaticRxObject<MyObj>;
RefCountTest(pMyObj);
odPrintConsoleString(L"\nReturn from RefCountTest()\n Reference counter: %d\n", pMyObj->numRefs());
getch();
delete pMyObj;
}
To insert the smart pointer functionality for all classes derived from your own class, you must derive your class from the OdRxObject class through the OdStaticRxObject template when you define it:
class MyObj : public OdStaticRxObject<OdRxObject>
{
public:
ODRX_HEAP_OPERATORS();
public:
MyObj();
~MyObj();
};
typedef OdSmartPtr<MyObj> MyObjPtr;
You do not need to use the OdStaticRxObject template in the code when you declare the variables. The test functions will have the following implementation:
void TestStaticInstance()
{
MyObj my;
RefCountTest(&my);
odPrintConsoleString(L"\nReturn from RefCountTest()\n Reference counter: %d\n", my.numRefs());
getch();
}
void TestDynamicInstance()
{
MyObj* pMyObj = new MyObj;
RefCountTest(pMyObj);
odPrintConsoleString(L"\nReturn from RefCountTest()\n Reference counter: %d\n", pMyObj->numRefs());
getch();
delete pMyObj;
}
The constructor and destructor for the MyObj class have the following implementation:
MyObj::MyObj()
{
odPrintConsoleString(L"\nObject is created [counter: %d]\n", numRefs());
}
MyObj::~MyObj()
{
odPrintConsoleString(L"\nObject is destroyed\n");
}
The result of the reference counting for both the static instance and dynamic instance are identical:
Object is created [counter: 1]
1.(pMy)->(pSmart1)
Reference counter: 1
2.(pSmart1)->(pSmart2),(pSmart3)
Reference counter: 1
3.(pSmart2)=NULL
Reference counter: 1
4.(pSmart3)->(pSmart4),(pSmart5)
Reference counter: 1
5.(pSmart1)=NULL, (pSmart4)=NULL
Reference counter: 1
6.(pSmart1)=NULL, (pSmart3)=NULL
Reference counter: 1
Return from RefCountTest()
Reference counter: 1
Object is destroyed
Return from Test
When the test function creates the instance of the MyObj class, the reference counter of the instance gets the value 1. When the function sets the address of the instance to any smart pointer, the reference counter does not change its own value. When the TestStaticInstance() function finishes, it automatically destroys the static instance independent of the reference counter value. When the TestDynamicInstance() function finishes, the reference counter is 1 and smart pointers cannot destroy the instance automatically. To delete the dynamic instance, the test function directly calls the delete operator.
Overview of Smart Pointer Classes
Creating an Empty Implementation of the Reference Counter
Example of Counting References
Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.
|