]> git.saurik.com Git - wxWidgets.git/blame - include/wx/scopedptr.h
Applied #15226 wxRichTextCtrl: Implement setting properties with undo for objects...
[wxWidgets.git] / include / wx / scopedptr.h
CommitLineData
5b222f1c 1/////////////////////////////////////////////////////////////////////////////
664e1314 2// Name: wx/scopedptr.h
5b222f1c
JS
3// Purpose: scoped smart pointer class
4// Author: Jesse Lovelace <jllovela@eos.ncsu.edu>
5b222f1c 5// Created: 06/01/02
5b222f1c 6// Copyright: (c) Jesse Lovelace and original Boost authors (see below)
058f225a 7// (c) 2009 Vadim Zeitlin
65571936 8// Licence: wxWindows licence
5b222f1c
JS
9/////////////////////////////////////////////////////////////////////////////
10
11// This class closely follows the implementation of the boost
7e548f6b 12// library scoped_ptr and is an adaption for c++ macro's in
77ffb593 13// the wxWidgets project. The original authors of the boost
5b222f1c
JS
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
664e1314
VZ
27#ifndef _WX_SCOPED_PTR_H_
28#define _WX_SCOPED_PTR_H_
5b222f1c
JS
29
30#include "wx/defs.h"
664e1314 31#include "wx/checkeddelete.h"
5b222f1c 32
89969a91 33// ----------------------------------------------------------------------------
058f225a 34// wxScopedPtr: A scoped pointer
89969a91
RR
35// ----------------------------------------------------------------------------
36
058f225a 37template <class T>
89969a91 38class wxScopedPtr
058f225a
VZ
39{
40public:
89969a91 41 typedef T element_type;
058f225a
VZ
42
43 wxEXPLICIT wxScopedPtr(T * ptr = NULL) : m_ptr(ptr) { }
44
4e0c592f 45 ~wxScopedPtr() { wxCHECKED_DELETE(m_ptr); }
058f225a 46
a60b0ddc
RR
47 // test for pointer validity: defining conversion to unspecified_bool_type
48 // and not more obvious bool to avoid implicit conversions to integer types
a2d03e4c
VZ
49#ifdef __BORLANDC__
50 // this compiler is too dumb to use unspecified_bool_type operator in tests
51 // of the form "if ( !ptr )"
52 typedef bool unspecified_bool_type;
53#else
a60b0ddc 54 typedef T *(wxScopedPtr<T>::*unspecified_bool_type)() const;
a2d03e4c 55#endif // __BORLANDC__
a60b0ddc
RR
56 operator unspecified_bool_type() const
57 {
58 return m_ptr ? &wxScopedPtr<T>::get : NULL;
59 }
60
058f225a
VZ
61 void reset(T * ptr = NULL)
62 {
63 if ( ptr != m_ptr )
64 {
bdd6a03d 65 wxCHECKED_DELETE(m_ptr);
058f225a
VZ
66 m_ptr = ptr;
67 }
68 }
69
70 T *release()
71 {
72 T *ptr = m_ptr;
73 m_ptr = NULL;
74 return ptr;
75 }
76
77 T & operator*() const
78 {
79 wxASSERT(m_ptr != NULL);
80 return *m_ptr;
81 }
82
83 T * operator->() const
84 {
85 wxASSERT(m_ptr != NULL);
86 return m_ptr;
87 }
88
89 T * get() const
90 {
91 return m_ptr;
92 }
93
94 void swap(wxScopedPtr& other)
95 {
96 T * const tmp = other.m_ptr;
97 other.m_ptr = m_ptr;
98 m_ptr = tmp;
99 }
100
101private:
102 T * m_ptr;
103
104 DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedPtr, T)
105};
106
89969a91
RR
107// ----------------------------------------------------------------------------
108// old macro based implementation
109// ----------------------------------------------------------------------------
110
89969a91 111/* The type being used *must* be complete at the time
5b222f1c
JS
112 that wxDEFINE_SCOPED_* is called or a compiler error will result.
113 This is because the class checks for the completeness of the type
89969a91 114 being used. */
5b222f1c
JS
115
116#define wxDECLARE_SCOPED_PTR(T, name) \
117class name \
118{ \
119private: \
120 T * m_ptr; \
121 \
122 name(name const &); \
123 name & operator=(name const &); \
124 \
125public: \
5455e227 126 wxEXPLICIT name(T * ptr = NULL) \
5b222f1c
JS
127 : m_ptr(ptr) { } \
128 \
129 ~name(); \
130 \
ba5619e0 131 void reset(T * ptr = NULL); \
5b222f1c 132 \
5455e227
VZ
133 T *release() \
134 { \
135 T *ptr = m_ptr; \
136 m_ptr = NULL; \
137 return ptr; \
138 } \
139 \
5b222f1c
JS
140 T & operator*() const \
141 { \
142 wxASSERT(m_ptr != NULL); \
143 return *m_ptr; \
144 } \
145 \
146 T * operator->() const \
147 { \
148 wxASSERT(m_ptr != NULL); \
149 return m_ptr; \
150 } \
151 \
152 T * get() const \
153 { \
154 return m_ptr; \
155 } \
156 \
157 void swap(name & ot) \
158 { \
159 T * tmp = ot.m_ptr; \
160 ot.m_ptr = m_ptr; \
161 m_ptr = tmp; \
162 } \
163};
164
165#define wxDEFINE_SCOPED_PTR(T, name)\
ba5619e0
VS
166void name::reset(T * ptr) \
167{ \
168 if (m_ptr != ptr) \
169 { \
170 wxCHECKED_DELETE(m_ptr); \
171 m_ptr = ptr; \
172 } \
173} \
5b222f1c
JS
174name::~name() \
175{ \
bb0a3c4a 176 wxCHECKED_DELETE(m_ptr); \
5b222f1c
JS
177}
178
d83eeece
VZ
179// this macro can be used for the most common case when you want to declare and
180// define the scoped pointer at the same time and want to use the standard
181// naming convention: auto pointer to Foo is called FooPtr
d0ee33f5
WS
182#define wxDEFINE_SCOPED_PTR_TYPE(T) \
183 wxDECLARE_SCOPED_PTR(T, T ## Ptr) \
d83eeece
VZ
184 wxDEFINE_SCOPED_PTR(T, T ## Ptr)
185
38f15267
VZ
186// ----------------------------------------------------------------------------
187// "Tied" scoped pointer: same as normal one but also sets the value of
188// some other variable to the pointer value
189// ----------------------------------------------------------------------------
190
191#define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \
259c43f6 192 wxDEFINE_SCOPED_PTR_TYPE(T) \
38f15267
VZ
193 class T ## TiedPtr : public T ## Ptr \
194 { \
195 public: \
196 T ## TiedPtr(T **pp, T *p) \
197 : T ## Ptr(p), m_pp(pp) \
198 { \
199 m_pOld = *pp; \
200 *pp = p; \
201 } \
202 \
203 ~ T ## TiedPtr() \
204 { \
205 *m_pp = m_pOld; \
206 } \
207 \
208 private: \
209 T **m_pp; \
210 T *m_pOld; \
259c43f6 211 };
38f15267 212
664e1314 213#endif // _WX_SCOPED_PTR_H_
d83eeece 214