#include "stdafx.h"
#include "UnitManager.h"

UnitManager::~UnitManager()
{
  for (std::list<UnitData*>::const_iterator it = m_unitDataList.begin(); it != m_unitDataList.end(); ++it)
  {
    (*it)->m_unit->setClientInfo(NULL);
    delete *it;
  }
}

void UnitManager::addUnitData(BWAPI::Unit* unit)
{
  if (unit->getClientInfo() == NULL)
  {
    UnitData* unitData = m_factory->instantiateUnitData(unit);
    unitData->m_posInManager = m_unitDataList.insert(m_unitDataList.end(), unitData);

    unit->setClientInfo(unitData);
  }
}

void UnitManager::removeUnitData(BWAPI::Unit* unit)
{
  UnitData* unitData = reinterpret_cast<UnitData*>(unit->getClientInfo());
  assert(unitData != NULL);

  m_unitDataList.erase(unitData->m_posInManager);

  delete unitData;

  unit->setClientInfo(NULL);
}
