OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

dlLibraryTable.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "dlLibraryTable.H"
00028 
00029 #include <dlfcn.h>
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 Foam::dlLibraryTable Foam::dlLibraryTable::loadedLibraries;
00034 
00035 
00036 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00037 
00038 Foam::dlLibraryTable::dlLibraryTable()
00039 :
00040     HashTable<fileName, void*, Hash<void*> >()
00041 {}
00042 
00043 
00044 Foam::dlLibraryTable::readDlLibrary::readDlLibrary
00045 (
00046     const dictionary& dict,
00047     const word& libsEntry
00048 )
00049 {
00050     open(dict, libsEntry);
00051 }
00052 
00053 
00054 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00055 
00056 Foam::dlLibraryTable::~dlLibraryTable()
00057 {
00058     forAllIter(dlLibraryTable, *this, iter)
00059     {
00060         dlclose(iter.key());
00061     }
00062 }
00063 
00064 
00065 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00066 
00067 bool Foam::dlLibraryTable::open(const fileName& functionLibName)
00068 {
00069     if (functionLibName.size())
00070     {
00071         void* functionLibPtr = 
00072             dlopen(functionLibName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
00073 
00074         if (!functionLibPtr)
00075         {
00076             WarningIn
00077             (
00078                 "dlLibraryTable::open(const fileName& functionLibName)"
00079             )   << "could not load " << dlerror()
00080                 << endl;
00081 
00082             return false;
00083         }
00084         else
00085         {
00086             if (!loadedLibraries.found(functionLibPtr))
00087             {
00088                 loadedLibraries.insert(functionLibPtr, functionLibName);
00089                 return true;
00090             }
00091             else
00092             {
00093                 return false;
00094             }
00095         }
00096     }
00097     else
00098     {
00099         return false;
00100     }
00101 }
00102 
00103 
00104 bool Foam::dlLibraryTable::open
00105 (
00106     const dictionary& dict,
00107     const word& libsEntry
00108 )
00109 {
00110     if (dict.found(libsEntry))
00111     {
00112         fileNameList libNames(dict.lookup(libsEntry));
00113 
00114         bool allOpened = (libNames.size() > 0);
00115 
00116         forAll(libNames, i)
00117         {
00118             allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
00119         }
00120 
00121         return allOpened;
00122     }
00123     else
00124     {
00125         return false;
00126     }
00127 }
00128 
00129 
00130 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd