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 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
231  {
232  return libNames_;
233  }
234 
235  //- Pointers to the libraries in use. Access with caution.
236  const UList<void*>& pointers() const
237  {
238  return libPtrs_;
239  }
240 
241  //- Clears the table, without attempting to close the libraries
242  void clear();
243 
244  //- Add to the list of names, but do not yet open.
245  // Ignores duplicate names.
246  bool append(const fileName& libName);
247 
248  //- Add to the list of names, but do not yet open.
249  // Ignores duplicate names.
250  label append(const UList<fileName>& libNames);
251 
252  //- Open named, but unopened libraries.
253  //- These names will normally have been added with the append() method.
254  bool open(bool verbose = true);
255 
256  //- Open the named library, warn by default if problems occur.
257  // An empty name is a silent no-op and always returns nullptr.
258  // \return a pointer to the library opened, or nullptr on failure.
259  void* open(const fileName& libName, bool verbose = true);
260 
261  //- Open the named libraries, warn by default if problems occur.
262  // Ignores duplicate names.
263  bool open(const UList<fileName>& libNames, bool verbose = true);
264 
265  //- Open the named libraries, warn by default if problems occur.
266  // Ignores duplicate names.
267  bool open
268  (
269  std::initializer_list<fileName> libNames,
270  bool verbose = true
271  );
272 
273  //- Close all libraries loaded by the table and remove the closed
274  //- functions from the table.
275  void close(bool verbose = true);
276 
277  //- Close the named library, optionally warn if problems occur
278  // Using an empty name is a no-op and always returns false.
279  bool close(const fileName& libName, bool verbose = true);
280 
281  //- Find the handle of the named library
282  // Using an empty name is a no-op and always returns nullptr.
283  void* findLibrary(const fileName& libName);
284 
285  //- Open libraries listed in the 'libsEntry' entry in the dictionary.
286  bool open
287  (
288  const word& libsEntry,
289  const dictionary& dict,
290  bool verbose = true
291  );
292 
293  //- Open libraries listed in the 'libsEntry' entry in the dictionary.
294  // Verbose = true
295  bool open(const dictionary& dict, const word& libsEntry);
297  //- Open all libraries listed in the 'libsEntry' entry in the
298  //- given dictionary and check the additions
299  //- to the given constructor table
300  template<class TablePtr>
301  bool open
302  (
303  const dictionary& dict,
304  const word& libsEntry,
305  const TablePtr& tablePtr,
306  bool verbose = true
307  );
308 
309 
310  // Info
311 
312  //- Return info proxy,
313  //- used to print library table information to a stream
314  InfoProxy<dlLibraryTable> info() const noexcept { return *this; }
315 };
316 
317 
318 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
319 
320 } // End namespace Foam
321 
322 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
323 
324 #ifdef NoRepository
325  #include "dlLibraryTableTemplates.C"
326 #endif
327 
328 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
329 
330 #endif
331 
332 // ************************************************************************* //
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.
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.
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.
const UList< fileName > & names() const
Names of the libraries in use, or requested.
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
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.
const UList< void * > & pointers() const
Pointers to the libraries in use. Access with caution.
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:43
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 the append() metho...
static int dlcloseOnTerminate
Use dlclose() when clearing the dlLibraryTable.
label size() const noexcept
The number of libraries loaded by the table.
Namespace for OpenFOAM.