blob: 5ecd2487ad2ebdbc7d34131372cc35a3e38ab7ed (
plain)
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
|
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
//
// File Name:
//
// UnknownBase.h
//
// Abstract:
//
// IUnknown implementation common to filter components derived from
// IUnknown.
//
#pragma once
namespace xpsrasfilter
{
template <class Interface>
class UnknownBase : public Interface
{
public:
UnknownBase() : m_cRef(1) { }
virtual ~UnknownBase() { };
//
//Routine Name:
//
// UnknownBase::QueryInterface
//
//Routine Description:
//
// Implements IUnknown QueryInterface.
//
//Arguments:
//
// riid - id of the interface
// ppv - void pointer to the requested interface
//
//Return Value:
//
// HRESULT
// S_OK - On success
// E_NOINTERFACE - Invalid interface
//
_Must_inspect_result_
HRESULT STDMETHODCALLTYPE
QueryInterface(
_In_ REFIID riid,
_Outptr_ PVOID *ppv
)
{
HRESULT hr = S_OK;
if (ppv == NULL)
{
WPP_LOG_ON_FAILED_HRESULT(E_POINTER);
return E_POINTER;
}
if (riid == IID_IUnknown)
{
*ppv = static_cast<IUnknown *>(this);
}
else if (riid == __uuidof(Interface))
{
*ppv = static_cast<Interface *>(this);
}
else
{
*ppv = NULL;
WPP_LOG_ON_FAILED_HRESULT(
hr = E_NOINTERFACE
);
}
if (SUCCEEDED(hr))
{
AddRef();
}
return hr;
}
//
//Routine Name:
//
// UnknownBase::AddRef
//
//Routine Description:
//
// Implements IUnknown reference count increment
// on the current interface.
//
//Arguments:
//
// None
//
//Return Value:
//
// ULONG
// New reference count
//
ULONG STDMETHODCALLTYPE
AddRef()
{
return ::InterlockedIncrement(&m_cRef);
}
//
//Routine Name:
//
// UnknownBase::Release
//
//Routine Description:
//
// Implements IUnknown reference count decrement
// on the current interface.
//
//Arguments:
//
// None
//
//Return Value:
//
// ULONG
// New reference count
//
//Note:
//
// The drv_at annotation tells Prefast to consider this object's memory
// freed after Release has been called.
//
_At_(this, __drv_freesMem(object))
ULONG STDMETHODCALLTYPE
Release()
{
ULONG cRef = ::InterlockedDecrement(&m_cRef);
if (0 == cRef)
{
delete this;
}
return cRef;
}
private:
volatile ULONG m_cRef; // interface reference count
};
} // namespace xpsrasfilter
|