/** @file weakrefuserdata.h
*	Userdata type for EA::Types::WeakRef
*	@author Chris Burns (cburns@ea.com)
*
*	EABaseTypes
*	(c) 2013 Electronic Arts Inc.
*/

#ifndef EABASETYPES_WEAKREFUSERDATA_H
#define EABASETYPES_WEAKREFUSERDATA_H

#include "EABaseTypes/datatypes.h"

namespace EA
{
	namespace Types
	{
		template<typename T>
		class WeakRefUserData : public UserData
		{
		public:

			virtual BaseTypeReturn Clone() { return Create(mFactory, mRef); }

			virtual bool Equals(UserDataIn pCompare)
			{
				return EA::Type::TypeOf(*this) == pCompare->GetUserDataType() && mRef == static_cast<WeakRefUserData<T>*>(pCompare.GetUnsafePointer())->mRef;
			}

			virtual EA::Type::TypeID GetUserDataType() { return EA::Type::TypeOf(*this); }

			static UserDataReturn Create(Factory* factory, AutoRefIn<T> ref)
			{
				return new(factory) WeakRefUserData<T>(factory, ref);
			}

			static AutoRef<T> AsRef(BaseTypeIn baseType)
			{
				if(baseType != NULL && baseType->GetType() == USERDATA_TYPE && baseType->AsUserData()->GetUserDataType() == EA::Type::TypeOf<WeakRefUserData<T> >())
				{
					return static_cast<WeakRefUserData<T>*>(baseType.GetUnsafePointer())->mRef;
				}
				else
				{
					return NULL;
				}
			}

		private:

			WeakRefUserData(Factory* factory, AutoRefIn<T> ref):UserData(factory), mRef(ref) {}

			WeakRef<T> mRef;
		};
	}
}

#endif // EABASETYPES_WEAKREFUSERDATA_H
