Smart Pointers

clone_function_allocator_policy Struct Reference

#include <smart_ptr.hpp>

List of all members.


Detailed Description

This is an allocator policy that uses traditional cloning method. This policy requires that the target class have a virtual clone function. The normal usage is to create a base class with a pure virtual clone function, and have each derived class implement the clone function. This includes derived derived classes, and so-on.
Example Usage.

struct ShapeWithVirtualCloneLogic
{
        ShapeWithVirtualCloneLogic(){}
        ~ShapeWithVirtualCloneLogic(){}
        ShapeWithVirtualCloneLogic(const ShapeWithVirtualCloneLogic& src){}
        virtual std::string GetTypeName()const = 0;
        virtual ShapeWithVirtualCloneLogic* do_clone()const=0; //Needed for clone_function_allocator_policy
};

struct CircleWithVirtualCloneLogic : public ShapeWithVirtualCloneLogic
{
        CircleWithVirtualCloneLogic(){}
        ~CircleWithVirtualCloneLogic(){}
        CircleWithVirtualCloneLogic(const CircleWithVirtualCloneLogic& src){}
        std::string GetTypeName()const{return "Circle";}
        ShapeWithVirtualCloneLogic* do_clone()const{return new CircleWithVirtualCloneLogic(*this);}//Needed for clone_function_allocator_policy
};

void example_clone_function_allocator_policy_usage()
{
        smart_ptr<ShapeWithVirtualCloneLogic, ownership_default_policy, clone_function_allocator_policy> vShape1(new CircleWithVirtualCloneLogic);
        smart_ptr<ShapeWithVirtualCloneLogic, ownership_default_policy, clone_function_allocator_policy> vShape2(vShape1);
        std::cout << vShape2->GetTypeName() << std::cout;
}

Note:
This policy is least capable of detecting slicing. Slicing can only be detected on runtime when cloning is performed. This policy also requires more maintenance, because each derived class has to implement the cloning function. Because of this cloning function requirement, this policy is less generic than the allocator_default_policy
See also:
smart_ptr, allocator_default_policy, clone_static_function_allocator_policy

Definition at line 165 of file smart_ptr.hpp.

Static Public Member Functions

template<typename T_obj>
static T_obj * allocate (const T_obj *ptr)
 allocate clones the input argument by calling virtual function do_clone.
template<typename T_obj>
static void deallocate (T_obj *ptr)


Member Function Documentation

static T_obj* allocate const T_obj *  ptr  )  [inline, static]
 

allocate clones the input argument by calling virtual function do_clone.

Precondition:
Base type requires do_clone virtual function, and pointee type requires do_clone implementation
Warning:
warning.gif slicing can occur if derived derived type fails to implement do_clone
Parameters:
[in] ptr valid pointer or NULL
Returns:
A new object, or NULL if input parameter is NULL

Definition at line 176 of file smart_ptr.hpp.

static void deallocate T_obj *  ptr  )  [inline, static]
 

Parameters:
[in] ptr valid pointer or NULL

Definition at line 184 of file smart_ptr.hpp.


The documentation for this struct was generated from the following file:
Generated on Wed Mar 29 21:58:59 2006 for Smart Pointers by  doxygen 1.4.6.Axter [Axter-Extended-Version]