Skip to content

Latest commit

 

History

History
701 lines (537 loc) · 13.6 KB

auto-handle-class.md

File metadata and controls

701 lines (537 loc) · 13.6 KB
descriptiontitlems.datems.topicf1_keywordshelpviewer_keywordsms.assetid
Learn more about: auto_handle Class
auto_handle Class
01/16/2019
reference
msclr::auto_handle::auto_handle
msclr::auto_handle::get
msclr::auto_handle::release
msclr::auto_handle::reset
msclr::auto_handle::swap
msclr::auto_handle::operator->
msclr::auto_handle::operator=
msclr::auto_handle::operator auto_handle
msclr::auto_handle::operator!
msclr::auto_handle class
a65604d1-ecbb-44fd-ae2f-696ddeeed9d6

auto_handle Class

Automatic resource management, which can be used to embed a virtual handle into a managed type.

Syntax

template<typename _element_type> ref classauto_handle;

Parameters

_element_type
The managed type to be embedded.

Members

Public constructors

NameDescription
auto_handle::auto_handleThe auto_handle constructor.
auto_handle::~auto_handleThe auto_handle destructor.

Public methods

NameDescription
auto_handle::getGets the contained object.
auto_handle::releaseReleases the object from auto_handle management.
auto_handle::resetDestroy the current owned object and optionally take possession of a new object.
auto_handle::swapSwaps objects with another auto_handle.

Public operators

NameDescription
auto_handle::operator->The member access operator.
auto_handle::operator=Assignment operator.
auto_handle::operator auto_handleType-cast operator between auto_handle and compatible types.
auto_handle::operator boolOperator for using auto_handle in a conditional expression.
auto_handle::operator!Operator for using auto_handle in a conditional expression.

Requirements

Header file <msclr\auto_handle.h>

Namespace msclr

auto_handle::auto_handle

The auto_handle constructor.

auto_handle(); auto_handle( _element_type ^ _ptr ); auto_handle( auto_handle<_element_type> % _right ); template<typename _other_type> auto_handle( auto_handle<_other_type> % _right );

Parameters

_ptr
The object to own.

_right
An existing auto_handle.

Example

// msl_auto_handle_auto_handle.cpp// compile with: /clr #include"msclr\auto_handle.h"usingnamespaceSystem;usingnamespacemsclr; ref classRefClassA { protected: String^ m_s; public:RefClassA(String^ s) : m_s(s) { Console::WriteLine( "in RefClassA constructor: " + m_s ); } ~RefClassA() { Console::WriteLine( "in RefClassA destructor: " + m_s ); } virtualvoidPrintHello() { Console::WriteLine( "Hello from {0} A!", m_s ); } }; ref classRefClassB : RefClassA { public:RefClassB( String^ s ) : RefClassA( s ) {} virtualvoidPrintHello() new { Console::WriteLine( "Hello from {0} B!", m_s ); } }; intmain() { { auto_handle<RefClassA> a(gcnew RefClassA( "first" ) ); a->PrintHello(); } { auto_handle<RefClassB> b(gcnew RefClassB( "second" ) ); b->PrintHello(); auto_handle<RefClassA> a(b); //construct from derived type a->PrintHello(); auto_handle<RefClassA> a2(a); //construct from same type a2->PrintHello(); } Console::WriteLine("done"); }
in RefClassA constructor: first Hello from first A! in RefClassA destructor: first in RefClassA constructor: second Hello from second B! Hello from second A! Hello from second A! in RefClassA destructor: second done 

auto_handle::~auto_handle

The auto_handle destructor.

~auto_handle();

Remarks

The destructor also destructs the owned object.

Example

// msl_auto_handle_dtor.cpp// compile with: /clr #include"msclr\auto_handle.h"usingnamespaceSystem;usingnamespacemsclr; ref classClassA { public:ClassA() { Console::WriteLine( "ClassA constructor" ); } ~ClassA() { Console::WriteLine( "ClassA destructor" ); } }; intmain() { // create a new scope for a: { auto_handle<ClassA> a = gcnew ClassA; } // a goes out of scope here, invoking its destructor// which in turns destructs the ClassA object.Console::WriteLine( "done" ); }
ClassA constructor ClassA destructor done 

auto_handle::get

Gets the contained object.

_element_type ^ get();

Return value

The contained object.

Example

