Empirical
ActionManager.h
Go to the documentation of this file.
1 
11 #ifndef EMP_CONTROL_ACTION_MANAGER
12 #define EMP_CONTROL_ACTION_MANAGER
13 
14 #include <string>
15 #include <unordered_map>
16 
17 #include "../tools/string_utils.h"
18 
19 #include "Action.h"
20 
21 namespace emp {
22 
23  class ActionManager {
24  private:
25  std::unordered_map<std::string, ActionBase *> action_map;
26  int next_id=1;
27  std::string prefix = "emp_action_";
28 
29  public:
30  ActionManager() : action_map() { ; }
31  ActionManager(ActionManager &&) = default; // Normal juggle is okay for move constructor
32  ActionManager(const ActionManager & in) : action_map(), next_id(in.next_id), prefix(in.prefix) {
33  // Copy all actions from input manager.
34  for (const auto & x : in.action_map) {
35  action_map[x.first] = x.second->Clone();
36  }
37  }
38  ~ActionManager() { for (auto & x : action_map) delete x.second; }
39 
41  int GetNextID() const { return next_id; }
42 
44  size_t GetSize() const { return action_map.size(); }
45 
47  ActionBase & operator[](const std::string & name) {
48  emp_assert(action_map.find(name) != action_map.end());
49  return *(action_map[name]);
50  }
51 
53  const ActionBase & operator[](const std::string & name) const {
54  auto it = action_map.find(name);
55  emp_assert(it != action_map.end());
56  return *(it->second);
57  }
58 
60  template <typename RETURN, typename... ARGS>
61  auto & Add(const std::function<RETURN(ARGS...)> & in_fun, const std::string & name) {
62  // Create the new action, save it, and return it.
63  auto * new_action = new Action<RETURN(ARGS...)>(in_fun, name);
64  action_map[name] = new_action;
65  return *new_action;
66  }
67 
69  template <typename RETURN, typename... ARGS>
70  auto & Add(const std::function<RETURN(ARGS...)> & in_fun) {
71  std::string name(prefix);
72  name += emp::to_string(next_id++);
73  return Add(in_fun, name);
74  }
75 
77  auto & Add(const ActionBase & action) {
78  auto * new_action = action.Clone();
79  action_map[action.GetName()] = new_action;
80  return *new_action;
81  }
82 
84  void PrintNames(std::ostream & os=std::cout) {
85  os << action_map.size() << " actions found:\n";
86  for (auto & x : action_map) os << " " << x.first << std::endl;
87  }
88  };
89 
90 }
91 
92 #endif
Definition: Action.h:28
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
ActionManager()
Definition: ActionManager.h:30
ActionBase & operator[](const std::string &name)
Look up an action with the specified name.
Definition: ActionManager.h:47
A mechanism to abstract functions from their underlying type and provide run-time names...
virtual ActionBase * Clone() const =0
Clone() will produce a pointer to a full copy of an Action, going through derived version...
Definition: Action.h:64
int GetNextID() const
Get the ID to be used for the next new function.
Definition: ActionManager.h:41
static const PrintStr endl("<br>")
Pre-define emp::endl to insert a "<br>" and thus acting like a newline.
auto & Add(const std::function< RETURN(ARGS...)> &in_fun, const std::string &name)
Add a functon to this manager with a pre-specified name.
Definition: ActionManager.h:61
auto & Add(const std::function< RETURN(ARGS...)> &in_fun)
Add a function to this manager with an auto-generated name.
Definition: ActionManager.h:70
auto & Add(const ActionBase &action)
Add an action to this manager.
Definition: ActionManager.h:77
size_t GetSize() const
How many actions are in this manager?
Definition: ActionManager.h:44
const std::string & GetName() const
Get the name of this action.
Definition: Action.h:43
If we are in emscripten, make sure to include the header.
Definition: array.h:37
~ActionManager()
Definition: ActionManager.h:38
#define emp_assert(...)
Definition: assert.h:199
void PrintNames(std::ostream &os=std::cout)
Print out the name of all actions maintained by this manager.
Definition: ActionManager.h:84
const ActionBase & operator[](const std::string &name) const
Look up an action with the specified name (const version)
Definition: ActionManager.h:53
Definition: ActionManager.h:23
ActionManager(const ActionManager &in)
Definition: ActionManager.h:32