Files
DocumentServer-v-9.2.0/core/OOXML/Base/SmartPtr.h
Yajbir Singh f1b860b25c
Some checks failed
check / markdownlint (push) Has been cancelled
check / spellchecker (push) Has been cancelled
updated
2025-12-11 19:03:17 +05:30

205 lines
4.7 KiB
C++

/*
* (c) Copyright Ascensio System SIA 2010-2023
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-6 Ernesta Birznieka-Upish
* street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
#pragma once
#include "../../DesktopEditor/common/Types.h"
namespace NSCommon
{
template <typename Type>
class smart_ptr
{
protected:
Type* m_pData;
mutable LONG* m_pCountRef;
public:
smart_ptr()
{
m_pData = NULL;
m_pCountRef = NULL;
}
smart_ptr(Type* pPointer)
{
m_pData = pPointer;
m_pCountRef = new LONG(1);
}
smart_ptr(const smart_ptr<Type>& pPointer)
{
m_pData = NULL;
m_pCountRef = NULL;
*this = pPointer;
}
~smart_ptr()
{
Release();
}
inline void Release()
{
if (NULL == m_pCountRef)
return;
*m_pCountRef -= 1;
if (0 >= *m_pCountRef)
{
if (m_pData)
delete m_pData;
delete m_pCountRef;
}
m_pData = NULL;
m_pCountRef = NULL;
}
inline void AddRef()
{
if (!IsInit() || (NULL == m_pCountRef))
return;
*m_pCountRef += 1;
}
inline LONG GetCountReference()
{
return m_pCountRef ? *m_pCountRef : 0;
}
smart_ptr<Type>& operator=(const Type& oSrc)
{
Release();
m_pData = new Type(oSrc);
m_pCountRef = new LONG(1);
return *this;
}
smart_ptr<Type>& operator=(Type* pType)
{
Release();
m_pData = pType;
m_pCountRef = new LONG(1);
return *this;
}
smart_ptr<Type>& operator=(const smart_ptr<Type>& oSrc)
{
Release();
if ((NULL == oSrc.m_pData) || (NULL == oSrc.m_pCountRef))
return *this;
*oSrc.m_pCountRef += 1;
Attach(oSrc.m_pData, oSrc.m_pCountRef);
return *this;
}
inline bool IsInit() const
{
return (NULL != m_pData);
}
inline bool is_init() const
{
return IsInit();
}
template<class T> inline const bool is()const
{
if (!IsInit())
return false;
T* pResult = dynamic_cast<T*>(const_cast<Type*>(m_pData));
return (NULL != pResult);
}
template<class T> inline const T& as()const
{
T* pResult = dynamic_cast<T*>(const_cast<Type*>(m_pData));
return *pResult;
}
template<class T> inline T& as()
{
T* pResult = dynamic_cast<T*>(const_cast<Type*>(m_pData));
return *pResult;
}
template <typename T>
inline void Attach(T* pCast, const LONG* pCountRef)
{
m_pData = pCast;
m_pCountRef = const_cast<LONG*>(pCountRef);
}
template<typename T>
inline smart_ptr<T> smart_dynamic_cast()const
{
smart_ptr<T> new_type;
if ((NULL == m_pData) || (NULL == m_pCountRef))
return new_type;
T* pCast = dynamic_cast<T*>(m_pData);
if (NULL == pCast)
return new_type;
*m_pCountRef += 1;
new_type.Attach(pCast, m_pCountRef);
return new_type;
}
inline Type* GetPointer() const { return m_pData; }
inline Type& operator*() { return *m_pData; }
inline Type* operator->() { return m_pData; }
inline const Type& operator*() const { return *m_pData; }
inline const Type* operator->() const { return m_pData; }
inline const Type& get() { return *m_pData; } const
inline void reset(Type* pPointer = NULL)
{
*this = pPointer;
}
};
template <typename T>
static inline void normalize_value(T& value, const T& min, const T& max)
{
if (value < min)
value = min;
else if (value > max)
value = max;
}
}
#ifndef _USE_NULLABLE_PROPERTY_
using namespace NSCommon;
#endif