Google

C++ Portable Types Library (PTypes) Version 1.7


Top: Basic types: unknown & component

#include <ptypes.h>

unknown::unknown();
virtual unknown::~unknown();

component::component();
virtual component::~component();

component* addref(component*);
bool release(component*); template <class T> class compref; int objalloc;

The unknown interface encapsulates the fundamental behavior common to most interfaces (classes) in PTypes, except string, cset, variant, mutex and rwlock. The unknown class has no semantic or functional meaning, however, deriving a class from unknown ensures that the descendant class could be used with various container objects, e.g. strlist and strmap.

The component class adds reference-counting functionality to unknown (see addref() and release() below). PTypes variants require the objects to be derived from component instead of unknown to be able to make assignments and destruction of variants properly. It should be noted that the reference counting scheme has a potential memory leak problem when there exists a circular reference between 2 or more objects.

A global variable objalloc is declared to keep track of the number of allocated objects in the application program, which can help you to find memory leaks or other potential bugs. If the memory is cleaned up properly, this value should be zero upon program termination. You can write code (possibly enclosed within #ifdef DEBUG ... #endif) which checks whether this value is zero at the end of main().

component* addref(component* c) increments the reference counter for the given object c. The return value is the same as c and is provided for convenience.

bool release(component* c) decrements the reference counter and destroys the object if the counter reached 0. Returns true if the object has been destroyed. Passing NULL to this function is not an error.

template <class T> class compref implements a 'smart' pointer to component (or any derivative class) with automatic reference counting. Use this template in place of a plain pointer declaration (e.g. compref<myclass> instead of myclass*) to automatically destroy objects as soon as there are no more references left. The behavior of compref pointers is similar to plain pointers, except that they perform additional actions when assigning new values to them.

See also: Lists, variant


PTypes home