dlLibraryTable.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "dlLibraryTable.H"
27 #include "OSspecific.H"
28 #include "int.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  defineTypeNameAndDebug(dlLibraryTable, 0);
35 }
36 
37 
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 
41 {}
42 
43 
45 (
46  const dictionary& dict,
47  const word& libsEntry
48 )
49 {
50  open(dict, libsEntry);
51 }
52 
53 
54 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
55 
57 {
58  forAllReverse(libPtrs_, i)
59  {
60  if (libPtrs_[i])
61  {
62  if (debug)
63  {
64  Info<< "dlLibraryTable::~dlLibraryTable() : closing "
65  << libNames_[i]
66  << " with handle " << uintptr_t(libPtrs_[i]) << endl;
67  }
68  dlClose(libPtrs_[i]);
69  }
70  }
71 }
72 
73 
74 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
75 
77 (
78  const fileName& functionLibName,
79  const bool verbose
80 )
81 {
82  if (functionLibName.size())
83  {
84  void* functionLibPtr = dlOpen(functionLibName, verbose);
85 
86  if (debug)
87  {
88  Info<< "dlLibraryTable::open : opened " << functionLibName
89  << " resulting in handle " << uintptr_t(functionLibPtr) << endl;
90  }
91 
92  if (!functionLibPtr)
93  {
94  if (verbose)
95  {
96  WarningIn
97  (
98  "dlLibraryTable::open(const fileName&, const bool)"
99  ) << "could not load " << functionLibName
100  << endl;
101  }
102 
103  return false;
104  }
105  else
106  {
107  libPtrs_.append(functionLibPtr);
108  libNames_.append(functionLibName);
109  return true;
110  }
111  }
112  else
113  {
114  return false;
115  }
116 }
117 
118 
120 (
121  const fileName& functionLibName,
122  const bool verbose
123 )
124 {
125  label index = -1;
126  forAllReverse(libNames_, i)
127  {
128  if (libNames_[i] == functionLibName)
129  {
130  index = i;
131  break;
132  }
133  }
134 
135  if (index != -1)
136  {
137  if (debug)
138  {
139  Info<< "dlLibraryTable::close : closing " << functionLibName
140  << " with handle " << uintptr_t(libPtrs_[index]) << endl;
141  }
142 
143  bool ok = dlClose(libPtrs_[index]);
144 
145  libPtrs_[index] = NULL;
146  libNames_[index] = fileName::null;
147 
148  if (!ok)
149  {
150  if (verbose)
151  {
152  WarningIn
153  (
154  "dlLibraryTable::close(const fileName&)"
155  ) << "could not close " << functionLibName
156  << endl;
157  }
158 
159  return false;
160  }
161 
162  return true;
163  }
164  return false;
165 }
166 
167 
168 void* Foam::dlLibraryTable::findLibrary(const fileName& functionLibName)
169 {
170  label index = -1;
171  forAllReverse(libNames_, i)
172  {
173  if (libNames_[i] == functionLibName)
174  {
175  index = i;
176  break;
177  }
178  }
179 
180  if (index != -1)
181  {
182  return libPtrs_[index];
183  }
184  return NULL;
185 }
186 
187 
189 (
190  const dictionary& dict,
191  const word& libsEntry
192 )
193 {
194  if (dict.found(libsEntry))
195  {
196  fileNameList libNames(dict.lookup(libsEntry));
197 
198  bool allOpened = !libNames.empty();
199 
200  forAll(libNames, i)
201  {
202  allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
203  }
204 
205  return allOpened;
206  }
207  else
208  {
209  return false;
210  }
211 }
212 
213 
214 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:306
bool close(const fileName &name, const bool verbose=true)
Close the named library, optionally with warnings if problems occur.
System integer.
bool empty() const
Return true if the UList is empty (ie, size() is zero).
Definition: UListI.H:313
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A class for handling words, derived from string.
Definition: word.H:59
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
messageStream Info
~dlLibraryTable()
Destructor.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
Namespace for OpenFOAM.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
#define WarningIn(functionName)
Report a warning using Foam::Warning.
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
Definition: POSIX.C:1168
bool open(const fileName &name, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
#define forAll(list, i)
Definition: UList.H:421
static const fileName null
An empty fileName.
Definition: fileName.H:97
#define forAllReverse(list, i)
Definition: UList.H:424
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
A class for handling file names.
Definition: fileName.H:69
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1196
void * findLibrary(const fileName &name)
Find the handle of the named library.
dlLibraryTable()
Construct null.
defineTypeNameAndDebug(combustionModel, 0)