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*); int objalloc;

The unknown interface encapsulates the fundamental behavior common to most interfaces (classes) in PTypes, except string, cset, 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.

To avoid implicit generation of virtual destructors by the compiler, always declare out-of-line (not inline) virtual destructors for all direct or indirect derivatives of unknown and component.

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 a code (possibly enclosed within #ifdef DEBUG ... #endif) which checks whether this value is zero just before the program exits.

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.

See also: Lists, variant


PTypes home