GGS(GenericGEANT4Simulation)Software  2.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
GGSGeoPluginManager.cpp
Go to the documentation of this file.
1 /*
2  * GGSGeoPluginManager.cpp
3  *
4  * Created on: 10 Jan 2012
5  * Author: Nicola Mori
6  */
7 
10 // Geant4 headers
11 #include "G4GeometryManager.hh"
12 #include "G4PhysicalVolumeStore.hh"
13 #include "G4LogicalVolumeStore.hh"
14 #include "G4SolidStore.hh"
15 
16 // GGS headers
18 #include "utils/GGSSmartLog.h"
19 
20 // C++ headers
21 // Hack to avoid warnings when using dlsym to load functions
22 // See http://agram66.blogspot.it/2011/10/dlsym-posix-c-gimme-break.html
23 #define dlsym dummy
24 #include <dlfcn.h>
25 #undef dlsym
26 extern "C" void *(*dlsym(void *handle, const char *symbol))();
27 
28 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
29 
31  static GGSGeoPluginManager instance;
32  return instance;
33 }
34 
35 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
36 
38 
39  //_geoConstruction will be deleted by the G4 geometry manager
40  if(_ggsLibrary)
41  dlclose(_ggsLibrary);
42 }
43 
44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
45 
46 bool GGSGeoPluginManager::LoadGeoPlugin(const std::string& libName) {
47 
48  static const std::string routineName("GGSGeoPluginManager::LoadGeoPlugin");
49 
50  // Clean any previously loaded geometry
51  G4GeometryManager::GetInstance()->OpenGeometry();
52  G4PhysicalVolumeStore::GetInstance()->Clean();
53  G4LogicalVolumeStore::GetInstance()->Clean();
54  G4SolidStore::GetInstance()->Clean();
55 
56  // Load the GGSGeometryConstruction library
57  _ggsLibrary = dlopen(libName.c_str(), RTLD_LAZY);
58 
59  if (!_ggsLibrary) {
60  COUT(ERROR) << "Cannot load library; " << dlerror() << ENDL;
61  // Reset errors
62  dlerror();
63  return false;
64  }
65 
66  // Reset errors
67  dlerror();
68 
69  // Load the symbols
70  GGSVGeometryConstruction* (*create_GGSGeometryConstruction)() = reinterpret_cast<GGSVGeometryConstruction* (*)()>(dlsym(_ggsLibrary, "GeometryBuilder"));
71  const char* dlsym_error = dlerror();
72  if (dlsym_error) {
73  COUT(ERROR) << "Cannot load symbol create; " << dlsym_error << ENDL;
74  dlclose(_ggsLibrary);
75  _ggsLibrary = NULL;
76  // Reset errors
77  dlerror();
78  return false;
79  }
80 
81  _geoConstruction = create_GGSGeometryConstruction();
82 
83  if (_geoConstruction)
84  return true;
85  else
86  return false;
87 }
88 
Abstract class needed to load GGS geometry.
~GGSGeoPluginManager()
Destructor.
static GGSGeoPluginManager & GetInstance()
Get the singleton instance.
Manager for geometry plugin.
#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 LoadGeoPlugin(const std::string &libName)
Loads the geometry plugin library.