OLD | NEW |
| (Empty) |
1 // Copyright 2005-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // This class is similar to CComContainedObject. The primary difference is with | |
17 // respect to QueryInterface, which this class handles itself, as opposed to | |
18 // CComContainedObject that delegates that to the outer unknown. | |
19 | |
20 #ifndef OMAHA_COMMON_CONTROLLED_OBJECT_H_ | |
21 #define OMAHA_COMMON_CONTROLLED_OBJECT_H_ | |
22 | |
23 #include <atlcom.h> | |
24 #include "base/scoped_ptr.h" | |
25 #include "omaha/base/debug.h" | |
26 #include "omaha/base/logging.h" | |
27 #include "omaha/base/scoped_ptr_address.h" | |
28 | |
29 namespace omaha { | |
30 | |
31 template <class Base> | |
32 class ControlledObject : public CComContainedObject<Base> { | |
33 public: | |
34 typedef CComContainedObject<Base> BaseClass; | |
35 typedef ControlledObject<Base> ControlledObj; | |
36 | |
37 // The base class CComContainedObject stores pv, and delegates to this | |
38 // controlling unknown subsequent calls to the lifetime methods | |
39 // AddRef/Release. | |
40 explicit ControlledObject(void* pv) : BaseClass(pv) {} | |
41 virtual ~ControlledObject() {} | |
42 | |
43 STDMETHOD(QueryInterface)(REFIID iid, void** ppv) throw() { | |
44 return _InternalQueryInterface(iid, ppv); | |
45 } | |
46 | |
47 // TODO(omaha): ASSERT on controlling_unknown. The unit tests need to be | |
48 // fixed for this. | |
49 static HRESULT WINAPI CreateInstance(IUnknown* controlling_unknown, | |
50 ControlledObj** pp) throw() { | |
51 ASSERT1(pp); | |
52 if (!controlling_unknown) { | |
53 CORE_LOG(LW, (_T("[CreateInstance - controlling_unknown is NULL]"))); | |
54 } | |
55 | |
56 *pp = NULL; | |
57 scoped_ptr<ControlledObj> p(new ControlledObj(controlling_unknown)); | |
58 if (!p.get()) { | |
59 return E_OUTOFMEMORY; | |
60 } | |
61 | |
62 HRESULT hr = p->FinalConstruct(); | |
63 if (FAILED(hr)) { | |
64 return hr; | |
65 } | |
66 | |
67 *pp = p.release(); | |
68 return S_OK; | |
69 } | |
70 | |
71 template <class Q> | |
72 HRESULT STDMETHODCALLTYPE QueryInterface(Q** pp) throw() { | |
73 return QueryInterface(__uuidof(Q), reinterpret_cast<void**>(pp)); | |
74 } | |
75 }; | |
76 | |
77 } // namespace omaha | |
78 | |
79 #endif // OMAHA_COMMON_CONTROLLED_OBJECT_H_ | |
80 | |
OLD | NEW |