GGS(GenericGEANT4Simulation)Software  2.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSMCPluginManager.cpp
Go to the documentation of this file.
1 /*
2  * GGSMCPluginManager.cpp
3  *
4  * Created on: 07 Aug 2013
5  * Author: Nicola Mori
6  */
7 
10 #include "utils/GGSSmartLog.h"
12 
13 // Hack to avoid warnings when using dlsym to load functions
14 // See http://agram66.blogspot.it/2011/10/dlsym-posix-c-gimme-break.html
15 #define dlsym dummy
16 #include <dlfcn.h>
17 #undef dlsym
18 extern "C" void *(*dlsym(void *handle, const char *symbol))();
19 
20 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
21 
23  static GGSMCPluginManager instance;
24  return instance;
25 }
26 
27 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
28 
30 
31  for (unsigned int i = 0; i < _files.size(); i++)
32  dlclose(_files[i]);
33 }
34 
35 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
36 
37 bool GGSMCPluginManager::LoadPlugin(const std::string& libName) {
38  static const std::string routineName("GGSMCPluginManager::LoadPlugin");
39 
40  void *library = dlopen(libName.data(), RTLD_NOW);
41 
42  if (!library) {
43  COUT(ERROR) << "Cannot load library: " << dlerror() << ENDL;
44  return false;
45  }
46 
47  _files.push_back(library);
48 
49  // Reset errors
50  dlerror();
51 
52  return true;
53 }
54 
55 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
56 
57 G4VUserPhysicsList *GGSMCPluginManager::LoadPhysicsListPlugin(const std::string& libName) {
58  static const std::string routineName("GGSMCPluginManager::LoadPhysicsListPlugin");
59 
60  void *library = dlopen(libName.data(), RTLD_LAZY);
61 
62  if (!library) {
63  COUT(ERROR) << "Cannot load library: " << dlerror() << ENDL;
64  return NULL;
65  }
66 
67  _files.push_back(library);
68 
69  // Reset errors
70  dlerror();
71  // Load the symbols
72  G4VUserPhysicsList* (*PLBuilder)() = reinterpret_cast<G4VUserPhysicsList* (*) ()>(dlsym(library, "PhysicsListBuilder"));
73 
74  const char* dlsym_error = dlerror();
75  if (dlsym_error) {
76  COUT(ERROR) << "Cannot load symbol PhysicsListBuilder: " << dlsym_error << ENDL;
77  return NULL;
78  }
79 
80  return PLBuilder();
81 }
static GGSMCPluginManager & GetInstance()
Get the singleton instance.
#define ENDL
Definition: GGSSmartLog.h:93
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:66
bool LoadPlugin(const std::string &libName)
Loads a plugin library.
~GGSMCPluginManager()
Destructor.
Manager for MC plugins.
G4VUserPhysicsList * LoadPhysicsListPlugin(const std::string &libName)
Loads a physics list plugin library.