Boost.Locale
hold_ptr.hpp
1 //
2 // Copyright (c) 2010 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_LOCALE_HOLD_PTR_H
9 #define BOOST_LOCALE_HOLD_PTR_H
10 
11 namespace boost {
12 namespace locale {
17  template<typename T>
18  class hold_ptr {
19  hold_ptr(hold_ptr const &other); // non copyable
20  hold_ptr const &operator=(hold_ptr const &other); // non assignable
21  public:
25  hold_ptr() : ptr_(0) {}
29  explicit hold_ptr(T *v) : ptr_(v) {}
30 
35  {
36  delete ptr_;
37  }
38 
42  T const *get() const { return ptr_; }
46  T *get() { return ptr_; }
47 
51  T const &operator *() const { return *ptr_; }
55  T &operator *() { return *ptr_; }
59  T const *operator->() const { return ptr_; }
63  T *operator->() { return ptr_; }
64 
68  T *release() { T *tmp=ptr_; ptr_=0; return tmp; }
69 
73  void reset(T *p=0)
74  {
75  if(ptr_) delete ptr_;
76  ptr_=p;
77  }
79  void swap(hold_ptr &other)
80  {
81  T *tmp=other.ptr_;
82  other.ptr_=ptr_;
83  ptr_=tmp;
84  }
85  private:
86  T *ptr_;
87  };
88 
89 } // locale
90 } // boost
91 
92 #endif
93 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
hold_ptr()
Definition: hold_ptr.hpp:25
T const & operator*() const
Definition: hold_ptr.hpp:51
hold_ptr(T *v)
Definition: hold_ptr.hpp:29
a smart pointer similar to std::auto_ptr but it is non-copyable and the underlying object has the sam...
Definition: hold_ptr.hpp:18
T const * operator->() const
Definition: hold_ptr.hpp:59
~hold_ptr()
Definition: hold_ptr.hpp:34
void reset(T *p=0)
Definition: hold_ptr.hpp:73
T * release()
Definition: hold_ptr.hpp:68
void swap(hold_ptr &other)
Swap two pointers.
Definition: hold_ptr.hpp:79
T * operator->()
Definition: hold_ptr.hpp:63