utils.h #1

  • //
  • guest/
  • perforce_software/
  • p4/
  • 2014-2/
  • 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 15903 Matt Attaway Everything should be happy now between the Workshop and the depot paths
//guest/perforce_software/p4/2014_2/support/utils.h
#1 15901 Matt Attaway Clean up code to fit modern Workshop naming standards
//guest/perforce_software/p4/2014.2/support/utils.h
#1 12189 Matt Attaway Initial (and much belated) drop of 2014.2 p4 source code