diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /embedserv/source/inprocserv/smartpointer.hxx | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'embedserv/source/inprocserv/smartpointer.hxx')
-rw-r--r-- | embedserv/source/inprocserv/smartpointer.hxx | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/embedserv/source/inprocserv/smartpointer.hxx b/embedserv/source/inprocserv/smartpointer.hxx new file mode 100644 index 000000000..5e1606cad --- /dev/null +++ b/embedserv/source/inprocserv/smartpointer.hxx @@ -0,0 +1,184 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_EMBEDSERV_SOURCE_INPROCSERV_SMARTPOINTER_HXX +#define INCLUDED_EMBEDSERV_SOURCE_INPROCSERV_SMARTPOINTER_HXX + +namespace inprocserv{ + +void WriteDebugInfo( DWORD pThis, char* pString, DWORD nToWrite ); + +template< class T > class ComSmart +{ + T* m_pInterface; + + void OwnRelease() + { + if ( m_pInterface ) + { + T* pInterface = m_pInterface; + m_pInterface = nullptr; + pInterface->Release(); + } + } + +public: + ComSmart() + : m_pInterface( nullptr ) + {} + + ComSmart( const ComSmart<T>& rObj ) + : m_pInterface( rObj.m_pInterface ) + { + if ( m_pInterface != nullptr ) + m_pInterface->AddRef(); + } + + explicit ComSmart( T* pInterface ) + : m_pInterface( pInterface ) + { + if ( m_pInterface != nullptr ) + m_pInterface->AddRef(); + } + + ~ComSmart() + { + OwnRelease(); + } + + ComSmart& operator=( const ComSmart<T>& rObj ) + { + if(this == &rObj) + return *this; + + OwnRelease(); + + m_pInterface = rObj.m_pInterface; + + if ( m_pInterface != nullptr ) + m_pInterface->AddRef(); + + return *this; + } + + ComSmart<T>& operator=( T* pInterface ) + { + OwnRelease(); + + m_pInterface = pInterface; + + if ( m_pInterface != nullptr ) + m_pInterface->AddRef(); + + return *this; + } + + explicit operator bool() const + { + return m_pInterface != nullptr; + } + + T* get() const + { + return m_pInterface; + } + + T& operator*() const + { + return *m_pInterface; + } + + T** operator&() + { + OwnRelease(); + + m_pInterface = nullptr; + + return &m_pInterface; + } + + T* operator->() const + { + return m_pInterface; + } + + BOOL operator==( const ComSmart<T>& rObj ) const + { + return ( m_pInterface == rObj.m_pInterface ); + } + + BOOL operator!=( const ComSmart<T>& rObj ) const + { + return ( m_pInterface != rObj.m_pInterface ); + } + + bool operator==( const T* pInterface ) const + { + return ( m_pInterface == pInterface ); + } + + BOOL operator!=( const T* pInterface ) const + { + return ( m_pInterface != pInterface ); + } +}; + +class CSGuard +{ + CRITICAL_SECTION* m_pCriticalSection; + +public: + explicit CSGuard( CRITICAL_SECTION* pCS ) + : m_pCriticalSection( pCS ) + { + if ( m_pCriticalSection ) + EnterCriticalSection( m_pCriticalSection ); + } + + ~CSGuard() + { + if ( m_pCriticalSection ) + LeaveCriticalSection( m_pCriticalSection ); + } +}; + +class ULONGGuard +{ + ULONG* m_pValue; + +public: + explicit ULONGGuard( ULONG* pValue ) + : m_pValue( pValue ) + { + if ( m_pValue ) + (*m_pValue)++; + } + + ~ULONGGuard() + { + if ( m_pValue ) + (*m_pValue)--; + } +}; + +} // namespace inprocserv + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |