|
|
|
| 1 |
|
/*** |
| 2 |
|
*event.h |
| 3 |
|
* |
| 4 |
|
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 |
|
* |
| 6 |
|
*Purpose: delegate_proxy_factory class |
| 7 |
|
* |
| 8 |
|
* [Public] |
| 9 |
|
* |
| 10 |
|
****/ |
| 11 |
|
|
| 12 |
|
#pragma once |
| 13 |
|
|
| 14 |
|
#ifndef __cplusplus_cli |
| 15 |
|
#error ERROR: msclr libraries are not compatible with /clr:oldSyntax |
| 16 |
|
#endif |
| 17 |
|
|
| 18 |
|
#include <gcroot.h> |
| 19 |
|
|
| 20 |
|
namespace msclr { |
| 21 |
|
namespace delegate_map { |
| 22 |
|
namespace internal { |
| 23 |
|
|
| 24 |
|
template <typename CLASS> class delegate_proxy_factory |
| 25 |
|
{ |
| 26 |
|
typedef typename CLASS::delegate_proxy_type proxy_type; |
| 27 |
|
gcroot<proxy_type^> m_gc_managed_native_delegate_proxy; |
| 28 |
|
|
| 29 |
|
public: |
| 30 |
|
delegate_proxy_factory() {} |
| 31 |
|
|
| 32 |
|
virtual ~delegate_proxy_factory() |
| 33 |
|
{ |
| 34 |
|
if((proxy_type^)m_gc_managed_native_delegate_proxy != nullptr) |
| 35 |
|
{ |
| 36 |
|
m_gc_managed_native_delegate_proxy->detach(); |
| 37 |
|
} |
| 38 |
|
} |
| 39 |
|
|
| 40 |
|
proxy_type^ get_proxy(CLASS* pNativeTarget) |
| 41 |
|
{ |
| 42 |
|
if((proxy_type^)m_gc_managed_native_delegate_proxy == nullptr) |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
#define BEGIN_DELEGATE_MAP(CLASS)\ |
| 56 |
|
ref class delegate_proxy_type;\ |
| 57 |
|
msclr::delegate_map::internal::delegate_proxy_factory<CLASS> m_delegate_map_proxy;\ |
| 58 |
|
\ |
| 59 |
|
ref class delegate_proxy_type\ |
| 60 |
|
{\ |
| 61 |
|
CLASS* m_p_native_target;\ |
| 62 |
|
public:\ |
| 63 |
|
delegate_proxy_type(CLASS* pNativeTarget) : m_p_native_target(pNativeTarget) {}\ |
| 64 |
|
void detach() { m_p_native_target = NULL; } |
| 65 |
|
|
| 66 |
|
#define EVENT_DELEGATE_ENTRY(MEMBER,ARG0,ARG1)\ |
| 67 |
|
void MEMBER(ARG0 arg0,ARG1 arg1)\ |
| 68 |
|
{\ |
| 69 |
|
if(m_p_native_target == NULL)\ |
| 70 |
|
throw gcnew System::ArgumentNullException("Delegate call failed: Native sink was not attached or has already detached from the managed proxy (m_p_native_target == NULL). Hint: see if native sink was destructed or not constructed properly");\ |
| 71 |
|
\ |
| 72 |
|
m_p_native_target->MEMBER(arg0,arg1);\ |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
#define END_DELEGATE_MAP()\ |
| 76 |
|
}; |
| 77 |
|
|
| 78 |
|
#define MAKE_DELEGATE(DELEGATE,MEMBER)\ |
| 79 |
|
gcnew DELEGATE(m_delegate_map_proxy.get_proxy(this),&delegate_proxy_type::MEMBER) |
| 80 |
|
|
|
|
|