OLD | NEW |
| (Empty) |
1 //+--------------------------------------------------------------------------- | |
2 // | |
3 // Copyright ( C ) Microsoft, 2002. | |
4 // | |
5 // File: scoped_any.h | |
6 // | |
7 // Contents: automatic resource management, a-la std::scoped_ptr | |
8 // | |
9 // Classes: scoped_any<> and various typedefs | |
10 // | |
11 // Functions: get | |
12 // reset | |
13 // release | |
14 // valid | |
15 // address | |
16 // | |
17 // Author: Eric Niebler ( ericne@microsoft.com ) | |
18 // | |
19 //---------------------------------------------------------------------------- | |
20 | |
21 #ifndef SCOPED_ANY | |
22 #define SCOPED_ANY | |
23 #include <cassert> | |
24 #include "smart_any_fwd.h" | |
25 | |
26 #pragma warning(push) | |
27 | |
28 // 4284 warning for operator-> returning non-pointer; | |
29 // compiler issues it even if -> is not used for the specific instance | |
30 #pragma warning(disable: 4284) | |
31 | |
32 namespace detail | |
33 { | |
34 // friend function definitions go in scoped_any_helper | |
35 template<typename T,class close_policy,class invalid_value,int unique> | |
36 struct scoped_any_helper; | |
37 } | |
38 | |
39 // wrap a resource to enforce strict ownership and ensure proper cleanup | |
40 template<typename T,class close_policy,class invalid_value,int unique> | |
41 class scoped_any | |
42 { | |
43 // disallow copy and assignment | |
44 scoped_any( scoped_any<T,close_policy,invalid_value,unique> const & ); | |
45 scoped_any<T,close_policy,invalid_value,unique> & operator=( | |
46 scoped_any<T,close_policy,invalid_value,unique> const & ); | |
47 | |
48 // disallow comparison of scoped_any's | |
49 bool operator==( detail::safe_bool ) const; | |
50 bool operator!=( detail::safe_bool ) const; | |
51 | |
52 typedef detail::safe_types<T,close_policy> safe_types; | |
53 | |
54 public: | |
55 typedef typename detail::holder<T>::type element_type; | |
56 typedef close_policy close_policy_type; | |
57 typedef typename safe_types::pointer_type pointer_type; | |
58 typedef typename safe_types::reference_type reference_type; | |
59 | |
60 // Fix-up the invalid_value type on older compilers | |
61 typedef typename detail::fixup_invalid_value<invalid_value>:: | |
62 template rebind<T>::type invalid_value_type; | |
63 | |
64 friend struct detail::scoped_any_helper<T,close_policy,invalid_value,unique>
; | |
65 | |
66 // construct from object pointer | |
67 explicit scoped_any( T t = invalid_value_type() ) | |
68 : m_t( t ) | |
69 { | |
70 } | |
71 | |
72 // destroy the object | |
73 ~scoped_any() | |
74 { | |
75 if( valid() ) | |
76 { | |
77 close_policy::close( m_t ); | |
78 } | |
79 } | |
80 | |
81 // return pointer to class object (assume pointer) | |
82 pointer_type operator->() const | |
83 { | |
84 #ifdef SMART_ANY_PTS | |
85 // You better not be applying operator-> to a handle! | |
86 static detail::static_assert<!detail::is_handle<T>::value> const cannot_
dereference_a_handle; | |
87 #endif | |
88 assert( valid() ); | |
89 return safe_types::to_pointer( m_t ); | |
90 } | |
91 | |
92 // for use when scoped_any appears in a conditional | |
93 operator detail::safe_bool() const | |
94 { | |
95 return valid() ? detail::safe_true : detail::safe_false; | |
96 } | |
97 | |
98 // for use when scoped_any appears in a conditional | |
99 bool operator!() const | |
100 { | |
101 return ! valid(); | |
102 } | |
103 | |
104 #ifdef SMART_ANY_PTS | |
105 // if this scoped_any is managing an array, we can use operator[] to index i
t | |
106 typename detail::deref<T>::type operator[]( int i ) const | |
107 { | |
108 static detail::static_assert<!detail::is_handle<T>::value> const cannot_
dereference_a_handle; | |
109 static detail::static_assert<!detail::is_delete<close_policy>::value> co
nst accessed_like_an_array_but_not_deleted_like_an_array; | |
110 assert( valid() ); | |
111 return m_t[ i ]; | |
112 } | |
113 | |
114 // unary operator* lets you write code like: | |
115 // scoped_any<foo*,close_delete> pfoo( new foo ); | |
116 // foo & f = *pfoo; | |
117 reference_type operator*() const | |
118 { | |
119 static detail::static_assert<!detail::is_handle<T>::value> const cannot_
dereference_a_handle; | |
120 assert( valid() ); | |
121 return smart_types::to_reference( m_t ); | |
122 } | |
123 #endif | |
124 | |
125 private: | |
126 | |
127 bool valid() const | |
128 { | |
129 // see if the managed resource is in the invalid state. | |
130 return m_t != static_cast<T>( invalid_value_type() ); | |
131 } | |
132 | |
133 // the wrapped object | |
134 element_type m_t; | |
135 }; | |
136 | |
137 namespace detail | |
138 { | |
139 // friend function definitions go in scoped_any_helper | |
140 template<typename T,class close_policy,class invalid_value,int unique> | |
141 struct scoped_any_helper | |
142 { | |
143 // return wrapped pointer | |
144 static T get( scoped_any<T,close_policy,invalid_value,unique> const & t
) | |
145 { | |
146 return t.m_t; | |
147 } | |
148 | |
149 // return wrapped pointer and give up ownership | |
150 static T release( scoped_any<T,close_policy,invalid_value,unique> & t ) | |
151 { | |
152 // Fix-up the invalid_value type on older compilers | |
153 typedef typename detail::fixup_invalid_value<invalid_value>:: | |
154 template rebind<T>::type invalid_value_type; | |
155 | |
156 T tmpT = t.m_t; | |
157 t.m_t = static_cast<T>( invalid_value_type() ); | |
158 return tmpT; | |
159 } | |
160 | |
161 // destroy designated object and store new pointer | |
162 static void reset( scoped_any<T,close_policy,invalid_value,unique> & t,
T newT ) | |
163 { | |
164 if( t.m_t != newT ) | |
165 { | |
166 if( t.valid() ) | |
167 { | |
168 close_policy::close( t.m_t ); | |
169 } | |
170 t.m_t = newT; | |
171 } | |
172 } | |
173 | |
174 typedef typename scoped_any<T,close_policy,invalid_value,unique>::elemen
t_type element_type; | |
175 | |
176 // return the address of the wrapped pointer | |
177 static element_type* address( scoped_any<T,close_policy,invalid_value,un
ique> & t ) | |
178 { | |
179 // check to make sure the wrapped object is in the invalid state | |
180 assert( !t.valid() ); | |
181 return address_of( t.m_t ); | |
182 } | |
183 }; | |
184 } | |
185 | |
186 // return wrapped resource | |
187 template<typename T,class close_policy,class invalid_value,int unique> | |
188 inline T get( scoped_any<T,close_policy,invalid_value,unique> const & t ) | |
189 { | |
190 return detail::scoped_any_helper<T,close_policy,invalid_value,unique>::get(
t ); | |
191 } | |
192 | |
193 // return true if the scoped_any contains a currently valid resource | |
194 template<typename T,class close_policy,class invalid_value,int unique> | |
195 inline bool valid( scoped_any<T,close_policy,invalid_value,unique> const & t ) | |
196 { | |
197 return t; | |
198 } | |
199 | |
200 // return wrapped resource and give up ownership | |
201 template<typename T,class close_policy,class invalid_value,int unique> | |
202 inline T release( scoped_any<T,close_policy,invalid_value,unique> & t ) | |
203 { | |
204 return detail::scoped_any_helper<T,close_policy,invalid_value,unique>::relea
se( t ); | |
205 } | |
206 | |
207 // destroy designated object and store new resource | |
208 template<typename T,class close_policy,class invalid_value,int unique> | |
209 inline void reset( scoped_any<T,close_policy,invalid_value,unique> & t ) | |
210 { | |
211 typedef typename detail::fixup_invalid_value<invalid_value>:: | |
212 template rebind<T>::type invalid_value_type; | |
213 detail::scoped_any_helper<T,close_policy,invalid_value,unique>::reset( t, in
valid_value_type() ); | |
214 } | |
215 | |
216 // destroy designated object and store new resource | |
217 template<typename T,class close_policy,class invalid_value,int unique,typename U
> | |
218 inline void reset( scoped_any<T,close_policy,invalid_value,unique> & t, U newT ) | |
219 { | |
220 detail::scoped_any_helper<T,close_policy,invalid_value,unique>::reset( t, ne
wT ); | |
221 } | |
222 | |
223 // return the address of the wrapped resource | |
224 // WARNING: this will assert if the value of the resource is | |
225 // anything other than invalid_value. | |
226 template<typename T,class close_policy,class invalid_value,int unique> | |
227 inline typename scoped_any<T,close_policy,invalid_value,unique>::element_type* | |
228 address( scoped_any<T,close_policy,invalid_value,unique> & t ) | |
229 { | |
230 return detail::scoped_any_helper<T,close_policy,invalid_value,unique>::addre
ss( t ); | |
231 } | |
232 | |
233 #pragma warning(pop) | |
234 | |
235 #endif | |
236 | |
237 // This causes the scoped_* typedefs to be defined | |
238 DECLARE_SMART_ANY_TYPEDEFS(scoped) | |
239 | |
240 #if defined(_OBJBASE_H_) & !defined(SCOPED_ANY_CO_INIT) | |
241 # define SCOPED_ANY_CO_INIT | |
242 typedef scoped_any<HRESULT,close_co,co_not_init> sc
oped_co_close; | |
243 | |
244 // Helper class for balancing calls to CoInitialize and CoUninitialize | |
245 struct scoped_co_init | |
246 { | |
247 explicit scoped_co_init( DWORD dwCoInit = COINIT_APARTMENTTHREADED ) | |
248 : m_hr( smart_co_init_helper( dwCoInit ) ) | |
249 { | |
250 } | |
251 HRESULT hresult() const | |
252 { | |
253 return get(m_hr); | |
254 } | |
255 scoped_co_close const m_hr; | |
256 }; | |
257 #endif | |
OLD | NEW |