// msl_auto_handle_get.cpp// compile with: /clr #include"msclr\auto_handle.h"usingnamespaceSystem;usingnamespacemsclr; ref classClassA { String^ m_s; public:ClassA( String^ s ) : m_s( s ){ Console::WriteLine( "in ClassA constructor:" + m_s ); } ~ClassA() { Console::WriteLine( "in ClassA destructor:" + m_s ); } voidPrintHello() { Console::WriteLine( "Hello from {0} A!", m_s ); } }; voidPrintA( ClassA^ a ) { a->PrintHello(); } intmain() { auto_handle<ClassA> a = gcnew ClassA( "first" ); a->PrintHello(); ClassA^ a2 = a.get(); a2->PrintHello(); PrintA( a.get() ); }
in ClassA constructor:first Hello from first A! Hello from first A! Hello from first A! in ClassA destructor:first 

auto_handle::release

Releases the object from auto_handle management.

_element_type ^ release();

Return value

The released object.

Example

// msl_auto_handle_release.cpp// compile with: /clr #include<msclr\auto_handle.h>usingnamespaceSystem;usingnamespacemsclr; ref classClassA { String^ m_s; public:ClassA( String^ s ) : m_s( s ) { Console::WriteLine( "ClassA constructor: " + m_s ); } ~ClassA() { Console::WriteLine( "ClassA destructor: " + m_s ); } voidPrintHello() { Console::WriteLine( "Hello from {0} A!", m_s ); } }; intmain() { ClassA^ a; // create a new scope: { auto_handle<ClassA> agc1 = gcnew ClassA( "first" ); auto_handle<ClassA> agc2 = gcnew ClassA( "second" ); a = agc1.release(); } // agc1 and agc2 go out of scope here a->PrintHello(); Console::WriteLine( "done" ); }
ClassA constructor: first ClassA constructor: second ClassA destructor: second Hello from first A! done 

auto_handle::reset

Destroy the current owned object and optionally take possession of a new object.

voidreset( _element_type ^ _new_ptr ); voidreset();

Parameters

_new_ptr
(Optional) The new object.

Example

// msl_auto_handle_reset.cpp// compile with: /clr #include<msclr\auto_handle.h>usingnamespaceSystem;usingnamespacemsclr; ref classClassA { String^ m_s; public:ClassA( String^ s ) : m_s( s ) { Console::WriteLine( "ClassA constructor: " + m_s ); } ~ClassA() { Console::WriteLine( "ClassA destructor: " + m_s ); } voidPrintHello() { Console::WriteLine( "Hello from {0} A!", m_s ); } }; intmain() { auto_handle<ClassA> agc1 = gcnew ClassA( "first" ); agc1->PrintHello(); ClassA^ ha = gcnew ClassA( "second" ); agc1.reset( ha ); // release first object, reference second agc1->PrintHello(); agc1.reset(); // release second object, set to nullptrConsole::WriteLine( "done" ); }
ClassA constructor: first Hello from first A! ClassA constructor: second ClassA destructor: first Hello from second A! ClassA destructor: second done 

auto_handle::swap

Swaps objects with another auto_handle.

voidswap( auto_handle<_element_type> % _right );

Parameters

_right
The auto_handle with which to swap objects.

Example

// msl_auto_handle_swap.cpp// compile with: /clr #include<msclr\auto_handle.h>usingnamespaceSystem;usingnamespacemsclr;intmain() { auto_handle<String> s1 = "string one"; auto_handle<String> s2 = "string two"; Console::WriteLine( "s1 = '{0}', s2 = '{1}'", s1->ToString(), s2->ToString() ); s1.swap( s2 ); Console::WriteLine( "s1 = '{0}', s2 = '{1}'", s1->ToString(), s2->ToString() ); }
s1 = 'string one', s2 = 'string two' s1 = 'string two', s2 = 'string one' 

auto_handle::operator->

The member access operator.

_element_type ^ operator->();

Return value

The object that's wrapped by auto_handle.

Example

// msl_auto_handle_op_arrow.cpp// compile with: /clr #include<msclr\auto_handle.h>usingnamespaceSystem;usingnamespacemsclr; ref classClassA { protected: String^ m_s; public:ClassA( String^ s ) : m_s( s ) {} virtualvoidPrintHello() { Console::WriteLine( "Hello from {0} A!", m_s ); } int m_i; }; intmain() { auto_handle<ClassA> a( gcnew ClassA( "first" ) ); a->PrintHello(); a->m_i = 5; Console::WriteLine( "a->m_i = {0}", a->m_i ); }
Hello from first A! a->m_i = 5 

auto_handle::operator=

Assignment operator.

