CYCLUS
dynamic_module.cc
Go to the documentation of this file.
1 #include "dynamic_module.h"
2 
3 #include <boost/filesystem.hpp>
4 #include <boost/algorithm/string.hpp>
5 
6 #include "context.h"
7 #include "env.h"
8 #include "agent.h"
9 #include "suffix.h"
10 
11 #include DYNAMICLOADLIB
12 
13 namespace fs = boost::filesystem;
14 
15 namespace cyclus {
16 
17 AgentSpec::AgentSpec(std::string path, std::string lib, std::string agent,
18  std::string alias)
19  : path_(path),
20  lib_(lib),
21  agent_(agent),
22  alias_(alias) {
23 
24  if (lib_ == "") {
25  lib_ = agent_;
26  }
27  if (alias_ == "") {
28  alias = agent_;
29  }
30 }
31 
33  agent_ = t->GetString("name");
34  lib_ = OptionalQuery<std::string>(t, "lib", agent_);
35  path_ = OptionalQuery<std::string>(t, "path", "");
36  alias_ = OptionalQuery<std::string>(t, "alias", agent_);
37 }
38 
39 AgentSpec::AgentSpec(std::string str_spec) {
40  std::vector<std::string> strs;
41  boost::split(strs, str_spec, boost::is_any_of(":"));
42  if (strs.size() != 3) {
43  throw ValueError("invalid agent spec string '" + str_spec + "'");
44  }
45  path_ = strs[0];
46  lib_ = strs[1];
47  agent_ = strs[2];
48  alias_ = agent_;
49 }
50 
51 std::string AgentSpec::Sanitize() {
52  std::string s = str();
53  boost::replace_all(s, "/", "_");
54  boost::replace_all(s, "-", "_");
55  boost::replace_all(s, ":", "_");
56  boost::replace_all(s, ".", "_");
57  return s;
58 }
59 
60 std::string AgentSpec::LibPath() {
61  return (fs::path(path_) / fs::path("lib" + lib_ + SUFFIX)).string();
62 }
63 
64 std::string AgentSpec::str() {
65  return path_ + ":" + lib_ + ":" + agent_;
66 }
67 
68 std::map<std::string, DynamicModule*> DynamicModule::modules_;
69 std::map<std::string, AgentCtor*> DynamicModule::man_ctors_;
70 
72  if (man_ctors_.count(spec.str()) > 0) { // for testing
73  Agent* a = man_ctors_[spec.str()](ctx);
74  a->spec(spec.str());
75  return a;
76  } else if (modules_.count(spec.str()) == 0) {
77  DynamicModule* dyn = new DynamicModule(spec);
78  modules_[spec.str()] = dyn;
79  }
80 
81  DynamicModule* dyn = modules_[spec.str()];
82  Agent* a = dyn->ConstructInstance(ctx);
83  a->spec(spec.str());
84  return a;
85 }
86 
88  bool rtn = true;
89  try {
90  DynamicModule dyn(spec);
91  } catch (cyclus::Error& e) {
92  rtn = false;
93  }
94  return rtn;
95 }
96 
98  std::map<std::string, DynamicModule*>::iterator it;
99  for (it = modules_.begin(); it != modules_.end(); it++) {
100  it->second->CloseLibrary();
101  delete it->second;
102  }
103  modules_.clear();
104  man_ctors_.clear();
105 }
106 
107 DynamicModule::DynamicModule(AgentSpec spec)
108  : module_library_(0),
109  ctor_(NULL) {
110  path_ = Env::FindModule(spec.LibPath());
111  ctor_name_ = "Construct" + spec.agent();
112  OpenLibrary();
113  SetConstructor();
114 }
115 
116 Agent* DynamicModule::ConstructInstance(Context* ctx) {
117  return ctor_(ctx);
118 }
119 
120 std::string DynamicModule::path() {
121  return path_;
122 }
123 
124 } // namespace cyclus
#define SUFFIX
Definition: suffix.h:1
A generic mechanism to manually manage exceptions.
Definition: error.h:12
static bool Exists(AgentSpec spec)
Tests that an agent spec really exists.
std::string Sanitize()
For values that are too big, too small, etc.
Definition: error.h:41
A class for extracting information from a given XML parser.
Definition: infile_tree.h:22
static std::string FindModule(std::string path)
Returns the full path to a module by searching through default install and CYCLUS_PATH directories...
Definition: env.cc:175
static void CloseAll()
Closes all statically loaded dynamic modules.
virtual std::string GetString(std::string query, int index=0)
investigates the current status and returns a string representing the content of a query at a given i...
Definition: infile_tree.cc:54
std::string spec()
The string representation of the agent spec that uniquely identifies the concrete agent class/module...
Definition: agent.h:358
std::string path()
The path to the module&#39;s shared object library.
A simulation context provides access to necessary simulation-global functions and state...
Definition: context.h:128
The abstract base class used by all types of agents that live and interact in a simulation.
Definition: agent.h:51
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
std::string agent()
static Agent * Make(Context *ctx, AgentSpec spec)
Returns a newly constructed agent for the given module spec.
std::string str()
std::string LibPath()
std::vector< std::string > split(const std::string &s, char delim)
Definition: sqlite_back.cc:28