1

I have the following implementation of a clone_ptr in an attempt to make safe copies of object pointers that need to be copied in a class, so instead of using the copy constructor, I was adviced to use smart pointers and create a clone pointer.

Clone_ptr implementation:

#include <algorithm>
#include <functional>
#include <xercesc/dom/DOM.hpp>

struct DOMImplementation_cloner
{
    template <typename T>
    T* operator()(T* pPtr) const
    {
        /* your clone code*/.
        T = DOMImplementationRegistry::getDOMImplementation(X("Core"));
    }
};

struct default_clone
{
    template <typename T>
    T* operator()(T* pPtr) const
    {
        return pPtr->clone();
    }
};

template <typename T, typename Cloner = default_clone>
    class clone_ptr
    {
    public:
        // types
        typedef T element_type;

        typedef element_type value_type;
        typedef const element_type const_value_type;
        typedef value_type* pointer;
        typedef const_value_type* const_pointer;
        typedef value_type& reference;
        typedef const_value_type& const_reference;

        // creation
        clone_ptr() :
        mPtr(0)
        {}

        explicit clone_ptr(pointer pPtr) :
        mPtr(pPtr)
        {}


        clone_ptr(const clone_ptr& pOther) :
        mPtr(pOther.get() ? mCloner(pOther.get()) : 0)
        {}

      /*  clone_ptr(const clone_ptr& pOther) :
        mPtr(pOther.get() ? pOther->clone() : 0),
        {}*/



        clone_ptr& operator=(clone_ptr pOther)
        {
            swap(*this, pOther);

            return *this;
        }

        ~clone_ptr()
        {
            delete get();
        }

        // observers
        pointer get() const
        {
            return mPtr;
        }

        pointer operator->() const
        {
            return get();
        }

        reference operator*() const
        {
            assert(get() != 0);
            return *get();
        }

        // modifiers
        pointer release()
        {
            pointer result = mPtr;
            mPtr = 0;

            return result;
        }

        void reset(pointer pPtr = 0)
        {
            *this = clone_ptr(pPtr);
        }

        // utility
        friend void swap(clone_ptr& pFirst, clone_ptr& pSecond)
        {
            std::swap(pFirst.mPtr, pSecond.mPtr);
        }


        /////////////////////


    // compare
    template <typename T1, typename T2>
    friend bool operator==(const clone_ptr<T1>& pFirst, const clone_ptr<T2>& pSecond)
    {
        return pFirst.get() == pSecond.get();
    }

    template <typename T1, typename T2>
    friend bool operator!=(const clone_ptr<T1>& pFirst, const clone_ptr<T2>& pSecond)
    {
        return !(pFirst == pSecond);
    }

    template <typename T1, typename T2>
    friend bool operator<(const clone_ptr<T1>& pFirst, const clone_ptr<T2>& pSecond)
    {
        return std::less<void*>()(pFirst.get(), pSecond.get());
    }

    template <typename T1, typename T2>
    friend bool operator<=(const clone_ptr<T1>& pFirst, const clone_ptr<T2>& pSecond)
    {
        return !(pFirst > pSecond);
    }

    template <typename T1, typename T2>
    friend bool operator>(const clone_ptr<T1>& pFirst, const clone_ptr<T2>& pSecond)
    {
        return pSecond < pFirst;
    }

    template <typename T1, typename T2>
    friend bool operator>=(const clone_ptr<T1>& pFirst, const clone_ptr<T2>& pSecond)
    {
        return !(pFirst < pSecond);
    }

    template <typename T1>
    friend bool operator!(const clone_ptr<T1>& pX)
    {
        return pX.get() == 0;
    }


    private:
        pointer mPtr;
        default_clone mCloner;
    };

/// Use of a xerces pointer so it can be copied/cloned safely

 private class member:
        clone_ptr<DOMImplementation, DOMImplementation_cloner> impl;

//compiler error:
 error C2039: 'clone' : is not a member of 'xercesc_3_1::DOMImplementation'

I don't understand why it is not using the DOMImplementation_cloner and tries to use the default_clone instead?

Can someone clarify as to what I'm doing wrong?

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415

2 Answers2

2

The type of the cloner is hard-coded to default_clone, instead of using the template parameter Cloner (see the last line of your class definition).

Edit Just to make sure you understand, your definition of mCloner should look like this:

Cloner mCloner;

This way the cloner will actually be of the type given by the template parameter.

One more thing. If you expect you clone_ptr to be used in a more general setting (e.g. by coworkers on other projects), you should make the type of the cloner a property of the type T. This can be done using type traits (this answer gives an example). This could look like this in your clone_ptr:

template< typename T, typename Cloner = cloner_traits< T >::cloner_type >
class clone_ptr {
    // ...
};

This would have the same default behavior as your current implementation. The advantage is that classes that require a special cloner would just specialize the cloner_traits template and the user of the class does not have to worry about choosing the appropriate cloner. If you still want to override the cloner for any class, you can still pass the cloner manually.

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
2

I recommend using clone_ptr class in following link: http://www.codeproject.com/KB/stl/clone_ptr.aspx

The above type of clone pointer does not need to have a clone function. There are other advantages to this type of clone pointer class, over above type implementation.

See article for more details.