Empirical
DataManager.h
Go to the documentation of this file.
1 
10 #ifndef EMP_DATA_MANAGER_H
11 #define EMP_DATA_MANAGER_H
12 
13 #include <map>
14 #include <string>
15 
16 #include "../base/assert.h"
17 #include "../tools/map_utils.h"
18 
19 #include "DataNode.h"
20 
21 namespace emp {
22 
24  template <typename VAL_TYPE, emp::data... MODS>
25  class DataManager {
26  private:
27  using data_t = VAL_TYPE;
28  using node_t = DataNode<data_t, MODS...>;
29 
30  std::map<std::string, node_t *> node_map;
31 
32  public:
33  DataManager() : node_map() { ; }
35  for (auto & x : node_map) delete x.second;
36  }
37 
39  size_t GetSize() const { return node_map.size(); }
41  auto & GetNodes() const { return node_map; }
42 
43  bool HasNode(const std::string & name) {
44  return Has(node_map, name);
45  }
46 
48  node_t & New(const std::string & name) {
49  emp_assert(!Has(node_map, name), name);
50  node_map[name] = new node_t;
51  return *(node_map[name]);
52  }
53 
56  void Delete(const std::string & name) {
57  emp_assert(Has(node_map, name), name);
58  node_map.erase(name);
59  }
60 
63  const node_t & Get(const std::string & name) const {
64  emp_assert(Has(node_map, name), name);
65  return *(node_map[name]);
66  }
67 
70  node_t & Get(const std::string & name) {
71  emp_assert(Has(node_map, name), name);
72  return *(node_map[name]);
73  }
74 
75  // == Operations that forward to DataNode objects ==
76 
87  template <typename... Ts>
88  void AddData(const std::string & name, Ts... extra) {
89  emp_assert(Has(node_map, name), name);
90  node_map[name]->Add(extra...);
91  }
92 
97  void ResetAll() {
98  for (auto & x : node_map) x.second->Reset();
99  }
100  };
101 
102 }
103 
104 #endif
void Delete(const std::string &name)
Definition: DataManager.h:56
DataNode objects track a specific type of data over the course of a run.
void AddData(const std::string &name, Ts...extra)
Definition: DataManager.h:88
DataManager()
Definition: DataManager.h:33
DataManagers handle sets of DataNode objects that all have the same tracking settings.
Definition: DataManager.h:25
node_t & New(const std::string &name)
Creates and adds a new DataNode, with the name specified in.
Definition: DataManager.h:48
data
A set of modifiers are available do describe DataNode.
Definition: DataNode.h:38
Definition: DataNode.h:648
void ResetAll()
Definition: DataManager.h:97
~DataManager()
Definition: DataManager.h:34
bool Has(const MAP_T &in_map, const KEY_T &key)
Take any map type, and run find to determine if a key is present.
Definition: map_utils.h:21
If we are in emscripten, make sure to include the header.
Definition: array.h:37
auto & GetNodes() const
Returns the std::map mapping node names (strings) to DataNodes.
Definition: DataManager.h:41
#define emp_assert(...)
Definition: assert.h:199
node_t & Get(const std::string &name)
Definition: DataManager.h:70
size_t GetSize() const
Returns the number of DataNodes in this DataManager.
Definition: DataManager.h:39
const node_t & Get(const std::string &name) const
Definition: DataManager.h:63
bool HasNode(const std::string &name)
Definition: DataManager.h:43