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-2016 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  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  {
89  << "Opened " << functionLibName
90  << " resulting in handle " << uintptr_t(functionLibPtr) << endl;
91  }
92 
93  if (!functionLibPtr)
94  {
95  if (verbose)
96  {
98  << "could not load " << functionLibName
99  << endl;
100  }
101 
102  return false;
103  }
104  else
105  {
106  libPtrs_.append(functionLibPtr);
107  libNames_.append(functionLibName);
108  return true;
109  }
110  }
111  else
112  {
113  return false;
114  }
115 }
116 
117 
119 (
120  const fileName& functionLibName,
121  const bool verbose
122 )
123 {
124  label index = -1;
125  forAllReverse(libNames_, i)
126  {
127  if (libNames_[i] == functionLibName)
128  {
129  index = i;
130  break;
131  }
132  }
133 
134  if (index != -1)
135  {
136  if (debug)
137  {
139  << "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  {
153  << "could not close " << functionLibName
154  << endl;
155  }
156 
157  return false;
158  }
159 
160  return true;
161  }
162  return false;
163 }
164 
165 
166 void* Foam::dlLibraryTable::findLibrary(const fileName& functionLibName)
167 {
168  label index = -1;
169  forAllReverse(libNames_, i)
170  {
171  if (libNames_[i] == functionLibName)
172  {
173  index = i;
174  break;
175  }
176  }
177 
178  if (index != -1)
179  {
180  return libPtrs_[index];
181  }
182  return NULL;
183 }
184 
185 
187 (
188  const dictionary& dict,
189  const word& libsEntry
190 )
191 {
192  if (dict.found(libsEntry))
193  {
194  fileNameList libNames(dict.lookup(libsEntry));
195 
196  bool allOpened = !libNames.empty();
197 
198  forAll(libNames, i)
199  {
200  allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
201  }
202 
203  return allOpened;
204  }
205  else
206  {
207  return false;
208  }
209 }
210 
211 
212 // ************************************************************************* //
bool open(const fileName &name, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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:69
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:313
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:253
dlLibraryTable()
Construct null.
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:440
~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:1054
A class for handling words, derived from string.
Definition: word.H:59
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.
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:1026
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451
#define InfoInFunction
Report an information message using Foam::Info.