wined3d: Properly check if an attribute is used in state_normalize().
[wine] / dlls / windowscodecs / clsfactory.c
1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "objbase.h"
29 #include "ocidl.h"
30 #include "initguid.h"
31 #include "wincodec.h"
32
33 #include "wincodecs_private.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38
39 typedef struct {
40     REFCLSID classid;
41     HRESULT (*constructor)(IUnknown*,REFIID,void**);
42 } classinfo;
43
44 static classinfo wic_classes[] = {
45     {&CLSID_WICImagingFactory, ImagingFactory_CreateInstance},
46     {&CLSID_WICBmpDecoder, BmpDecoder_CreateInstance},
47     {&CLSID_WICBmpEncoder, BmpEncoder_CreateInstance},
48     {&CLSID_WICGifDecoder, GifDecoder_CreateInstance},
49     {&CLSID_WICIcoDecoder, IcoDecoder_CreateInstance},
50     {&CLSID_WICJpegDecoder, JpegDecoder_CreateInstance},
51     {&CLSID_WICDefaultFormatConverter, FormatConverter_CreateInstance},
52     {0}};
53
54 typedef struct {
55     const IClassFactoryVtbl *lpIClassFactoryVtbl;
56     LONG                    ref;
57     classinfo               *info;
58 } ClassFactoryImpl;
59
60 static HRESULT WINAPI ClassFactoryImpl_QueryInterface(IClassFactory *iface,
61     REFIID iid, void **ppv)
62 {
63     ClassFactoryImpl *This = (ClassFactoryImpl*)iface;
64     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
65
66     if (!ppv) return E_INVALIDARG;
67
68     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IClassFactory, iid))
69     {
70         *ppv = This;
71     }
72     else
73     {
74         *ppv = NULL;
75         return E_NOINTERFACE;
76     }
77
78     IUnknown_AddRef((IUnknown*)*ppv);
79     return S_OK;
80 }
81
82 static ULONG WINAPI ClassFactoryImpl_AddRef(IClassFactory *iface)
83 {
84     ClassFactoryImpl *This = (ClassFactoryImpl*)iface;
85     ULONG ref = InterlockedIncrement(&This->ref);
86
87     TRACE("(%p) refcount=%u\n", iface, ref);
88
89     return ref;
90 }
91
92 static ULONG WINAPI ClassFactoryImpl_Release(IClassFactory *iface)
93 {
94     ClassFactoryImpl *This = (ClassFactoryImpl*)iface;
95     ULONG ref = InterlockedDecrement(&This->ref);
96
97     TRACE("(%p) refcount=%u\n", iface, ref);
98
99     if (ref == 0)
100         HeapFree(GetProcessHeap(), 0, This);
101
102     return ref;
103 }
104
105 static HRESULT WINAPI ClassFactoryImpl_CreateInstance(IClassFactory *iface,
106     IUnknown *pUnkOuter, REFIID riid, void **ppv)
107 {
108     ClassFactoryImpl *This = (ClassFactoryImpl*)iface;
109
110     return This->info->constructor(pUnkOuter, riid, ppv);
111 }
112
113 static HRESULT WINAPI ClassFactoryImpl_LockServer(IClassFactory *iface, BOOL lock)
114 {
115     TRACE("(%p, %i): stub\n", iface, lock);
116     return E_NOTIMPL;
117 }
118
119 static const IClassFactoryVtbl ClassFactoryImpl_Vtbl = {
120     ClassFactoryImpl_QueryInterface,
121     ClassFactoryImpl_AddRef,
122     ClassFactoryImpl_Release,
123     ClassFactoryImpl_CreateInstance,
124     ClassFactoryImpl_LockServer
125 };
126
127 static HRESULT ClassFactoryImpl_Constructor(classinfo *info, REFIID riid, LPVOID *ppv)
128 {
129     ClassFactoryImpl *This;
130     HRESULT ret;
131
132     *ppv = NULL;
133
134     This = HeapAlloc(GetProcessHeap(), 0, sizeof(ClassFactoryImpl));
135     if (!This) return E_OUTOFMEMORY;
136
137     This->lpIClassFactoryVtbl = &ClassFactoryImpl_Vtbl;
138     This->ref = 1;
139     This->info = info;
140
141     ret = IClassFactory_QueryInterface((IClassFactory*)This, riid, ppv);
142     IClassFactory_Release((IClassFactory*)This);
143
144     return ret;
145 }
146
147 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
148 {
149     HRESULT ret;
150     classinfo *info=NULL;
151     int i;
152
153     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
154
155     if (!rclsid || !iid || !ppv)
156         return E_INVALIDARG;
157
158     *ppv = NULL;
159
160     for (i=0; wic_classes[i].classid; i++)
161     {
162         if (IsEqualCLSID(wic_classes[i].classid, rclsid))
163         {
164             info = &wic_classes[i];
165             break;
166         }
167     }
168
169     if (info)
170         ret = ClassFactoryImpl_Constructor(info, iid, ppv);
171     else
172         ret = CLASS_E_CLASSNOTAVAILABLE;
173
174     TRACE("<-- %08X\n", ret);
175     return ret;
176 }