| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| #pragma once
|
|
|
| #if !defined (_INC_MSCLR_GCROOT)
|
| #define _INC_MSCLR_GCROOT
|
| #include <yvals_core.h>
|
| #if _STL_COMPILER_PREPROCESSOR
|
|
|
| #include <stddef.h>
|
|
|
|
|
| #ifdef __cplusplus_cli
|
| #define __GCHANDLE_TO_VOIDPTR(x) ((GCHandle::operator System::IntPtr(x)).ToPointer())
|
| #define __VOIDPTR_TO_GCHANDLE(x) (GCHandle::operator GCHandle(System::IntPtr(x)))
|
| #define __NULLPTR nullptr
|
| #else
|
| #define __GCHANDLE_TO_VOIDPTR(x) ((GCHandle::op_Explicit(x)).ToPointer())
|
| #define __VOIDPTR_TO_GCHANDLE(x) (GCHandle::op_Explicit(x))
|
| #define __NULLPTR 0
|
| #endif
|
|
|
| #ifndef __DEFINE_GCROOT_IN_GLOBAL_NAMESPACE
|
| namespace msclr
|
| {
|
| #endif
|
|
|
| template <class T> struct gcroot {
|
|
|
| typedef System::Runtime::InteropServices::GCHandle GCHandle;
|
|
|
|
|
|
|
|
|
| [System::Diagnostics::DebuggerStepThroughAttribute]
|
| [System::Security::SecuritySafeCritical]
|
| gcroot() {
|
| _handle = __GCHANDLE_TO_VOIDPTR(GCHandle::Alloc(__NULLPTR));
|
| }
|
|
|
|
|
|
|
|
|
| gcroot(T t) {
|
| _handle = __GCHANDLE_TO_VOIDPTR(GCHandle::Alloc(t));
|
| }
|
|
|
| gcroot(const gcroot& r) {
|
|
|
| _handle = __GCHANDLE_TO_VOIDPTR(
|
| GCHandle::Alloc(
|
| __VOIDPTR_TO_GCHANDLE(r._handle).Target ));
|
| }
|
|
|
|
|
|
|
|
|
| [System::Diagnostics::DebuggerStepThroughAttribute]
|
| [System::Security::SecurityCritical]
|
| ~gcroot() {
|
| GCHandle g = __VOIDPTR_TO_GCHANDLE(_handle);
|
| g.Free();
|
| _handle = 0;
|
| }
|
|
|
| [System::Diagnostics::DebuggerStepThroughAttribute]
|
| [System::Security::SecurityCritical]
|
| gcroot& operator=(T t) {
|
|
|
| __VOIDPTR_TO_GCHANDLE(_handle).Target = t;
|
| return *this;
|
| }
|
|
|
| gcroot& operator=(const gcroot &r) {
|
|
|
| T t = (T)r;
|
| __VOIDPTR_TO_GCHANDLE(_handle).Target = t;
|
| return *this;
|
| }
|
|
|
| void swap( gcroot<T> & _right )
|
| {
|
| void* t = _handle;
|
| _handle = _right._handle;
|
| _right._handle = t;
|
| }
|
|
|
|
|
| [System::Security::SecuritySafeCritical]
|
| operator T () const {
|
|
|
| return static_cast<T>( __VOIDPTR_TO_GCHANDLE(_handle).Target );
|
| }
|
|
|
|
|
|
|
| [System::Security::SecuritySafeCritical]
|
| T operator->() const {
|
|
|
| return static_cast<T>(__VOIDPTR_TO_GCHANDLE(_handle).Target);
|
| }
|
|
|
| private:
|
|
|
|
|
|
|
|
|
|
|
|
|
| void* _handle;
|
| T* operator& ();
|
| };
|
|
|
| template<typename T>
|
| void swap( gcroot<T> & _left,
|
| gcroot<T> & _right )
|
| {
|
| _left.swap( _right );
|
| }
|
|
|
| #ifndef __DEFINE_GCROOT_IN_GLOBAL_NAMESPACE
|
| }
|
| #endif
|
|
|
| #undef __GCHANDLE_TO_VOIDPTR
|
| #undef __VOIDPTR_TO_GCHANDLE
|
| #undef __NULLPTR
|
|
|
| #endif
|
| #endif
|
|
|