vpr::LibraryFinder Class Reference

Helper class that will perform dynamic library discovery based on a file extension (e.g., .so or .dll) in a specific directory. More...

#include <vpr/DynLoad/LibraryFinder.h>

List of all members.

Public Types

typedef std::vector< vpr::LibraryPtrLibraryList
 Helper typedef to simplify user code a little.

Public Member Functions

 LibraryFinder (const std::string &libDir, const std::string &libExt, bool scanNow=true)
 
Postcondition:
The named directory is scanned for shared libraries if scanNow is true.

void setLibraryDirectory (const std::string &dir)
 Changes the name of the directory to be searched for shared libraries.
void setLibraryExtension (const std::string &ext)
 Changes the shared library file extension.
void setDirAndExt (const std::string &dir, const std::string &ext)
 Changes the name of the directory to be searched for shared libraries and the shared library file extension.
const LibraryListgetLibraries () const
 Returns the current list of shared libraries found in the library directory.
void rescan ()
 Performs a fresh search of the current library directory for files ending in a platform-specific shared library extension (.so, .dll, etc.


Detailed Description

Helper class that will perform dynamic library discovery based on a file extension (e.g., .so or .dll) in a specific directory.

A vector of all discovered dynamic libraries will be created.

Definition at line 62 of file LibraryFinder.h.


Member Typedef Documentation

typedef std::vector<vpr::LibraryPtr> vpr::LibraryFinder::LibraryList

Helper typedef to simplify user code a little.

Definition at line 66 of file LibraryFinder.h.


Constructor & Destructor Documentation

vpr::LibraryFinder::LibraryFinder ( const std::string &  libDir,
const std::string &  libExt,
bool  scanNow = true 
) [inline]

Postcondition:
The named directory is scanned for shared libraries if scanNow is true.

Parameters:
libDir The name of the directory to search for shared libraries. This directory can be an absolute platform-specific path.
libExt The platform-specific extension that is used to identify shared libraries. This should be of the form "so" (for most UNIX-based systems) or "dll" (for Win32). Note that the "." is not included in the enxtension string.
scanNow A boolean value stating where the directory scan should be performed immediately upon object construction. If it is false, the burden is on the calling code to invoke rescan() before getLibraries(). This parameter is optional, and it defaults to true.
See also:
rescan

getLibraries

Definition at line 87 of file LibraryFinder.h.

00089       : mLibDir(libDir, boost::filesystem::native), mLibExt(libExt)
00090    {
00091       if ( scanNow )
00092       {
00093          rescan();
00094       }
00095    }


Member Function Documentation

void vpr::LibraryFinder::setLibraryDirectory ( const std::string &  dir  )  [inline]

Changes the name of the directory to be searched for shared libraries.

After changing the name, the new directory is scanned for shared libraries.

Postcondition:
The current directory is changed to the given value, and the new directory is scanned for shared libraries.
Parameters:
dir The name of the directory to search for shared libraries. This directory can be an absolute platform-specific path.

Definition at line 108 of file LibraryFinder.h.

00109    {
00110       mLibDir = boost::filesystem::path(dir, boost::filesystem::native);
00111       rescan();
00112    }

void vpr::LibraryFinder::setLibraryExtension ( const std::string &  ext  )  [inline]

Changes the shared library file extension.

After changing the extension, the current directory is rescanned.

Postcondition:
The library file extension is changed to the given value, and the new directory is scanned for shared libraries.
Parameters:
ext The platform-specific extension that is used to identify shared libraries. This should be of the form "so" (for most UNIX-based systems) or "dll" (for Win32). Note that the "." is not included in the enxtension string.

Definition at line 126 of file LibraryFinder.h.

00127    {
00128       mLibExt = ext;
00129       rescan();
00130    }

void vpr::LibraryFinder::setDirAndExt ( const std::string &  dir,
const std::string &  ext 
) [inline]

Changes the name of the directory to be searched for shared libraries and the shared library file extension.

After changing these settings, the new directory is scanned for shared libraries. This method can be used to reduce disk I/O. Whereas the combination of setLibraryDirectory() and setLibraryExtension() would result in two scans, this will only perform one directory scan.

Postcondition:
The library file extension is changed to the given value, and the new directory is scanned for shared libraries.
Parameters:
dir The name of the directory to search for shared libraries. This directory can be an absolute platform-specific path.
ext The platform-specific extension that is used to identify shared libraries. This should be of the form "so" (for most UNIX-based systems) or "dll" (for Win32). Note that the "." is not included in the enxtension string.

Definition at line 150 of file LibraryFinder.h.

00151    {
00152       mLibDir = boost::filesystem::path(dir, boost::filesystem::native);
00153       mLibExt = ext;
00154       rescan();
00155    }

const LibraryList& vpr::LibraryFinder::getLibraries (  )  const [inline]

Returns the current list of shared libraries found in the library directory.

The returned list may be empty. All vpr::LibraryPtr objects will be in a state where the library has not actually been loaded. They are simply objects that encapsulate the shared library that could be loaded and used by the caller.

Precondition:
The library directory has already been scanned.

Definition at line 166 of file LibraryFinder.h.

00167    {
00168       return mLibList;
00169    }

void vpr::LibraryFinder::rescan (  ) 

Performs a fresh search of the current library directory for files ending in a platform-specific shared library extension (.so, .dll, etc.

). Each time this method is invoked, the directory will be rescanned to account for asynchronous changes to the file system, changes to the name of the searched directory, or changes to the shared library file extension string. As a result, calling this method over and over will cause a lot of disk activity.

Definition at line 56 of file LibraryFinder.cpp.

References clrOutNORM, clrRED, vprDBG_CRITICAL_LVL, vprDBG_ERROR(), vprDEBUG, and vprDEBUG_FLUSH.

00057 {
00058    // Wipe out the current list of known libraries.  This will cause the
00059    // memory for each to be deleted, thereby causing the destructor for
00060    // vpr::Library to be invoked.
00061    mLibList.clear();
00062 
00063    if ( fs::is_directory(mLibDir) )
00064    {
00065       fs::directory_iterator end_itr;
00066 
00067       for ( fs::directory_iterator file(mLibDir); file != end_itr; ++file )
00068       {
00069 #if BOOST_VERSION < 103400
00070          // Ignore directories.  Normal files and symlinks are fine.
00071          if ( ! fs::is_directory(*file) )
00072          {
00073             // Construct a substring of file->leaf() that contains only the
00074             // file extension.  We require that the file we will match have
00075             // names that end with mLibExt.
00076             const std::string::size_type pos = file->leaf().size() - mLibExt.size();
00077             const std::string file_ext = file->leaf().substr(pos);
00078 
00079             if ( file_ext == mLibExt )
00080             {
00081                mLibList.push_back(vpr::LibraryPtr(new vpr::Library(file->native_file_string())));
00082             }
00083          }
00084 #else
00085          // Ignore directories.  Normal files and symlinks are fine.
00086          if ( ! fs::is_directory(file->status()) )
00087          {
00088             // Construct a substring of file->path.leaf() that contains only
00089             // the file extension. We require that the file we will match have
00090             // names that end with mLibExt.
00091             const std::string::size_type pos =
00092                file->path().leaf().size() - mLibExt.size();
00093             const std::string file_ext = file->path().leaf().substr(pos);
00094 
00095             if ( file_ext == mLibExt )
00096             {
00097                mLibList.push_back(
00098                   vpr::LibraryPtr(
00099                      new vpr::Library(file->path().native_file_string())
00100                   )
00101                );
00102             }
00103          }
00104 #endif
00105       }
00106    }
00107    else
00108    {
00109       vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
00110          << clrOutNORM(clrRED, "ERROR") << ": " << mLibDir.native_file_string()
00111          << " is not a directory\n" << vprDEBUG_FLUSH;
00112    }
00113 }


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:55:04 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1