|
Boost : |
From: Powell, Gary (powellg_at_[hidden])
Date: 2003-09-29 17:29:52
Powell > I'm a little concerned that it appears you can create bad pointers for
Powell > this class by inadvertently not using placement new. I'd feel better
Powell > about it if there was some way to prevent this.
Abrahams >Generic smart pointer factories are the right way:
Abrahams > shifted_ptr<U> p = make_shifted_ptr<T>(arg1, arg2, arg3, ref(arg4));
So you are saying remove the constructor,
template<class U>
shifted_ptr( U *);
and replace it with one that takes a temporary that is created only by make_shifted_ptr?
template<class U>
shifted_ptr( shifted_ptr::details::hold_ptr<U> &);
and add the following pseudo code:
namespace shifted_ptr {
namespace details {
template <class T>
class hold_ptr
{
private:
T *m_ptr;
public:
T * release() { // hand it off to a shifted_ptr
T * result = m_ptr;
m_ptr = 0;
return result;
}
hold_ptr(T *rhs)
: m_ptr(rhs) {}
~hold_ptr() { // fail to hand it off to a shifted_ptr.
if (m_ptr) {
~(*m_ptr)(); // destruct it.
m_ptr = 0;
}
}
};
} // details
template<class T, class MemoryModel>
hold_ptr<T> make_shifted_ptr(MemoryModel mm);
template<class T, class Arg1, class MemoryModel>
hold_ptr<T> make_shifted_ptr(class Arg1, mm);
.....
} // shifted_ptr
You may have to pass the memory model into the hold_ptr class to know what the right thing to do destruct a lost ptr, I haven't fully thought this through. Anyway it seems a solvable problem with limited overhead and better validation that the pointers held by shifted_ptr are of the right type.
-Gary-
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk