/src/wxwidgets/include/wx/scopedptr.h
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /////////////////////////////////////////////////////////////////////////////  | 
2  |  | // Name:        wx/scopedptr.h  | 
3  |  | // Purpose:     scoped smart pointer class  | 
4  |  | // Author:      Jesse Lovelace <jllovela@eos.ncsu.edu>  | 
5  |  | // Created:     06/01/02  | 
6  |  | // Copyright:   (c) Jesse Lovelace and original Boost authors (see below)  | 
7  |  | //              (c) 2009 Vadim Zeitlin  | 
8  |  | // Licence:     wxWindows licence  | 
9  |  | /////////////////////////////////////////////////////////////////////////////  | 
10  |  |  | 
11  |  | //  This class closely follows the implementation of the boost  | 
12  |  | //  library scoped_ptr and is an adaptation for c++ macro's in  | 
13  |  | //  the wxWidgets project. The original authors of the boost  | 
14  |  | //  scoped_ptr are given below with their respective copyrights.  | 
15  |  |  | 
16  |  | //  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.  | 
17  |  | //  Copyright (c) 2001, 2002 Peter Dimov  | 
18  |  | //  | 
19  |  | //  Permission to copy, use, modify, sell and distribute this software  | 
20  |  | //  is granted provided this copyright notice appears in all copies.  | 
21  |  | //  This software is provided "as is" without express or implied  | 
22  |  | //  warranty, and with no claim as to its suitability for any purpose.  | 
23  |  | //  | 
24  |  | //  See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.  | 
25  |  | //  | 
26  |  |  | 
27  |  | #ifndef _WX_SCOPED_PTR_H_  | 
28  |  | #define _WX_SCOPED_PTR_H_  | 
29  |  |  | 
30  |  | // Everything in this file is deprecated and must not be used any longer,  | 
31  |  | // simply use std::unique_ptr<> instead.  | 
32  |  |  | 
33  |  | #include "wx/defs.h"  | 
34  |  | #include "wx/checkeddelete.h"  | 
35  |  |  | 
36  |  | // ----------------------------------------------------------------------------  | 
37  |  | // wxScopedPtr: A scoped pointer  | 
38  |  | // ----------------------------------------------------------------------------  | 
39  |  |  | 
40  |  | template <class T>  | 
41  |  | class wxScopedPtr  | 
42  |  | { | 
43  |  | public:  | 
44  |  |     typedef T element_type;  | 
45  |  |  | 
46  |  |     explicit wxScopedPtr(T * ptr = nullptr) : m_ptr(ptr) { } | 
47  |  |  | 
48  |  |     ~wxScopedPtr() { wxCHECKED_DELETE(m_ptr); } | 
49  |  |  | 
50  |  |     // test for pointer validity: defining conversion to unspecified_bool_type  | 
51  |  |     // and not more obvious bool to avoid implicit conversions to integer types  | 
52  |  |     typedef T *(wxScopedPtr<T>::*unspecified_bool_type)() const;  | 
53  |  |  | 
54  |  |     operator unspecified_bool_type() const  | 
55  |  |     { | 
56  |  |         return m_ptr ? &wxScopedPtr<T>::get : nullptr;  | 
57  |  |     }  | 
58  |  |  | 
59  |  |     void reset(T * ptr = nullptr)  | 
60  |  |     { | 
61  |  |         if ( ptr != m_ptr )  | 
62  |  |         { | 
63  |  |             wxCHECKED_DELETE(m_ptr);  | 
64  |  |             m_ptr = ptr;  | 
65  |  |         }  | 
66  |  |     }  | 
67  |  |  | 
68  |  |     T *release()  | 
69  |  |     { | 
70  |  |         T *ptr = m_ptr;  | 
71  |  |         m_ptr = nullptr;  | 
72  |  |         return ptr;  | 
73  |  |     }  | 
74  |  |  | 
75  |  |     T & operator*() const  | 
76  |  |     { | 
77  |  |         wxASSERT(m_ptr != nullptr);  | 
78  |  |         return *m_ptr;  | 
79  |  |     }  | 
80  |  |  | 
81  |  |     T * operator->() const  | 
82  |  |     { | 
83  |  |         wxASSERT(m_ptr != nullptr);  | 
84  |  |         return m_ptr;  | 
85  |  |     }  | 
86  |  |  | 
87  |  |     T * get() const  | 
88  |  |     { | 
89  |  |         return m_ptr;  | 
90  |  |     }  | 
91  |  |  | 
92  |  |     void swap(wxScopedPtr& other)  | 
93  |  |     { | 
94  |  |         T * const tmp = other.m_ptr;  | 
95  |  |         other.m_ptr = m_ptr;  | 
96  |  |         m_ptr = tmp;  | 
97  |  |     }  | 
98  |  |  | 
99  |  | private:  | 
100  |  |     T * m_ptr;  | 
101  |  |  | 
102  |  |     wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedPtr, T);  | 
103  |  | };  | 
104  |  |  | 
105  |  | // ----------------------------------------------------------------------------  | 
106  |  | // old macro based implementation  | 
107  |  | // ----------------------------------------------------------------------------  | 
108  |  |  | 
109  |  | /* The type being used *must* be complete at the time  | 
110  |  |    that wxDEFINE_SCOPED_* is called or a compiler error will result.  | 
111  |  |    This is because the class checks for the completeness of the type  | 
112  |  |    being used. */  | 
113  |  |  | 
114  |  | #define wxDECLARE_SCOPED_PTR(T, name) \  | 
115  |  | class name                          \  | 
116  |  | {                                   \ | 
117  |  | private:                            \  | 
118  |  |     T * m_ptr;                      \  | 
119  |  |                                     \  | 
120  |  |     name(name const &);             \  | 
121  |  |     name & operator=(name const &); \  | 
122  |  |                                     \  | 
123  |  | public:                             \  | 
124  |  |     explicit name(T * ptr = nullptr)   \  | 
125  | 0  |     : m_ptr(ptr) { }                \ | 
126  |  |                                     \  | 
127  |  |     ~name();                        \  | 
128  |  |                                     \  | 
129  |  |     void reset(T * ptr = nullptr);     \  | 
130  |  |                                     \  | 
131  |  |     T *release()                    \  | 
132  | 0  |     {                               \ | 
133  | 0  |         T *ptr = m_ptr;             \  | 
134  | 0  |         m_ptr = nullptr;               \  | 
135  | 0  |         return ptr;                 \  | 
136  | 0  |     }                               \  | 
137  |  |                                     \  | 
138  |  |     T & operator*() const           \  | 
139  | 0  |     {                               \ | 
140  | 0  |         wxASSERT(m_ptr != nullptr);    \  | 
141  | 0  |         return *m_ptr;              \  | 
142  | 0  |     }                               \  | 
143  |  |                                     \  | 
144  |  |     T * operator->() const          \  | 
145  | 0  |     {                               \ | 
146  | 0  |         wxASSERT(m_ptr != nullptr);    \  | 
147  | 0  |         return m_ptr;               \  | 
148  | 0  |     }                               \  | 
149  |  |                                     \  | 
150  |  |     T * get() const                 \  | 
151  | 0  |     {                               \ | 
152  | 0  |         return m_ptr;               \  | 
153  | 0  |     }                               \  | 
154  |  |                                     \  | 
155  |  |     void swap(name & ot)            \  | 
156  | 0  |     {                               \ | 
157  | 0  |         T * tmp = ot.m_ptr;         \  | 
158  | 0  |         ot.m_ptr = m_ptr;           \  | 
159  | 0  |         m_ptr = tmp;                \  | 
160  | 0  |     }                               \  | 
161  |  | };  | 
162  |  |  | 
163  |  | #define wxDEFINE_SCOPED_PTR(T, name)\  | 
164  | 0  | void name::reset(T * ptr)           \  | 
165  | 0  | {                                   \ | 
166  | 0  |     if (m_ptr != ptr)               \  | 
167  | 0  |     {                               \ | 
168  | 0  |         wxCHECKED_DELETE(m_ptr);    \  | 
169  | 0  |         m_ptr = ptr;                \  | 
170  | 0  |     }                               \  | 
171  | 0  | }                                   \  | 
172  | 0  | name::~name()                       \  | 
173  | 0  | {                                   \ | 
174  | 0  |     wxCHECKED_DELETE(m_ptr);        \  | 
175  | 0  | }  | 
176  |  |  | 
177  |  | // this macro can be used for the most common case when you want to declare and  | 
178  |  | // define the scoped pointer at the same time and want to use the standard  | 
179  |  | // naming convention: auto pointer to Foo is called FooPtr  | 
180  |  | #define wxDEFINE_SCOPED_PTR_TYPE(T)    \  | 
181  |  |     wxDECLARE_SCOPED_PTR(T, T ## Ptr)  \  | 
182  |  |     wxDEFINE_SCOPED_PTR(T, T ## Ptr)  | 
183  |  |  | 
184  |  | // ----------------------------------------------------------------------------  | 
185  |  | // "Tied" scoped pointer: same as normal one but also sets the value of  | 
186  |  | //                        some other variable to the pointer value  | 
187  |  | // ----------------------------------------------------------------------------  | 
188  |  |  | 
189  |  | #define wxDEFINE_TIED_SCOPED_PTR_TYPE(T)                                      \  | 
190  |  |     wxDEFINE_SCOPED_PTR_TYPE(T)                                               \  | 
191  |  |     class T ## TiedPtr : public T ## Ptr                                      \  | 
192  |  |     {                                                                         \ | 
193  |  |     public:                                                                   \  | 
194  |  |         T ## TiedPtr(T **pp, T *p)                                            \  | 
195  | 0  |             : T ## Ptr(p), m_pp(pp)                                           \  | 
196  | 0  |         {                                                                     \ | 
197  | 0  |             m_pOld = *pp;                                                     \  | 
198  | 0  |             *pp = p;                                                          \  | 
199  | 0  |         }                                                                     \  | 
200  |  |                                                                               \  | 
201  |  |         ~ T ## TiedPtr()                                                      \  | 
202  | 0  |         {                                                                     \ | 
203  | 0  |             *m_pp = m_pOld;                                                   \  | 
204  | 0  |         }                                                                     \  | 
205  |  |                                                                               \  | 
206  |  |     private:                                                                  \  | 
207  |  |         T **m_pp;                                                             \  | 
208  |  |         T *m_pOld;                                                            \  | 
209  |  |     };  | 
210  |  |  | 
211  |  | #endif // _WX_SCOPED_PTR_H_  | 
212  |  |  |