OLD | NEW |
| (Empty) |
1 // Copyright 2010 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 is an implementation of IMarshal that always marshals by value. class T | |
17 // needs to derive from MarshalByValue, and expose IMarshal through | |
18 // QueryInterface. class T also needs to expose the IPersistStream-style methods | |
19 // GetSizeMax(), Save(), and Load(). | |
20 | |
21 #ifndef OMAHA_BASE_MARSHAL_BY_VALUE_H_ | |
22 #define OMAHA_BASE_MARSHAL_BY_VALUE_H_ | |
23 | |
24 #include <atlbase.h> | |
25 #include "omaha/base/debug.h" | |
26 | |
27 namespace omaha { | |
28 | |
29 template <class T> | |
30 class ATL_NO_VTABLE MarshalByValue : public IMarshal { | |
31 public: | |
32 STDMETHOD(GetUnmarshalClass)(REFIID, void*, DWORD, void*, DWORD, | |
33 CLSID* clsid) { | |
34 ASSERT1(clsid); | |
35 *clsid = T::GetObjectCLSID(); | |
36 return S_OK; | |
37 } | |
38 | |
39 STDMETHOD(ReleaseMarshalData)(IStream* stream) { | |
40 UNREFERENCED_PARAMETER(stream); | |
41 return S_OK; | |
42 } | |
43 | |
44 STDMETHOD(DisconnectObject)(DWORD) { | |
45 return S_OK; | |
46 } | |
47 | |
48 STDMETHOD(GetMarshalSizeMax)(REFIID, void*, DWORD, void*, DWORD, | |
49 DWORD* size) { | |
50 ASSERT1(size); | |
51 | |
52 T* persist = static_cast<T*>(this); | |
53 ULARGE_INTEGER size_max = {0}; | |
54 HRESULT hr = persist->GetSizeMax(&size_max); | |
55 if (FAILED(hr)) { | |
56 return hr; | |
57 } | |
58 | |
59 *size = size_max.LowPart; | |
60 return S_OK; | |
61 } | |
62 | |
63 STDMETHOD(MarshalInterface)(IStream* stream, REFIID, void*, DWORD, void*, | |
64 DWORD) { | |
65 ASSERT1(stream); | |
66 | |
67 T* persist = static_cast<T*>(this); | |
68 return persist->Save(stream, FALSE); | |
69 } | |
70 | |
71 STDMETHOD(UnmarshalInterface)(IStream* stream, REFIID iid, void** ptr) { | |
72 ASSERT1(stream); | |
73 ASSERT1(ptr); | |
74 | |
75 T* persist = static_cast<T*>(this); | |
76 HRESULT hr = persist->Load(stream); | |
77 if (FAILED(hr)) { | |
78 return hr; | |
79 } | |
80 | |
81 return persist->QueryInterface(iid, ptr); | |
82 } | |
83 }; | |
84 | |
85 } // namespace omaha | |
86 | |
87 #endif // OMAHA_BASE_MARSHAL_BY_VALUE_H_ | |
88 | |
OLD | NEW |