///////////////////////////////////////////////////////////////////////////////
/// @file deResource_Helper.hpp
///
/// @brief Global resource handler, inline implementation header
///
/// @author Assassin
///
/// This file is the intellectual property of Novus Delta, LLC.. Usage of the
/// contents of this file is subject to the Destiny3D Member License which
/// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
///
/// This file is distributed "AS IS" without warranty of any kind.  Novus
/// Delta, LLC. does not guarantee the fitness of the contents of this file
/// for any particular purpose.
///
/// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
///
/// <hr>
///                                 Change History
/// <hr>
///
/// @date May 2004
/// @author Assassin
/// @remarks Creation
///
///////////////////////////////////////////////////////////////////////////////

#ifndef DERESOURCE_HELPER_HPP
#define DERESOURCE_HELPER_HPP

#include "deResource.hpp"

//=================================================================================
// deResourceBase Class
//
// Middle-man class which all resource handling classes must derive from
//=================================================================================

/// inherit from this class instead of IdeResourceBase to get basic functionality implemented
class deResourceBase : virtual public IdeResourceBase, public deRefCountBase
{
public:
	deBoolean IsDirty(void) 
	{ return m_Dirty; }
	const char * GetFilename(void)
	{ return m_Filename; }
	void GetFilenameBuffer(char * buffer, long buffersize)
	{ int len = min(255,buffersize); memcpy(buffer, m_Filename, len); buffer[len] = '\0';}
	
	// pure virtual must be overridden
	IdeResourceBase * MakeCopy() = 0;
	
	void * GetRscInterface(long interface_id)
	{ if (interface_id == 0) { return (IdeResourceBase*)(this); } DEBUGRETURN(NULL, "base GetRscInterface called\nyou need to implement a derived version"); }
protected:
	virtual ~deResourceBase(void)
	{ RemoveMe(); m_HashTable = NULL; }
	deResourceBase(const char * filename)
		: m_Dirty(deFALSE), m_HashTable(NULL), m_Manager(NULL)
	{ 
		int i; 
		for (i = 0; i < 255 && filename[i]; i++)
			m_Filename[i] = filename[i];
		m_Filename[i] = '\0';
	}
	deResourceBase()
		: m_Dirty(deTRUE), m_HashTable(NULL), m_Manager(NULL)
	{ m_Filename[0] = '\0'; }
	void SetDirty()
	{ m_Dirty = DE_TRUE; }
	deBoolean RemoveMe(void)
	{ if (m_Manager) { if(!m_Manager->RemoveResource(this)) return deFALSE;} return deTRUE;  }
	
private:
	char m_Filename[256];
	deBoolean m_Dirty;
	void * m_HashTable;
	IdeResourceManager * m_Manager;
	
private:
	void SetHash(void *HashTable)
	{ m_HashTable = HashTable; }
	void SetManager(IdeResourceManager *Manager)
	{ m_Manager = Manager; }
	void *GetHash(void)
	{ return m_HashTable; }
	IdeResourceManager *GetManager(void)
	{ return m_Manager; }
};

#endif  //End: DERESOURCE_HELPER_HPP
