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-2024 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 Foam_dlLibraryTable_H
44 #define Foam_dlLibraryTable_H
45 
46 #include "fileName.H"
47 #include "DynamicList.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&, const InfoProxy<dlLibraryTable>&);
59 
60 
61 /*---------------------------------------------------------------------------*\
62  Class dlLibraryTable Declaration
63 \*---------------------------------------------------------------------------*/
64 
65 class dlLibraryTable
66 {
67  // Static Data
68 
69  //- Global singleton of dynamic libraries
70  static std::unique_ptr<dlLibraryTable> global_;
71 
72 
73  // Private Data
74 
75  //- Pointers to the loaded libraries
76  DynamicList<void*> libPtrs_;
77 
78  //- Names of loaded libraries, or of libraries to be loaded
79  DynamicList<fileName> libNames_;
80 
81 
82  // Private Member Functions
83 
84  //- Load/unload hook.
85  // \return true if the function was found and executed
86  static bool functionHook
87  (
88  const bool load,
89  void* handle,
90  const std::string& funcName,
91  const bool verbose,
92  const std::string& context
93  );
94 
95 
96  //- Open specified library name and return pointer.
97  // Warning messages, but no additional side-effects.
98  void* openLibrary(const fileName& libName, bool verbose);
99 
100 
101 public:
102 
103  // Static Data Members
104 
105  //- Use dlclose() when clearing the dlLibraryTable.
106  // This is presumably \em cleaner, but can also remove functions
107  // that are still needed (eg, to terminate the library itself)
108  static int dlcloseOnTerminate;
109 
110 
111  // Public Data Types
112 
113  //- Global loader/unloader function type (C-linkage)
114  // Called with true on load, false on unload.
115  typedef void (*loaderType)(bool);
116 
117 
118  //- Declare name of the class and its debug switch
119  ClassName("dlLibraryTable");
120 
121 
122  // Generated Methods
123 
124  //- Default construct
125  dlLibraryTable() = default;
126 
127  //- No copy construct
128  dlLibraryTable(const dlLibraryTable&) = delete;
129 
130  //- Move construct
131  dlLibraryTable(dlLibraryTable&&) = default;
132 
133  //- No copy assignment
134  void operator=(const dlLibraryTable&) = delete;
135 
136  //- Move assignment
138 
139 
140  // Constructors
141 
142  //- Open specified libraries, warn by default if problems occur
143  // Ignores duplicate names.
144  explicit dlLibraryTable
145  (
146  const UList<fileName>& libNames,
147  bool verbose = true
148  );
149 
150  //- Open specified libraries, warn by default if problems occur
151  // Ignores duplicate names.
152  explicit dlLibraryTable
153  (
154  std::initializer_list<fileName> libNames,
155  bool verbose = true
156  );
157 
158  //- Open libraries listed in 'libsEntry' entry in the dictionary,
159  //- warn by default if problems occur
161  (
162  const word& libsEntry,
163  const dictionary& dict,
164  bool verbose = true
165  );
166 
167  //- Open libraries listed in 'libsEntry' entry in the dictionary,
168  //- warn by default if problems occur
170  (
171  const dictionary& dict,
172  const word& libsEntry,
173  bool verbose = true
174  );
175 
176 
177  //- Destructor. Closes all libraries loaded by the table.
178  ~dlLibraryTable();
179 
180 
181  // Static Member Functions
182 
183  //- Library basename without leading 'lib' or trailing '.so'
184  static word basename(const fileName& libPath);
185 
186  //- Library fullname, prefix with 'lib', suffix with '.so'
187  // \note the suffix is system-dependent
188  static word fullname(word libName);
189 
190  //- Table of global libraries
191  static dlLibraryTable& libs();
192 
193  //- Low-level interface to execute global "void funcName(true)"
194  //- from the library, typically for additional loading.
195  // If called, it should be the first step after opening a library.
196  // \return true if the function was found and executed
197  static bool loadHook
198  (
199  void* handle,
200  const std::string& funcName,
201  const bool verbose = false,
202  const std::string& context = ""
203  );
204 
205  //- Low-level interface to execute global "void funcName(false)"
206  //- from the library, typically for unloading.
207  // If called, it should be the last step before closing a library.
208  // \return true if the function was found and executed
209  static bool unloadHook
210  (
211  void* handle,
212  const std::string& funcName,
213  const bool verbose = false,
214  const std::string& context = ""
215  );
216 
217 
218  // Member Functions
219 
220  //- True if there are no libraries loaded by the table
221  bool empty() const noexcept;
222 
223  //- The number of libraries loaded by the table
224  label size() const noexcept;
225 
226  //- Names of the libraries in use
227  List<fileName> loaded() const;
228 
229  //- Names of the libraries in use, or requested
230  const UList<fileName>& names() const noexcept
231  {
232  return libNames_;
233  }
234 
235  //- Pointers to the libraries in use. Access with caution.
236  const UList<void*>& pointers() const noexcept
237  {
238  return libPtrs_;
239  }
240 
241  //- Clears the table, without attempting to close the libraries
242  void clear();
243 
244 
245  //- Add to the list of names, but do not yet open.
246  // Ignores empty and duplicate names.
247  bool push_back(const fileName& libName);
248 
249  //- Add to the list of names, but do not yet open.
250  // Ignores empty and duplicate names.
251  label push_back(const UList<fileName>& libNames);
252 
253 
254  //- Open named, but unopened libraries.
255  //- These names will normally have been added with push_back().
256  bool open(bool verbose = true);
257 
258  //- Open the named library, warn by default if problems occur.
259  // An empty name is a silent no-op and always returns nullptr.
260  // \return a pointer to the library opened, or nullptr on failure.
261  void* open(const fileName& libName, bool verbose = true);
262 
263  //- Open the named libraries, warn by default if problems occur.
264  // Ignores duplicate names.
265  bool open(const UList<fileName>& libNames, bool verbose = true);
266 
267  //- Open the named libraries, warn by default if problems occur.
268  // Ignores duplicate names.
269  bool open
270  (
271  std::initializer_list<fileName> libNames,
272  bool verbose = true
273  );
274 
275  //- Close all libraries loaded by the table and remove the closed
276  //- functions from the table.
277  void close(bool verbose = true);
278 
279  //- Close the named library, optionally warn if problems occur
280  // Using an empty name is a no-op and always returns false.
281  bool close(const fileName& libName, bool verbose = true);
282 
283  //- Find the handle of the named library
284  // Using an empty name is a no-op and always returns nullptr.
285  void* findLibrary(const fileName& libName);
286 
287  //- Open libraries listed in the 'libsEntry' entry in the dictionary.
288  bool open
289  (
290  const word& libsEntry,
291  const dictionary& dict,
292  bool verbose = true
293  );
294 
295  //- Open libraries listed in the 'libsEntry' entry in the dictionary.
296  // Verbose = true
297  bool open(const dictionary& dict, const word& libsEntry);
298 
299  //- Open all libraries listed in the 'libsEntry' entry in the
300  //- given dictionary and check the additions
301  //- to the given constructor table
302  template<class TablePtr>
303  bool open
304  (
305  const dictionary& dict,
306  const word& libsEntry,
307  const TablePtr& tablePtr,
308  bool verbose = true
309  );
310 
311 
312  // Info
313 
314  //- Return info proxy,
315  //- used to print library table information to a stream
316  InfoProxy<dlLibraryTable> info() const noexcept { return *this; }
317 
318 
319  // Housekeeping
320 
321  //- Add to the list of names, but do not yet open.
322  //FOAM_DEPRECATED_FOR(2024-04, "push_back()")
323  bool append(const fileName& libName)
324  {
325  return push_back(libName);
326  }
327 
328  //- Add to the list of names, but do not yet open.
329  //FOAM_DEPRECATED_FOR(2024-04, "push_back()")
330  label append(const UList<fileName>& libNames)
331  {
332  return push_back(libNames);
333  }
334 };
335 
336 
337 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
338 
339 } // End namespace Foam
340 
341 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342 
343 #ifdef NoRepository
344  #include "dlLibraryTableTemplates.C"
345 #endif
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 #endif
350 
351 // ************************************************************************* //
dictionary dict
A class for handling file names.
Definition: fileName.H:72
ClassName("dlLibraryTable")
Declare name of the class and its debug switch.
void operator=(const dlLibraryTable &)=delete
No copy assignment.
const UList< fileName > & names() const noexcept
Names of the libraries in use, or requested.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
InfoProxy< dlLibraryTable > info() const noexcept
Return info proxy, used to print library table information to a stream.
const UList< void * > & pointers() const noexcept
Pointers to the libraries in use. Access with caution.
void clear()
Clears the table, without attempting to close the libraries.
List< fileName > loaded() const
Names of the libraries in use.
~dlLibraryTable()
Destructor. Closes all libraries loaded by the table.
static dlLibraryTable & libs()
Table of global libraries.
void close(bool verbose=true)
Close all libraries loaded by the table and remove the closed functions from the table.
void(* loaderType)(bool)
Global loader/unloader function type (C-linkage)
static bool loadHook(void *handle, const std::string &funcName, const bool verbose=false, const std::string &context="")
Low-level interface to execute global "void funcName(true)" from the library, typically for additiona...
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
static word basename(const fileName &libPath)
Library basename without leading &#39;lib&#39; or trailing &#39;.so&#39;.
A class for handling words, derived from Foam::string.
Definition: word.H:63
bool push_back(const fileName &libName)
Add to the list of names, but do not yet open.
static word fullname(word libName)
Library fullname, prefix with &#39;lib&#39;, suffix with &#39;.so&#39;.
dlLibraryTable()=default
Default construct.
A table of dynamically loaded libraries.
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:105
const direction noexcept
Definition: Scalar.H:258
bool append(const fileName &libName)
Add to the list of names, but do not yet open.
bool empty() const noexcept
True if there are no libraries loaded by the table.
static bool unloadHook(void *handle, const std::string &funcName, const bool verbose=false, const std::string &context="")
Low-level interface to execute global "void funcName(false)" from the library, typically for unloadin...
A helper class for outputting values to Ostream.
Definition: ensightCells.H:44
void * findLibrary(const fileName &libName)
Find the handle of the named library.
bool open(bool verbose=true)
Open named, but unopened libraries. These names will normally have been added with push_back()...
static int dlcloseOnTerminate
Use dlclose() when clearing the dlLibraryTable.
label size() const noexcept
The number of libraries loaded by the table.
Namespace for OpenFOAM.