]>
Commit | Line | Data |
---|---|---|
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) | |
371a5b4e | 9 | // Licence: wxWindows licence |
5b222f1c JS |
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 & operator*() const \ | |
86 | { \ | |
87 | wxASSERT(m_ptr != NULL); \ | |
88 | return *m_ptr; \ | |
89 | } \ | |
90 | \ | |
91 | T * operator->() const \ | |
92 | { \ | |
93 | wxASSERT(m_ptr != NULL); \ | |
94 | return m_ptr; \ | |
95 | } \ | |
96 | \ | |
97 | T * get() const \ | |
98 | { \ | |
99 | return m_ptr; \ | |
100 | } \ | |
101 | \ | |
102 | void swap(name & ot) \ | |
103 | { \ | |
104 | T * tmp = ot.m_ptr; \ | |
105 | ot.m_ptr = m_ptr; \ | |
106 | m_ptr = tmp; \ | |
107 | } \ | |
108 | }; | |
109 | ||
110 | #define wxDEFINE_SCOPED_PTR(T, name)\ | |
111 | name::~name() \ | |
112 | { \ | |
113 | wxCHECKED_DELETE(m_ptr) \ | |
114 | } | |
115 | ||
116 | #define wxDECLARE_SCOPED_ARRAY(T, name)\ | |
117 | class name \ | |
118 | { \ | |
119 | private: \ | |
120 | T * m_ptr; \ | |
121 | name(name const &); \ | |
122 | name & operator=(name const &); \ | |
123 | \ | |
124 | public: \ | |
125 | wxEXPLICIT name(T * p = NULL) : m_ptr(p) \ | |
126 | {} \ | |
127 | \ | |
128 | ~name(); \ | |
129 | void reset(T * p = NULL); \ | |
130 | \ | |
131 | T & operator[](long int i) const\ | |
132 | { \ | |
133 | wxASSERT(m_ptr != NULL); \ | |
134 | wxASSERT(i >= 0); \ | |
135 | return m_ptr[i]; \ | |
136 | } \ | |
137 | \ | |
138 | T * get() const \ | |
139 | { \ | |
140 | return m_ptr; \ | |
141 | } \ | |
142 | \ | |
143 | void swap(name & ot) \ | |
144 | { \ | |
145 | T * tmp = ot.m_ptr; \ | |
146 | ot.m_ptr = m_ptr; \ | |
147 | m_ptr = tmp; \ | |
148 | } \ | |
149 | }; | |
150 | ||
151 | #define wxDEFINE_SCOPED_ARRAY(T, name) \ | |
152 | name::~name() \ | |
153 | { \ | |
154 | wxCHECKED_DELETE_ARRAY(m_ptr) \ | |
155 | } \ | |
156 | void name::reset(T * p){ \ | |
157 | if (m_ptr != p) \ | |
158 | { \ | |
159 | wxCHECKED_DELETE_ARRAY(m_ptr); \ | |
160 | m_ptr = p; \ | |
161 | } \ | |
162 | } | |
163 | ||
164 | #endif |