source: trunk/Utilities/DirectShowReferenceSource/Sample/Unity01/RenderingPlugin/Unity/IUnityInterface.h @ 638

Last change on this file since 638 was 638, checked in by roman, 8 years ago

Media source texture reader and Unity 3D sample project

File size: 4.9 KB
Line 
1#pragma once
2
3// Unity native plugin API
4// Compatible with C99
5
6#if defined(__CYGWIN32__)
7        #define UNITY_INTERFACE_API __stdcall
8        #define UNITY_INTERFACE_EXPORT __declspec(dllexport)
9#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(WINAPI_FAMILY)
10        #define UNITY_INTERFACE_API __stdcall
11        #define UNITY_INTERFACE_EXPORT __declspec(dllexport)
12#elif defined(__MACH__) || defined(__ANDROID__) || defined(__linux__) || defined(__QNX__)
13        #define UNITY_INTERFACE_API
14        #define UNITY_INTERFACE_EXPORT
15#else
16        #define UNITY_INTERFACE_API
17        #define UNITY_INTERFACE_EXPORT
18#endif
19
20
21
22// Unity Interface GUID
23// Ensures cross plugin uniqueness.
24//
25// Template specialization is used to produce a means of looking up a GUID from it's payload type at compile time.
26// The net result should compile down to passing around the GUID.
27//
28// UNITY_REGISTER_INTERFACE_GUID should be placed in the header file of any payload definition outside of all namespaces.
29// The payload structure and the registration GUID are all that is required to expose the interface to other systems.
30struct UnityInterfaceGUID
31{
32#ifdef __cplusplus
33        UnityInterfaceGUID(unsigned long long high, unsigned long long low)
34        : m_GUIDHigh(high)
35        , m_GUIDLow(low)
36        {
37        }
38
39        UnityInterfaceGUID(const UnityInterfaceGUID& other)
40        {
41                m_GUIDHigh = other.m_GUIDHigh;
42                m_GUIDLow  = other.m_GUIDLow;
43        }
44
45        UnityInterfaceGUID& operator=(const UnityInterfaceGUID& other)
46        {
47                m_GUIDHigh = other.m_GUIDHigh;
48                m_GUIDLow  = other.m_GUIDLow;
49                return *this;
50        }
51
52        bool Equals(const UnityInterfaceGUID& other)   const { return m_GUIDHigh == other.m_GUIDHigh && m_GUIDLow == other.m_GUIDLow; }
53        bool LessThan(const UnityInterfaceGUID& other) const { return m_GUIDHigh < other.m_GUIDHigh || (m_GUIDHigh == other.m_GUIDHigh && m_GUIDLow < other.m_GUIDLow); }
54#endif
55        unsigned long long m_GUIDHigh;
56        unsigned long long m_GUIDLow;
57};
58#ifdef __cplusplus
59inline bool operator==(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return left.Equals(right); }
60inline bool operator!=(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return !left.Equals(right); }
61inline bool operator< (const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return left.LessThan(right); }
62inline bool operator> (const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return right.LessThan(left); }
63inline bool operator>=(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return !operator< (left,right); }
64inline bool operator<=(const UnityInterfaceGUID& left, const UnityInterfaceGUID& right) { return !operator> (left,right); }
65#else
66typedef struct UnityInterfaceGUID UnityInterfaceGUID;
67#endif
68
69
70
71#define UNITY_GET_INTERFACE_GUID(TYPE) TYPE##_GUID
72#define UNITY_GET_INTERFACE(INTERFACES, TYPE) (TYPE*)INTERFACES->GetInterface(UNITY_GET_INTERFACE_GUID(TYPE));
73
74#ifdef __cplusplus
75        #define UNITY_DECLARE_INTERFACE(NAME) \
76        struct NAME : IUnityInterface
77
78        template<typename TYPE>                                        \
79        inline const UnityInterfaceGUID GetUnityInterfaceGUID();       \
80
81        #define UNITY_REGISTER_INTERFACE_GUID(HASHH, HASHL, TYPE)      \
82        const UnityInterfaceGUID TYPE##_GUID(HASHH, HASHL);            \
83        template<>                                                     \
84        inline const UnityInterfaceGUID GetUnityInterfaceGUID<TYPE>()  \
85        {                                                              \
86            return UNITY_GET_INTERFACE_GUID(TYPE);                     \
87        }
88#else
89        #define UNITY_DECLARE_INTERFACE(NAME) \
90        typedef struct NAME NAME;             \
91        struct NAME
92
93        #define UNITY_REGISTER_INTERFACE_GUID(HASHH, HASHL, TYPE) \
94        const UnityInterfaceGUID TYPE##_GUID = {HASHH, HASHL};
95#endif
96
97
98
99#ifdef __cplusplus
100struct IUnityInterface
101{
102};
103#else
104typedef void IUnityInterface;
105#endif
106
107
108
109typedef struct IUnityInterfaces
110{
111        // Returns an interface matching the guid.
112        // Returns nullptr if the given interface is unavailable in the active Unity runtime.
113        IUnityInterface* (UNITY_INTERFACE_API * GetInterface)(UnityInterfaceGUID guid);
114
115        // Registers a new interface.
116        void (UNITY_INTERFACE_API * RegisterInterface)(UnityInterfaceGUID guid, IUnityInterface* ptr);
117
118#ifdef __cplusplus
119        // Helper for GetInterface.
120        template <typename INTERFACE>
121        INTERFACE* Get()
122        {
123                return static_cast<INTERFACE*>(GetInterface(GetUnityInterfaceGUID<INTERFACE>()));
124        }
125
126        // Helper for RegisterInterface.
127        template <typename INTERFACE>
128        void Register(IUnityInterface* ptr)
129        {
130                RegisterInterface(GetUnityInterfaceGUID<INTERFACE>(), ptr);
131        }
132#endif
133} IUnityInterfaces;
134
135
136
137#ifdef __cplusplus
138extern "C" {
139#endif
140
141// If exported by a plugin, this function will be called when the plugin is loaded.
142void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces);
143// If exported by a plugin, this function will be called when the plugin is about to be unloaded.
144void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload();
145
146#ifdef __cplusplus
147}
148#endif
Note: See TracBrowser for help on using the repository browser.