/*! \page chapreference_counting Reference Counting and Handle Types \author Stefan Schirra (stschirr@mpi-sb.mpg.de) # Reference counting # As of release 2.1, a reference counting scheme is used for the kernel objects in the kernels `Cartesian` and `Homogeneous`. All copies of an object share a common representation object storing the data associated with a kernel object; see Figure \ref figrefcounted . The motivation is to save space by avoiding storing the same data more than once and to save time in the copying procedure. Of course, whether we actually save time and space depends on the size of the data that we would have to copy without sharing representations. The drawback is an indirection in accessing the data. Such an indirection is bad in terms of cache efficiency. Thus there are also non-reference-counting kernels available `Simple_cartesian` and `Simple_homogeneous`. \anchor figrefcounted \image html reference_counting.gif "Objects using reference counting (bottom) share common representation; copying creates a new handle (drawn at the right) pointing to the same representation as the object copied. Without reference counting (top) all data are copied to the new object (drawn at the right);" The reference counting in the kernel objects is not visible to a user and does not affect the interface of the objects. The representation object is often called the body. The object possibly sharing its representation with others is called a handle, because its data consists of a pointer to its representation object only. If the implementation of the member functions is located with the representation object and the functions in the handle just forward calls to the body, the scheme implements the bridge design pattern, which is used to separate an interface from its implementation. The intent of this design pattern is to allow for exchanging implementations of member functions hidden to the user, especially at runtime. # Handle & Rep # The two classes `Handle` and `Rep` provide reference counting functionality; see Figure \ref figHandleRep . By deriving from these classes, the reference counting functionality is inherited. The class `Rep` provides a counter; representation classes derive from this class. The class `Handle` takes care of all the reference-counting related stuff. In particular, it provides appropriate implementations of copy constructor, copy assignment, and destructor. These functions take care of the counter in the common representation. Classes sharing reference-counted representation objects (of a class derived from `Rep`) do not have to worry about the reference counting, with the exception of non-copy-constructors. There a new representation object must be created and the pointer to the representation object must be set. If `CGAL_USE_LEDA` is defined and `CGAL_NO_LEDA_HANDLE` is not defined, the types `Handle` and `Rep` are set to the \leda types `handle_base` and `handle_rep`, respectively (yes, without a `leda_`-prefix). Use of the \leda class `handle_rep` implies that \leda memory management is used for the representation types. \code{.cpp} typedef handle_base Handle; typedef handle_rep Rep; \endcode Scavenging \leda, we provide the identical functionality in the \cgal classes `Leda_like_handle` and `Leda_like_rep`. If \leda is not available or `CGAL_NO_LEDA_HANDLE` is set, `Handle` and `Rep` correspond to these types. \code{.cpp} typedef Leda_like_handle Handle; typedef Leda_like_rep Rep; \endcode \anchor figHandleRep \image html handle_rep.gif "UML class diagram for Handle & Rep scheme." # Using Handle & Rep # In order to make use of the reference counting provided by the classes `Handle` and `Rep`, your interface class (the class providing the interface of the geometric object) must be derived from `Handle` and your representation class (the class containing the data to be shared) must be derived from `Rep`: \code{.cpp} class My_rep : public Rep { /*...*/ }; class My_geo_object : public Handle { public: My_geo_object(const My_geo_object& m); My_geo_object(Arg1, Arg2); }; \endcode The class `My_geo_object` is responsible for allocating and constructing the `My_rep` object "on the heap". Typically, a constructor call is forwarded to a corresponding constructor of `My_rep`. The address of the new `My_rep` is assigned to `PTR` inherited from `Handle`, e.g.: \code{.cpp} My_geo_object::My_geo_object(Arg1 a1, Arg2 a2) { PTR = new My_rep(a1, a2); } \endcode The default constructor of `Handle` is called automatically by the compiler and the reference counting is initialized. You always have to define a copy constructor for `My_geo_object` and to call the copy constructor of `Handle` there: \code{.cpp} My_geo_object::My_geo_object(const My_geo_object& m) : Handle( m) {} \endcode That's it! There is no need to define a copy assignment operator nor is there a need to define a destructor for the derived class `My_geo_object`! Handle & Rep does the rest for you! You get this functionality by including \ref CGAL/Handle.h It is common practice to add a (protected) member function `ptr()` to the class `My_geo_object`, which casts the `PTR` pointer from `Rep*` to the actual type `My_rep*`. Note that this scheme is meant for non-modifiable types. You are not allowed to modify data in the representation object, because the data are possibly shared with other `My_geo_object` objects. # Templated handles # Factoring out the common functionality in base classes enables re-use of the code, but there is also a major drawback. The `Handle` class does not know the type of the representation object. It maintains a `Rep*` pointer. Therefore, this pointer must be cast to a pointer to the actual type in the classes derived from `Handle`. Moreover, since the `Handle` calls the destructor for the representation through a `Rep*`, the destructor of `Rep` must be virtual. Finally, debugging is difficult, again, because the `Handle` class does not know the type of the representation object. Making the actual type of the representation object a template parameter of the handle solves these problems. This is implemented in class template `Handle_for`. This class assumes that the reference-counted class provides the following member functions to manage its internal reference counting: