summaryrefslogtreecommitdiff
path: root/include/systools
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2021-02-25 14:22:57 +0100
committerMike Kaganski <mike.kaganski@collabora.com>2021-02-26 07:55:25 +0100
commit881174442b942d79fe1da52ff209f8b9eae2c99a (patch)
treeff1a41d7596b7937b3cfb58618421a87343c874c /include/systools
parent5170614a3160dceba63478291a00bbde9d37729f (diff)
Slightly simplify COMReference
Change-Id: I6447dbdc3e2549d0e55feeee80249bdf75924ca8 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111466 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/systools')
-rw-r--r--include/systools/win32/comtools.hxx78
1 files changed, 17 insertions, 61 deletions
diff --git a/include/systools/win32/comtools.hxx b/include/systools/win32/comtools.hxx
index 8f7ce87e7636..9f8489b8faac 100644
--- a/include/systools/win32/comtools.hxx
+++ b/include/systools/win32/comtools.hxx
@@ -17,19 +17,14 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
-#ifndef INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
-#define INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
+#pragma once
#include <string>
#include <stdexcept>
#include <objbase.h>
-namespace sal
+namespace sal::systools
{
-namespace systools
-{
- typedef int HRESULT;
-
/* Simple exception class for propagating COM errors */
class ComError : public std::runtime_error
{
@@ -39,10 +34,7 @@ namespace systools
hr_(hr)
{}
- HRESULT GetHresult() const
- {
- return hr_;
- }
+ HRESULT GetHresult() const { return hr_; }
private:
HRESULT hr_;
@@ -53,19 +45,8 @@ namespace systools
class COMReference
{
public:
- COMReference() :
- com_ptr_(NULL)
- {
- }
-
- explicit COMReference(T* comptr) :
- com_ptr_(comptr)
- {
- addRef();
- }
-
/* Explicitly controllable whether AddRef will be called or not */
- COMReference(T* comptr, bool bAddRef) :
+ COMReference(T* comptr = nullptr, bool bAddRef = true) :
com_ptr_(comptr)
{
if (bAddRef)
@@ -80,8 +61,7 @@ namespace systools
COMReference<T>& operator=(const COMReference<T>& other)
{
- if (other.com_ptr_)
- other.com_ptr_->AddRef();
+ other.addRef();
release();
com_ptr_ = other.com_ptr_;
return *this;
@@ -95,10 +75,7 @@ namespace systools
return *this;
}
- ~COMReference()
- {
- release();
- }
+ ~COMReference() { release(); }
template<typename InterfaceType>
COMReference<InterfaceType> QueryInterface(REFIID iid)
@@ -114,70 +91,49 @@ namespace systools
return ip;
}
- T* operator->() const
- {
- return com_ptr_;
- }
+ T* operator->() const { return com_ptr_; }
- T& operator*() const
- {
- return *com_ptr_;
- }
+ T& operator*() const { return *com_ptr_; }
/* Necessary for assigning com_ptr_ from functions like
CoCreateInstance which require a 'void**' */
T** operator&()
{
- release();
- com_ptr_ = NULL;
+ clear();
return &com_ptr_;
}
- T* get() const
- {
- return com_ptr_;
- }
+ T* get() const { return com_ptr_; }
COMReference<T>& clear()
{
release();
- com_ptr_ = NULL;
+ com_ptr_ = nullptr;
return *this;
}
- bool is() const
- {
- return (com_ptr_ != NULL);
- }
+ bool is() const { return (com_ptr_ != nullptr); }
private:
- ULONG addRef()
+ void addRef() const
{
- ULONG cnt = 0;
if (com_ptr_)
- cnt = com_ptr_->AddRef();
- return cnt;
+ com_ptr_->AddRef();
}
- ULONG release()
+ void release() const
{
- ULONG cnt = 0;
if (com_ptr_)
- cnt = com_ptr_->Release();
- return cnt;
+ com_ptr_->Release();
}
- private:
T* com_ptr_;
};
-} // systools
-} // sal
+} // sal::systools
/* Typedefs for some popular COM interfaces */
typedef sal::systools::COMReference<IDataObject> IDataObjectPtr;
typedef sal::systools::COMReference<IStream> IStreamPtr;
-#endif // INCLUDED_SYSTOOLS_WIN32_COMTOOLS_HXX
-
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */