LibraryDYLD.h

Go to the documentation of this file.
00001 /****************** <VPR heading BEGIN do not edit this line> *****************
00002  *
00003  * VR Juggler Portable Runtime
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Patrick Hartling, Kevin Meinert, Carolina Cruz-Neira
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile$
00010  * Date modified: $Date: 2005-01-17 22:34:01 -0600 (Mon, 17 Jan 2005) $
00011  * Version:       $Revision: 16635 $
00012  * -----------------------------------------------------------------
00013  *
00014  ****************** <VPR heading END do not edit this line> ******************/
00015 
00016 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00017  *
00018  * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
00019  *
00020  * Original Authors:
00021  *   Allen Bierbaum, Christopher Just,
00022  *   Patrick Hartling, Kevin Meinert,
00023  *   Carolina Cruz-Neira, Albert Baker
00024  *
00025  * This library is free software; you can redistribute it and/or
00026  * modify it under the terms of the GNU Library General Public
00027  * License as published by the Free Software Foundation; either
00028  * version 2 of the License, or (at your option) any later version.
00029  *
00030  * This library is distributed in the hope that it will be useful,
00031  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00032  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00033  * Library General Public License for more details.
00034  *
00035  * You should have received a copy of the GNU Library General Public
00036  * License along with this library; if not, write to the
00037  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00038  * Boston, MA 02111-1307, USA.
00039  *
00040  *************** <auto-copyright.pl END do not edit this line> ***************/
00041 
00042 #ifndef _VPR_LIBRARY_DYLD_H_
00043 #define _VPR_LIBRARY_DYLD_H_
00044 
00045 #include <vpr/vprConfig.h>
00046 
00047 #include <stdlib.h>
00048 #include <string>
00049 
00050 #include <vpr/Util/Assert.h>
00051 #include <vpr/Util/ReturnStatus.h>
00052 
00053 #if defined (__GNUC__) && __GNUC__ > 3
00054 #define dl_restrict __restrict
00055 #else
00056 #define dl_restrict
00057 #endif
00058 
00059 
00060 namespace vpr
00061 {
00062 
00073 class LibraryDYLD
00074 {
00075 public:
00085    LibraryDYLD(const std::string& name)
00086       : mName(name)
00087       , mLibrary(NULL)
00088    {
00089       ;
00090    }
00091 
00101    LibraryDYLD()
00102       : mName("")
00103       , mLibrary(NULL)
00104    {
00105       ;
00106    }
00107 
00111    LibraryDYLD(const LibraryDYLD& lib)
00112       : mName("")
00113       , mLibrary(NULL)
00114    {
00115       copy(lib);
00116    }
00117 
00121    ~LibraryDYLD()
00122    {
00123       if ( NULL != mLibrary )
00124       {
00125          unload();
00126       }
00127    }
00128 
00132    LibraryDYLD& operator=(const LibraryDYLD& lib)
00133    {
00134       copy(lib);
00135       return *this;
00136    }
00137 
00141    const std::string& getName() const
00142    {
00143       return mName;
00144    }
00145 
00153    vpr::ReturnStatus load();
00154 
00163    vpr::ReturnStatus unload();
00164 
00171    bool isLoaded() const
00172    {
00173       return mLibrary != NULL;
00174    }
00175 
00177 
00195    void* findSymbol(const char* symbolName)
00196    {
00197       // If no library has been loaded yet, do it now.  This is done to mimic
00198       // the NSPR behavior.
00199       if ( NULL == mLibrary )
00200       {
00201          load();
00202          vprASSERT(NULL != mLibrary && "Could not load any library");
00203       }
00204 
00205       return internalDlsym(mLibrary, symbolName);
00206    }
00207 
00208    void* findSymbol(const std::string& symbolName)
00209    {
00210       return findSymbol(symbolName.c_str());
00211    }
00213 
00215 
00219    static void* findSymbolAndLibrary(const char* symbolName, LibraryDYLD& lib);
00220 
00221    static void* findSymbolAndLibrary(const std::string& symbolName,
00222                                      LibraryDYLD& lib)
00223    {
00224       return findSymbolAndLibrary(symbolName.c_str(), lib);
00225    }
00227 
00228 protected:
00232    void copy(const LibraryDYLD& lib)
00233    {
00234       this->mName    = lib.mName;
00235       this->mLibrary = lib.mLibrary;
00236    }
00237 
00238    void* internalDlopen(const char* filename, int flag);
00239 
00240    const char* internalDlerror();
00241 
00242    void* internalDlsym(void* dl_restrict handle,
00243                        const char* dl_restrict symbol);
00244 
00245    int internalDlclose(void* handle);
00246 
00247 private:
00248    std::string mName;      
00249    void*       mLibrary;   
00250 };
00251 
00252 } // End of vpr namespace
00253 
00254 
00255 #endif /* _VPR_LIBRARY_DYLD_H_ */

Generated on Thu Jan 4 10:52:09 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1