utils.h #1

  • //
  • guest/
  • perforce_software/
  • p4/
  • 2014-1/
  • support/
  • utils.h
  • View
  • Commits
  • Open Download .zip Download (2 KB)
/*
 * @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 15902 Matt Attaway A second renaming that I will not obliterate as a badge of shame
//guest/perforce_software/p4/2014_1/support/utils.h
#1 15901 Matt Attaway Clean up code to fit modern Workshop naming standards
//guest/perforce_software/p4/2014.1/support/utils.h
#1 12188 Matt Attaway Move 'main' p4 into a release specific directory in prep for new releases
//guest/perforce_software/p4/support/utils.h
#1 9129 Matt Attaway Initial commit of the 2014.1 p4/p4api source code