osmanip
Library with useful output stream tools like: color and style manipulators, progress bars and terminal graphics.
multi_progress_bar.hpp
Go to the documentation of this file.
1 //====================================================
2 // File data
3 //====================================================
13 //====================================================
14 // Preprocessor settings
15 //====================================================
16 #pragma once
17 #ifndef OSMANIP_PROGRESSBAR_MULTIPROGRESSBAR_HPP
18 #define OSMANIP_PROGRESSBAR_MULTIPROGRESSBAR_HPP
19 
20 //====================================================
21 // Headers
22 //====================================================
23 
24 // My headers
27 
28 // STD headers
29 #include <cstddef>
30 #include <cstdint>
31 #include <mutex>
32 #include <string>
33 #include <tuple>
34 #include <type_traits>
35 #include <utility>
36 
37 namespace osm {
38 
39  //====================================================
40  // Structs
41  //====================================================
42 
43  // indices
49  template <size_t... Is>
50  struct indices {};
51 
52  // gen_indices
59  template <size_t N, size_t... Is>
60  struct gen_indices : gen_indices<N - 1, N - 1, Is...> {};
61 
62  // gen_indices specialization
69  template <size_t... Is>
70  struct gen_indices<0, Is...> : indices<Is...> {};
71 
72  //====================================================
73  // make_MultiProgressBar
74  //====================================================
80  template <class... Indicators>
82  public:
83 
84  // Parametric constructor
91  template <class... Inds>
92  make_MultiProgressBar(Inds &&...bars) : bars_{std::forward<Inds>(bars)...}, last_updated_index(0) {}
93 
94  // size
100  static size_t size() { return sizeof...(Indicators); }
101 
102  // for_one
113  template <class Func, class... Args>
114  void for_one(size_t idx, Func &&func, Args &&...args) {
115  call_one(idx, gen_indices<sizeof...(Indicators)>(), std::forward<Func>(func),
116  std::forward<Args>(args)...);
117  }
118 
119  // for_each
130  template <class Func, class... Args>
131  void for_each(Func &&func, Args &&...args) {
132  call_all(gen_indices<sizeof...(Indicators)>(), std::forward<Func>(func), std::forward<Args>(args)...);
133  }
134 
135  private:
136 
137  // call_one
150  template <size_t... Ids, class Func, class... Args>
151  void call_one(size_t idx, indices<Ids...>, Func func, Args &&...args) {
152  std::lock_guard<std::mutex> lock{mutex_};
153  int32_t idx_delta = idx - last_updated_index;
154  std::string direction;
155 
156  if (idx_delta < 0) {
157  direction = "up";
158  idx_delta = -idx_delta;
159  } else {
160  direction = "down";
161  }
162  for (int32_t i = 0; i < idx_delta; i++) {
163  osm::cout << feat(crs, direction, 1);
164  }
165  last_updated_index = idx;
166  [](...) {}
167 
168  ((idx == Ids &&
169  ((void)std::forward<Func>(func)(std::get<Ids>(bars_), std::forward<Args>(args)...), false))...);
170  }
171 
172  // call_all
184  template <size_t... Ids, class Func, class... Args>
185  void call_all(indices<Ids...>, Func func, Args &&...args) {
186  std::lock_guard<std::mutex> lock{mutex_};
187  auto dummy = {(func(std::get<Ids>(bars_), args...), 0)...};
188  (void)dummy;
189  }
190 
191  // Attributes
192  std::tuple<Indicators &...> bars_;
193  std::mutex mutex_;
194  uint32_t last_updated_index;
195  };
196 
197  //====================================================
198  // MultiProgressBar
199  //====================================================
208  template <class... Indicators>
210  return {std::forward<Indicators>(inds)...};
211  }
212 
213  //====================================================
214  // type_identity
215  //====================================================
221  template <class T>
222  struct type_identity {
223  using type = T;
224  };
225 
226  //====================================================
227  // updater
228  //====================================================
233  struct updater {
234  template <template <class> class PB, class bar_type>
235  auto operator()(PB<bar_type> &pb, typename type_identity<bar_type>::type v) const
236  -> decltype(pb.update(bar_type{})) {
237  return pb.update(v);
238  }
239  };
240 } // namespace osm
241 
242 #endif
Template class used to create multi progress bars.
Definition: multi_progress_bar.hpp:81
make_MultiProgressBar(Inds &&...bars)
Construct a new make MultiProgressBar object.
Definition: multi_progress_bar.hpp:92
void for_each(Func &&func, Args &&...args)
Method used to update progress bars simultaneously.
Definition: multi_progress_bar.hpp:131
static size_t size()
Return the number of the indicators.
Definition: multi_progress_bar.hpp:100
void for_one(size_t idx, Func &&func, Args &&...args)
Method used to update the progress bar for one progress bar only.
Definition: multi_progress_bar.hpp:114
Definition: canvas.cpp:30
make_MultiProgressBar< typename std::remove_reference< Indicators >::type... > MultiProgressBar(Indicators &&...inds)
Helper function used for deduction guides.
Definition: multi_progress_bar.hpp:209
std::ostream cout
const string_pair_map crs
It is used to store the cursor commands.
Definition: cursor.cpp:42
const std::string & feat(const std::unordered_map< std::string, std::string > &generic_map, const std::string &feat_string)
It takes an std::map object as the first argument and an std::string object (map key) as the second a...
Definition: common.cpp:41
Type to generate indices for parameter packs.
Definition: multi_progress_bar.hpp:60
Type to generate indices for parameter packs.
Definition: multi_progress_bar.hpp:50
Struct used to to typedef the functor.
Definition: multi_progress_bar.hpp:222
T type
Definition: multi_progress_bar.hpp:223
Functor used to call the ProgressBar class update method.
Definition: multi_progress_bar.hpp:233
auto operator()(PB< bar_type > &pb, typename type_identity< bar_type >::type v) const -> decltype(pb.update(bar_type{}))
Definition: multi_progress_bar.hpp:235