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-2015 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  {
97  << "could not load " << functionLibName
98  << endl;
99  }
100 
101  return false;
102  }
103  else
104  {
105  libPtrs_.append(functionLibPtr);
106  libNames_.append(functionLibName);
107  return true;
108  }
109  }
110  else
111  {
112  return false;
113  }
114 }
115 
116 
118 (
119  const fileName& functionLibName,
120  const bool verbose
121 )
122 {
123  label index = -1;
124  forAllReverse(libNames_, i)
125  {
126  if (libNames_[i] == functionLibName)
127  {
128  index = i;
129  break;
130  }
131  }
132 
133  if (index != -1)
134  {
135  if (debug)
136  {
137  Info<< "dlLibraryTable::close : closing " << functionLibName
138  << " with handle " << uintptr_t(libPtrs_[index]) << endl;
139  }
140 
141  bool ok = dlClose(libPtrs_[index]);
142 
143  libPtrs_[index] = NULL;
144  libNames_[index] = fileName::null;
145 
146  if (!ok)
147  {
148  if (verbose)
149  {
151  << "could not close " << functionLibName
152  << endl;
153  }
154 
155  return false;
156  }
157 
158  return true;
159  }
160  return false;
161 }
162 
163 
164 void* Foam::dlLibraryTable::findLibrary(const fileName& functionLibName)
165 {
166  label index = -1;
167  forAllReverse(libNames_, i)
168  {
169  if (libNames_[i] == functionLibName)
170  {
171  index = i;
172  break;
173  }
174  }
175 
176  if (index != -1)
177  {
178  return libPtrs_[index];
179  }
180  return NULL;
181 }
182 
183 
185 (
186  const dictionary& dict,
187  const word& libsEntry
188 )
189 {
190  if (dict.found(libsEntry))
191  {
192  fileNameList libNames(dict.lookup(libsEntry));
193 
194  bool allOpened = !libNames.empty();
195 
196  forAll(libNames, i)
197  {
198  allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
199  }
200 
201  return allOpened;
202  }
203  else
204  {
205  return false;
206  }
207 }
208 
209 
210 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
int.H
System integer.
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::dlLibraryTable::findLibrary
void * findLibrary(const fileName &name)
Find the handle of the named library.
Definition: dlLibraryTable.C:164
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::dlLibraryTable::close
bool close(const fileName &name, const bool verbose=true)
Close the named library, optionally with warnings if problems occur.
Definition: dlLibraryTable.C:118
Foam::label
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
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::Info
messageStream Info
Foam::dlOpen
void * dlOpen(const fileName &lib, const bool check=true)
Open a shared library. Return handle to library. Print error message.
Definition: POSIX.C:1161
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
forAllReverse
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:418
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::fileName::null
static const fileName null
An empty fileName.
Definition: fileName.H:97
Foam::List
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
Foam::dlLibraryTable::open
bool open(const fileName &name, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
Definition: dlLibraryTable.C:77
dlLibraryTable.H
Foam::dlClose
bool dlClose(void *)
Close a dlopened library using handle. Return true if successful.
Definition: POSIX.C:1212
Foam::dlLibraryTable::dlLibraryTable
dlLibraryTable()
Construct null.
Definition: dlLibraryTable.C:40
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::dlLibraryTable::~dlLibraryTable
~dlLibraryTable()
Destructor.
Definition: dlLibraryTable.C:56