CYCLUS
dynamic_module.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_DYNAMIC_MODULE_H_
2 #define CYCLUS_SRC_DYNAMIC_MODULE_H_
3 
4 #include <string>
5 #include <map>
6 
7 #include "error.h"
8 
9 // for testing
10 class SimInitTest;
11 
12 namespace cyclus {
13 
14 class Agent;
15 class Context;
16 
17 typedef Agent* AgentCtor(Context*);
18 
19 class InfileTree;
20 
21 class AgentSpec {
22  public:
23  AgentSpec() {}
25  AgentSpec(std::string path, std::string lib, std::string agent,
26  std::string alias);
27  AgentSpec(std::string str_spec);
28 
29  std::string Sanitize();
30  std::string LibPath();
31  std::string str();
32 
33  std::string path() { return path_; }
34  std::string lib() { return lib_; }
35  std::string agent() { return agent_; }
36  std::string alias() { return alias_; }
37 
38  private:
39  std::string path_;
40  std::string lib_;
41  std::string agent_;
42  std::string alias_;
43 };
44 
45 std::string SanitizeSpec(std::string module_spec);
46 
47 std::string ModuleConstructor(std::string module_spec);
48 
50  public:
51  /// Convenience class for using a stack variable to auto-destruct all loaded
52  /// dynamic modules.
53  class Closer {
54  public:
55  ~Closer() {
56  CloseAll();
57  }
58  };
59 
60  /// Returns a newly constructed agent for the given module spec.
61  static Agent* Make(Context* ctx, AgentSpec spec);
62 
63  /// Tests that an agent spec really exists.
64  static bool Exists(AgentSpec spec);
65 
66  /// Closes all statically loaded dynamic modules. This should always be called
67  /// before process termination. This must be called AFTER all agents have
68  /// been destructed.
69  static void CloseAll();
70 
71  /// The path to the module's shared object library.
72  std::string path();
73 
74  private:
75  /// Creates a new dynamically loadable module.
76  /// @param name the name of the module
78 
79  /// construct an instance of this module
80  /// @return a fresh instance
81  Agent* ConstructInstance(Context* ctx);
82 
83  /// closes the loaded module dynamic lib
84  void CloseLibrary();
85 
86  /// all dynamically loaded modules are
87  /// added to this map when loaded.
88  static std::map<std::string, DynamicModule*> modules_;
89 
90  /// for testing - see sim_init_tests
91  friend class ::SimInitTest;
92  /// for testing - see sim_init_tests
93  static std::map<std::string, AgentCtor*> man_ctors_;
94 
95  /// the name of the module
96  std::string path_;
97 
98  /// the name of the module
99  std::string ctor_name_;
100 
101  /// the library to open and close
102  void* module_library_;
103 
104  /// a functor for the constructor
105  AgentCtor* ctor_;
106 
107  /// uses dlopen to open the module shared lib
108  void OpenLibrary();
109 
110  /// sets the constructor member
111  void SetConstructor();
112 };
113 
114 } // namespace cyclus
115 
116 #endif // CYCLUS_SRC_DYNAMIC_MODULE_H_
std::string Sanitize()
A class for extracting information from a given XML parser.
Definition: infile_tree.h:22
std::string path()
std::string alias()
std::string lib()
std::string SanitizeSpec(std::string module_spec)
std::string ModuleConstructor(std::string module_spec)
Convenience class for using a stack variable to auto-destruct all loaded dynamic modules.
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()
Agent * AgentCtor(Context *)
std::string str()
std::string LibPath()