The library supports smart pointer technology for accessing database objects and managing memory at run-time. To work with smart pointers, use the following classes and templates:
The OdRxObject abstract class provides the base interface for all classes and is used only for deriving new classes. You cannot create an instance of this class. It declares three methods for working with smart pointers:
These methods do not have an implementation and are the declaration of the base interface for working with smart pointers. Classes derived from the OdRxObject class must redefine these methods for modifying the reference counter. Smart pointers will call the redefined methods to count the number of references to the instances of the class. You can either implement these methods in the class that you specify or you can use the OdRxObjectImpl and OdStaticRxObject templates to create the standard implementation of these methods.
The OdRxObjectImpl template creates the standard implementation of the numRefs(), addRef(), and release() methods for classes derived from the OdRxObject class. This template wraps the specified class using a new class that inherits all methods of the specified class, defines the new, delete, and assignment operators for it, declares the reference counter as the core structure, implements the numRefs() method that returns the value of reference counter, implements the addRef() method that increments the reference counter, implements the release() method that decrements the reference counter, checks whether the reference counter has a positive value, and calls the delete operator when the reference counter is a zero. The overloaded delete operator destroys the object of the specified class and wrapper class together. The reference counter declared by the template is the OdRefCounter structure which implements the counting operations with interlock and performs the synchronizing between threads and across processes. The OdRxObjectImpl template also defines the pseudo-constructor for the specified class that creates the instance of the class, correctly initializes the reference counter, and creates and returns the smart pointer to it. If you create the smart pointers for instances created by the OdRxObjectImpl template, the smart pointers will change the reference counter.
The OdStaticRxObject template creates the empty implementation of the addRef() and release() methods for classes derived from the OdRxObject class. Static instances are created when the object is declared in the program scope and are automatically destroyed when the object abandons its own scope. A program cannot lose the memory allocated for static instances. Therefore, static instances do not require the reference counter. The OdStaticRxObject template wraps the specified class and creates the empty implementation of the addRef() and release() methods. The numRefs() method returns 1. If you create the smart pointers for instances created by the OdStaticRxObject template, the smart pointers will not change the reference counter.
To use the OdStaticRxObject template for dynamic instances, you must implement the new and delete operators in the specified class derived from the OdRxObject class. You can use either the ODRX_HEAP_OPERATORS macro, which creates the standard implementation of the new and delete operators in the specified class, or the ODRX_USING_HEAP_OPERATORS macro, which inherits the implementation of the new and delete operators from the parent class. The OdRxObjectImpl template automatically defines the new and delete operators using the ODRX_HEAP_OPERATORS macro.
The OdRxObjectPtr class implements the non-typified smart pointer that can control instances of any class derived from the OdRxObject base class. You can immediately create the instances of this class in your program, that is, smart pointers, and assign them the addresses of any instances. The type control is absent.
The OdSmartPtr template implements the typified smart pointer for the specified class. You must use this class in the (typedef) statement for defining the smart pointer type that you will use for creating smart pointers in your program. When you perform the different operations with smart pointers of the defined type, they check the class type and generate an exception if the types are different. All database classes have corresponding typified smart pointer classes with the "Ptr" suffix, for example the OdDbCirclePtr class is the smart pointer to the instances of the OdDbCircle class.
OdRxObjectPtr and OdSmartPtr implement the base functionality of smart pointers: attaching an instance of an object to the smart pointer, detaching an instance from the smart pointer, assigning smart pointers, comparing two smart pointers, checking for NULL, getting the address of the referenced object, getting properties for the referenced object, calling methods for the referenced object, counting references to the referenced object, and destroying the referenced object when the reference counter becomes zero.
The OdBaseObjectPtr class is the base class for the OdSmartPtr template and implements the base smart pointer actions: getting the raw pointer and checking for NULL.
When used properly, smart pointer classes help guarantee the proper management of heap objects, both in normal execution cases and in cases where exceptions transfer control out of the current scope.
Creating an Empty Implementation of the Reference Counter
Creating a Standard Implementation of the Reference Counter
Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.
|