LibreOffice Module shell (master) 1
COMRefCounted.hpp
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3* This file is part of the LibreOffice project.
4*
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/.
8*/
9
10#ifndef INCLUDED_SHELL_INC_SPSUPP_COMREFCOUNTED_HPP
11#define INCLUDED_SHELL_INC_SPSUPP_COMREFCOUNTED_HPP
12
13#include <objbase.h>
14#include <assert.h>
15
16template <class... Interfaces>
17class COMRefCounted : public Interfaces...
18{
19public:
20 virtual ~COMRefCounted() {}
21
22 // IUnknown methods
23
24 ULONG STDMETHODCALLTYPE AddRef() override
25 {
26 return static_cast<ULONG>(::InterlockedIncrement(&m_nRef));
27 }
28
29 ULONG STDMETHODCALLTYPE Release() override
30 {
31 assert(m_nRef > 0);
32 if (::InterlockedDecrement(&m_nRef) == 0)
33 {
34 delete this;
35 return 0;
36 }
37 return static_cast<ULONG>(m_nRef);
38 }
39
40private:
41 LONG m_nRef = 1; // Created referenced
42};
43
44#endif
45
46/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ULONG STDMETHODCALLTYPE Release() override
ULONG STDMETHODCALLTYPE AddRef() override
virtual ~COMRefCounted()
LONG