FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <OpenFOAM/dlLibraryTable.H>
00027 #include <OpenFOAM/debug.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     // Don't bother with calling dlclose(), the OS does so anyways and it can
00059     // even cause problems if the generated finalization code decides to unload
00060     // the loaded library before calling this dtor.
00061 }
00062 
00063 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00064 
00065 bool Foam::dlLibraryTable::open(const fileName& functionLibName)
00066 {
00067     if (functionLibName.size())
00068     {
00069         void* functionLibPtr = NULL;
00070         // set up the list of paths to search the library in
00071         fileNameList searchPathList;
00072         // add the paths listed in controlDict::LibrarySearchPaths to the list
00073         // if the library name is not absolute
00074         if (functionLibName[0] != '/' && debug::controlDict().found("LibrarySearchPaths"))
00075         {
00076             debug::controlDict().lookup("LibrarySearchPaths") >> searchPathList;
00077         }
00078         // in any case, we want to also try the default search paths
00079         // (i.e. LD_LIBRARY_PATH etc.) or, if functionLibName is an
00080         // absolute path, that one. So we append an empty string.
00081         searchPathList.setSize(searchPathList.size()+1,fileName());
00082         forAllConstIter(fileNameList, searchPathList, pathI)
00083         {
00084             // construct the full name
00085             fileName functionLibPath;
00086             if (!pathI->empty())
00087                 functionLibPath = *pathI / functionLibName;
00088             else
00089                 functionLibPath = functionLibName;
00090             functionLibPtr = dlopen(functionLibPath.c_str(), RTLD_LAZY|RTLD_GLOBAL);
00091 
00092 #ifdef darwin
00093             if(!functionLibPtr && functionLibPath.ext()=="so") {
00094                 functionLibPath=functionLibPath.lessExt()+".dylib";
00095                 functionLibPtr =
00096                     dlopen(functionLibPath.c_str(), RTLD_LAZY|RTLD_GLOBAL);
00097             }
00098 #endif
00099             // if successfully loaded, stop searching and display some info
00100             if (functionLibPtr)
00101             {
00102                 Info<< "Loaded  " << functionLibName;
00103                 if (pathI->empty())
00104                     Info<< " from the default search path";
00105                 else
00106                     Info<< " from " << functionLibPath;
00107                 Info<< endl;
00108                 break;
00109             }
00110         }
00111         if (!functionLibPtr)
00112         {
00113             WarningIn
00114             (
00115                 "dlLibraryTable::open(const fileName& functionLibName)"
00116             )   << "could not load " << dlerror()
00117                 << endl;
00118 
00119             return false;
00120         }
00121         else
00122         {
00123             if (!loadedLibraries.found(functionLibPtr))
00124             {
00125                 loadedLibraries.insert(functionLibPtr, functionLibName);
00126                 return true;
00127             }
00128             else
00129             {
00130                 return false;
00131             }
00132         }
00133     }
00134     else
00135     {
00136         return false;
00137     }
00138 }
00139 
00140 
00141 bool Foam::dlLibraryTable::open
00142 (
00143     const dictionary& dict,
00144     const word& libsEntry
00145 )
00146 {
00147     if (dict.found(libsEntry))
00148     {
00149         fileNameList libNames(dict.lookup(libsEntry));
00150 
00151         bool allOpened = (libNames.size() > 0);
00152 
00153         forAll(libNames, i)
00154         {
00155             allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
00156         }
00157 
00158         return allOpened;
00159     }
00160     else
00161     {
00162         return false;
00163     }
00164 }
00165 
00166 
00167 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines