Go to Overview over all GrAL packages.
Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

copy_traits_base Struct Template Reference

Compile-time branch on how to copy pointees. More...

#include <copy-traits.h>

Inheritance diagram for copy_traits_base:

Inheritance graph
[legend]
List of all members.

Static Public Methods

T * clone (T const &t)
void destroy (T *t)

Detailed Description

template<class T>
struct copy_traits_base< T >

Compile-time branch on how to copy pointees.

If we have a pointer to an abstract base of a polymorphic type, we cannot copy the content by simply calling operator new. Instead, we must use some virtual member function like clone(). This is a problem when using polymorphic types together with smart pointers (how do they copy the pointer?). Also, if we want to use the 'pimpl' idiom together with a smart pointer, we cannot call delete on the opaque pointer-to-impl inside the smart pointer.

These differences are hidden by T* copy_traits<T>::clone(const T&) and copy_traits<T>::destroy(T*) Default implementation in copy_traits_base for non-polymorphic types is simply operator new / delete.

For T being an abstract class, this implmentation has to be overridden with a method like T::clone() by specializing copy_traits<> for T.

For an opaque type, we can declare a specialization along with the forward declaration of the type:

    class impl;
    template<>
    struct copy_traits<impl> {
      static impl* clone  (impl const&);
      static void  destroy(impl*);
   };

and define the specialization with the definition of the type:

   class impl { ... };
   
   impl* copy_traits<impl>::clone  (impl const& i) { return new impl(i);}
   void  copy_traits<impl>::destroy(impl      * i) { delete i;}

Definition at line 56 of file copy-traits.h.


Member Function Documentation

template<class T>
T* copy_traits_base< T >::clone T const &    t [inline, static]
 

Reimplemented in copy_traits< testA >.

Definition at line 57 of file copy-traits.h.

template<class T>
void copy_traits_base< T >::destroy T *    t [inline, static]
 

Definition at line 58 of file copy-traits.h.


The documentation for this struct was generated from the following file:
Copyright (c) Guntram Berti 1997-2002. See the GrAL Homepage for up-to-date information.

Generated at Tue Feb 26 15:57:04 2002 for Utilities by doxygen 1.2.11-20011104 written by Dimitri van Heesch, © 1997-2000