Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Value_Ptr.h
blob220184be263864380a59fdf1e7f2616b3d96c426
1 // -*- C++ -*-
3 //==========================================================================
4 /**
5 * @file Value_Ptr.h
7 * Value_Ptr implementation based on code in Herb Sutter's book "More
8 * Exceptional C++".
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"
19 #include <algorithm>
21 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
23 namespace ACE
25 /**
26 * @struct VP_traits
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\<\>
39 * specialization.
41 template <typename T>
42 struct VP_traits
44 /// Copy the given object.
45 static T * clone (T const * p) { return new T (*p); }
48 /**
49 * @class Value_Ptr
51 * @brief Smart pointer implementation designed for use as a class
52 * member.
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
60 * containers.
62 * @see Item 31 in "More Exceptional C++" by Herb Sutter.
64 template <typename T>
65 class Value_Ptr
67 public:
68 /// Constructor.
69 explicit Value_Ptr (T * p = 0) : p_ (p) {}
71 /// Destructor.
72 ~Value_Ptr () { delete this->p_; }
74 /// Deference operator.
75 T & operator* () const { return *this->p_; }
77 /// Pointer operator.
78 T * operator-> () const { return this->p_; }
80 /// Non-throwing swap operation used to make assignment strongly
81 /// exception-safe.
82 /**
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
86 * template class!?
88 void swap (Value_Ptr & other) { std::swap (this->p_, other.p_); }
90 /// Copy constructor.
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);
99 this->swap (temp);
100 return *this;
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);
114 this->swap (temp);
115 return *this;
118 private:
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;
126 private:
127 template <typename U> friend class Value_Ptr;
129 /// Object owned by this @c Value_Ptr.
130 T * p_;
134 ACE_END_VERSIONED_NAMESPACE_DECL
136 #endif /* ACE_VALUE_PTR_H */