[Orxonox-commit 903] r5626 - in code/branches/libraries: cmake src src/core

landauf at orxonox.net landauf at orxonox.net
Tue Aug 11 22:22:55 CEST 2009


Author: landauf
Date: 2009-08-11 22:22:55 +0200 (Tue, 11 Aug 2009)
New Revision: 5626

Added:
   code/branches/libraries/src/core/DynLib.cc
   code/branches/libraries/src/core/DynLib.h
   code/branches/libraries/src/core/DynLibManager.cc
   code/branches/libraries/src/core/DynLibManager.h
Modified:
   code/branches/libraries/cmake/BuildConfig.cmake
   code/branches/libraries/cmake/TargetUtilities.cmake
   code/branches/libraries/src/SpecialConfig.h.in
   code/branches/libraries/src/core/CMakeLists.txt
   code/branches/libraries/src/core/Core.cc
   code/branches/libraries/src/core/Core.h
   code/branches/libraries/src/core/CorePrereqs.h
Log:
Added a dynamic library loader (more or less a copy of Ogre::DynLibManager but with some adjustments for Orxonox). This allows us to load plugins at runtime. Plugin-libraries must be declared with the "PLUGIN" switch in ORXONOX_ADD_LIBRARY in CMake.

Modified: code/branches/libraries/cmake/BuildConfig.cmake
===================================================================
--- code/branches/libraries/cmake/BuildConfig.cmake	2009-08-11 15:57:18 UTC (rev 5625)
+++ code/branches/libraries/cmake/BuildConfig.cmake	2009-08-11 20:22:55 UTC (rev 5626)
@@ -171,7 +171,7 @@
 
 # when building, don't use the install RPATH already
 # (but later on when installing)
-SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) 
+SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
 
 # the RPATH to be used when installing
 SET(CMAKE_INSTALL_RPATH ${ORXONOX_LIBRARY_INSTALL_PATH})

Modified: code/branches/libraries/cmake/TargetUtilities.cmake
===================================================================
--- code/branches/libraries/cmake/TargetUtilities.cmake	2009-08-11 15:57:18 UTC (rev 5625)
+++ code/branches/libraries/cmake/TargetUtilities.cmake	2009-08-11 20:22:55 UTC (rev 5626)
@@ -34,6 +34,7 @@
  #      NO_DLL_INTERFACE:  Link statically with MSVC
  #      NO_SOURCE_GROUPS:  Don't create msvc source groups
  #      STATIC/SHARED:     Inherited from ADD_LIBRARY
+ #      PLUGIN:            For dynamic plugin libraries
  #      WIN32:             Inherited from ADD_EXECUTABLE
  #      PCH_NO_DEFAULT:    Do not make precompiled header files default if
  #                         specified with PCH_FILE
@@ -79,7 +80,7 @@
   # Specify all possible options (either switch or with add. arguments)
   SET(_switches   FIND_HEADER_FILES  EXCLUDE_FROM_ALL  ORXONOX_EXTERNAL
                   NO_DLL_INTERFACE   NO_SOURCE_GROUPS  ${_additional_switches}
-                  PCH_NO_DEFAULT NO_INSTALL)
+                  PCH_NO_DEFAULT     NO_INSTALL        PLUGIN)
   SET(_list_names LINK_LIBRARIES  VERSION   SOURCE_FILES  DEFINE_SYMBOL
                   TOLUA_FILES     PCH_FILE  PCH_EXCLUDE OUTPUT_NAME)
   PARSE_MACRO_ARGUMENTS("${_switches}" "${_list_names}" ${ARGN})
@@ -146,15 +147,29 @@
     ENDIF()
   ENDIF()
 
+  # PLUGIN A
+  IF(_arg_PLUGIN)
+    SET(_arg_PLUGIN MODULE)
+    SET(_arg_SHARED)
+    SET(_arg_STATIC)
+  ENDIF()
+
   # Add the library/executable
   IF("${_target_type}" STREQUAL "LIBRARY")
-    ADD_LIBRARY(${_target_name} ${_arg_STATIC} ${_arg_SHARED}
+    ADD_LIBRARY(${_target_name} ${_arg_STATIC} ${_arg_SHARED} ${_arg_PLUGIN}
                 ${_arg_EXCLUDE_FROM_ALL} ${_${_target_name}_files})
   ELSE()
     ADD_EXECUTABLE(${_target_name} ${_arg_WIN32} ${_arg_EXCLUDE_FROM_ALL}
                    ${_${_target_name}_files})
   ENDIF()
 
+  # PLUGIN B
+  IF (_arg_PLUGIN)
+    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_PLUGIN_OUTPUT_DIRECTORY})
+    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_PLUGIN_OUTPUT_DIRECTORY})
+    ADD_PLUGIN(${_target_name})
+  ENDIF()
+
   # LINK_LIBRARIES
   IF(_arg_LINK_LIBRARIES)
     TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBRARIES})
@@ -173,7 +188,7 @@
   ENDIF()
 
   # OUTPUT_NAME
-  IF(_arg_OUTPUT_NAME )
+  IF(_arg_OUTPUT_NAME)
     SET_TARGET_PROPERTIES(${_target_name} PROPERTIES OUTPUT_NAME  ${_arg_OUTPUT_NAME})
   ENDIF()
 
@@ -183,11 +198,37 @@
   ENDIF()
 
   IF(NOT _arg_STATIC AND NOT _arg_NO_INSTALL)
+    SET(_library_destination ${ORXONOX_LIBRARY_INSTALL_PATH})
+    IF (_arg_PLUGIN)
+      SET(_library_destination ${ORXONOX_PLUGIN_INSTALL_PATH})
+    ENDIF()
+
     INSTALL(TARGETS ${_target_name}
       RUNTIME DESTINATION ${ORXONOX_RUNTIME_INSTALL_PATH}
-      LIBRARY DESTINATION ${ORXONOX_LIBRARY_INSTALL_PATH}
+      LIBRARY DESTINATION ${_library_destination}
       #ARCHIVE DESTINATION ${ORXONOX_ARCHIVE_INSTALL_PATH}
     )
   ENDIF()
 
 ENDFUNCTION(TU_ADD_TARGET)
+
+
+# Creates a helper file with name <name_of_the_library>.plugin
+# This helps finding dynamically loadable plugins at runtime
+
+FUNCTION(ADD_PLUGIN _name)
+  # We use the properties to get the name because the librarys name may differ from
+  # the targets name (for example orxonox <-> liborxonox)
+
+  GET_TARGET_PROPERTY(_target_loc ${_name} LOCATION)
+  GET_FILENAME_COMPONENT(_target_name ${_target_loc} NAME_WE)
+
+  SET(_plugin_filename "${CMAKE_PLUGIN_OUTPUT_DIRECTORY}/${_target_name}.plugin")
+
+  FILE(WRITE ${_plugin_filename})
+
+  INSTALL(
+    FILES ${_plugin_filename}
+    DESTINATION ${ORXONOX_PLUGIN_INSTALL_PATH}
+  )
+ENDFUNCTION(ADD_PLUGIN)

Modified: code/branches/libraries/src/SpecialConfig.h.in
===================================================================
--- code/branches/libraries/src/SpecialConfig.h.in	2009-08-11 15:57:18 UTC (rev 5625)
+++ code/branches/libraries/src/SpecialConfig.h.in	2009-08-11 20:22:55 UTC (rev 5626)
@@ -86,6 +86,7 @@
     const char ORXONOX_CONFIG_DEV_PATH[]      = "@CMAKE_CONFIG_OUTPUT_DIRECTORY@/" BOOST_PP_STRINGIZE(CMAKE_BUILD_TYPE);
     const char ORXONOX_LOG_DEV_PATH[]         = "@CMAKE_LOG_OUTPUT_DIRECTORY@/"    BOOST_PP_STRINGIZE(CMAKE_BUILD_TYPE);
 #else
+    const char ORXONOX_PLUGIN_DEV_PATH[]      = "@CMAKE_PLUGIN_OUTPUT_DIRECTORY@";
     const char ORXONOX_CONFIG_DEV_PATH[]      = "@CMAKE_CONFIG_OUTPUT_DIRECTORY@";
     const char ORXONOX_LOG_DEV_PATH[]         = "@CMAKE_LOG_OUTPUT_DIRECTORY@";
 #endif

Modified: code/branches/libraries/src/core/CMakeLists.txt
===================================================================
--- code/branches/libraries/src/core/CMakeLists.txt	2009-08-11 15:57:18 UTC (rev 5625)
+++ code/branches/libraries/src/core/CMakeLists.txt	2009-08-11 20:22:55 UTC (rev 5626)
@@ -22,6 +22,8 @@
   ConfigFileManager.cc
   ConfigValueContainer.cc
   Core.cc
+  DynLib.cc
+  DynLibManager.cc
   Event.cc
   Game.cc
   GameMode.cc

Modified: code/branches/libraries/src/core/Core.cc
===================================================================
--- code/branches/libraries/src/core/Core.cc	2009-08-11 15:57:18 UTC (rev 5625)
+++ code/branches/libraries/src/core/Core.cc	2009-08-11 20:22:55 UTC (rev 5626)
@@ -39,6 +39,7 @@
 #include <fstream>
 #include <cstdlib>
 #include <cstdio>
+#include <boost/version.hpp>
 #include <boost/filesystem.hpp>
 #include <OgreRenderWindow.h>
 
@@ -67,6 +68,7 @@
 #include "ConfigFileManager.h"
 #include "ConfigValueIncludes.h"
 #include "CoreIncludes.h"
+#include "DynLibManager.h"
 #include "Factory.h"
 #include "GameMode.h"
 #include "GraphicsManager.h"
@@ -79,6 +81,13 @@
 #include "TclThreadManager.h"
 #include "input/InputManager.h"
 
+// Boost 1.36 has some issues with deprecated functions that have been omitted
+#if (BOOST_VERSION == 103600)
+#  define BOOST_LEAF_FUNCTION filename
+#else
+#  define BOOST_LEAF_FUNCTION leaf
+#endif
+
 namespace orxonox
 {
     //! Static pointer to the singleton
@@ -259,8 +268,52 @@
         // Set the hard coded fixed paths
         this->setFixedPaths();
 
-        // TODO: Load plugins
+        // Create a new dynamic library manager
+        this->dynLibManager_.reset(new DynLibManager());
 
+        // Load plugins
+        try
+        {
+            // We search for helper files with the following extension
+            std::string pluginextension = ".plugin";
+            size_t pluginextensionlength = pluginextension.size();
+
+            // Search in the directory of our executable
+            boost::filesystem::path searchpath = this->getRootPath() / ORXONOX_PLUGIN_INSTALL_PATH;
+
+            boost::filesystem::directory_iterator file(searchpath);
+            boost::filesystem::directory_iterator end;
+
+            // Iterate through all files
+            while (file != end)
+            {
+                std::string filename = file->BOOST_LEAF_FUNCTION();
+
+                // Check if the file ends with the exension in question
+                if (filename.size() > pluginextensionlength)
+                {
+                    if (filename.substr(filename.size() - pluginextensionlength) == pluginextension)
+                    {
+                        // We've found a helper file - now load the library with the same name
+                        std::string library = filename.substr(0, filename.size() - pluginextensionlength);
+                        boost::filesystem::path librarypath = searchpath / library;
+
+                        this->dynLibManager_->load(librarypath.string());
+                    }
+                }
+
+                ++file;
+            }
+        }
+        catch (const std::exception& e)
+        {
+            COUT(1) << "An error occurred while loading plugins: " << e.what() << std::endl;
+        }
+        catch (...)
+        {
+            COUT(1) << "An error occurred while loading plugins." << std::endl;
+        }
+
         // Parse command line arguments AFTER the plugins have been loaded (static code!)
         CommandLine::parseCommandLine(cmdLine);
 

Modified: code/branches/libraries/src/core/Core.h
===================================================================
--- code/branches/libraries/src/core/Core.h	2009-08-11 15:57:18 UTC (rev 5625)
+++ code/branches/libraries/src/core/Core.h	2009-08-11 20:22:55 UTC (rev 5626)
@@ -118,6 +118,7 @@
             void setThreadAffinity(int limitToCPU);
 
             // Mind the order for the destruction!
+            scoped_ptr<DynLibManager>     dynLibManager_;
             scoped_ptr<SignalHandler>     signalHandler_;
             SimpleScopeGuard              identifierDestroyer_;
             SimpleScopeGuard              consoleCommandDestroyer_;

Modified: code/branches/libraries/src/core/CorePrereqs.h
===================================================================
--- code/branches/libraries/src/core/CorePrereqs.h	2009-08-11 15:57:18 UTC (rev 5625)
+++ code/branches/libraries/src/core/CorePrereqs.h	2009-08-11 20:22:55 UTC (rev 5626)
@@ -111,6 +111,8 @@
     class ConfigValueContainer;
     class ConsoleCommand;
     class Core;
+    class DynLib;
+    class DynLibManager;
     struct Event;
     class EventContainer;
     class Executor;

Added: code/branches/libraries/src/core/DynLib.cc
===================================================================
--- code/branches/libraries/src/core/DynLib.cc	                        (rev 0)
+++ code/branches/libraries/src/core/DynLib.cc	2009-08-11 20:22:55 UTC (rev 5626)
@@ -0,0 +1,134 @@
+/*
+-----------------------------------------------------------------------------
+This source file is part of OGRE
+    (Object-oriented Graphics Rendering Engine)
+For the latest info, see http://www.ogre3d.org/
+
+Copyright (c) 2000-2006 Torus Knot Software Ltd
+Also see acknowledgements in Readme.html
+
+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.
+
+You may alternatively use this source under the terms of a specific version of
+the OGRE Unrestricted License provided you have obtained such a license from
+Torus Knot Software Ltd.
+-----------------------------------------------------------------------------
+*/
+
+// 08/11/2009: Small adjustments for Orxonox by Fabian 'x3n' Landau
+
+#include "DynLib.h"
+
+#include "util/Exception.h"
+
+#ifdef ORXONOX_PLATFORM_WINDOWS
+#  define WIN32_LEAN_AND_MEAN
+#  ifndef NOMINMAX
+#    define NOMINMAX // required to stop windows.h messing up std::min
+#  endif
+#  include <windows.h>
+#endif
+
+#ifdef ORXONOX_PLATFORM_APPLE
+#   include "macPlugins.h"
+#endif
+
+namespace orxonox
+{
+    //-----------------------------------------------------------------------
+    DynLib::DynLib( const std::string& name )
+    {
+        mName = name;
+        m_hInst = NULL;
+    }
+
+    //-----------------------------------------------------------------------
+    DynLib::~DynLib()
+    {
+    }
+
+    //-----------------------------------------------------------------------
+    void DynLib::load()
+    {
+        // Log library load
+        COUT(2) << "Loading plugin " << mName << std::endl;
+
+		std::string name = mName;
+#ifdef ORXONOX_PLATFORM_LINUX
+        // dlopen() does not add .so to the filename, like windows does for .dll
+        if (name.substr(name.length() - 3, 3) != ".so")
+           name += ".so";
+#endif
+
+        m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD( name.c_str() );
+
+        if( !m_hInst )
+            ThrowException(
+                General,
+                "Could not load dynamic library " + mName +
+                ".  System Error: " + dynlibError());
+    }
+
+    //-----------------------------------------------------------------------
+    void DynLib::unload()
+    {
+        // Log library unload
+        COUT(4) << "Unloading plugin " << mName << std::endl;
+
+        if( DYNLIB_UNLOAD( m_hInst ) )
+		{
+            ThrowException(
+                General,
+                "Could not unload dynamic library " + mName +
+                ".  System Error: " + dynlibError());
+		}
+
+    }
+
+    //-----------------------------------------------------------------------
+    void* DynLib::getSymbol( const std::string& strName ) const throw()
+    {
+        return (void*)DYNLIB_GETSYM( m_hInst, strName.c_str() );
+    }
+    //-----------------------------------------------------------------------
+    std::string DynLib::dynlibError( void )
+    {
+#if defined(ORXONOX_PLATFORM_WINDOWS)
+        LPVOID lpMsgBuf;
+        FormatMessage(
+            FORMAT_MESSAGE_ALLOCATE_BUFFER |
+            FORMAT_MESSAGE_FROM_SYSTEM |
+            FORMAT_MESSAGE_IGNORE_INSERTS,
+            NULL,
+            GetLastError(),
+            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+            (LPTSTR) &lpMsgBuf,
+            0,
+            NULL
+            );
+        std::string ret = (char*)lpMsgBuf;
+        // Free the buffer.
+        LocalFree( lpMsgBuf );
+        return ret;
+#elif defined(ORXONOX_PLATFORM_LINUX)
+        return std::string(dlerror());
+#elif defined(ORXONOX_PLATFORM_APPLE)
+        return std::string(mac_errorBundle());
+#else
+        return std::string("");
+#endif
+    }
+
+}

Added: code/branches/libraries/src/core/DynLib.h
===================================================================
--- code/branches/libraries/src/core/DynLib.h	                        (rev 0)
+++ code/branches/libraries/src/core/DynLib.h	2009-08-11 20:22:55 UTC (rev 5626)
@@ -0,0 +1,121 @@
+/*
+-----------------------------------------------------------------------------
+This source file is part of OGRE
+    (Object-oriented Graphics Rendering Engine)
+For the latest info, see http://www.ogre3d.org/
+
+Copyright (c) 2000-2006 Torus Knot Software Ltd
+Also see acknowledgements in Readme.html
+
+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.
+
+You may alternatively use this source under the terms of a specific version of
+the OGRE Unrestricted License provided you have obtained such a license from
+Torus Knot Software Ltd.
+-----------------------------------------------------------------------------
+*/
+
+// 08/11/2009: Small adjustments for Orxonox by Fabian 'x3n' Landau
+
+#ifndef _DynLib_H__
+#define _DynLib_H__
+
+#include "CorePrereqs.h"
+
+#include <string>
+
+#if defined(ORXONOX_PLATFORM_WINDOWS)
+#    define DYNLIB_HANDLE hInstance
+#    define DYNLIB_LOAD( a ) LoadLibraryEx( a, NULL, LOAD_WITH_ALTERED_SEARCH_PATH )
+#    define DYNLIB_GETSYM( a, b ) GetProcAddress( a, b )
+#    define DYNLIB_UNLOAD( a ) !FreeLibrary( a )
+
+struct HINSTANCE__;
+typedef struct HINSTANCE__* hInstance;
+
+#elif defined(ORXONOX_PLATFORM_LINUX)
+#    define DYNLIB_HANDLE void*
+#    define DYNLIB_LOAD( a ) dlopen( a, RTLD_LAZY | RTLD_GLOBAL)
+#    define DYNLIB_GETSYM( a, b ) dlsym( a, b )
+#    define DYNLIB_UNLOAD( a ) dlclose( a )
+
+#elif defined(ORXONOX_PLATFORM_APPLE)
+#    define DYNLIB_HANDLE CFBundleRef
+#    define DYNLIB_LOAD( a ) mac_loadExeBundle( a )
+#    define DYNLIB_GETSYM( a, b ) mac_getBundleSym( a, b )
+#    define DYNLIB_UNLOAD( a ) mac_unloadExeBundle( a )
+#endif
+
+namespace orxonox
+{
+    /** Resource holding data about a dynamic library.
+        @remarks
+            This class holds the data required to get symbols from
+            libraries loaded at run-time (i.e. from DLL's for so's)
+        @author
+            Adrian Cearnãu (cearny at cearny.ro)
+        @since
+            27 January 2002
+        @see
+            Resource
+    */
+	class _CoreExport DynLib
+    {
+	protected:
+		std::string mName;
+        /// Gets the last loading error
+        std::string dynlibError(void);
+    public:
+        /** Default constructor - used by DynLibManager.
+            @warning
+                Do not call directly
+        */
+        DynLib( const std::string& name );
+
+        /** Default destructor.
+        */
+        ~DynLib();
+
+        /** Load the library
+        */
+        void load();
+        /** Unload the library
+        */
+        void unload();
+		/// Get the name of the library
+		const std::string& getName(void) const { return mName; }
+
+        /**
+            Returns the address of the given symbol from the loaded library.
+            @param
+                strName The name of the symbol to search for
+            @returns
+                If the function succeeds, the returned value is a handle to
+                the symbol.
+            @par
+                If the function fails, the returned value is <b>NULL</b>.
+
+        */
+        void* getSymbol( const std::string& strName ) const throw();
+
+    protected:
+
+        /// Handle to the loaded library.
+        DYNLIB_HANDLE m_hInst;
+    };
+
+}
+
+#endif

Added: code/branches/libraries/src/core/DynLibManager.cc
===================================================================
--- code/branches/libraries/src/core/DynLibManager.cc	                        (rev 0)
+++ code/branches/libraries/src/core/DynLibManager.cc	2009-08-11 20:22:55 UTC (rev 5626)
@@ -0,0 +1,86 @@
+/*
+-----------------------------------------------------------------------------
+This source file is part of OGRE
+    (Object-oriented Graphics Rendering Engine)
+For the latest info, see http://www.ogre3d.org/
+
+Copyright (c) 2000-2006 Torus Knot Software Ltd
+Also see acknowledgements in Readme.html
+
+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.
+
+You may alternatively use this source under the terms of a specific version of
+the OGRE Unrestricted License provided you have obtained such a license from
+Torus Knot Software Ltd.
+-----------------------------------------------------------------------------
+*/
+
+// 08/11/2009: Small adjustments for Orxonox by Fabian 'x3n' Landau
+
+#include "DynLibManager.h"
+
+#include "DynLib.h"
+
+namespace orxonox
+{
+    //-----------------------------------------------------------------------
+    //! Static pointer to the singleton
+    DynLibManager* DynLibManager::singletonPtr_s  = 0;
+
+    //-----------------------------------------------------------------------
+	DynLibManager::DynLibManager()
+	{
+	}
+	//-----------------------------------------------------------------------
+    DynLib* DynLibManager::load( const std::string& filename)
+    {
+		DynLibList::iterator i = mLibList.find(filename);
+		if (i != mLibList.end())
+		{
+			return i->second;
+		}
+		else
+		{
+	        DynLib* pLib = new DynLib(filename);
+			pLib->load();
+        	mLibList[filename] = pLib;
+	        return pLib;
+		}
+    }
+	//-----------------------------------------------------------------------
+	void DynLibManager::unload(DynLib* lib)
+	{
+		DynLibList::iterator i = mLibList.find(lib->getName());
+		if (i != mLibList.end())
+		{
+			mLibList.erase(i);
+		}
+		lib->unload();
+		delete lib;
+	}
+	//-----------------------------------------------------------------------
+    DynLibManager::~DynLibManager()
+    {
+        // Unload & delete resources in turn
+        for( DynLibList::iterator it = mLibList.begin(); it != mLibList.end(); ++it )
+        {
+            it->second->unload();
+            delete it->second;
+        }
+
+        // Empty the list
+        mLibList.clear();
+    }
+}

Added: code/branches/libraries/src/core/DynLibManager.h
===================================================================
--- code/branches/libraries/src/core/DynLibManager.h	                        (rev 0)
+++ code/branches/libraries/src/core/DynLibManager.h	2009-08-11 20:22:55 UTC (rev 5626)
@@ -0,0 +1,87 @@
+/*
+-----------------------------------------------------------------------------
+This source file is part of OGRE
+    (Object-oriented Graphics Rendering Engine)
+For the latest info, see http://www.ogre3d.org/
+
+Copyright (c) 2000-2006 Torus Knot Software Ltd
+Also see acknowledgements in Readme.html
+
+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.
+
+You may alternatively use this source under the terms of a specific version of
+the OGRE Unrestricted License provided you have obtained such a license from
+Torus Knot Software Ltd.
+-----------------------------------------------------------------------------
+*/
+
+// 08/11/2009: Small adjustments for Orxonox by Fabian 'x3n' Landau
+
+#ifndef _DynLibManager_H__
+#define _DynLibManager_H__
+
+#include "CorePrereqs.h"
+#include "util/Singleton.h"
+
+namespace orxonox
+{
+    /** Manager for Dynamic-loading Libraries.
+        @remarks
+            This manager keeps a track of all the open dynamic-loading
+            libraries, opens them and returns references to already-open
+            libraries.
+    */
+    class _CoreExport DynLibManager: public Singleton<DynLibManager>
+    {
+        friend class Singleton<DynLibManager>;
+
+        protected:
+            typedef std::map<std::string, DynLib*> DynLibList;
+            DynLibList mLibList;
+
+        public:
+            /** Default constructor.
+                @note
+                    <br>Should never be called as the singleton is automatically
+                    created during the creation of the Root object.
+                @see
+                    Root::Root
+            */
+            DynLibManager();
+
+            /** Default destructor.
+                @see
+                    Root::~Root
+            */
+            virtual ~DynLibManager();
+
+            /** Loads the passed library.
+                @param
+                    filename The name of the library. The extension can be omitted
+            */
+            DynLib* load(const std::string& filename);
+
+            /** Unloads the passed library.
+            @param
+            filename The name of the library. The extension can be omitted
+            */
+            void unload(DynLib* lib);
+
+        private:
+            static DynLibManager* singletonPtr_s;
+    };
+}
+
+#endif




More information about the Orxonox-commit mailing list