]>
Commit | Line | Data |
---|---|---|
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) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // This class closely follows the implementation of the boost | |
13 | // library scoped_ptr and is an adaption for c++ macro's in | |
14 | // the wxWindows project. The original authors of the boost | |
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 | ||
33 | /* checked deleters are used to make sure that the | |
34 | type being deleted is really a complete type. | |
35 | - Jesse Lovelace <jllovela@eos.ncsu.edu> | |
36 | */ | |
37 | ||
38 | #define wxCHECKED_DELETE(ptr) \ | |
39 | if (true) { \ | |
40 | typedef char complete[sizeof(*ptr)]; \ | |
41 | delete ptr; \ | |
42 | } | |
43 | ||
44 | #define wxCHECKED_DELETE_ARRAY(ptr) \ | |
45 | if (true) { \ | |
46 | typedef char complete[sizeof(*ptr)]; \ | |
47 | delete [] ptr; \ | |
48 | } | |
49 | ||
50 | /* These scoped pointers are *not* assignable and cannot be used | |
51 | within a container. Look for wxDECLARE_SHARED_PTR for this | |
52 | functionality. | |
53 | ||
54 | In addition, the type being used *must* be complete at the time | |
55 | that wxDEFINE_SCOPED_* is called or a compiler error will result. | |
56 | This is because the class checks for the completeness of the type | |
57 | being used. | |
58 | */ | |
59 | ||
60 | ||
61 | #define wxDECLARE_SCOPED_PTR(T, name) \ | |
62 | class name \ | |
63 | { \ | |
64 | private: \ | |
65 | T * m_ptr; \ | |
66 | \ | |
67 | name(name const &); \ | |
68 | name & operator=(name const &); \ | |
69 | \ | |
70 | public: \ | |
71 | wxEXPLICIT name(T * ptr = NULL) \ | |
72 | : m_ptr(ptr) { } \ | |
73 | \ | |
74 | ~name(); \ | |
75 | \ | |
76 | void reset(T * ptr = NULL) \ | |
77 | { \ | |
78 | if (m_ptr != ptr) \ | |
79 | { \ | |
80 | delete m_ptr; \ | |
81 | m_ptr = ptr; \ | |
82 | } \ | |
83 | } \ | |
84 | \ | |
85 | T *release() \ | |
86 | { \ | |
87 | T *ptr = m_ptr; \ | |
88 | m_ptr = NULL; \ | |
89 | return ptr; \ | |
90 | } \ | |
91 | \ | |
92 | T & operator*() const \ | |
93 | { \ | |
94 | wxASSERT(m_ptr != NULL); \ | |
95 | return *m_ptr; \ | |
96 | } \ | |
97 | \ | |
98 | T * operator->() const \ | |
99 | { \ | |
100 | wxASSERT(m_ptr != NULL); \ | |
101 | return m_ptr; \ | |
102 | } \ | |
103 | \ | |
104 | T * get() const \ | |
105 | { \ | |
106 | return m_ptr; \ | |
107 | } \ | |
108 | \ | |
109 | void swap(name & ot) \ | |
110 | { \ | |
111 | T * tmp = ot.m_ptr; \ | |
112 | ot.m_ptr = m_ptr; \ | |
113 | m_ptr = tmp; \ | |
114 | } \ | |
115 | }; | |
116 | ||
117 | #define wxDEFINE_SCOPED_PTR(T, name)\ | |
118 | name::~name() \ | |
119 | { \ | |
120 | wxCHECKED_DELETE(m_ptr) \ | |
121 | } | |
122 | ||
123 | #define wxDECLARE_SCOPED_ARRAY(T, name)\ | |
124 | class name \ | |
125 | { \ | |
126 | private: \ | |
127 | T * m_ptr; \ | |
128 | name(name const &); \ | |
129 | name & operator=(name const &); \ | |
130 | \ | |
131 | public: \ | |
132 | wxEXPLICIT name(T * p = NULL) : m_ptr(p) \ | |
133 | {} \ | |
134 | \ | |
135 | ~name(); \ | |
136 | void reset(T * p = NULL); \ | |
137 | \ | |
138 | T & operator[](long int i) const\ | |
139 | { \ | |
140 | wxASSERT(m_ptr != NULL); \ | |
141 | wxASSERT(i >= 0); \ | |
142 | return m_ptr[i]; \ | |
143 | } \ | |
144 | \ | |
145 | T * get() const \ | |
146 | { \ | |
147 | return m_ptr; \ | |
148 | } \ | |
149 | \ | |
150 | void swap(name & ot) \ | |
151 | { \ | |
152 | T * tmp = ot.m_ptr; \ | |
153 | ot.m_ptr = m_ptr; \ | |
154 | m_ptr = tmp; \ | |
155 | } \ | |
156 | }; | |
157 | ||
158 | #define wxDEFINE_SCOPED_ARRAY(T, name) \ | |
159 | name::~name() \ | |
160 | { \ | |
161 | wxCHECKED_DELETE_ARRAY(m_ptr) \ | |
162 | } \ | |
163 | void name::reset(T * p){ \ | |
164 | if (m_ptr != p) \ | |
165 | { \ | |
166 | wxCHECKED_DELETE_ARRAY(m_ptr); \ | |
167 | m_ptr = p; \ | |
168 | } \ | |
169 | } | |
170 | ||
171 | #endif |