/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */

#include "KeosDynLibManager.h"
#include "KeosDynLib.h"
#include "KeosException.h"

namespace Keos
{
    //=======================================================================
    // CDynLibManager implementation
    //=======================================================================

    //-----------------------------------------------------------------------
	CDynLibManager::CDynLibManager()
	{
	}

	//-----------------------------------------------------------------------
    CDynLib* CDynLibManager::Load( const String& filename)
    {        
        CDynLib* pLib = new CDynLib(filename);
        try { pLib->Load(); }
        catch (const CDynLibError& E)
        {   
            delete pLib;
            throw E;
        }
        m_LibList[filename] = pLib;
        return pLib;
    }

	//-----------------------------------------------------------------------
	void CDynLibManager::Unload(CDynLib* lib)
	{
		DynLibList::iterator i = m_LibList.find(lib->GetName());
		if (i != m_LibList.end())
		{
		    m_LibList.erase(i);
		}
		lib->Unload();
        delete lib;
	}

	//-----------------------------------------------------------------------
    CDynLibManager::~CDynLibManager()
    {
        // Unload & delete resources in turn
        for( DynLibList::iterator it = m_LibList.begin(); it != m_LibList.end(); ++it )
        {
            it->second->Unload();
            delete it->second;
        }

        // Empty the list
        m_LibList.clear();
    }
} // namespace Keos
