11 #ifndef EMP_CONTROL_ACTION_MANAGER 12 #define EMP_CONTROL_ACTION_MANAGER 15 #include <unordered_map> 17 #include "../tools/string_utils.h" 25 std::unordered_map<std::string, ActionBase *> action_map;
27 std::string prefix =
"emp_action_";
34 for (
const auto & x : in.action_map) {
35 action_map[x.first] = x.second->Clone();
44 size_t GetSize()
const {
return action_map.size(); }
48 emp_assert(action_map.find(name) != action_map.end());
49 return *(action_map[name]);
54 auto it = action_map.find(name);
60 template <
typename RETURN,
typename... ARGS>
61 auto &
Add(
const std::function<RETURN(ARGS...)> & in_fun,
const std::string & name) {
63 auto * new_action =
new Action<RETURN(ARGS...)>(in_fun, name);
64 action_map[name] = new_action;
69 template <
typename RETURN,
typename... ARGS>
70 auto &
Add(
const std::function<RETURN(ARGS...)> & in_fun) {
71 std::string name(prefix);
73 return Add(in_fun, name);
78 auto * new_action = action.
Clone();
79 action_map[action.
GetName()] = new_action;
85 os << action_map.size() <<
" actions found:\n";
86 for (
auto & x : action_map) os <<
" " << x.first <<
std::endl;
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...
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