Branch libreoffice-5-0-4
[LibreOffice.git] / include / systools / win32 / comtools.hxx
blob0cbe54f1acafa962f69c590ba6af03c689086933
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
21 #define INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
23 #include <string>
24 #include <stdexcept>
25 #if defined _MSC_VER
26 #pragma warning(push,1)
27 #endif
28 #include <objbase.h>
29 #if defined _MSC_VER
30 #pragma warning(pop)
31 #endif
33 namespace sal
35 namespace systools
37 typedef int HRESULT;
39 /* Simple exception class for propagating COM errors */
40 class ComError : public std::runtime_error
42 public:
43 ComError(const std::string& message, HRESULT hr) :
44 std::runtime_error(message),
45 hr_(hr)
48 HRESULT GetHresult() const
50 return hr_;
53 private:
54 HRESULT hr_;
57 /* A simple COM smart pointer template */
58 template <typename T>
59 class COMReference
61 public:
62 COMReference() :
63 com_ptr_(NULL)
67 explicit COMReference(T* comptr) :
68 com_ptr_(comptr)
70 addRef();
73 /* Explicitly controllable whether AddRef will be called or not */
74 COMReference(T* comptr, bool bAddRef) :
75 com_ptr_(comptr)
77 if (bAddRef)
78 addRef();
81 COMReference(const COMReference<T>& other) :
82 com_ptr_(other.com_ptr_)
84 addRef();
87 COMReference<T>& operator=(const COMReference<T>& other)
89 if (other.com_ptr_)
90 other.com_ptr_->AddRef();
91 release();
92 com_ptr_ = other.com_ptr_;
93 return *this;
96 COMReference<T>& operator=(T* comptr)
98 release();
99 com_ptr_ = comptr;
100 addRef();
101 return *this;
104 ~COMReference()
106 release();
109 template<typename InterfaceType>
110 COMReference<InterfaceType> QueryInterface(REFIID iid)
112 COMReference<InterfaceType> ip;
113 HRESULT hr = E_FAIL;
114 if (com_ptr_)
115 hr = com_ptr_->QueryInterface(iid, reinterpret_cast<LPVOID*>(&ip));
117 if (FAILED(hr))
118 throw ComError("QueryInterface failed: Interface not supported!", hr);
120 return ip;
123 T* operator->() const
125 return com_ptr_;
128 T& operator*() const
130 return *com_ptr_;
133 /* Necessary for assigning com_ptr_ from functions like
134 CoCreateInstance which require a 'void**' */
135 T** operator&()
137 release();
138 com_ptr_ = NULL;
139 return &com_ptr_;
142 T* get() const
144 return com_ptr_;
147 COMReference<T>& clear()
149 release();
150 com_ptr_ = NULL;
151 return *this;
154 bool is() const
156 return (com_ptr_ != NULL);
159 private:
160 ULONG addRef()
162 ULONG cnt = 0;
163 if (com_ptr_)
164 cnt = com_ptr_->AddRef();
165 return cnt;
168 ULONG release()
170 ULONG cnt = 0;
171 if (com_ptr_)
172 cnt = com_ptr_->Release();
173 return cnt;
176 private:
177 T* com_ptr_;
180 } // systools
181 } // sal
183 /* Typedefs for some popular COM interfaces */
184 typedef sal::systools::COMReference<IDataObject> IDataObjectPtr;
185 typedef sal::systools::COMReference<IStream> IStreamPtr;
186 typedef sal::systools::COMReference<IEnumFORMATETC> IEnumFORMATETCPtr;
188 #endif // INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */