Smart pointers

Throughout PKIFv2, reference-counted smart pointers are extensively utilized to simplify memory management. Most objects returned from functions and many parameters to functions are passed via smart pointers. By default, PKIFv2 uses the smart pointer class provided by the Boost library. Smart pointer objects are always named by appending "Ptr" to the end of the class name associated with the smart pointer. Additional information about the Boost library can be found at www.boost.org.

The following example shows how to create and use a smart pointer to a certificate object. After associating a raw pointer with a smart pointer, object applications need not, and must not, call delete on the raw pointer and should interact with the raw pointer via the smart pointer object only.

CPKIFCertificatePtr cert(new CPKIFCertificate());

cert->Decode(pbCertEncoded, cbCertEncoded);

As with raw pointers, it is frequently necessary to determine if the pointer associated with a smart pointer instance is NULL. The following example demonstrates how to check the above certificate object for existence:

if(cert != (CPKIFCertificate*)NULL)

{

//pointer is not NULL and may be used 

}

As noted above, PKIFv2 uses reference counted smart pointers. Thus, when an application associates one object with another object, both the application and the receiving object have references to the same object. Changes to that object will effect both the application and the receiving object. Many objects are read-only, so changes are not possible. For example, the value stored in CPKIFOID objects is defined at construction time and cannot be changed.