FEDEM Solver  R8.0
Source code of the dynamics solver
FFaDynamicLibraryBase.H
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2023 SAP SE
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // This file is part of FEDEM - https://openfedem.org
7 
13 #ifndef FFA_DYNAMIC_LIBRARY_BASE_H
14 #define FFA_DYNAMIC_LIBRARY_BASE_H
15 
16 #include <map>
17 #include <vector>
18 #include <string>
19 
20 #if defined(win32) || defined(win64)
21 #define NOGDI
22 #define WIN32_LEAN_AND_MEAN
23 #include <windows.h>
25 typedef void (*DLPROC) ();
26 #else
28 typedef void* DLPROC;
29 #endif
30 
31 
37 {
38 protected:
42  virtual ~FFaDynamicLibraryBase() { this->unloadAll(); }
43 
46 
47 public:
48  bool load(const std::string& libName, bool silence = false);
49  bool unload(const std::string& libName, bool silence = false);
50  void unloadAll();
52  bool areLibsLoaded() const { return !myLibHandles.empty(); }
54  const char* getLibrary(size_t idx = 1) const;
55 
56 protected:
57  DLPROC getProcAddr(const std::string& fName, bool silence = false) const;
58  DLPROC getProcAddr(const std::string& cName, const std::string& fName,
59  LanguageBinding& lang, bool silence = false) const;
60  DLPROC getProcAddress(const std::string& fName, size_t procID) const;
61  DLPROC getProcAddress(const std::string& cName, const std::string& fName,
62  LanguageBinding& lang, size_t procID) const;
63 
64 private:
66 #if defined(win32) || defined(win64)
67  typedef std::pair<LanguageBinding,HINSTANCE> LibHandle;
68 #else
69  typedef std::pair<LanguageBinding,void*> LibHandle;
70 #endif
72  typedef std::pair<DLPROC,LanguageBinding> cache_info;
73 
74  mutable std::vector<cache_info> myProcCache;
75  std::map<std::string,LibHandle> myLibHandles;
76 
78  static DLPROC getAddress(const LibHandle& lib, const std::string& fname);
79 };
80 
81 #endif
void * DLPROC
Function pointer on UNIX.
Definition: FFaDynamicLibraryBase.H:28
static char * fname[_MAX_DBFIL]
Definition: binaryDB.c:35
Base class for dynamic loading of shared object libraries.
Definition: FFaDynamicLibraryBase.H:37
bool areLibsLoaded() const
Returns whether dynamics libraries have been loaded or not.
Definition: FFaDynamicLibraryBase.H:52
DLPROC getProcAddr(const std::string &fName, bool silence=false) const
Returns the function pointer for the named function.
Definition: FFaDynamicLibraryBase.C:158
bool load(const std::string &libName, bool silence=false)
Loads the library named libName.
Definition: FFaDynamicLibraryBase.C:32
std::map< std::string, LibHandle > myLibHandles
Dynamic library container.
Definition: FFaDynamicLibraryBase.H:75
LanguageBinding
Enums defining the supported language bindings.
Definition: FFaDynamicLibraryBase.H:45
@ Undefined
Definition: FFaDynamicLibraryBase.H:45
@ C
Definition: FFaDynamicLibraryBase.H:45
@ Fortran
Definition: FFaDynamicLibraryBase.H:45
std::vector< cache_info > myProcCache
Function pointer cache.
Definition: FFaDynamicLibraryBase.H:74
void unloadAll()
Unloads all libraries and clears the function cache.
Definition: FFaDynamicLibraryBase.C:109
FFaDynamicLibraryBase()
Default constructor.
Definition: FFaDynamicLibraryBase.H:40
std::pair< LanguageBinding, void * > LibHandle
Dynamic library handle with associated language binding.
Definition: FFaDynamicLibraryBase.H:69
virtual ~FFaDynamicLibraryBase()
The destructor unloads all dynamically loaded libraries.
Definition: FFaDynamicLibraryBase.H:42
bool unload(const std::string &libName, bool silence=false)
Unloads the library named libName.
Definition: FFaDynamicLibraryBase.C:79
static DLPROC getAddress(const LibHandle &lib, const std::string &fname)
Returns the pointer to a named function.
Definition: FFaDynamicLibraryBase.C:141
std::pair< DLPROC, LanguageBinding > cache_info
Function pointer with language binding.
Definition: FFaDynamicLibraryBase.H:72
DLPROC getProcAddress(const std::string &fName, size_t procID) const
Returns the function pointer for the given function.
Definition: FFaDynamicLibraryBase.C:237
const char * getLibrary(size_t idx=1) const
Returns the file name of the dynamic library, if loaded.
Definition: FFaDynamicLibraryBase.C:128