nss: upgrade to release 3.73
[LibreOffice.git] / include / systools / win32 / comtools.hxx
blob8f7ce87e76360901d77638fd6774b09e11c5ac2c
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 #include <objbase.h>
27 namespace sal
29 namespace systools
31 typedef int HRESULT;
33 /* Simple exception class for propagating COM errors */
34 class ComError : public std::runtime_error
36 public:
37 ComError(const std::string& message, HRESULT hr) :
38 std::runtime_error(message),
39 hr_(hr)
42 HRESULT GetHresult() const
44 return hr_;
47 private:
48 HRESULT hr_;
51 /* A simple COM smart pointer template */
52 template <typename T>
53 class COMReference
55 public:
56 COMReference() :
57 com_ptr_(NULL)
61 explicit COMReference(T* comptr) :
62 com_ptr_(comptr)
64 addRef();
67 /* Explicitly controllable whether AddRef will be called or not */
68 COMReference(T* comptr, bool bAddRef) :
69 com_ptr_(comptr)
71 if (bAddRef)
72 addRef();
75 COMReference(const COMReference<T>& other) :
76 com_ptr_(other.com_ptr_)
78 addRef();
81 COMReference<T>& operator=(const COMReference<T>& other)
83 if (other.com_ptr_)
84 other.com_ptr_->AddRef();
85 release();
86 com_ptr_ = other.com_ptr_;
87 return *this;
90 COMReference<T>& operator=(T* comptr)
92 release();
93 com_ptr_ = comptr;
94 addRef();
95 return *this;
98 ~COMReference()
100 release();
103 template<typename InterfaceType>
104 COMReference<InterfaceType> QueryInterface(REFIID iid)
106 COMReference<InterfaceType> ip;
107 HRESULT hr = E_FAIL;
108 if (com_ptr_)
109 hr = com_ptr_->QueryInterface(iid, reinterpret_cast<LPVOID*>(&ip));
111 if (FAILED(hr))
112 throw ComError("QueryInterface failed: Interface not supported!", hr);
114 return ip;
117 T* operator->() const
119 return com_ptr_;
122 T& operator*() const
124 return *com_ptr_;
127 /* Necessary for assigning com_ptr_ from functions like
128 CoCreateInstance which require a 'void**' */
129 T** operator&()
131 release();
132 com_ptr_ = NULL;
133 return &com_ptr_;
136 T* get() const
138 return com_ptr_;
141 COMReference<T>& clear()
143 release();
144 com_ptr_ = NULL;
145 return *this;
148 bool is() const
150 return (com_ptr_ != NULL);
153 private:
154 ULONG addRef()
156 ULONG cnt = 0;
157 if (com_ptr_)
158 cnt = com_ptr_->AddRef();
159 return cnt;
162 ULONG release()
164 ULONG cnt = 0;
165 if (com_ptr_)
166 cnt = com_ptr_->Release();
167 return cnt;
170 private:
171 T* com_ptr_;
174 } // systools
175 } // sal
177 /* Typedefs for some popular COM interfaces */
178 typedef sal::systools::COMReference<IDataObject> IDataObjectPtr;
179 typedef sal::systools::COMReference<IStream> IStreamPtr;
181 #endif // INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */