SharedLibraryManager.h

Go to the documentation of this file.
00001 /*<!-------------------------------------------------------------------->
00002    @class  SharedLibraryPlatform
00003    @author Travis Fischer (fisch0920@gmail.com)
00004    @date   Summer 2008
00005    
00006    @brief
00007       Public interface to loading and unloading an arbitrary number of shared 
00008    libraries.
00009    <!-------------------------------------------------------------------->**/
00010 
00011 #ifndef SHARED_LIBRARY_MANAGER_H_
00012 #define SHARED_LIBRARY_MANAGER_H_
00013 
00014 #include <dynamic/SharedLibraryCommon.h>
00015 #include <map>
00016 
00017 class SharedLibraryPlatform;
00018 class SharedLibrary;
00019 
00020 struct SharedLibraryReference {
00021    SharedLibraryHandle *handle;
00022    int                  refCount;
00023 
00024    inline SharedLibraryReference(SharedLibraryHandle *h = NULL, int r = 0)
00025       : handle(h), refCount(r)
00026    { }
00027    
00028    inline SharedLibraryReference &operator=(const SharedLibraryReference &rhs)
00029    {
00030       handle   = rhs.handle;
00031       refCount = rhs.refCount;
00032       
00033       return *this;
00034    }
00035 };
00036 
00037 DECLARE_STL_TYPEDEF2(std::map<std::string, SharedLibraryReference>, 
00038                      SharedLibraryMap);
00039 
00040 class DLLEXPORT SharedLibraryManager {
00041    public:
00042       ///@name Constructors
00043       //@{-----------------------------------------------------------------
00044       
00045       inline   SharedLibraryManager(SharedLibraryPlatform *platform)
00046          : m_platform(platform)
00047       {
00048          ASSERT(m_platform != NULL);
00049       }
00050       
00051       virtual ~SharedLibraryManager()
00052       { }
00053       
00054       
00055       //@}-----------------------------------------------------------------
00056       ///@name Main usage interface
00057       //@{-----------------------------------------------------------------
00058       
00059       /**
00060        * @brief
00061        *    Attempts to load/initialize the given shared library, optionally 
00062        * specifying the given open flags to the platform-dependent loader.
00063        *
00064        * SharedLibraries are reference-counted, so if the specified library 
00065        * is already loaded, the a second copy of the wrapper SharedLibrary 
00066        * will be returned and the reference count will be incremented 
00067        * accordingly.
00068        * 
00069        * @returns a SharedLibrary wrapper on success or NULL on failure
00070        */
00071       virtual SharedLibrary *load(const std::string &name, int openFlags = 0);
00072       
00073       /**
00074        * @brief
00075        *    Decrements the reference count on the given shared library. The 
00076        * library will only be unloaded once no more references to it exist.
00077        *
00078        * @returns true on successful decrement, false otherwise
00079        */
00080       virtual bool unload(const std::string &name);
00081       
00082       /**
00083        * @returns whether or not the given library is currently loaded with 
00084        * respect to this manager.
00085        */
00086       virtual bool isLoaded(const std::string &name) const;
00087       
00088       /**
00089        * @brief
00090        *    Searches all shared libraries that are currently loaded for the 
00091        * given symbol
00092        * 
00093        * @returns the handle of the shared library containing the given symbol 
00094        *    or NULL if the symbol could not be found
00095        */
00096       virtual void *getSymbol(const std::string &symbol);
00097       
00098       /**
00099        * @returns a human-readable string describing the last error which 
00100        *    occurred within this SharedLibrary
00101        */
00102       virtual std::string getLastError();
00103       
00104       
00105       //@}-----------------------------------------------------------------
00106       
00107    protected:
00108       /// Maps library names to reference counts and platform-dependent handles
00109       SharedLibraryMap m_map;
00110       
00111       /// Platform-dependent interface to low-level DSO routines
00112       SharedLibraryPlatform *m_platform;
00113 };
00114 
00115 #endif // SHARED_LIBRARY_MANAGER_H_
00116 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6