initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / dlLibraryTable / dlLibraryTable.C
blob96515eb22aa0210c4bbd698d68b7054f1a2397fe
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "dlLibraryTable.H"
29 #include <dlfcn.h>
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 Foam::dlLibraryTable Foam::dlLibraryTable::loadedLibraries;
36 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
38 Foam::dlLibraryTable::dlLibraryTable()
40     HashTable<fileName, void*, Hash<void*> >()
44 Foam::dlLibraryTable::readDlLibrary::readDlLibrary
46     const dictionary& dict,
47     const word& libsEntry
50     open(dict, libsEntry);
54 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
56 Foam::dlLibraryTable::~dlLibraryTable()
58     forAllIter(dlLibraryTable, *this, iter)
59     {
60         dlclose(iter.key());
61     }
65 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
67 bool Foam::dlLibraryTable::open(const fileName& functionLibName)
69     if (functionLibName.size())
70     {
71         void* functionLibPtr = 
72             dlopen(functionLibName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
74         if (!functionLibPtr)
75         {
76             WarningIn
77             (
78                 "dlLibraryTable::open(const fileName& functionLibName)"
79             )   << "could not load " << dlerror()
80                 << endl;
82             return false;
83         }
84         else
85         {
86             if (!loadedLibraries.found(functionLibPtr))
87             {
88                 loadedLibraries.insert(functionLibPtr, functionLibName);
89                 return true;
90             }
91             else
92             {
93                 return false;
94             }
95         }
96     }
97     else
98     {
99         return false;
100     }
104 bool Foam::dlLibraryTable::open
106     const dictionary& dict,
107     const word& libsEntry
110     if (dict.found(libsEntry))
111     {
112         fileNameList libNames(dict.lookup(libsEntry));
114         bool allOpened = (libNames.size() > 0);
116         forAll(libNames, i)
117         {
118             allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
119         }
121         return allOpened;
122     }
123     else
124     {
125         return false;
126     }
130 // ************************************************************************* //