CYCLUS
unix_helper_functions.h
Go to the documentation of this file.
1 // This is the dynamic loading implementation for UNIX machines
2 #ifndef CYCLUS_SRC_UNIX_HELPER_FUNCTIONS_H_
3 #define CYCLUS_SRC_UNIX_HELPER_FUNCTIONS_H_
4 
5 #include <dlfcn.h>
6 
7 #include "error.h"
8 #include "platform.h"
9 
10 namespace cyclus {
11 
12 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13 void DynamicModule::OpenLibrary() {
14  // Do not change this to RTLD_LAZY, because it randomly breaks modules discovery!
15  module_library_ = dlopen(path_.c_str(), RTLD_NOW);
16 
17  if (!module_library_) {
18  std::string err_msg = "Unable to load agent shared object file: ";
19  err_msg += dlerror();
20  throw IOError(err_msg);
21  }
22 
23  dlerror(); // reset errors
24 }
25 
26 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
27 void DynamicModule::SetConstructor() {
28  ctor_ = (AgentCtor*)
29  dlsym(module_library_, ctor_name_.c_str());
30 
31  if (!ctor_) {
32  std::stringstream ss;
33  std::string agent = ctor_name_;
34  agent.erase(0, 9); // len(Construct) == 9
35  ss << "Could not find agent " << agent << " in module library "
36  << path_.c_str() << " (" << dlerror() << ").";
37  throw IOError(ss.str());
38  }
39 
40  dlerror(); // reset errors
41 }
42 
43 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44 void DynamicModule::CloseLibrary() {
45  if (module_library_) {
46  int exit_code = dlclose(module_library_);
47  if (exit_code != 0) {
48  std::string err_msg = "Error closing shared object file: ";
49  err_msg += dlerror();
50  throw IOError(err_msg);
51  }
52  dlerror(); // reset errors
53  }
54 }
55 
56 } // namespace cyclus
57 
58 #endif // CYCLUS_SRC_UNIX_HELPER_FUNCTIONS_H_
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
Agent * AgentCtor(Context *)