Empirical
BatchConfig.h
Go to the documentation of this file.
1 // This file is part of Empirical, https://github.com/devosoft/Empirical
2 // Copyright (C) Michigan State University, 2017.
3 // Released under the MIT Software license; see doc/LICENSE
4 //
5 //
6 // A tool to control a series of runs and keep them updated.
7 
8 #ifndef EMP_BATCH_CONFIG_H
9 #define EMP_BATCH_CONFIG_H
10 
11 #include "../base/vector.h"
12 
13 namespace emp {
14 
18 
19  template <typename RUN_T, CONFIG_T>
20  class BatchConfig {
21  private:
22  struct RunInfo {
23  size_t id;
24  CONFIG_T config;
25 
26  RunInfo(size_t & _id, const CONFIG_T & _config) : id(_id), config(_config) { ; }
27  };
28 
30  size_t cur_run;
31 
32  using start_fun_t = std::function<void(const CONFIG_T &)>;
33  start_fun_t start_fun;
34 
35  public:
36  BatchConfig(const start_fun_t & f) : cur_run(0), start_fun(f) { ; }
37 
38  size_t GetSize() const { return runs.size(); }
39  size_t GetCurRun() const { return cur_run; }
40  CONFIG_T & GetConfig(size_t id) { return runs[id].config; }
41  const CONFIG_T & GetConfig(size_t id) const { return runs[id].config; }
42 
43  void AddRun(const CONFIG_T & in_config) {
44  runs.emplace_back(runs.size(), in_config);
45  }
46 
47  bool Start() {
48  if (cur_run >= runs.size()) return false;
49  start_fun(runs[cur_run].config);
50  cur_run++;
51  return true;
52  }
53  };
54 
55  // If no config type is explicitly provided, pull it from the run type.
56  template <typename RUN_T> using BatchConfig = BatchConfig<RUN_T, RUN_T::config_t>;
57 }
58 
59 #endif
void AddRun(const CONFIG_T &in_config)
Definition: BatchConfig.h:43
const CONFIG_T & GetConfig(size_t id) const
Definition: BatchConfig.h:41
size_t size() const
Definition: vector.h:151
void emplace_back(ARGS &&...args)
Definition: vector.h:219
size_t GetCurRun() const
Definition: BatchConfig.h:39
size_t GetSize() const
Definition: BatchConfig.h:38
bool Start()
Definition: BatchConfig.h:47
Definition: BatchConfig.h:20
If we are in emscripten, make sure to include the header.
Definition: array.h:37
CONFIG_T & GetConfig(size_t id)
Definition: BatchConfig.h:40
BatchConfig(const start_fun_t &f)
Definition: BatchConfig.h:36