/********************************************************************************
Copyright © 2008 Jason Morley
-----------------------------------------------------------------------------
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*********************************************************************************/
namespace A2D
{
namespace Base
{
template<class T> class MM_Pointer
{
public:
MM_Pointer()
{
_mv_pnCount = 0;
_mv_pPointer = 0;
};
MM_Pointer(T* pT)
{
_mv_pnCount = new unsigned int(0);
_mv_pPointer = pT;
AddRef();
}
MM_Pointer(const MM_Pointer<T>& pT)
{
_mv_pnCount = pT._mv_pnCount;
_mv_pPointer = pT._mv_pPointer;
AddRef();
}
MM_Pointer& operator=(T* pT)
{
Release();
if ( _mv_pnCount == 0 ) _mv_pnCount = new unsigned int(0);
_mv_pPointer = pT;
AddRef();
return *this;
}
MM_Pointer& operator=(MM_Pointer<T>& pT)
{
Release();
_mv_pnCount = pT._mv_pnCount;
_mv_pPointer = pT._mv_pPointer;
AddRef();
return *this;
}
virtual ~MM_Pointer()
{
if ( _mv_pPointer != 0 ) Release();
}
void Drop()
{
Release();
_mv_pnCount = 0;
_mv_pPointer = 0;
}
T& operator*() const
{
return *_mv_pPointer;
}
T* operator->() const
{
return _mv_pPointer;
}
bool IsValid()
{
return ( _mv_pnCount !=0);
}
unsigned int GetRefCount()
{
if ( !IsValid() ) return 0;
return *_mv_pnCount;
}
bool operator==(const T* p) const
{
return ( _mv_pPointer == p );
}
bool operator!=(const T* p) const
{
return ( _mv_pPointer != p );
}
bool operator==(const MM_Pointer<T>& p) const
{
return ( _mv_pPointer == p._mv_pPointer );
}
bool operator!=(const MM_Pointer<T>& p) const
{
return ( _mv_pPointer != p._mv_pPointer );
}
protected:
void AddRef()
{
if ( IsValid() )
{
//std::cout << "Ref Added" << std::endl;
*_mv_pnCount = *_mv_pnCount + 1;
}
}
void Release()
{
if ( !IsValid() ) return;
//if ( *_mv_pnCount == 0 ) std::cout << "Trying to delete gone" << std::endl;
if ( *_mv_pnCount > 0 )
{
//std::cout << "Ref Removed" << std::endl;
*_mv_pnCount -= 1;
//std::cout << *_mv_pnCount << std::endl;
if ( *_mv_pnCount == 0 ) DeleteThis();
}
}
void DeleteThis()
{
//std::cout << "Deleting" << std::endl;
if ( _mv_pnCount != 0 ) {delete _mv_pnCount; _mv_pnCount = 0;}
if ( _mv_pPointer != 0 ) {delete _mv_pPointer; _mv_pPointer = 0;}
}
private:
unsigned int* _mv_pnCount;
T* _mv_pPointer;
};
}
}