Empirical
Action.h
Go to the documentation of this file.
1 
14 #ifndef EMP_CONTROL_ACTION
15 #define EMP_CONTROL_ACTION
16 
17 #include <functional>
18 #include <string>
19 
20 namespace emp {
21 
27 
28  class ActionBase {
29  protected:
30  std::string name;
31 
32  ActionBase(const std::string & in_name) : name(in_name) { ; }
33 
34  // Protected: ActionBase should not be copied directly, only through derived class.
35  ActionBase(const ActionBase &) = default;
36  ActionBase(ActionBase &&) = default;
37  ActionBase & operator=(const ActionBase &) = default;
38  ActionBase & operator=(ActionBase &&) = default;
39  public:
40  virtual ~ActionBase() { ; }
41 
43  const std::string & GetName() const { return name; }
44 
46  virtual size_t GetArgCount() const = 0;
47 
49  virtual ActionBase * Clone() const = 0;
50  };
51 
54  template <size_t ARG_COUNT>
55  class ActionSize : public ActionBase {
56  protected:
57  ActionSize(const std::string & in_name) : ActionBase(in_name) { ; }
58  public:
59  size_t GetArgCount() const { return ARG_COUNT; }
60  };
61 
64  template <typename... ARGS> class Action;
65 
68  template <typename... ARGS>
69  class Action<void(ARGS...)> : public ActionSize<sizeof...(ARGS)> {
70  protected:
71  std::function<void(ARGS...)> fun;
72  public:
73  using this_t = Action<void(ARGS...)>;
74  using parent_t = ActionSize<sizeof...(ARGS)>;
75 
76  Action(const std::function<void(ARGS...)> & in_fun, const std::string & in_name="")
77  : parent_t(in_name), fun(in_fun) { ; }
78  template <typename RETURN>
79  Action(const std::function<RETURN(ARGS...)> & in_fun, const std::string & in_name="")
80  : parent_t(in_name)
81  , fun([in_fun](ARGS &&... args){in_fun(std::forward<ARGS>(args)...);}) { ; }
82  Action(const this_t &) = default;
83  Action(this_t &&) = default;
84 
85  this_t & operator=(const this_t &) = default;
86  this_t & operator=(this_t &&) = default;
87 
88  const std::function<void(ARGS...)> & GetFun() const { return fun; };
89 
91  void Call(ARGS &&... args) { return fun(std::forward<ARGS>(args)...); }
92 
94  this_t * Clone() const { return new this_t(*this); }
95  };
96 
97 
100  template <typename RETURN, typename... ARGS>
101  class Action<RETURN(ARGS...)> : public ActionSize<sizeof...(ARGS)> {
102  protected:
103  std::function<RETURN(ARGS...)> fun;
104  public:
105  using fun_t = RETURN(ARGS...);
107  using parent_t = ActionSize<sizeof...(ARGS)>;
108 
109  Action(const std::function<RETURN(ARGS...)> & in_fun, const std::string & in_name="")
110  : parent_t(in_name), fun(in_fun) { ; }
111  Action(const this_t &) = default;
112  Action(this_t &&) = default;
113 
114  this_t & operator=(const this_t &) = default;
115  this_t & operator=(this_t &&) = default;
116 
117  const std::function<fun_t> & GetFun() const { return fun; };
118 
120  RETURN Call(ARGS &&... args) { return fun(std::forward<ARGS>(args)...); }
121 
123  this_t * Clone() const { return new this_t(*this); }
124  };
125 
127  template <typename RETURN, typename... ARGS>
128  auto make_action(const std::function<RETURN(ARGS...)> & in_fun, const std::string & name="") {
129  return Action<RETURN(ARGS...)>(in_fun, name);
130  }
131 }
132 
133 #endif
Definition: Action.h:28
ActionBase(const std::string &in_name)
Definition: Action.h:32
RETURN(ARGS...) fun_t
Definition: Action.h:105
size_t GetArgCount() const
Get number of arguments this action takes.
Definition: Action.h:59
virtual ~ActionBase()
Definition: Action.h:40
std::string name
A unique name for this action so it can be called at runtime.
Definition: Action.h:30
virtual size_t GetArgCount() const =0
Get number of arguments this action takes.
this_t * Clone() const
Build a copy of this Action.
Definition: Action.h:94
virtual ActionBase * Clone() const =0
Clone() will produce a pointer to a full copy of an Action, going through derived version...
Definition: Action.h:69
this_t * Clone() const
Build a copy of this Action.
Definition: Action.h:123
auto make_action(const std::function< RETURN(ARGS...)> &in_fun, const std::string &name="")
Build an action object using this function.
Definition: Action.h:128
std::function< RETURN(ARGS...)> fun
The specific function associated with this action.
Definition: Action.h:103
Definition: Action.h:64
Definition: Action.h:55
Action(const std::function< RETURN(ARGS...)> &in_fun, const std::string &in_name="")
Definition: Action.h:79
Action(const std::function< void(ARGS...)> &in_fun, const std::string &in_name="")
Definition: Action.h:76
std::function< void(ARGS...)> fun
The specific function associated with this action.
Definition: Action.h:71
ActionSize(const std::string &in_name)
Definition: Action.h:57
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
RETURN Call(ARGS &&...args)
Call the function associated with this action.
Definition: Action.h:120
const std::function< void(ARGS...)> & GetFun() const
Definition: Action.h:88
void Call(ARGS &&...args)
Call the function associated with this action.
Definition: Action.h:91
ActionBase & operator=(const ActionBase &)=default
Action(const std::function< RETURN(ARGS...)> &in_fun, const std::string &in_name="")
Definition: Action.h:109
const std::function< fun_t > & GetFun() const
Definition: Action.h:117