]>
Commit | Line | Data |
---|---|---|
23324ae1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
664e1314 | 2 | // Name: sharedptr.h |
e54c96f1 | 3 | // Purpose: interface of wxSharedPtr<T> |
23324ae1 | 4 | // Author: wxWidgets team |
526954c5 | 5 | // Licence: wxWindows licence |
23324ae1 FM |
6 | ///////////////////////////////////////////////////////////////////////////// |
7 | ||
8 | /** | |
664e1314 | 9 | A smart pointer with non-intrusive reference counting. |
7c913512 | 10 | |
f090e4ef | 11 | It is modelled after @c boost::shared_ptr<> and can be used with STL |
664e1314 | 12 | containers and wxVector<T> unlike @c std::auto_ptr<> and wxScopedPtr<T>. |
7c913512 | 13 | |
23324ae1 | 14 | @library{wxbase} |
9fa8c9bf | 15 | @category{smartpointers} |
7c913512 | 16 | |
73473b3e | 17 | @see wxScopedPtr<T>, wxWeakRef<T>, wxObjectDataPtr<T> |
23324ae1 | 18 | */ |
9fa8c9bf | 19 | template<typename T> |
7c913512 | 20 | class wxSharedPtr<T> |
23324ae1 FM |
21 | { |
22 | public: | |
23324ae1 | 23 | /** |
9fa8c9bf VS |
24 | Constructor. |
25 | ||
26 | Creates shared pointer from the raw pointer @a ptr and takes ownership | |
27 | of it. | |
23324ae1 | 28 | */ |
b1b95a65 | 29 | wxEXPLICIT wxSharedPtr(T* ptr = NULL); |
9fa8c9bf | 30 | |
4852df90 VS |
31 | /** |
32 | Constructor. | |
33 | ||
34 | Creates shared pointer from the raw pointer @a ptr and deleter @a d | |
35 | and takes ownership of it. | |
36 | ||
37 | @param ptr The raw pointer. | |
38 | @param d Deleter - a functor that is called instead of delete to | |
39 | free the @a ptr raw pointer when its reference count drops to | |
40 | zero. | |
41 | ||
42 | @since 3.0 | |
43 | */ | |
44 | template<typename Deleter> | |
45 | wxEXPLICIT wxSharedPtr(T* ptr, Deleter d); | |
46 | ||
b1b95a65 FM |
47 | /** |
48 | Copy constructor. | |
49 | */ | |
9fa8c9bf | 50 | wxSharedPtr(const wxSharedPtr<T>& tocopy); |
23324ae1 FM |
51 | |
52 | /** | |
53 | Destructor. | |
54 | */ | |
5cb947fd | 55 | ~wxSharedPtr(); |
23324ae1 FM |
56 | |
57 | /** | |
58 | Returns pointer to its object or @NULL. | |
59 | */ | |
328f5751 | 60 | T* get() const; |
23324ae1 FM |
61 | |
62 | /** | |
7c913512 | 63 | Conversion to a boolean expression (in a variant which is not |
664e1314 | 64 | convertible to anything but a boolean expression). |
b1b95a65 FM |
65 | |
66 | If this class contains a valid pointer it will return @true, if it contains | |
67 | a @NULL pointer it will return @false. | |
23324ae1 | 68 | */ |
328f5751 | 69 | operator unspecified_bool_type() const; |
23324ae1 FM |
70 | |
71 | /** | |
b1b95a65 FM |
72 | Returns a reference to the object. |
73 | ||
74 | If the internal pointer is @NULL this method will cause an assert in debug mode. | |
23324ae1 | 75 | */ |
328f5751 | 76 | T operator*() const; |
23324ae1 FM |
77 | |
78 | /** | |
37494cb3 RR |
79 | Smart pointer member access. Returns pointer to its object. |
80 | ||
81 | If the internal pointer is @NULL this method will cause an assert in debug mode. | |
23324ae1 | 82 | */ |
b1b95a65 | 83 | T* operator->() const; |
23324ae1 FM |
84 | |
85 | /** | |
b1b95a65 FM |
86 | Assignment operator. |
87 | ||
88 | Releases any previously held pointer and creates a reference to @a ptr. | |
23324ae1 | 89 | */ |
b1b95a65 | 90 | wxSharedPtr<T>& operator=(T* ptr); |
23324ae1 FM |
91 | |
92 | /** | |
b1b95a65 FM |
93 | Assignment operator. |
94 | ||
95 | Releases any previously held pointer and creates a reference to the | |
96 | same object as @a topcopy. | |
97 | */ | |
78e37b46 | 98 | wxSharedPtr<T>& operator=(const wxSharedPtr<T>& tocopy); |
b1b95a65 FM |
99 | |
100 | /** | |
101 | Reset pointer to @a ptr. | |
102 | ||
103 | If the reference count of the previously owned pointer was 1 it will be deleted. | |
23324ae1 | 104 | */ |
4cc4bfaf | 105 | void reset(T* ptr = NULL); |
23324ae1 | 106 | |
4852df90 VS |
107 | /** |
108 | Reset pointer to @a ptr. | |
109 | ||
110 | If the reference count of the previously owned pointer was 1 it will be deleted. | |
111 | ||
112 | @param ptr The new raw pointer. | |
113 | @param d Deleter - a functor that is called instead of delete to | |
114 | free the @a ptr raw pointer when its reference count drops to | |
115 | zero. | |
116 | ||
117 | @since 3.0 | |
118 | */ | |
119 | template<typename Deleter> | |
120 | void reset(T* ptr, Deleter d); | |
121 | ||
23324ae1 FM |
122 | /** |
123 | Returns @true if this is the only pointer pointing to its object. | |
124 | */ | |
328f5751 | 125 | bool unique() const; |
23324ae1 FM |
126 | |
127 | /** | |
128 | Returns the number of pointers pointing to its object. | |
129 | */ | |
328f5751 | 130 | long use_count() const; |
23324ae1 | 131 | }; |
e54c96f1 | 132 |