OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // ClassFactory<class> | 5 // ClassFactory<class> |
6 // is a simple class factory object for the parameterized class. | 6 // is a simple class factory object for the parameterized class. |
7 // | 7 // |
8 #ifndef _CLASSFACTORY_H_ | 8 #ifndef _CLASSFACTORY_H_ |
9 #define _CLASSFACTORY_H_ | 9 #define _CLASSFACTORY_H_ |
10 | 10 |
11 #include <unknwn.h> | 11 #include <unknwn.h> |
12 | 12 |
13 // GenericClassFactory | 13 // GenericClassFactory |
14 // provides the basic COM plumbing to implement IClassFactory, and | 14 // provides the basic COM plumbing to implement IClassFactory, and |
15 // maintains a static count on the number of these objects in existence. | 15 // maintains a static count on the number of these objects in existence. |
16 // It remains for subclasses to implement CreateInstance. | 16 // It remains for subclasses to implement CreateInstance. |
17 class GenericClassFactory : public IClassFactory { | 17 class GenericClassFactory : public IClassFactory { |
18 public: | 18 public: |
19 GenericClassFactory(); | 19 GenericClassFactory(); |
20 ~GenericClassFactory(); | 20 ~GenericClassFactory(); |
21 | 21 |
22 //IUnknown methods | 22 //IUnknown methods |
23 STDMETHOD(QueryInterface)(REFIID iid, LPVOID* ppvObject); | 23 STDMETHOD(QueryInterface)(REFIID iid, LPVOID* ppvObject); |
24 STDMETHOD_(ULONG, AddRef)(); | 24 STDMETHOD_(ULONG, AddRef)(); |
25 STDMETHOD_(ULONG, Release)(); | 25 STDMETHOD_(ULONG, Release)(); |
26 | 26 |
27 //IClassFactory methods | 27 //IClassFactory methods |
28 STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID* ppvObject)
= 0; | 28 STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, |
| 29 LPVOID* ppvObject) = 0; |
29 STDMETHOD(LockServer)(BOOL fLock); | 30 STDMETHOD(LockServer)(BOOL fLock); |
30 | 31 |
31 // generally handy for DllUnloadNow -- count of existing descendant objects | 32 // generally handy for DllUnloadNow -- count of existing descendant objects |
32 static LONG GetObjectCount() { return object_count_; } | 33 static LONG GetObjectCount() { return object_count_; } |
33 | 34 |
34 protected: | 35 protected: |
35 LONG reference_count_; // mind the reference counting for this object | 36 LONG reference_count_; // mind the reference counting for this object |
36 static LONG object_count_; // count of all these objects | 37 static LONG object_count_; // count of all these objects |
37 }; | 38 }; |
38 | 39 |
39 | 40 |
40 // OneClassFactory<T> | 41 // OneClassFactory<T> |
41 // Knows how to be a factory for T's | 42 // Knows how to be a factory for T's |
42 template <class T> | 43 template <class T> |
43 class OneClassFactory : public GenericClassFactory | 44 class OneClassFactory : public GenericClassFactory |
44 { | 45 { |
45 public: | 46 public: |
46 //IClassFactory methods | 47 //IClassFactory methods |
47 STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObject); | 48 STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObject); |
48 }; | 49 }; |
49 | 50 |
50 | 51 |
51 template <class T> | 52 template <class T> |
52 STDMETHODIMP OneClassFactory<T>::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid
, void** result) { | 53 STDMETHODIMP OneClassFactory<T>::CreateInstance(LPUNKNOWN pUnkOuter, |
| 54 REFIID riid, void** result) { |
53 *result = NULL; | 55 *result = NULL; |
54 | 56 |
55 if(pUnkOuter != NULL) | 57 if(pUnkOuter != NULL) |
56 return CLASS_E_NOAGGREGATION; | 58 return CLASS_E_NOAGGREGATION; |
57 | 59 |
58 T* const obj = new T(); | 60 T* const obj = new T(); |
59 if(!obj) | 61 if(!obj) |
60 return E_OUTOFMEMORY; | 62 return E_OUTOFMEMORY; |
61 | 63 |
62 obj->AddRef(); | 64 obj->AddRef(); |
63 HRESULT const hr = obj->QueryInterface(riid, result); | 65 HRESULT const hr = obj->QueryInterface(riid, result); |
64 obj->Release(); | 66 obj->Release(); |
65 | 67 |
66 return hr; | 68 return hr; |
67 } | 69 } |
68 | 70 |
69 #endif | 71 #endif |
70 | 72 |
OLD | NEW |