|
|
|
1 |
|
/*** |
2 |
|
*auto_handle.h |
3 |
|
* |
4 |
|
* Copyright (c) Microsoft Corporation. All rights reserved. |
5 |
|
* |
6 |
|
*Purpose: automatic resource management, like std::auto_ptr for ref classes |
7 |
|
* |
8 |
|
* [Public] |
9 |
|
* |
10 |
|
****/ |
11 |
|
|
12 |
|
#pragma once |
13 |
|
|
14 |
|
#if !defined(_INC_MSCLR_AUTO_HANDLE) |
15 |
|
|
16 |
|
#ifndef __cplusplus_cli |
17 |
|
#error ERROR: msclr libraries are not compatible with /clr:oldSyntax |
18 |
|
#endif |
19 |
|
|
20 |
|
#include <msclr\safebool.h> |
21 |
|
|
22 |
|
namespace msclr |
23 |
|
{ |
24 |
|
|
25 |
|
// wrap a resource to enforce strict ownership and ensure proper cleanup |
26 |
|
template<typename _element_type> |
27 |
|
ref class auto_handle |
28 |
|
{ |
29 |
|
private: |
30 |
|
// disallow explicit comparisons to _safe_bool |
31 |
|
bool operator==( _detail_class::_safe_bool ); |
32 |
|
bool operator!=( _detail_class::_safe_bool ); |
33 |
|
|
34 |
|
public: |
35 |
|
|
36 |
|
// Constructors |
37 |
|
|
38 |
|
auto_handle() |
39 |
|
: m_handle( nullptr ) |
40 |
|
{ |
41 |
|
} |
42 |
|
|
43 |
|
// construct from object pointer |
44 |
|
auto_handle( _element_type ^ _ptr ) |
45 |
|
: m_handle( _ptr ) |
46 |
|
{ |
47 |
|
} |
48 |
|
|
49 |
|
// construct by assuming pointer from _right auto_handle |
50 |
|
auto_handle(auto_handle<_element_type> % _right ) |
51 |
|
: m_handle( _right.release() ) |
52 |
|
{ |
53 |
|
} |
54 |
|
|
55 |
|
template<typename _other_type> |
56 |
|
auto_handle( auto_handle<_other_type> % _right ) |
57 |
|
: m_handle( _right.release() ) |
58 |
|
{ |
59 |
|
} |
60 |
|
|
61 |
|
// assign compatible _right |
62 |
|
auto_handle<_element_type> % operator=( |
63 |
|
auto_handle<_element_type> % _right ) |
64 |
|
{ |
65 |
|
reset( _right.release() ); |
66 |
|
return *this; |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
|
template<typename _other_type> |
71 |
|
auto_handle<_element_type> % operator=( |
72 |
|
auto_handle<_other_type> % _right ) |
73 |
|
{ |
74 |
|
reset( _right.release() ); |
75 |
|
return *this; |
76 |
|
} |
77 |
|
|
78 |
|
_element_type ^ get() |
79 |
|
{ |
80 |
|
return m_handle; |
81 |
|
} |
82 |
|
|
83 |
|
// return pointer to class object (assume pointer) |
84 |
|
_element_type ^ operator->() |
85 |
|
{ |
86 |
|
return m_handle; |
87 |
|
} |
88 |
|
|
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
|
template<typename _other_type> |
103 |
|
operator auto_handle<_other_type>() |
104 |
|
{ |
105 |
|
return auto_handle<_other_type>( *this ); |
106 |
|
} |
107 |
|
|
108 |
|
void swap( auto_handle<_element_type> % _right ) |
109 |
|
{ |
110 |
|
auto_handle<_element_type> tmp = _right; |
111 |
|
_right = *this; |
112 |
|
*this = tmp; |
113 |
|
} |
114 |
|
|
115 |
|
void reset( _element_type ^ _new_ptr ) |
116 |
|
{ |
117 |
|
if( m_handle != _new_ptr ) |
118 |
|
{ |
119 |
|
if( valid() ) |
120 |
|
{ |
121 |
|
delete m_handle; |
122 |
|
} |
123 |
|
m_handle = _new_ptr; |
124 |
|
} |
125 |
|
} |
126 |
|
|
127 |
|
void reset( ) |
128 |
|
{ |
129 |
|
reset(nullptr); |
130 |
|
} |
131 |
|
|
132 |
|
_element_type ^ release() |
133 |
|
{ |
134 |
|
_element_type ^_tmp_ptr = m_handle; |
135 |
|
m_handle = nullptr; |
136 |
|
return _tmp_ptr; |
137 |
|
} |
138 |
|
|
139 |
|
// destroy the object |
140 |
|
~auto_handle() |
141 |
|
{ |
142 |
|
if( valid() ) |
143 |
|
{ |
144 |
|
delete m_handle; |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
private: |
149 |
|
|
150 |
|
bool valid() |
151 |
|
{ |
152 |
|
// see if the managed resource is in the invalid state. |
153 |
|
return m_handle != nullptr; |
154 |
|
|
155 |
|
} |
156 |
|
|
157 |
|
// the wrapped object |
158 |
|
_element_type ^ m_handle; |
159 |
|
}; |
160 |
|
|
161 |
|
// swap the contents of two auto_handle objects |
162 |
|
template<typename _element_type> |
163 |
|
void swap( auto_handle<_element_type> % _left, |
164 |
|
auto_handle<_element_type> % _right ) |
165 |
|
{ |
166 |
|
_left.swap( _right ); |
167 |
|
} |
168 |
|
|
169 |
|
} // namespace msclr |
170 |
|
|
171 |
|
#define _INC_MSCLR_AUTO_HANDLE |
172 |
|
|
173 |
|
#endif // _INC_MSCLR_AUTO_HANDLE |
174 |
|
|
|
|
|