diff options
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | ace/CDR_Base.h | 2 | ||||
-rw-r--r-- | ace/Value_Ptr.h | 127 |
3 files changed, 140 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog index 233ea83a640..5f02f1f1b3c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +Wed Apr 6 13:41:13 2005 Ossama Othman <ossama@dre.vanderbilt.edu> + + * ace/CDR_Base.h: + + Corrected comment. + + * ace/Value_Ptr.h: + + Value_Ptr implementation based on code in Herb Sutter's book + "More Exceptional C++". It is a smart pointer implementation + designed for use as a class member. + Wed Apr 6 19:25:12 UTC 2005 Johnny Willemsen <jwillemsen@remedy.nl> * ace/Sock_Connect.cpp: diff --git a/ace/CDR_Base.h b/ace/CDR_Base.h index 32c003a9284..c8f500192c4 100644 --- a/ace/CDR_Base.h +++ b/ace/CDR_Base.h @@ -43,7 +43,7 @@ // MSVC++ 6 can't handle partial template specializations so fall // back on an unsigned char typedef. # include "ace/If_Then_Else.h" -#endif /* _MSC_VER < 1310 */ +#endif /* !_MSC_VER || _MSC_VER >= 1310 */ class ACE_Message_Block; diff --git a/ace/Value_Ptr.h b/ace/Value_Ptr.h new file mode 100644 index 00000000000..95f0d465ce0 --- /dev/null +++ b/ace/Value_Ptr.h @@ -0,0 +1,127 @@ +// -*- C++ -*- + +//========================================================================== +/** + * @file ValuePtr.h + * + * $Id$ + * + * Value_Ptr implementation based on code in Herb Sutter's book "More + * Exceptional C++". + * + * @author Ossama Othman <ossama@dre.vanderbilt.edu> + */ +//========================================================================== + +#ifndef ACE_VALUE_PTR_H +#define ACE_VALUE_PTR_H + +#include <algorithm> + +namespace ACE +{ + /** + * @struct VP_traits + * + * @brief @c Value_Ptr traits template structure. + * + * The @c Value_Ptr template class delegates some operations to this + * template traits structure. + */ + 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 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) + { + Value_Ptr temp (other); + this->swap (temp); + return *this; + } + + /// 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) + { + Value_Ptr temp (other); + this->swap (temp); + return *this; + } + + private: + + /// Copying method invoked when copy constructing. + template <typename U> + T * create_from (U const * p) const + { + return p ? VP_traits<U>::clone (p) : 0; + } + + private: + + template <typename U> friend class Value_Ptr; + + /// Object owned by this @c Value_Ptr. + T * p_; + + }; + +} + +#endif /* ACE_VALUE_PTR_H */ |