/*
* @file utils.h
*
* @brief miscellaneous utilties available to all
*
* Threading: none
*
* @invariants:
*
* Copyright (c) 2012 Perforce Software
* Confidential. All Rights Reserved.
* @author Wendy Heffner
*
* Creation Date: December 5, 2012
*/
////////////////////////////////////////////////////////////////////////////
// RefCount - CLASS //
////////////////////////////////////////////////////////////////////////////
class RefCount
{
public:
RefCount()
{
count=0;
}
const int &Increment()
{
++count;
return count;
}
const int &Decrement()
{
--count;
return count;
}
int Value()
{
return count;
}
private:
int count;
};
////////////////////////////////////////////////////////////////////////////
// SmartPointer - CLASS //
////////////////////////////////////////////////////////////////////////////
/*
* Reference counted smart pointer. If last smart pointer referencing the object
* of type T leaves allocation scope then the destructor of smart pointer
* will delete the referenced T object.
*/
template < typename T > class SmartPointer
{
public:
SmartPointer() : pData(0), ref(0)
{
ref = new RefCount();
ref->Increment();
};
SmartPointer(T* t) : pData(t), ref(0)
{
ref = new RefCount();
ref->Increment();
};
SmartPointer(const SmartPointer<T>& ptr) : pData(ptr.pData), ref(ptr.ref)
{
ref->Increment();
};
~SmartPointer()
{
if( ref->Decrement() == 0)
{
delete pData;
delete ref;
}
};
T& operator* ()
{
return *pData;
};
T* operator-> ()
{
return pData;
};
SmartPointer<T>& operator = (const SmartPointer<T>& ptr)
{
// Am I on both rhs and lhs if so skip
if (this != &ptr)
{
if( ref->Decrement() == 0)
{
delete pData;
delete ref;
}
pData = ptr.pData;
ref = ptr.ref;
ref->Increment();
}
return *this;
};
private:
T* pData; // data pointer
RefCount* ref; // reference count
};
# |
Change |
User |
Description |
Committed |
|
#1
|
19992 |
devkix |
"Forking branch 2016-1 of perforce_software-p4 to devkix-p4." |
|
|
//guest/perforce_software/p4/2016-1/support/utils.h |
#1
|
19472 |
Liz Lam |
Initial add of the 2016.1 p4/p4api source code. |
|
|