ACE Value_Ptr的用法
来源:互联网 发布:网络广播设备 编辑:程序博客网 时间:2024/06/05 08:37
ACE的Value_Ptr和auto_ptr基本一致,不过就是多实现了一个clone的方法。使用Value_Ptr不需要有太多的注意,只要知道这个Smart pointer只能Swap数据,不能Share数据。
/*** @struct VP_traits
*
* @brief @c Value_Ptr traits template structure.
*
* The @c Value_Ptr template class delegates some operations to this
* template traits structure.
*
* Specialize this trait template if cloning through copy
* construction is not sufficient. For example, to avoid slicing
* when copying an object through a base class pointer, one can
* implement a virtual "clone" method that can be used to
* polymorphically invoke the appropriate cloning operation(s).
* That virtual method would then be invoked by the @c VP_traits/</>
* specialization.
*/
template <typename T>
struct VP_traits
{
/// Copy the given object.
static T * clone (T const * p) { return new T (*p); }
};
/**
* @class Value_Ptr
*
* @brief Smart pointer implementation designed for use as a class
* member.
*
* Using a @c std::auto_ptr/</> as a class member is sometimes
* problematic since ownership of memory is transferred when copying
* such members. This @c Value_Ptr class is explicitly designed to
* avoid such problems by performing copies of the underlying object
* rather than transfer ownership. This, for example, allows it to
* be readily used as a member in classes placed inside STL
* containers.
*
* @see Item 31 in "More Exceptional C++" by Herb Sutter.
*/
template <typename T>
class Value_Ptr
{
public:
/// Constructor.
explicit Value_Ptr (T * p = 0) : p_ (p) { }
/// Destructor.
~Value_Ptr (void) { delete this->p_; }
/// Deference operator.
T & operator* (void) const { return *this->p_; }
/// Pointer operator.
T * operator-> (void) const { return this->p_; }
/// Non-throwing swap operation used to make assignment strongly
/// exception-safe.
/**
* @note As implemented, the swap operation may not work correctly
* for @c auto_ptr/</>s, but why would one use an @c
* auto_ptr/</> as the template argument for this particular
* template class!?
*/
void swap (Value_Ptr & other) { std::swap (this->p_, other.p_); }
/// Copy constructor.
Value_Ptr (Value_Ptr const & other)
: p_ (create_from (other.p_)) { }
/// Assignment operator.
Value_Ptr & operator= (Value_Ptr const & other)
{
// Strongly exception-safe.
Value_Ptr temp (other);
this->swap (temp);
return *this;
}
#ifndef ACE_LACKS_MEMBER_TEMPLATES
// Compiler can't handle member templates so we lose converting
// copy operations.
/// Converting copy constructor.
template <typename U>
Value_Ptr (Value_Ptr<U> const & other)
: p_ (create_from (other.p_)) { }
/// Converting assignment operator.
template <typename U>
Value_Ptr & operator= (Value_Ptr<U> const & other)
{
// Strongly exception-safe.
Value_Ptr temp (other);
this->swap (temp);
return *this;
}
#endif /* !ACE_LACKS_MEMBER_TEMPLATES */
private:
#ifndef ACE_LACKS_MEMBER_TEMPLATES
/// Copying method invoked when copy constructing.
template <typename U>
T * create_from (U const * p) const
{
return p ? VP_traits<U>::clone (p) : 0;
}
#else
// Compiler can't handle member templates so we lose converting
// copy operations.
/// Copying method invoked when copy constructing.
T * create_from (T const * p) const
{
return p ? VP_traits<T>::clone (p) : 0;
}
#endif /* !ACE_LACKS_MEMBER_TEMPLATES */
private:
#ifndef ACE_LACKS_MEMBER_TEMPLATES
template <typename U> friend class Value_Ptr;
#endif /* !ACE_LACKS_MEMBER_TEMPLATES */
/// Object owned by this @c Value_Ptr.
T * p_;
};
}
- ACE Value_Ptr的用法
- ACE守护的用法
- ACE的Reactor用法
- ACE 基本的 TCP/IP Socket 用法
- ACE-ACE的安装
- ACE有界栈用法
- ACE学习笔记一(基本的TCP/IP Socket用法)
- ACE双向链表用法
- ACE的安装,输出hello ace
- ACE学习一:ACE的体系结构
- 关于ACE 6.0.0新版本对APG一书中ACE_static_case宏用法的补充
- ACE的一个例子
- ACE的文件操作
- ACE 的文件操作
- ACE的日志入门
- ACE初始化的惆怅
- ACE的读写锁
- ACE库的备忘
- 转载:计算几何算法(含源代码)
- VclSkin使用教程
- 使用 dom4j 解析 XML
- c#基础知识学习--属性(一)
- VclSkin使用教程
- ACE Value_Ptr的用法
- 将Java应用程序安装成Windows的服务
- windows中的快捷键
- 手机预设指令集
- 实用debug命令
- 8大 DOS网络命令
- Business Process
- Log4cxx中多用户的解决方法之一
- CSDN,我来了!