]> git.saurik.com Git - wxWidgets.git/blame - include/wx/ptr_scpd.h
various cleanups
[wxWidgets.git] / include / wx / ptr_scpd.h
CommitLineData
5b222f1c
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/ptr_scpd.h
3// Purpose: scoped smart pointer class
4// Author: Jesse Lovelace <jllovela@eos.ncsu.edu>
5// Modified by:
6// Created: 06/01/02
7// RCS-ID: $Id$
8// Copyright: (c) Jesse Lovelace and original Boost authors (see below)
65571936 9// Licence: wxWindows licence
5b222f1c
JS
10/////////////////////////////////////////////////////////////////////////////
11
12// This class closely follows the implementation of the boost
7e548f6b 13// library scoped_ptr and is an adaption for c++ macro's in
77ffb593 14// the wxWidgets project. The original authors of the boost
5b222f1c
JS
15// scoped_ptr are given below with their respective copyrights.
16
17// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
18// Copyright (c) 2001, 2002 Peter Dimov
19//
20// Permission to copy, use, modify, sell and distribute this software
21// is granted provided this copyright notice appears in all copies.
22// This software is provided "as is" without express or implied
23// warranty, and with no claim as to its suitability for any purpose.
24//
25// See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
26//
27
28#ifndef __WX_SCOPED_POINTER__
29#define __WX_SCOPED_POINTER__
30
31#include "wx/defs.h"
32
d566b182
VZ
33/*
34 checked deleters are used to make sure that the type being deleted is really
35 a complete type.: otherwise sizeof() would result in a compile-time error
36
37 do { ... } while ( 0 ) construct is used to have an anonymous scope
38 (otherwise we could have name clashes between different "complete"s) but
39 still force a semicolon after the macro
5b222f1c
JS
40*/
41
373a5fb3
WS
42#ifdef __WATCOMC__
43 #define wxPRE_NO_WARNING_SCOPE for(int i=0;i<1;i++)
44 #define wxPOST_NO_WARNING_SCOPE
45#else
46 #define wxPRE_NO_WARNING_SCOPE do
47 #define wxPOST_NO_WARNING_SCOPE while ( 0 )
48#endif
49
d566b182 50#define wxCHECKED_DELETE(ptr) \
373a5fb3 51 wxPRE_NO_WARNING_SCOPE \
d566b182
VZ
52 { \
53 typedef char complete[sizeof(*ptr)]; \
54 delete ptr; \
373a5fb3 55 } wxPOST_NO_WARNING_SCOPE
d566b182
VZ
56
57#define wxCHECKED_DELETE_ARRAY(ptr) \
373a5fb3 58 wxPRE_NO_WARNING_SCOPE \
d566b182
VZ
59 { \
60 typedef char complete[sizeof(*ptr)]; \
61 delete [] ptr; \
373a5fb3 62 } wxPOST_NO_WARNING_SCOPE
5b222f1c
JS
63
64/* These scoped pointers are *not* assignable and cannot be used
65 within a container. Look for wxDECLARE_SHARED_PTR for this
66 functionality.
67
68 In addition, the type being used *must* be complete at the time
69 that wxDEFINE_SCOPED_* is called or a compiler error will result.
70 This is because the class checks for the completeness of the type
71 being used.
72*/
73
74
75#define wxDECLARE_SCOPED_PTR(T, name) \
76class name \
77{ \
78private: \
79 T * m_ptr; \
80 \
81 name(name const &); \
82 name & operator=(name const &); \
83 \
84public: \
5455e227 85 wxEXPLICIT name(T * ptr = NULL) \
5b222f1c
JS
86 : m_ptr(ptr) { } \
87 \
88 ~name(); \
89 \
90 void reset(T * ptr = NULL) \
91 { \
92 if (m_ptr != ptr) \
93 { \
94 delete m_ptr; \
95 m_ptr = ptr; \
96 } \
97 } \
98 \
5455e227
VZ
99 T *release() \
100 { \
101 T *ptr = m_ptr; \
102 m_ptr = NULL; \
103 return ptr; \
104 } \
105 \
5b222f1c
JS
106 T & operator*() const \
107 { \
108 wxASSERT(m_ptr != NULL); \
109 return *m_ptr; \
110 } \
111 \
112 T * operator->() const \
113 { \
114 wxASSERT(m_ptr != NULL); \
115 return m_ptr; \
116 } \
117 \
118 T * get() const \
119 { \
120 return m_ptr; \
121 } \
122 \
123 void swap(name & ot) \
124 { \
125 T * tmp = ot.m_ptr; \
126 ot.m_ptr = m_ptr; \
127 m_ptr = tmp; \
128 } \
129};
130
131#define wxDEFINE_SCOPED_PTR(T, name)\
132name::~name() \
133{ \
bb0a3c4a 134 wxCHECKED_DELETE(m_ptr); \
5b222f1c
JS
135}
136
d83eeece
VZ
137// this macro can be used for the most common case when you want to declare and
138// define the scoped pointer at the same time and want to use the standard
139// naming convention: auto pointer to Foo is called FooPtr
140#define wxDEFINE_SCOPED_PTR_TYPE(T) \
141 wxDECLARE_SCOPED_PTR(T, T ## Ptr); \
142 wxDEFINE_SCOPED_PTR(T, T ## Ptr)
143
144// the same but for arrays instead of simple pointers
5b222f1c
JS
145#define wxDECLARE_SCOPED_ARRAY(T, name)\
146class name \
147{ \
148private: \
149 T * m_ptr; \
150 name(name const &); \
151 name & operator=(name const &); \
152 \
153public: \
154 wxEXPLICIT name(T * p = NULL) : m_ptr(p) \
155 {} \
156 \
157 ~name(); \
158 void reset(T * p = NULL); \
159 \
160 T & operator[](long int i) const\
161 { \
162 wxASSERT(m_ptr != NULL); \
163 wxASSERT(i >= 0); \
164 return m_ptr[i]; \
165 } \
166 \
167 T * get() const \
168 { \
169 return m_ptr; \
170 } \
171 \
172 void swap(name & ot) \
173 { \
174 T * tmp = ot.m_ptr; \
175 ot.m_ptr = m_ptr; \
176 m_ptr = tmp; \
177 } \
178};
179
180#define wxDEFINE_SCOPED_ARRAY(T, name) \
bb0a3c4a
VZ
181name::~name() \
182{ \
183 wxCHECKED_DELETE_ARRAY(m_ptr); \
184} \
185void name::reset(T * p){ \
186 if (m_ptr != p) \
187 { \
188 wxCHECKED_DELETE_ARRAY(m_ptr); \
189 m_ptr = p; \
190 } \
5b222f1c
JS
191}
192
38f15267
VZ
193// ----------------------------------------------------------------------------
194// "Tied" scoped pointer: same as normal one but also sets the value of
195// some other variable to the pointer value
196// ----------------------------------------------------------------------------
197
198#define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \
199 wxDEFINE_SCOPED_PTR_TYPE(T); \
200 class T ## TiedPtr : public T ## Ptr \
201 { \
202 public: \
203 T ## TiedPtr(T **pp, T *p) \
204 : T ## Ptr(p), m_pp(pp) \
205 { \
206 m_pOld = *pp; \
207 *pp = p; \
208 } \
209 \
210 ~ T ## TiedPtr() \
211 { \
212 *m_pp = m_pOld; \
213 } \
214 \
215 private: \
216 T **m_pp; \
217 T *m_pOld; \
218 }
219
d83eeece
VZ
220#endif // __WX_SCOPED_POINTER__
221