Listing 1: The original smart_ptr initialization mechanism.
ref_counted::ref_counted() {
pCount_ = static_cast<unsigned int*>(
SmallObject<>::operator new(sizeof(unsigned int)));
assert(pCount_);
*pCount_ = 1;
}
bool ref_counted::release(const P&) {
if (!--*pCount_) {
SmallObject<>::operator delete(pCount_, sizeof(unsigned int));
return true;
}
return false;
}
class smart_ptr
: public storage_policy<T>
, public ownership_policy<typename storage_policy<T>::PointerType>
, public checking_policy<typename storage_policy<T>::stored_type>
, public conversion_policy
{ ... };
smart_ptr::smart_ptr(const stored_type& p) : SP(p)
{ KP::OnInit(GetImpl(*this)); }
smart_ptr::~smart_ptr() {
if (OP::release(GetImpl(*static_cast<SP*>(this)))) {
SP::Destroy();
}
}