STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
auto_handle.h
Go to the documentation of this file.
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 /* __cplusplus_cli */
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
33 
34  public:
35 
36  // Constructors
37 
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
51  : m_handle( _right.release() )
52  {
53  }
54 
55  template<typename _other_type>
57  : m_handle( _right.release() )
58  {
59  }
60 
61  // assign compatible _right
64  {
65  reset( _right.release() );
66  return *this;
67  }
68 
69 
70  template<typename _other_type>
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 
89  // for use when auto_handle appears in a conditional
91  {
93  }
94 
95  // for use when auto_handle appears in a conditional
96  bool operator!()
97  {
98  return ! valid();
99  }
100 
101 
102  template<typename _other_type>
104  {
105  return auto_handle<_other_type>( *this );
106  }
107 
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
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>
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 /* !defined (_INC_MSCLR_AUTO_HANDLE) */
auto_handle(auto_handle< _other_type >%_right)
Definition: auto_handle.h:56
void swap(auto_gcroot< _element_type > &_left, auto_gcroot< _element_type > &_right)
Definition: auto_gcroot.h:240
_element_type release()
Definition: auto_handle.h:132
System::String _safe_bool
Definition: safebool.h:37
bool operator==(_detail_class::_safe_bool)
auto_handle< _element_type > operator=(auto_handle< _element_type >%_right)
Definition: auto_handle.h:62
void reset(_element_type^_new_ptr)
Definition: auto_handle.h:115
Definition: gcroot.h:43
auto_handle< _element_type > operator=(auto_handle< _other_type >%_right)
Definition: auto_handle.h:71
static _safe_bool const _safe_false
Definition: safebool.h:39
bool operator!=(_detail_class::_safe_bool)
auto_handle()
Definition: auto_handle.h:38
static _safe_bool const _safe_true
Definition: safebool.h:38
auto_handle(auto_handle< _element_type >%_right)
Definition: auto_handle.h:50
~auto_handle()
Definition: auto_handle.h:140
void swap(auto_handle< _element_type >%_right)
Definition: auto_handle.h:108
Definition: auto_handle.h:27
auto_handle(_element_type^_ptr)
Definition: auto_handle.h:44
bool operator!()
Definition: auto_handle.h:96
bool valid()
Definition: auto_handle.h:150
_element_type m_handle
Definition: auto_handle.h:158
void reset()
Definition: auto_handle.h:127
_element_type operator->()
Definition: auto_handle.h:84