/*
 * 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.
 *
 */

#ifndef KEOS_DYNLIB_H
#define KEOS_DYNLIB_H

#include "KeosPrerequisites.h"

#if KEOS_PLATFORM == KEOS_PLATFORM_WIN32
#    define DYNLIB_HANDLE hInstance
#    define DYNLIB_LOAD( a ) LoadLibrary( a )
#    define DYNLIB_GETSYM( a, b ) GetProcAddress( a, b )
#    define DYNLIB_UNLOAD( a ) !FreeLibrary( a )

struct HINSTANCE__;
typedef struct HINSTANCE__* hInstance;
#endif

namespace Keos 
{
    //-----------------------------------------------------------------------
    /** Resource holding data about a dynamic library.
     *  This class holds the data required to get symbols from
     *  libraries loaded at run-time (i.e. from DLL's for so's).
     */
    class KEOS_EXPORT CDynLib
    {
	  protected:
        //! Name of the library
		    String m_strName;

        /** Return error information in library loading.
         *  @return Error information.
         */
        String DynlibError( void ) ;

    public:
        /** Default constructor - used by DynLibManager.
         *  @warning Do not call directly.
         *  @param strName Name of the library.
         */
        CDynLib( const String& strName );

        /** Default destructor.
         */
        ~CDynLib();

        /** Load the library.
         */
        void Load();

        /** Unload the library.
         */
        void Unload();

		/** Get the name of the library.
         *  @return The name of the library.
         */
		const String& GetName(void) const 
        { return m_strName; }

        /** Returns the address of the given symbol from the loaded library.
         *  @param strName The name of the symbol to search for.
         *  @return 
         *      If the function succeeds, the returned value is a handle to
         *      the symbol, if the function fails, the returned value is <b>NULL</b>.
         */
        void* GetSymbol( const String& strName ) const throw();

    protected:

        //! Handle to the loaded library.
        DYNLIB_HANDLE m_hInst;
    };
} // namespace Keos

#endif // KEOS_DYNLIB_H
