Chris@16
|
1 //
|
Chris@16
|
2 // Copyright (c) 2010 Artyom Beilis (Tonkikh)
|
Chris@16
|
3 //
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
5 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 //
|
Chris@16
|
8 #ifndef BOOST_LOCALE_HOLD_PTR_H
|
Chris@16
|
9 #define BOOST_LOCALE_HOLD_PTR_H
|
Chris@16
|
10
|
Chris@16
|
11 namespace boost {
|
Chris@16
|
12 namespace locale {
|
Chris@16
|
13 ///
|
Chris@16
|
14 /// \brief a smart pointer similar to std::auto_ptr but it is non-copyable and the
|
Chris@16
|
15 /// underlying object has the same constness as the pointer itself (unlike an ordinary pointer).
|
Chris@16
|
16 ///
|
Chris@16
|
17 template<typename T>
|
Chris@16
|
18 class hold_ptr {
|
Chris@16
|
19 hold_ptr(hold_ptr const &other); // non copyable
|
Chris@16
|
20 hold_ptr const &operator=(hold_ptr const &other); // non assignable
|
Chris@16
|
21 public:
|
Chris@16
|
22 ///
|
Chris@16
|
23 /// Create new empty pointer
|
Chris@16
|
24 ///
|
Chris@16
|
25 hold_ptr() : ptr_(0) {}
|
Chris@16
|
26 ///
|
Chris@16
|
27 /// Create a pointer that holds \a v, ownership is transferred to smart pointer
|
Chris@16
|
28 ///
|
Chris@16
|
29 explicit hold_ptr(T *v) : ptr_(v) {}
|
Chris@16
|
30
|
Chris@16
|
31 ///
|
Chris@16
|
32 /// Destroy smart pointer and the object it owns.
|
Chris@16
|
33 ///
|
Chris@16
|
34 ~hold_ptr()
|
Chris@16
|
35 {
|
Chris@16
|
36 delete ptr_;
|
Chris@16
|
37 }
|
Chris@16
|
38
|
Chris@16
|
39 ///
|
Chris@16
|
40 /// Get a const pointer to the object
|
Chris@16
|
41 ///
|
Chris@16
|
42 T const *get() const { return ptr_; }
|
Chris@16
|
43 ///
|
Chris@16
|
44 /// Get a mutable pointer to the object
|
Chris@16
|
45 ///
|
Chris@16
|
46 T *get() { return ptr_; }
|
Chris@16
|
47
|
Chris@16
|
48 ///
|
Chris@16
|
49 /// Get a const reference to the object
|
Chris@16
|
50 ///
|
Chris@16
|
51 T const &operator *() const { return *ptr_; }
|
Chris@16
|
52 ///
|
Chris@16
|
53 /// Get a mutable reference to the object
|
Chris@16
|
54 ///
|
Chris@16
|
55 T &operator *() { return *ptr_; }
|
Chris@16
|
56 ///
|
Chris@16
|
57 /// Get a const pointer to the object
|
Chris@16
|
58 ///
|
Chris@16
|
59 T const *operator->() const { return ptr_; }
|
Chris@16
|
60 ///
|
Chris@16
|
61 /// Get a mutable pointer to the object
|
Chris@16
|
62 ///
|
Chris@16
|
63 T *operator->() { return ptr_; }
|
Chris@16
|
64
|
Chris@16
|
65 ///
|
Chris@16
|
66 /// Transfer an ownership on the pointer to user
|
Chris@16
|
67 ///
|
Chris@16
|
68 T *release() { T *tmp=ptr_; ptr_=0; return tmp; }
|
Chris@16
|
69
|
Chris@16
|
70 ///
|
Chris@16
|
71 /// Set new value to pointer, previous object is destroyed, ownership on new object is transferred
|
Chris@16
|
72 ///
|
Chris@16
|
73 void reset(T *p=0)
|
Chris@16
|
74 {
|
Chris@16
|
75 if(ptr_) delete ptr_;
|
Chris@16
|
76 ptr_=p;
|
Chris@16
|
77 }
|
Chris@16
|
78 /// Swap two pointers
|
Chris@16
|
79 void swap(hold_ptr &other)
|
Chris@16
|
80 {
|
Chris@16
|
81 T *tmp=other.ptr_;
|
Chris@16
|
82 other.ptr_=ptr_;
|
Chris@16
|
83 ptr_=tmp;
|
Chris@16
|
84 }
|
Chris@16
|
85 private:
|
Chris@16
|
86 T *ptr_;
|
Chris@16
|
87 };
|
Chris@16
|
88
|
Chris@16
|
89 } // locale
|
Chris@16
|
90 } // boost
|
Chris@16
|
91
|
Chris@16
|
92 #endif
|
Chris@16
|
93 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
|