slot 0.0.1
A real time UI render framework
载入中...
搜索中...
未找到
slang-com-helper.h
浏览该文件的文档.
1#ifndef SLANG_COM_HELPER_H
2#define SLANG_COM_HELPER_H
3
7#include "slang.h"
8
9#include <algorithm>
10#include <atomic>
11#include <iterator>
12
13/* !!!!!!!!!!!!!!!!!!!!! Macros to help checking SlangResult !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
14
17#ifndef SLANG_HANDLE_RESULT_FAIL
18 #define SLANG_HANDLE_RESULT_FAIL(x)
19#endif
20
23#define SLANG_RETURN_ON_FAIL(x) \
24 { \
25 SlangResult _res = (x); \
26 if (SLANG_FAILED(_res)) \
27 { \
28 SLANG_HANDLE_RESULT_FAIL(_res); \
29 return _res; \
30 } \
31 }
34#define SLANG_RETURN_VOID_ON_FAIL(x) \
35 { \
36 SlangResult _res = (x); \
37 if (SLANG_FAILED(_res)) \
38 { \
39 SLANG_HANDLE_RESULT_FAIL(_res); \
40 return; \
41 } \
42 }
44#define SLANG_RETURN_FALSE_ON_FAIL(x) \
45 { \
46 SlangResult _res = (x); \
47 if (SLANG_FAILED(_res)) \
48 { \
49 SLANG_HANDLE_RESULT_FAIL(_res); \
50 return false; \
51 } \
52 }
54#define SLANG_RETURN_NULL_ON_FAIL(x) \
55 { \
56 SlangResult _res = (x); \
57 if (SLANG_FAILED(_res)) \
58 { \
59 SLANG_HANDLE_RESULT_FAIL(_res); \
60 return nullptr; \
61 } \
62 }
63
66#define SLANG_ASSERT_ON_FAIL(x) \
67 { \
68 SlangResult _res = (x); \
69 if (SLANG_FAILED(_res)) \
70 { \
71 assert(false); \
72 return _res; \
73 } \
74 }
76#define SLANG_ASSERT_VOID_ON_FAIL(x) \
77 { \
78 SlangResult _res = (x); \
79 if (SLANG_FAILED(_res)) \
80 { \
81 assert(false); \
82 return; \
83 } \
84 }
85
86/* !!!!!!!!!!!!!!!!!!!!!!! C++ helpers !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
87
88#if defined(__cplusplus)
89namespace Slang
90{
91
92// Alias SlangResult to Slang::Result
93typedef SlangResult Result;
94// Alias SlangUUID to Slang::Guid
95typedef SlangUUID Guid;
96
97} // namespace Slang
98
99// Operator == and != for Guid/SlangUUID
100
101SLANG_FORCE_INLINE bool operator==(const Slang::Guid& aIn, const Slang::Guid& bIn)
102{
103 return aIn.data1 == bIn.data1 && aIn.data2 == bIn.data2 && aIn.data3 == bIn.data3 &&
104 std::equal(aIn.data4, aIn.data4 + std::size(aIn.data4), bIn.data4);
105}
106
107SLANG_FORCE_INLINE bool operator!=(const Slang::Guid& a, const Slang::Guid& b)
108{
109 return !(a == b);
110}
111
112 /* !!!!!!!! Macros to simplify implementing COM interfaces !!!!!!!!!!!!!!!!!!!!!!!!!!!! */
113
114 /* Assumes underlying implementation has a member m_refCount that is initialized to 0 and can
115 have ++ and -- operate on it. For SLANG_IUNKNOWN_QUERY_INTERFACE to work - must have a method
116 'getInterface' that returns valid pointers for the Guid, or nullptr if not found. */
117
118 #define SLANG_IUNKNOWN_QUERY_INTERFACE \
119 SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface( \
120 SlangUUID const& uuid, \
121 void** outObject) SLANG_OVERRIDE \
122 { \
123 ISlangUnknown* intf = getInterface(uuid); \
124 if (intf) \
125 { \
126 addRef(); \
127 *outObject = intf; \
128 return SLANG_OK; \
129 } \
130 return SLANG_E_NO_INTERFACE; \
131 }
132
133 #define SLANG_IUNKNOWN_ADD_REF \
134 SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE \
135 { \
136 return ++m_refCount; \
137 }
138
139 #define SLANG_IUNKNOWN_RELEASE \
140 SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE \
141 { \
142 --m_refCount; \
143 if (m_refCount == 0) \
144 { \
145 delete this; \
146 return 0; \
147 } \
148 return m_refCount; \
149 }
150
151 #define SLANG_IUNKNOWN_ALL \
152 SLANG_IUNKNOWN_QUERY_INTERFACE \
153 SLANG_IUNKNOWN_ADD_REF \
154 SLANG_IUNKNOWN_RELEASE
155
156 // ------------------------ RefObject IUnknown -----------------------------
157
158 #define SLANG_REF_OBJECT_IUNKNOWN_QUERY_INTERFACE \
159 SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface( \
160 SlangUUID const& uuid, \
161 void** outObject) SLANG_OVERRIDE \
162 { \
163 void* intf = getInterface(uuid); \
164 if (intf) \
165 { \
166 addReference(); \
167 *outObject = intf; \
168 return SLANG_OK; \
169 } \
170 return SLANG_E_NO_INTERFACE; \
171 }
172
173 #define SLANG_REF_OBJECT_IUNKNOWN_ADD_REF \
174 SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE \
175 { \
176 return (uint32_t)addReference(); \
177 }
178 #define SLANG_REF_OBJECT_IUNKNOWN_RELEASE \
179 SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE \
180 { \
181 return (uint32_t)releaseReference(); \
182 }
183
184 #define SLANG_REF_OBJECT_IUNKNOWN_ALL \
185 SLANG_REF_OBJECT_IUNKNOWN_QUERY_INTERFACE \
186 SLANG_REF_OBJECT_IUNKNOWN_ADD_REF \
187 SLANG_REF_OBJECT_IUNKNOWN_RELEASE
188
189#endif // defined(__cplusplus)
190
191#endif
bool operator==(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.hpp:14737
bool operator!=(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.hpp:14762
Definition slang-com-ptr.h:10
int32_t SlangResult
Definition slang-cpp-prelude.h:300
#define SLANG_FORCE_INLINE
Definition slang-cpp-prelude.h:286
Definition slang-cpp-prelude.h:293
uint32_t data1
Definition slang-cpp-prelude.h:294