auto_handle<_element_type> % operator=( auto_handle<_element_type> % _right ); template<typename _other_type> auto_handle<_element_type> % operator=( auto_handle<_other_type> % _right );

Parameters

_right
The auto_handle to be assigned to the current auto_handle.

Return value

The current auto_handle, now owning _right.

Example

// msl_auto_handle_op_assign.cpp// compile with: /clr #include<msclr\auto_handle.h>usingnamespaceSystem;usingnamespacemsclr; ref classClassA { protected: String^ m_s; public:ClassA(String^ s) : m_s(s) { Console::WriteLine( "in ClassA constructor: " + m_s ); } ~ClassA() { Console::WriteLine( "in ClassA destructor: " + m_s ); } virtualvoidPrintHello() { Console::WriteLine( "Hello from {0} A!", m_s ); } }; ref classClassB : ClassA { public:ClassB( String^ s ) : ClassA( s ) {} virtualvoidPrintHello() new { Console::WriteLine( "Hello from {0} B!", m_s ); } }; intmain() { auto_handle<ClassA> a; auto_handle<ClassA> a2(gcnew ClassA( "first" ) ); a = a2; // assign from same type a->PrintHello(); auto_handle<ClassB> b(gcnew ClassB( "second" ) ); b->PrintHello(); a = b; // assign from derived type a->PrintHello(); Console::WriteLine("done"); }
in ClassA constructor: first Hello from first A! in ClassA constructor: second Hello from second B! in ClassA destructor: first Hello from second A! done in ClassA destructor: second 

auto_handle::operator auto_handle

Type-cast operator between auto_handle and compatible types.

template<typename _other_type> operator auto_handle<_other_type>();

Return value

The current auto_handle cast to auto_handle<_other_type>.

Example

// msl_auto_handle_op_auto_handle.cpp// compile with: /clr #include<msclr\auto_handle.h>usingnamespaceSystem;usingnamespacemsclr; ref classClassA { protected: String^ m_s; public:ClassA( String^ s ) : m_s( s ) {} virtualvoidPrintHello() { Console::WriteLine( "Hello from {0} A!", m_s ); } }; ref classClassB : ClassA { public:ClassB( String ^ s) : ClassA( s ) {} virtualvoidPrintHello() new { Console::WriteLine( "Hello from {0} B!", m_s ); } }; intmain() { auto_handle<ClassB> b = gcnew ClassB("first"); b->PrintHello(); auto_handle<ClassA> a = (auto_handle<ClassA>)b; a->PrintHello(); }
Hello from first B! Hello from first A! 

auto_handle::operator bool

Operator for using auto_handle in a conditional expression.

operatorbool();

Return value

true if the wrapped object is valid; false otherwise.

Remarks

This operator actually converts to _detail_class::_safe_bool which is safer than bool because it can't be converted to an integral type.

Example

// msl_auto_handle_operator_bool.cpp// compile with: /clr #include<msclr\auto_handle.h>usingnamespaceSystem;usingnamespacemsclr;intmain() { auto_handle<String> s1; auto_handle<String> s2 = "hi"; if ( s1 ) Console::WriteLine( "s1 is valid" ); if ( !s1 ) Console::WriteLine( "s1 is invalid" ); if ( s2 ) Console::WriteLine( "s2 is valid" ); if ( !s2 ) Console::WriteLine( "s2 is invalid" ); s2.reset(); if ( s2 ) Console::WriteLine( "s2 is now valid" ); if ( !s2 ) Console::WriteLine( "s2 is now invalid" ); }
s1 is invalid s2 is valid s2 is now invalid 

auto_handle::operator!

Operator for using auto_handle in a conditional expression.

booloperator!();

Return value

true if the wrapped object is invalid; false otherwise.

Example

// msl_auto_handle_operator_not.cpp// compile with: /clr #include<msclr\auto_handle.h>usingnamespaceSystem;usingnamespacemsclr;intmain() { auto_handle<String> s1; auto_handle<String> s2 = "something"; if ( s1) Console::WriteLine( "s1 is valid" ); if ( !s1 ) Console::WriteLine( "s1 is invalid" ); if ( s2 ) Console::WriteLine( "s2 is valid" ); if ( !s2 ) Console::WriteLine( "s2 is invalid" ); s2.reset(); if ( s2 ) Console::WriteLine( "s2 is now valid" ); if ( !s2 ) Console::WriteLine( "s2 is now invalid" ); }
s1 is invalid s2 is valid s2 is now invalid 
close