#include "munga.h"
#pragma hdrstop

#include "wrhous.h"

// #define LOCAL_TEST

#if defined(LOCAL_TEST)
#	define Test_Tell(n) DEBUG_STREAM << n
#else
#	define Test_Tell(n)
#endif

//#######################################################################
// WarehouseObject
//#######################################################################
WarehouseObject::WarehouseObject(
	const char *new_name,
	Logical keep_forever
):
	Plug(WarehouseObject::WarehouseObjectClassID)
{
	Test_Tell("WarehouseObject::WarehouseObject(" << new_name << "\n");
	Str_Copy(name, new_name, sizeof(name));
	resourceID = ResourceDescription::NullResourceType;

	referenceCount = 1;		// at least one reference exists, otherwise no obj
	keepForever = keep_forever;
	Check_Fpu();
}

WarehouseObject::WarehouseObject(
	ResourceDescription::ResourceID new_resource_id,
	Logical keep_forever
):
	Plug(WarehouseObject::WarehouseObjectClassID)
{
	Test_Tell("WarehouseObject::WarehouseObject(" << new_resource_id << "\n");
	name[0] = '\0';
	resourceID = new_resource_id;

	referenceCount = 1;		// at least one reference exists, otherwise no obj
	keepForever = keep_forever;
	Check_Fpu();
}

WarehouseObject::~WarehouseObject()
{
	Check(this);
   Check_Fpu();
}

Logical
	WarehouseObject::TestInstance() const
{
	return True;
}

//#######################################################################
// WarehouseBin
//#######################################################################
WarehouseBin::WarehouseBin() :
	Node(WarehouseBin::WarehouseBinClassID),
	instanceList(this)
{
	Test_Tell("WarehouseBin::WarehouseBin()\n");
	Check_Pointer(this);

	Check_Fpu();
}

WarehouseBin::~WarehouseBin()
{
	Check(this);
}

Logical
	WarehouseBin::TestInstance() const
{
	return True;
}

WarehouseObject *
	WarehouseBin::Find(const char *old_name)
{
	Check(this);

	ChainIteratorOf<WarehouseObject*>
		i(instanceList);

	WarehouseObject
		*object_instance;

	while ((object_instance=i.ReadAndNext()) != NULL)
	{
		Check(object_instance);
		if (stricmp(object_instance->name, old_name) == 0)
		{
			Check_Fpu();
			return object_instance;
		}
	}
	Check_Fpu();
	return NULL;
}

WarehouseObject *
	WarehouseBin::Find(ResourceDescription::ResourceID old_resource_id)
{
	Check(this);

	ChainIteratorOf<WarehouseObject*>
		i(instanceList);

	WarehouseObject
		*object_instance;

	while ((object_instance=i.ReadAndNext()) != NULL)
	{
		Check(object_instance);
		if (object_instance->resourceID == old_resource_id)
		{
			Check_Fpu();
			return object_instance;
		}
	}
	Check_Fpu();
	return NULL;
}

//#######################################################################
// Warehouse
//#######################################################################
Warehouse::Warehouse()
{
	Test_Tell("Warehouse::Warehouse\n");
	Check_Pointer(this);
}

Warehouse::~Warehouse()
{
	Test_Tell("Warehouse::~Warehouse\n");
	Check(this);
	Purge();
	Check_Fpu();
}

void
	Warehouse::Purge()
{
	Check(this);

	bitMapBin.Purge();
	pixelMap8Bin.Purge();
	palette8Bin.Purge();

	Check_Fpu();
}


Logical
	Warehouse::TestInstance() const
{
	Check_Pointer(this);
	return True;
}

