Views
Foundation Package API Reference Guide
Product Documentation:
Views Documentation Home
Classes | Macros
smartptr.h File Reference
#include <ilviews/macros.h>
#include <stdlib.h>

Classes

class  IlvSmartData
 Base class for reference counting. More...
 

Macros

#define IlvDerivedPointerClass(SmartClass, BaseSmartClass, DataClass, EXPORT)
 
#define IlvPointerClass(SmartClass, DataClass, EXPORT)
 Base class for pointers that handle a reference counter for the objects they point to. More...
 

Detailed Description

Library: views
Declaration of the reference counting management classes, IlvSmartData and IlvSmartPointer,

Macro Definition Documentation

◆ IlvDerivedPointerClass

#define IlvDerivedPointerClass (   SmartClass,
  BaseSmartClass,
  DataClass,
  EXPORT 
)

This macro defines subclasses of IlvSmartPointer that point to subclasses of a base reference counting class. For example, if you define a subclass MyDialogWindow: public MyWindow, you must use the following macro to be able to use objects of type pMyDialogWindow in the same way as you would use a MyDialogWindow* object: IlvDerivedPointerClass(pMyDialogWindow,pMyWindow,MyDialogWindow,MYPACKAGE)

See also
IlvSmartPointer.

◆ IlvPointerClass

#define IlvPointerClass (   SmartClass,
  DataClass,
  EXPORT 
)

Base class for pointers that handle a reference counter for the objects they point to.

All the objects of the class IlvSmartData that are allocated dynamically must be referenced by at least one SmartPointer object to be valid.

Smart pointers handle a reference counter for the objects they point to and destroy these objects when they are no longer visible. The use of smart pointers avoids memory leaks and greatly simplifies and secures memory management.

You handle a smart pointer pA to an object of class A exactly like an A* object. Some compilers are not able to analyze correctly the pA::operator A*(), thus requiring that you cast the smart pointer into a pointer explicitly.

Guidelines

  • Regular pointers versus Smart Pointers
    Assigning and deleting a smart pointer is fast, but slower than assigning and deleting a regular pointer. As a consequence, you should always make sure that you use the appropriate type of pointer. Here is a simple rule you can follow to determine which kind
    of pointer should be used in given situation: When a pointer has to be declared inside a pair of braces ({}), it should be a smart pointer. When it has to be declared inside a pair of parentheses (()), it should be a regular pointer.
    Here is an example:
    class MyWindow
    : public IlvSmartData {
    ...
    };
    IlvPointerClass(pMyWindow, MyWindow, MYPACKAGE); // defines the class pMyWindow
    class MyDocument {
    pMyWindow myWindow; // A smart pointer, because we need the object
    // to remain valid as long as the whole object
    // is valid.
    void setWindow(MyWindow* window) {
    // A regular pointer:
    // we don't need to change the reference count when
    // passing the object as an argument.
    pMyWindow tmpW = window; // A smart pointer:
    // we want the pointer to remain valid within
    // the method's scope.
    for (MyWindow* w = window; w != 0; w = w->next()) {
    // A regular pointer: w remains valid within this scope.
    // and we don't need to change the counters.
    }
    myWindow = window;
    }
    };
  • Cycles
    Reference counters maintained by smart pointers often generate reference cycles where objects reference each other in a circular way. The objects involved in reference cycles are never deleted because they are always pointed to by another object and, as a consequence, their reference counter is always positive. One solution to overcome this problem is to break the loop explicitly when you know that some objects will no longer be needed. A better solution, however, is to organize the data as a hierarchical structure that specifies which object is the parent and which object is the child. In a hierarchical structure, children should point to their parent via regular pointers instead of smart pointers.

    In the above example, a pointer to MyDocument in MyWindow should be MyDocument* instead of IlvSmartPointerTo<MyDocument>.

See also
IlvSmartData, IlvDerivedPointerClass.
IlvPointerClass
#define IlvPointerClass(SmartClass, DataClass, EXPORT)
Base class for pointers that handle a reference counter for the objects they point to.
Definition: smartptr.h:247
IlvSmartData
Base class for reference counting.
Definition: smartptr.h:76