Boost.Locale
hold_ptr.hpp
1 //
2 // Copyright (c) 2010 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // https://www.boost.org/LICENSE_1_0.txt
6 
7 #ifndef BOOST_LOCALE_HOLD_PTR_H
8 #define BOOST_LOCALE_HOLD_PTR_H
9 
10 #include <boost/locale/config.hpp>
11 
12 namespace boost {
13 namespace locale {
18  template<typename T>
19  class hold_ptr {
20  hold_ptr(hold_ptr const &other); // non copyable
21  hold_ptr const &operator=(hold_ptr const &other); // non assignable
22  public:
26  hold_ptr() : ptr_(0) {}
30  explicit hold_ptr(T *v) : ptr_(v) {}
31 
36  {
37  delete ptr_;
38  }
39 
43  T const *get() const { return ptr_; }
47  T *get() { return ptr_; }
48 
52  T const &operator *() const { return *ptr_; }
56  T &operator *() { return *ptr_; }
60  T const *operator->() const { return ptr_; }
64  T *operator->() { return ptr_; }
65 
69  T *release() { T *tmp=ptr_; ptr_=0; return tmp; }
70 
74  void reset(T *p=0)
75  {
76  if(ptr_) delete ptr_;
77  ptr_=p;
78  }
80  void swap(hold_ptr &other)
81  {
82  T *tmp=other.ptr_;
83  other.ptr_=ptr_;
84  ptr_=tmp;
85  }
86  private:
87  T *ptr_;
88  };
89 
90 } // locale
91 } // boost
92 
93 #endif
hold_ptr()
Definition: hold_ptr.hpp:26
T const * operator->() const
Definition: hold_ptr.hpp:60
T * get()
Definition: hold_ptr.hpp:47
T const & operator *() const
Definition: hold_ptr.hpp:52
T const * get() const
Definition: hold_ptr.hpp:43
hold_ptr(T *v)
Definition: hold_ptr.hpp:30
a smart pointer similar to std::auto_ptr but it is non-copyable and the underlying object has the sam...
Definition: hold_ptr.hpp:19
~hold_ptr()
Definition: hold_ptr.hpp:35
void reset(T *p=0)
Definition: hold_ptr.hpp:74
T & operator *()
Definition: hold_ptr.hpp:56
T * release()
Definition: hold_ptr.hpp:69
void swap(hold_ptr &other)
Swap two pointers.
Definition: hold_ptr.hpp:80
T * operator->()
Definition: hold_ptr.hpp:64