📄 alexandr.txt
字号:
Generic <Programming>
by Andrei Alexandrescu and David B. Held
Listing 1:
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();
}
}
Listing 2: The resource_manager class in action
template <class storage_policy, class ownership_policy>
class resource_manager
: public optimally_inherit<storage_policy, ownership_policy>::type
{
...
~resource_manager() {
if (!ownership_policy::release(get_impl(*this))) {
storage_policy::release();
}
}
};
template <
typename T,
template <typename> class ownership_policy = ref_counted,
template <typename> class conversion_policy = disallow_conversion,
template <typename> class checking_policy = assert_check,
template <typename> class storage_policy = scalar_storage
>
class smart_ptr
: public optimally_inherit<
resource_manager<
storage_policy<T>,
ownership_policy<T>
>,
optimally_inherit<
checking_policy<T>,
conversion_policy<T>
>
>
{
...
};
2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -