cse332: Laboratory Exercise 9
Using and verifying Smart Pointer class (also known as a handle or counted pointer class).
Due: During your lab session on Friday 31 March.


For this lab assignment you are to take the Handle class from lab 8 and extend it to support compile time policies and ordering comparisons.

Compile time policies

You are to add a second template parameter that specifies the ownership policy of the contained pointer. You are then to define two different policies: 1) reference counted and 2) deep copy. The reference counted policy is simply the behavior of the current handler class. However, the deep copy policy must allocate and initialize a new object when performing any copy operation (assignment or copy construction). You will most likely find it necessary to use template parameters such as the following
template <class T> class OPolicy
{
  private:
    T *ptr_;
  protected:
    OPolicy();
    OPolicy(T *);
    OPolicy(const T &);
    OPolicy(const OPolicy &)
    virtual ~OPolicy() {}
    ... // define any other required methods
};

template <class T, template <class> class OP=OPolicy> class Handle
 : private OP<T>
{
  public:
    Handle();
    explicit Handle(T *ptr);
    explicit Handle(const T &obj);
    Handle(const Handle& h);
    Handle(T *ptr) : OP<T>(ptr) {std::cout << "Handle()\n";}
    virtual ~Handle() {std::cout << "~Handle()\n";}
    ... // define other required methods 
};

Your solution must handle two distinct cases:

  1. The Handle class references a base class (static type) but the actual object is a derived class (dynamic type). In this case to prevent slicing you will want the derived class to implement the clone() member method (ptr->clone()).
  2. The static and dynamic types of the object referenced by the handle class is the same. In this case using the new operator and copy constructor is sufficient (new T(*ptr)).
I suggest using traits which are specialized for classes supporting the clone method:
template  class PolicyTraits {
  public:
    static T* clone(const T *ptr) {return new T(*ptr);}
};
template <> class PolicyTraits<Clonable> {
  public:
    static Clonable* clone(const Clonable *ptr) {return ptr->clone();}
};

Equality comparable

We want the handler class to useful in situations where pointers are used. In particular if we have two handle objects we should be able to conveniently determine if they refer to the same object or not. For example,

Handle ptr(new int);
Handle ptr2 = ptr;

if (ptr == ptr2)
  cout << "Same\n";
else
  cout << "Different\n";
You must define the equality operator and inequality operators.

Testing

Update your existing test code to account for the updated class. provide a class later in the week which you must use in your testing. The test program submitted with your lab must correctly handle situations where the static and dynamic pointer types differ.

References

For an overview of smart pointers see the book chapter by Andrei Alexandrescu. You may also find the smart pointers url at boost.org interesting.

How to turn in:

Submit via email.