summaryrefslogtreecommitdiff
path: root/print/XPSDrvSmpl/src/inc/cunknown.h
blob: c7cceff08ebadf5a80b8aa0deae0666ec3433cfa (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
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
/*++

Copyright (c) 2005 Microsoft Corporation

All rights reserved.

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.

File Name:

   cunknown.h

Abstract:

   Provides a simple template implementation for classes that need to
   implement IUnknown.

--*/

#pragma once

template <class _T>
class CUnknown : public _T
{
public:
    /*++

    Routine Name:

        CUnknown

    Routine Description:

        CUnknown class constructor

    Arguments:

        IIDTarget - The target interface guid

    Return Value:

        None

    --*/
    CUnknown(
        REFIID IIDTarget
        ) :
        m_IIDTarget(IIDTarget),
        m_cRef(1)
    {
    }

    /*++

    Routine Name:

        ~CUnknown

    Routine Description:

        CUnknown class destructor

    Arguments:

        None

    Return Value:

        None

    --*/
    virtual ~CUnknown()
    {
    }

    //
    // IUnknown methods
    //
    /*++

    Routine Name:

        QueryInterface

    Routine Description:

        This routine implements the IUnknown::QueryInterface method. Returns the raw interface
        pointer for the requested IID if it matches the target IID

    Arguments:

        riid - The IID of the requested interface
        ppv  - The raw interface pointer

    Return Value:

        HRESULT
        S_OK - On success
        E_*  - On error

    --*/
    virtual HRESULT STDMETHODCALLTYPE
    QueryInterface(
              REFIID riid,
        _Out_ PVOID* ppv
        )
    {
        HRESULT hr = S_OK;

        if (ppv != NULL)
        {
            if (riid == IID_IUnknown ||
                riid == m_IIDTarget)
            {
                *ppv = static_cast<_T*>(this);
            }
            else
            {
                *ppv = NULL;

                hr = E_NOINTERFACE;
            }
        }
        else
        {
            hr = E_POINTER;
        }

        if (SUCCEEDED(hr))
        {
            AddRef();
        }

        return hr;
    }

    /*++

    Routine Name:

        AddRef

    Routine Description:

        This routine increments the reference count on the current interface

    Arguments:

        None

    Return Value:

        HRESULT
        S_OK - On success
        E_*  - On error

    --*/
    virtual ULONG STDMETHODCALLTYPE
    AddRef()
    {
        return InterlockedIncrement(&m_cRef);
    }

    /*++

    Routine Name:

        Release

    Routine Description:

        This routine decrements the reference count on the current interface

    Arguments:

        None

    Return Value:

        HRESULT
        S_OK - On success
        E_*  - On error

    Notes:

        The drv_at annotation tells Prefast to consider this object's memory
        freed after Release has been called.

    --*/
    virtual
    ULONG STDMETHODCALLTYPE
    Release()
    {
        ULONG cRef = InterlockedDecrement(&m_cRef);

        if (0 == cRef)
        {
            delete this;
        }

        return cRef;
    }

private:
    LONG m_cRef;

    IID  m_IIDTarget;
};