/** \page ref The Reference Library
The Reference Library provides a means to automatically free
memory that is no longer needed.
.
\section refthread Thread Safety of the Reference Counting Package
The referencing counting package is thread-safe if the CPP macro
REF_USE_LOCKS is defined to 1. This means that Ref's to a particular
object can be created and reassigned and destroyed in different
threads. However, the Ref's themselves are not thread-safe.
For example, a static Ref cannot be simultaneously modified from
multiple threads.
Because there is an overhead associated with locking access to an
object's reference count, locking is not turned on by default,
and, thus, making and deleting references to an object in
multiple threads is not thread-safe by default. The
RefCount::use_locks member is passed a bool value to turn locking
on and off on a per object basis.
\section refcust Customizing the Reference Counting Package
The behaviour of the package can be modified at compile time
with the following five macros, each of which should be undefined, 0, or 1:
- REF_CHECK_STACK
-
If this is 1, referenced objects are checked to see if they
reside on the stack, in which case storage for the object is not managed,
if management is enabled.
- REF_MANAGE
-
If this is 1, the unmanage member is enabled.
- REF_CHECK_MAX_NREF
-
If this is 1, the reference count is checked before
it is incremented to make sure it isn't too big.
- REF_CHECK_MIN_NREF
-
If this is 1, the reference count is checked before
it is decremented to make sure it isn't already zero.
- REF_USE_LOCKS
-
If this is 1, modification of the reference count
is locked to allow thread-safe execution.
If a macro is undefined, then the behaviour is architecture
dependent---usually, the macro will be set to 1 in this case.
For maximum efficiency and for normal operation after the program is
debugged, compile with all of the above macros defined to zero.
This can also be done by defining REF_OPTIMIZE.
An include file can be used to set these options as well. This has
the advantage that dependency checking will force an automatic
recompile of all affected files if the options change. This is done
in the file scconfig.h, which is produced by the automated configuration
procedure.
Note that all source code that uses references must be compiled with
the same value for REF_MANAGE.
Changing this can change the storage layout and the interpretation of
the reference count data.
\section refexample A Reference Example
Following is a simple example of how to manage memory with reference
counts.
\#include
class A: virtual public RefCount {};
class B: public A {};
int
main()
{
Ref\ a1(new A);
Ref\ a2;
// Create another reference to the A object pointed to by a1.
a2 = a1;
// Make a2 refer to a new A object.
a2 = new A;
// a2 was the only reference to the second A object, so setting
// a2 to the null object will cause the second A object to be
// deleted.
a2 = 0;
Ref\ b(new B);
// An object of type Ref\ can be assigned to an object of type
// Ref\ as long as X* can be assigned to Y*.
a1 = b;
// An automatic dynamic cast can be done by using the left shift
// operator.
b << a1;
// The B object will be deleted here because all of the references
// to it go out of scope and destroyed.
return 0;
}
*/