Chris@16: // Chris@16: // Copyright (c) 2010 Artyom Beilis (Tonkikh) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: #ifndef BOOST_LOCALE_HOLD_PTR_H Chris@16: #define BOOST_LOCALE_HOLD_PTR_H Chris@16: Chris@16: namespace boost { Chris@16: namespace locale { Chris@16: /// Chris@16: /// \brief a smart pointer similar to std::auto_ptr but it is non-copyable and the Chris@16: /// underlying object has the same constness as the pointer itself (unlike an ordinary pointer). Chris@16: /// Chris@16: template Chris@16: class hold_ptr { Chris@16: hold_ptr(hold_ptr const &other); // non copyable Chris@16: hold_ptr const &operator=(hold_ptr const &other); // non assignable Chris@16: public: Chris@16: /// Chris@16: /// Create new empty pointer Chris@16: /// Chris@16: hold_ptr() : ptr_(0) {} Chris@16: /// Chris@16: /// Create a pointer that holds \a v, ownership is transferred to smart pointer Chris@16: /// Chris@16: explicit hold_ptr(T *v) : ptr_(v) {} Chris@16: Chris@16: /// Chris@16: /// Destroy smart pointer and the object it owns. Chris@16: /// Chris@16: ~hold_ptr() Chris@16: { Chris@16: delete ptr_; Chris@16: } Chris@16: Chris@16: /// Chris@16: /// Get a const pointer to the object Chris@16: /// Chris@16: T const *get() const { return ptr_; } Chris@16: /// Chris@16: /// Get a mutable pointer to the object Chris@16: /// Chris@16: T *get() { return ptr_; } Chris@16: Chris@16: /// Chris@16: /// Get a const reference to the object Chris@16: /// Chris@16: T const &operator *() const { return *ptr_; } Chris@16: /// Chris@16: /// Get a mutable reference to the object Chris@16: /// Chris@16: T &operator *() { return *ptr_; } Chris@16: /// Chris@16: /// Get a const pointer to the object Chris@16: /// Chris@16: T const *operator->() const { return ptr_; } Chris@16: /// Chris@16: /// Get a mutable pointer to the object Chris@16: /// Chris@16: T *operator->() { return ptr_; } Chris@16: Chris@16: /// Chris@16: /// Transfer an ownership on the pointer to user Chris@16: /// Chris@16: T *release() { T *tmp=ptr_; ptr_=0; return tmp; } Chris@16: Chris@16: /// Chris@16: /// Set new value to pointer, previous object is destroyed, ownership on new object is transferred Chris@16: /// Chris@16: void reset(T *p=0) Chris@16: { Chris@16: if(ptr_) delete ptr_; Chris@16: ptr_=p; Chris@16: } Chris@16: /// Swap two pointers Chris@16: void swap(hold_ptr &other) Chris@16: { Chris@16: T *tmp=other.ptr_; Chris@16: other.ptr_=ptr_; Chris@16: ptr_=tmp; Chris@16: } Chris@16: private: Chris@16: T *ptr_; Chris@16: }; Chris@16: Chris@16: } // locale Chris@16: } // boost Chris@16: Chris@16: #endif Chris@16: // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4