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-2018 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  {
65  << "Closing " << libNames_[i]
66  << " with handle " << uintptr_t(libPtrs_[i]) << endl;
67  }
68  if (!dlClose(libPtrs_[i]))
69  {
70  WarningInFunction<< "Failed closing " << libNames_[i]
71  << " with handle " << uintptr_t(libPtrs_[i]) << endl;
72  }
73  }
74  }
75 }
76 
77 
78 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
79 
81 (
82  const fileName& functionLibName,
83  const bool verbose
84 )
85 {
86  if (functionLibName.size())
87  {
88  void* functionLibPtr = dlOpen
89  (
90  fileName(functionLibName).expand(),
91  verbose
92  );
93 
94  if (debug)
95  {
97  << "Opened " << functionLibName
98  << " resulting in handle " << uintptr_t(functionLibPtr) << endl;
99  }
100 
101  if (!functionLibPtr)
102  {
103  if (verbose)
104  {
106  << "could not load " << functionLibName
107  << endl;
108  }
109 
110  return false;
111  }
112  else
113  {
114  libPtrs_.append(functionLibPtr);
115  libNames_.append(functionLibName);
116  return true;
117  }
118  }
119  else
120  {
121  return false;
122  }
123 }
124 
125 
127 (
128  const fileName& functionLibName,
129  const bool verbose
130 )
131 {
132  label index = -1;
133  forAllReverse(libNames_, i)
134  {
135  if (libNames_[i] == functionLibName)
136  {
137  index = i;
138  break;
139  }
140  }
141 
142  if (index != -1)
143  {
144  if (debug)
145  {
147  << "Closing " << functionLibName
148  << " with handle " << uintptr_t(libPtrs_[index]) << endl;
149  }
150 
151  bool ok = dlClose(libPtrs_[index]);
152 
153  libPtrs_[index] = nullptr;
154  libNames_[index] = fileName::null;
155 
156  if (!ok)
157  {
158  if (verbose)
159  {
161  << "could not close " << functionLibName
162  << endl;
163  }
164 
165  return false;
166  }
167 
168  return true;
169  }
170  return false;
171 }
172 
173 
174 void* Foam::dlLibraryTable::findLibrary(const fileName& functionLibName)
175 {
176  label index = -1;
177  forAllReverse(libNames_, i)
178  {
179  if (libNames_[i] == functionLibName)
180  {
181  index = i;
182  break;
183  }
184  }
185 
186  if (index != -1)
187  {
188  return libPtrs_[index];
189  }
190  return nullptr;
191 }
192 
193 
195 (
196  const dictionary& dict,
197  const word& libsEntry
198 )
199 {
200  if (dict.found(libsEntry))
201  {
202  fileNameList libNames(dict.lookup(libsEntry));
203 
204  bool allOpened = !libNames.empty();
205 
206  forAll(libNames, i)
207  {
208  allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
209  }
210 
211  return allOpened;
212  }
213  else
214  {
215  return false;
216  }
217 }
218 
219 
220 // ************************************************************************* //
bool open(const fileName &name, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:438
#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:313
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:75
System integer.
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
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:158
static const fileName null
An empty fileName.
Definition: fileName.H:97
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
dlLibraryTable()
Construct null.
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:446
~dlLibraryTable()
Destructor.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void * findLibrary(const fileName &name)
Find the handle of the named library.
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1268
A class for handling words, derived from string.
Definition: word.H:59
bool close(const fileName &name, const bool verbose=true)
Close the named library, optionally with warnings if problems occur.
defineTypeNameAndDebug(combustionModel, 0)
#define WarningInFunction
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:1240
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:583
#define InfoInFunction
Report an information message using Foam::Info.