dlLibraryTable.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 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 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  defineTypeNameAndDebug(dlLibraryTable, 0);
34 }
35 
37 
38 
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
42 {}
43 
44 
46 (
47  const dictionary& dict,
48  const word& libsEntry
49 )
50 {
51  open(dict, libsEntry);
52 }
53 
54 
55 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
56 
58 {
59  forAllReverse(libPtrs_, i)
60  {
61  if (libPtrs_[i])
62  {
63  if (debug)
64  {
66  << "Closing " << libNames_[i]
67  << " with handle " << uintptr_t(libPtrs_[i]) << endl;
68  }
69  if (!dlClose(libPtrs_[i]))
70  {
71  WarningInFunction<< "Failed closing " << libNames_[i]
72  << " with handle " << uintptr_t(libPtrs_[i]) << endl;
73  }
74  }
75  }
76 }
77 
78 
79 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
80 
82 (
83  const fileName& libName,
84  const bool verbose
85 )
86 {
87  if (libName.size())
88  {
89  void* libPtr = dlOpen
90  (
91  fileName(libName).expand(),
92  verbose
93  );
94 
95  if (debug)
96  {
98  << "Opened " << libName
99  << " resulting in handle " << uintptr_t(libPtr) << endl;
100  }
101 
102  if (!libPtr)
103  {
104  if (verbose)
105  {
107  << "could not load " << libName
108  << endl;
109  }
110 
111  return false;
112  }
113  else
114  {
115  libPtrs_.append(libPtr);
116  libNames_.append(libName);
117  return true;
118  }
119  }
120  else
121  {
122  return false;
123  }
124 }
125 
126 
128 (
129  const fileNameList& libNames,
130  const bool verbose
131 )
132 {
133  bool allOpened = !libNames.empty();
134 
135  forAll(libNames, i)
136  {
137  allOpened = open(libNames[i], verbose) && allOpened;
138  }
139 
140  return allOpened;
141 }
142 
143 
145 (
146  const dictionary& dict,
147  const word& libsEntry
148 )
149 {
150  if (dict.found(libsEntry))
151  {
152  fileNameList libNames(dict.lookup(libsEntry));
153 
154  bool allOpened = !libNames.empty();
155 
156  forAll(libNames, i)
157  {
158  allOpened = open(libNames[i]) && allOpened;
159  }
160 
161  return allOpened;
162  }
163  else
164  {
165  return false;
166  }
167 }
168 
169 
171 (
172  const fileName& libName,
173  const bool verbose
174 )
175 {
176  label index = -1;
177  forAllReverse(libNames_, i)
178  {
179  if (libNames_[i] == libName)
180  {
181  index = i;
182  break;
183  }
184  }
185 
186  if (index != -1)
187  {
188  if (debug)
189  {
191  << "Closing " << libName
192  << " with handle " << uintptr_t(libPtrs_[index]) << endl;
193  }
194 
195  bool ok = dlClose(libPtrs_[index]);
196 
197  libPtrs_[index] = nullptr;
198  libNames_[index] = fileName::null;
199 
200  if (!ok)
201  {
202  if (verbose)
203  {
205  << "could not close " << libName
206  << endl;
207  }
208 
209  return false;
210  }
211 
212  return true;
213  }
214  return false;
215 }
216 
217 
219 {
220  label index = -1;
221  forAllReverse(libNames_, i)
222  {
223  if (libNames_[i] == libName)
224  {
225  index = i;
226  break;
227  }
228  }
229 
230  if (index != -1)
231  {
232  return libPtrs_[index];
233  }
234  return nullptr;
235 }
236 
237 
238 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
string expand(const string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Expand occurrences of variables according to the mapping.
Definition: stringOps.C:69
A class for handling file names.
Definition: fileName.H:79
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
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
static const fileName null
An empty fileName.
Definition: fileName.H:97
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
dlLibraryTable()
Construct null.
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:446
~dlLibraryTable()
Destructor.
dlLibraryTable libs
Table of loaded dynamic libraries.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1264
A class for handling words, derived from string.
Definition: word.H:59
A table of dynamically loaded libraries.
bool close(const fileName &name, const bool verbose=true)
Close the named library, optionally with warnings if problems occur.
defineTypeNameAndDebug(combustionModel, 0)
bool open(const fileName &libName, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
#define WarningInFunction
Report a warning using Foam::Warning.
void * findLibrary(const fileName &libName)
Find the handle of the named library.
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
Definition: POSIX.C:1236
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864
#define InfoInFunction
Report an information message using Foam::Info.