-
Notifications
You must be signed in to change notification settings - Fork 47
/
event.h
executable file
·79 lines (65 loc) · 2.43 KB
/
event.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/***
*event.h
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose: delegate_proxy_factory class
*
* [Public]
*
****/
#pragma once
#ifndef __cplusplus_cli
#error ERROR: msclr libraries are not compatible with /clr:oldSyntax
#endif /* __cplusplus_cli */
#include <gcroot.h>
namespace msclr {
namespace delegate_map {
namespace internal {
template <typename CLASS> class delegate_proxy_factory
{
typedef typename CLASS::delegate_proxy_type proxy_type;
gcroot<proxy_type^> m_gc_managed_native_delegate_proxy;
public:
delegate_proxy_factory() {}
virtual ~delegate_proxy_factory()
{
if((proxy_type^)m_gc_managed_native_delegate_proxy != nullptr)
{
m_gc_managed_native_delegate_proxy->detach();
}
}
proxy_type^ get_proxy(CLASS* pNativeTarget)
{
if((proxy_type^)m_gc_managed_native_delegate_proxy == nullptr)
{
m_gc_managed_native_delegate_proxy = gcnew proxy_type(pNativeTarget);
}
return (proxy_type^)m_gc_managed_native_delegate_proxy;
}
};
}
}
}
#define BEGIN_DELEGATE_MAP(CLASS)\
ref class delegate_proxy_type;\
msclr::delegate_map::internal::delegate_proxy_factory<CLASS> m_delegate_map_proxy;\
\
ref class delegate_proxy_type\
{\
CLASS* m_p_native_target;\
public:\
delegate_proxy_type(CLASS* pNativeTarget) : m_p_native_target(pNativeTarget) {}\
void detach() { m_p_native_target = NULL; }
#define EVENT_DELEGATE_ENTRY(MEMBER,ARG0,ARG1)\
void MEMBER(ARG0 arg0,ARG1 arg1)\
{\
if(m_p_native_target == NULL)\
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");\
\
m_p_native_target->MEMBER(arg0,arg1);\
}
#define END_DELEGATE_MAP()\
};
#define MAKE_DELEGATE(DELEGATE,MEMBER)\
gcnew DELEGATE(m_delegate_map_proxy.get_proxy(this),&delegate_proxy_type::MEMBER)