3 //==========================================================================
7 * Value_Ptr implementation based on code in Herb Sutter's book "More
10 * @author Ossama Othman <ossama@dre.vanderbilt.edu>
12 //==========================================================================
14 #ifndef ACE_VALUE_PTR_H
15 #define ACE_VALUE_PTR_H
17 #include /**/ "ace/config-lite.h"
21 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
28 * @brief @c Value_Ptr traits template structure.
30 * The @c Value_Ptr template class delegates some operations to this
31 * template traits structure.
33 * Specialize this trait template if cloning through copy
34 * construction is not sufficient. For example, to avoid slicing
35 * when copying an object through a base class pointer, one can
36 * implement a virtual "clone" method that can be used to
37 * polymorphically invoke the appropriate cloning operation(s).
38 * That virtual method would then be invoked by the @c VP_traits\<\>
44 /// Copy the given object.
45 static T
* clone (T
const * p
) { return new T (*p
); }
51 * @brief Smart pointer implementation designed for use as a class
54 * Using a @c std::auto_ptr\<\> as a class member is sometimes
55 * problematic since ownership of memory is transferred when copying
56 * such members. This @c Value_Ptr class is explicitly designed to
57 * avoid such problems by performing copies of the underlying object
58 * rather than transfer ownership. This, for example, allows it to
59 * be readily used as a member in classes placed inside STL
62 * @see Item 31 in "More Exceptional C++" by Herb Sutter.
69 explicit Value_Ptr (T
* p
= 0) : p_ (p
) {}
72 ~Value_Ptr () { delete this->p_
; }
74 /// Deference operator.
75 T
& operator* () const { return *this->p_
; }
78 T
* operator-> () const { return this->p_
; }
80 /// Non-throwing swap operation used to make assignment strongly
83 * @note As implemented, the swap operation may not work correctly
84 * for @c auto_ptr\<\>s, but why would one use an @c
85 * auto_ptr\<\> as the template argument for this particular
88 void swap (Value_Ptr
& other
) { std::swap (this->p_
, other
.p_
); }
91 Value_Ptr (Value_Ptr
const & other
)
92 : p_ (create_from (other
.p_
)) { }
94 /// Assignment operator.
95 Value_Ptr
& operator= (Value_Ptr
const & other
)
97 // Strongly exception-safe.
98 Value_Ptr
temp (other
);
103 /// Converting copy constructor.
104 template <typename U
>
105 Value_Ptr (Value_Ptr
<U
> const & other
)
106 : p_ (create_from (other
.p_
)) { }
108 /// Converting assignment operator.
109 template <typename U
>
110 Value_Ptr
& operator= (Value_Ptr
<U
> const & other
)
112 // Strongly exception-safe.
113 Value_Ptr
temp (other
);
119 /// Copying method invoked when copy constructing.
120 template <typename U
>
121 T
* create_from (U
const * p
) const
123 return p
? VP_traits
<U
>::clone (p
) : 0;
127 template <typename U
> friend class Value_Ptr
;
129 /// Object owned by this @c Value_Ptr.
134 ACE_END_VERSIONED_NAMESPACE_DECL
136 #endif /* ACE_VALUE_PTR_H */