EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_library.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ library
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors: Ernesto Mainegra-Hing
27 #
28 ###############################################################################
29 */
30 
31 
37 #include "egs_library.h"
38 #include "egs_functions.h"
39 
40 #ifdef WIN32
41 
42  #include <windows.h>
43 
44  #define DLL_HANDLE HMODULE
45  #define LOAD_LIBRARY(fname) LoadLibrary(fname)
46  #define FREE_LIBRARY(lib) FreeLibrary(lib);
47  #define RESOLVE_SYMBOL(lib,symb) (void *) GetProcAddress(lib,symb)
48 
49 #else
50 
51  #include <dlfcn.h>
52 
53  #define DLL_HANDLE void*
54  #define LOAD_LIBRARY(fname) dlopen(fname,RTLD_LAZY)
55  #define FREE_LIBRARY(lib) !dlclose(lib)
56  #define RESOLVE_SYMBOL(lib,symb) dlsym(lib,symb)
57 
58 #endif
59 
60 #include <string>
61 
62 using namespace std;
63 
64 #ifndef SKIP_DOXYGEN
65 
69 class EGS_LOCAL EGS_PrivateLibrary {
70 public:
71  DLL_HANDLE lib;
72  string name, fname;
73  bool au;
74  EGS_PrivateLibrary(const char *lib_name, const char *path = 0);
75  ~EGS_PrivateLibrary();
76  bool load();
77  void *resolve(const char *symb);
78  bool unload();
79  static char fs;
80  static const char *lib_prefix;
81  static const char *lib_suffix;
82 };
83 
84 #ifdef WIN32
85  #ifdef CYGWIN
86  char EGS_PrivateLibrary::fs = '/';
87  #else
88  char EGS_PrivateLibrary::fs = '\\';
89  #endif
90  const char *EGS_PrivateLibrary::lib_prefix = "";
91  const char *EGS_PrivateLibrary::lib_suffix = ".dll";
92 #else
93  char EGS_PrivateLibrary::fs = '/';
94  const char *EGS_PrivateLibrary::lib_prefix = "lib";
95  const char *EGS_PrivateLibrary::lib_suffix = ".so";
96 #endif
97 
98 
99 EGS_PrivateLibrary::EGS_PrivateLibrary(const char *lib_name, const char *path) {
100  au = true;
101  lib = 0;
102  if (!lib_name) {
103  egsWarning("EGS_Library::EGS_Library: null library name?\n");
104  return;
105  }
106  name = lib_name;
107  if (path) {
108  fname = path;
109  if (fname[fname.size()-1] != fs) {
110  fname += fs;
111  }
112  }
113  fname += lib_prefix;
114  fname += lib_name;
115  fname += lib_suffix;
116 #ifdef LIB_DEBUG
117  egsInformation("EGS_Library::EGS_Library: file name is <%s>\n",fname.c_str());
118 #endif
119 }
120 
121 EGS_PrivateLibrary::~EGS_PrivateLibrary() {
122  if (au) {
123  unload();
124  }
125 }
126 
127 bool EGS_PrivateLibrary::load() {
128  if (lib) {
129  return true;
130  }
131  lib = LOAD_LIBRARY(fname.c_str());
132  /*
133  if (!lib ) {
134  const char *tmp = dlerror(); egsWarning("load library: %s\n",tmp);
135  }
136  */
137 #ifdef DLL_DEBUG
138  egsInformation("In EGS_PrivateLibrary::load(): name = %s lib = 0x%x\n",
139  name.c_str(),lib);
140 #endif
141  if (lib) {
142  return true;
143  }
144  else {
145  egsWarning("EGS_Library::load(): failed to load library %s\n",
146  fname.c_str());
147 #ifdef WIN32
148  egsWarning(" error was: %d\n",GetLastError());
149 #else
150  egsWarning(" error was: %s\n",dlerror());
151 #endif
152  return false;
153  }
154 }
155 
156 void *EGS_PrivateLibrary::resolve(const char *symb) {
157  if (!lib) {
158  if (!load()) {
159  return 0;
160  }
161  }
162  void *result = RESOLVE_SYMBOL(lib,symb);
163 #ifdef DLL_DEBUG
164  egsInformation("In EGS_PrivateLibrary::resolve: symbol = %s result = 0x%x\n",
165  symb,result);
166 #endif
167  return result;
168 }
169 
170 bool EGS_PrivateLibrary::unload() {
171  if (!lib) {
172  return true;
173  }
174  bool result = FREE_LIBRARY(lib);
175  if (result) {
176  lib = 0;
177  }
178  return result;
179 }
180 #endif
181 
182 EGS_Library::EGS_Library(const char *lib_name, const char *path) {
183  pl = new EGS_PrivateLibrary(lib_name,path);
184 }
185 
187  delete pl;
188 }
189 
191  return pl->load();
192 }
193 
194 void *EGS_Library::resolve(const char *symb) {
195  return pl->resolve(symb);
196 }
197 
199  return pl->unload();
200 }
201 
202 bool EGS_Library::isLoaded() const {
203  return (bool) pl->lib;
204 }
205 
207  return pl->au;
208 }
209 
211  pl->au = u;
212 }
213 
214 const char *EGS_Library::libraryName() const {
215  return pl->name.c_str();
216 }
217 
218 const char *EGS_Library::libraryFile() const {
219  return pl->fname.c_str();
220 }
221 
222 void *EGS_Library::resolve(const char *lname, const char *func,
223  const char *path) {
224  EGS_PrivateLibrary p(lname,path);
225  p.au = false;
226  if (!p.load()) {
227  return 0;
228  }
229  return p.resolve(func);
230 }
const char * libraryFile() const
Returns the name of the DSO, including full path and platform-specific prefix and extension...
const char * libraryName() const
Returns the name of the library object as given in the constructor.
EGS_Library(const char *lib_name, const char *path=0)
Constructs the library object and sets the DSO name to lib_name.
Global egspp functions header file.
bool unload()
Unloads the library.
void setUnload(bool u)
Set automatic unloading to u.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
EGS_Library class header file.
~EGS_Library()
Destructs the library object.
bool load()
Loads the library.
void * resolve(const char *func)
Returns the address of the exported symbol func.
bool autoUnload() const
Returns true if the library automatically unloads when the object is destructed, false otherwise...
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.
bool isLoaded() const
Returns true if the library is loaded, false otherwise.