-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnknownImpl.h
151 lines (134 loc) · 3.92 KB
/
UnknownImpl.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
/*
* Outlook Integration library.
*
* Copyright (c) 2016, Ixperta Solutions s.r.o.
*
* This work is based on
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _JMSOFFICECOMM_UNKNOWNIMPL_H_
#define _JMSOFFICECOMM_UNKNOWNIMPL_H_
#include "Log.h"
#include "OutOfProcessServer.h"
#include "StringUtils.h"
#include "WeakReferenceSource.h"
#ifdef _MSC_VER
#define FUNC_NAME_MACRO WIDEN(__FUNCSIG__)
#else
#define FUNC_NAME_MACRO WIDEN(__PRETTY_FUNCTION__)
#endif
#define STDMETHODIMP_E_NOTIMPL_STUB \
{ \
DEBUG(FUNC_NAME_MACRO); \
return E_NOTIMPL; \
}
/**
* Represents a base implementation of the <tt>IUnknown</tt> interface.
*
* @author Lyubomir Marinov
*/
template <class T, REFIID IID_T>
class UnknownImpl
: public T
{
public:
// IUnknown
STDMETHODIMP QueryInterface(REFIID iid, PVOID *obj)
{
HRESULT hr;
if (!obj)
hr = E_POINTER;
else if (IID_IUnknown == iid)
{
AddRef();
*obj = static_cast<LPUNKNOWN>(this);
hr = S_OK;
}
else if (IID_T == iid)
{
AddRef();
*obj = static_cast<T *>(this);
hr = S_OK;
}
else if (IID_IWeakReferenceSource == iid)
{
if (!_weakReferenceSource)
_weakReferenceSource = new WeakReferenceSource(this);
_weakReferenceSource->AddRef();
*obj = static_cast<IWeakReferenceSource *>(_weakReferenceSource);
hr = S_OK;
}
else
{
*obj = NULL;
hr = E_NOINTERFACE;
}
if (FAILED(hr))
{
LPOLESTR olestr;
if (SUCCEEDED(::StringFromIID(iid, &olestr)))
{
LPTSTR tstr
#ifdef _UNICODE
= olestr;
#else
= StringUtils::WideCharToMultiByte(olestr);
#endif /* #ifdef _UNICODE */
if (tstr)
{
DEBUG(
_T("UnknownImpl::QueryInterface: this=%p; iid=%s;\n"),
(PVOID) this,
tstr);
if (tstr != olestr)
::free(tstr);
}
::CoTaskMemFree(olestr);
}
}
return hr;
}
STDMETHODIMP_(ULONG) AddRef() { return ++_refCount; }
STDMETHODIMP_(ULONG) Release()
{
ULONG refCount = --_refCount;
if (!refCount)
delete this;
DEBUG(
_T("UnknownImpl::Release: this=%p; refCount=%lu;\n"),
(PVOID) this,
refCount);
return refCount;
}
protected:
UnknownImpl()
: _refCount(1),
_weakReferenceSource(NULL)
{
OutOfProcessServer::addRef();
}
virtual ~UnknownImpl()
{
if (_weakReferenceSource)
delete _weakReferenceSource;
OutOfProcessServer::release();
}
private:
ULONG _refCount;
WeakReferenceSource *_weakReferenceSource;
};
#endif /* #ifndef _JMSOFFICECOMM_UNKNOWNIMPL_H_ */