dlLibraryTable.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2018-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::dlLibraryTable
29 
30 Description
31  A table of dynamically loaded libraries.
32 
33 SeeAlso
34  Foam::dlOpen
35  Foam::dlClose
36 
37 SourceFiles
38  dlLibraryTable.C
39  dlLibraryTableTemplates.C
40 
41 \*---------------------------------------------------------------------------*/
42 
43 #ifndef dlLibraryTable_H
44 #define dlLibraryTable_H
45 
46 #include "DynamicList.H"
47 #include "fileName.H"
48 #include "InfoProxy.H"
49 #include <memory>
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 // Forward Declarations
57 class dlLibraryTable;
58 Ostream& operator<<(Ostream& os, const InfoProxy<dlLibraryTable>& info);
59 
60 /*---------------------------------------------------------------------------*\
61  Class dlLibraryTable Declaration
62 \*---------------------------------------------------------------------------*/
63 
64 class dlLibraryTable
65 {
66  // Static Data
67 
68  //- Global singleton of dynamic libraries
69  static std::unique_ptr<dlLibraryTable> global_;
70 
71 
72  // Private Data
73 
74  //- Pointers to the loaded libraries
75  DynamicList<void*> libPtrs_;
76 
77  //- Names of loaded libraries, or of libraries to be loaded
78  DynamicList<fileName> libNames_;
79 
80 
81  // Private Member Functions
82 
83  //- Load/unload hook.
84  // \return true if the function was found and executed
85  static bool functionHook
86  (
87  const bool load,
88  void* handle,
89  const std::string& funcName,
90  const bool verbose,
91  const std::string& context
92  );
93 
94 
95  //- Open specified library name and return pointer.
96  // Warning messages, but no additional side-effects.
97  void* openLibrary(const fileName& libName, bool verbose);
98 
99 
100 public:
101 
102  // Static Data Members
103 
104  //- Use dlclose() when clearing the dlLibraryTable.
105  // This is presumably \em cleaner, but can also remove functions
106  // that are still needed (eg, to terminate the library itself)
107  static int dlcloseOnTerminate;
108 
109 
110  // Public Data Types
111 
112  //- Global loader/unloader function type (C-linkage)
113  // Called with true on load, false on unload.
114  typedef void (*loaderType)(bool);
115 
116 
117  //- Declare name of the class and its debug switch
118  ClassName("dlLibraryTable");
119 
120 
121  // Generated Methods
122 
123  //- Default construct
124  dlLibraryTable() = default;
125 
126  //- No copy construct
127  dlLibraryTable(const dlLibraryTable&) = delete;
128 
129  //- Move construct
130  dlLibraryTable(dlLibraryTable&&) = default;
131 
132  //- No copy assignment
133  void operator=(const dlLibraryTable&) = delete;
134 
135  //- Move assignment
137 
138 
139  // Constructors
140 
141  //- Open specified libraries, warn by default if problems occur
142  // Ignores duplicate names.
143  explicit dlLibraryTable
144  (
145  const UList<fileName>& libNames,
146  bool verbose = true
147  );
148 
149  //- Open specified libraries, warn by default if problems occur
150  // Ignores duplicate names.
151  explicit dlLibraryTable
152  (
153  std::initializer_list<fileName> libNames,
154  bool verbose = true
155  );
156 
157  //- Open libraries listed in 'libsEntry' entry in the dictionary,
158  //- warn by default if problems occur
160  (
161  const word& libsEntry,
162  const dictionary& dict,
163  bool verbose = true
164  );
165 
166  //- Open libraries listed in 'libsEntry' entry in the dictionary,
167  //- warn by default if problems occur
169  (
170  const dictionary& dict,
171  const word& libsEntry,
172  bool verbose = true
173  );
174 
175 
176  //- Destructor. Closes all libraries loaded by the table.
177  ~dlLibraryTable();
178 
179 
180  // Static Member Functions
181 
182  //- Library basename without leading 'lib' or trailing '.so'
183  static word basename(const fileName& libPath);
184 
185  //- Library fullname, prefix with 'lib', suffix with '.so'
186  // \note the suffix is system-dependent
187  static word fullname(word libName);
188 
189  //- Table of global libraries
190  static dlLibraryTable& libs();
191 
192  //- Low-level interface to execute global "void funcName(true)"
193  //- from the library, typically for additional loading.
194  // If called, it should be the first step after opening a library.
195  // \return true if the function was found and executed
196  static bool loadHook
197  (
198  void* handle,
199  const std::string& funcName,
200  const bool verbose = false,
201  const std::string& context = ""
202  );
203 
204  //- Low-level interface to execute global "void funcName(false)"
205  //- from the library, typically for unloading.
206  // If called, it should be the last step before closing a library.
207  // \return true if the function was found and executed
208  static bool unloadHook
209  (
210  void* handle,
211  const std::string& funcName,
212  const bool verbose = false,
213  const std::string& context = ""
214  );
215 
216 
217  // Member Functions
218 
219  //- True if there are no libraries loaded by the table
220  bool empty() const;
221 
222  //- The number of libraries loaded by the table
223  label size() const;
224 
225  //- Names of the libraries in use
226  List<fileName> loaded() const;
227 
228  //- Names of the libraries in use, or requested
229  const UList<fileName>& names() const
230  {
231  return libNames_;
232  }
233 
234  //- Pointers to the libraries in use. Access with caution.
235  const UList<void*>& pointers() const
236  {
237  return libPtrs_;
238  }
239 
240  //- Clears the table, without attempting to close the libraries
241  void clear();
242 
243  //- Add to the list of names, but do not yet open.
244  // Ignores duplicate names.
245  bool append(const fileName& libName);
246 
247  //- Add to the list of names, but do not yet open.
248  // Ignores duplicate names.
249  label append(const UList<fileName>& libNames);
250 
251  //- Open named, but unopened libraries.
252  //- These names will normally have been added with the append() method.
253  bool open(bool verbose = true);
254 
255  //- Open the named library, warn by default if problems occur.
256  // An empty name is a silent no-op and always returns nullptr.
257  // \return a pointer to the library opened, or nullptr on failure.
258  void* open(const fileName& libName, bool verbose = true);
259 
260  //- Open the named libraries, warn by default if problems occur.
261  // Ignores duplicate names.
262  bool open(const UList<fileName>& libNames, bool verbose = true);
263 
264  //- Open the named libraries, warn by default if problems occur.
265  // Ignores duplicate names.
266  bool open
267  (
268  std::initializer_list<fileName> libNames,
269  bool verbose = true
270  );
271 
272  //- Close all libraries loaded by the table and remove the closed
273  //- functions from the table.
274  void close(bool verbose = true);
275 
276  //- Close the named library, optionally warn if problems occur
277  // Using an empty name is a no-op and always returns false.
278  bool close(const fileName& libName, bool verbose = true);
279 
280  //- Find the handle of the named library
281  // Using an empty name is a no-op and always returns nullptr.
282  void* findLibrary(const fileName& libName);
283 
284  //- Open libraries listed in the 'libsEntry' entry in the dictionary.
285  bool open
286  (
287  const word& libsEntry,
288  const dictionary& dict,
289  bool verbose = true
290  );
291 
292  //- Open libraries listed in the 'libsEntry' entry in the dictionary.
293  // Verbose = true
294  bool open(const dictionary& dict, const word& libsEntry);
295 
296  //- Open all libraries listed in the 'libsEntry' entry in the
297  //- given dictionary and check the additions
298  //- to the given constructor table
299  template<class TablePtr>
300  bool open
301  (
302  const dictionary& dict,
303  const word& libsEntry,
304  const TablePtr& tablePtr,
305  bool verbose = true
306  );
307 
308 
309  // Info
310 
311  //- Return info proxy.
312  // Used to print library table information to a stream
314  {
315  return *this;
316  }
317 };
318 
319 
320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
321 
322 } // End namespace Foam
323 
324 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
325 
326 #ifdef NoRepository
327  #include "dlLibraryTableTemplates.C"
328 #endif
329 
330 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
331 
332 #endif
333 
334 // ************************************************************************* //
Foam::dlLibraryTable::loaded
List< fileName > loaded() const
Names of the libraries in use.
Definition: dlLibraryTable.C:293
Foam::dlLibraryTable::pointers
const UList< void * > & pointers() const
Pointers to the libraries in use. Access with caution.
Definition: dlLibraryTable.H:234
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
Foam::dlLibraryTable::findLibrary
void * findLibrary(const fileName &libName)
Find the handle of the named library.
Definition: dlLibraryTable.C:570
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::InfoProxy
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:47
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::dlLibraryTable::close
void close(bool verbose=true)
Definition: dlLibraryTable.C:317
Foam::dlLibraryTable::basename
static word basename(const fileName &libPath)
Library basename without leading 'lib' or trailing '.so'.
Definition: dlLibraryTable.C:60
Foam::DynamicList< void * >
InfoProxy.H
Foam::dlLibraryTable::empty
bool empty() const
True if there are no libraries loaded by the table.
Definition: dlLibraryTable.C:256
Foam::dlLibraryTable::info
InfoProxy< dlLibraryTable > info() const
Return info proxy.
Definition: dlLibraryTable.H:312
Foam::dlLibraryTable::unloadHook
static bool unloadHook(void *handle, const std::string &funcName, const bool verbose=false, const std::string &context="")
Definition: dlLibraryTable.C:154
Foam::dlLibraryTable::operator=
void operator=(const dlLibraryTable &)=delete
No copy assignment.
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::dlLibraryTable::append
bool append(const fileName &libName)
Add to the list of names, but do not yet open.
Definition: dlLibraryTable.C:379
fileName.H
Foam::dlLibraryTable::names
const UList< fileName > & names() const
Names of the libraries in use, or requested.
Definition: dlLibraryTable.H:228
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::dlLibraryTable::open
bool open(bool verbose=true)
Definition: dlLibraryTable.C:409
Foam::dlLibraryTable::dlcloseOnTerminate
static int dlcloseOnTerminate
Use dlclose() when clearing the dlLibraryTable.
Definition: dlLibraryTable.H:106
Foam::dlLibraryTable::libs
static dlLibraryTable & libs()
Table of global libraries.
Definition: dlLibraryTable.C:80
Foam::dlLibraryTable::ClassName
ClassName("dlLibraryTable")
Declare name of the class and its debug switch.
Foam::List< fileName >
Foam::dlLibraryTable::fullname
static word fullname(word libName)
Library fullname, prefix with 'lib', suffix with '.so'.
Definition: dlLibraryTable.C:68
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
bool
bool
Definition: EEqn.H:20
Foam::dlLibraryTable::loadHook
static bool loadHook(void *handle, const std::string &funcName, const bool verbose=false, const std::string &context="")
Definition: dlLibraryTable.C:142
DynamicList.H
dlLibraryTableTemplates.C
Foam::dlLibraryTable::size
label size() const
The number of libraries loaded by the table.
Definition: dlLibraryTable.C:270
Foam::dlLibraryTable::clear
void clear()
Clears the table, without attempting to close the libraries.
Definition: dlLibraryTable.C:286
Foam::dlLibraryTable::loaderType
void(* loaderType)(bool)
Global loader/unloader function type (C-linkage)
Definition: dlLibraryTable.H:113
Foam::dlLibraryTable::dlLibraryTable
dlLibraryTable()=default
Default construct.
Foam::dlLibraryTable::~dlLibraryTable
~dlLibraryTable()
Destructor. Closes all libraries loaded by the table.
Definition: dlLibraryTable.C:245