-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGmpiSdkCommon.h
253 lines (221 loc) · 5.26 KB
/
GmpiSdkCommon.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#pragma once
#ifndef _GMPI_SDK_COMMON_H_INCLUDED // ignore source file in multiple locations.
#define _GMPI_SDK_COMMON_H_INCLUDED
/*
GMPI - Generalized Music Plugin Interface specification.
Copyright 2007-2024 Jeff McClintock.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
#include <string>
#include "GmpiApiCommon.h"
#include "RefCountMacros.h"
namespace gmpi
{
// Helper for managing lifetime of reference-counted interface pointer
template<class wrappedObjT>
class shared_ptr
{
wrappedObjT* obj = {};
public:
shared_ptr(){}
explicit shared_ptr(wrappedObjT* newobj)
{
assign(newobj);
}
shared_ptr(const shared_ptr<wrappedObjT>& value)
{
assign(value.obj);
}
// Attach object without incrementing ref count. For objects created with new.
void attach(wrappedObjT* newobj)
{
auto old = obj;
obj = newobj;
if( old )
{
old->release();
}
}
~shared_ptr()
{
if( obj )
{
obj->release();
}
}
operator wrappedObjT*( )
{
return obj;
}
const wrappedObjT* operator=( wrappedObjT* value )
{
assign(value);
return value;
}
shared_ptr<wrappedObjT>& operator=( shared_ptr<wrappedObjT>& value )
{
assign(value.get());
return *this;
}
bool operator==( const wrappedObjT* other ) const
{
return obj == other;
}
bool operator==( const shared_ptr<wrappedObjT>& other ) const
{
return obj == other.obj;
}
wrappedObjT* operator->( ) const
{
return obj;
}
wrappedObjT*& get()
{
return obj;
}
wrappedObjT** getAddressOf()
{
assert(obj == nullptr); // Free it before you re-use it!
return &obj;
}
wrappedObjT** put()
{
// Free it before you re-use it
if (obj){obj->release();}
obj = nullptr;
return &obj;
}
void** put_void() noexcept
{
return reinterpret_cast<void**>(put());
}
gmpi::api::IUnknown** asIUnknownPtr()
{
assert(obj == 0); // Free it before you re-use it!
return reinterpret_cast<gmpi::api::IUnknown**>(&obj);
}
template<typename I>
shared_ptr<I> as()
{
shared_ptr<I> returnInterface;
if (obj)
{
obj->queryInterface(&I::guid, returnInterface.put_void());
}
return returnInterface;
}
bool isNull()
{
return obj == nullptr;
}
private:
// Attach object and increment ref count.
void assign(wrappedObjT* newobj)
{
attach(newobj);
if( newobj )
{
newobj->addRef();
}
}
};
template<typename INTERFACE>
INTERFACE* as(api::IUnknown* com_object)
{
INTERFACE* result{};
com_object->queryInterface(&INTERFACE::guid, (void**)&result);
return result;
}
// helper wraps an interface as a value-type
// TODO: copy WINUI3 stype get(), put()
template<class InterfaceClass>
class IWrapper
{
protected:
mutable gmpi::shared_ptr<InterfaceClass> m_ptr;
//? IWrapper() {}
IWrapper(IWrapper const& other) : m_ptr(other.m_ptr) {}
IWrapper(IWrapper&& other) : m_ptr(std::move(other.m_ptr)) {}
void Copy(InterfaceClass* other) { m_ptr = other; }
void Move(IWrapper&& other) { m_ptr = std::move(other.m_ptr); }
public:
IWrapper(/*gmpi::api::IUnknown* other*/) : m_ptr(nullptr)
{
}
// TODO copy winrt::com_ptr
inline InterfaceClass* get() const
{
return m_ptr.get();
}
InterfaceClass** put() noexcept
{
m_ptr = nullptr;
return m_ptr.getAddressOf();
}
explicit operator bool() {
return m_ptr != nullptr;
}
private: // need em?
inline gmpi::api::IUnknown*& Unknown() { return m_ptr.get(); };
inline void** asIMpUnknownPtr() { return m_ptr.put(); };
inline bool isNull() { return m_ptr == nullptr; }
void setNull() { m_ptr = nullptr; }
};
// Helper for returning strings.
class ReturnString : public gmpi::api::IString
{
std::string cppString;
public:
/*
MpString() {}
MpString(const std::string& other) : cppString(other)
{
}
MpString(const char* pData, int32_t pSize) : cppString(pData, pSize)
{
}
*/
gmpi::ReturnCode setData(const char* data, int32_t size) override
{
if (size < 1)
{
cppString.clear();
}
else
{
cppString.assign(data, static_cast<size_t>(size));
}
return gmpi::ReturnCode::Ok;
}
int32_t getSize() override
{
return static_cast<int32_t>(cppString.size());
}
const char* getData() override
{
return cppString.data();
}
const char* c_str() const
{
return cppString.c_str();
}
const std::string& str() const
{
return cppString;
}
// identification and reference counting
GMPI_QUERYINTERFACE_METHOD(gmpi::api::IString);
GMPI_REFCOUNT_NO_DELETE;
};
} // namespace gmpi
#endif // _GMPI_SDK_COMMON_H_