SigUtil  0.95
Utility modules for modern C++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
time_watch.hpp
Go to the documentation of this file.
1 /*
2 Copyright© 2014 Akihiro Nishimura
3 
4 This software is released under the MIT License.
5 http://opensource.org/licenses/mit-license.php
6 */
7 
8 #ifndef SIG_UTIL_TIMEWATCH_HPP
9 #define SIG_UTIL_TIMEWATCH_HPP
10 
11 #include "../sigutil.hpp"
12 #include "../helper/maybe.hpp"
13 #include <chrono>
14 
15 
17 
18 namespace sig
19 {
21 
59 template <class Clock = std::chrono::system_clock>
60 class TimeWatch
61 {
62  using Time = typename Clock::time_point;
63  using Duration = decltype(std::declval<Time>() - std::declval<Time>());
64 
65  Time st;
66  std::vector<Duration> laps;
67  std::vector<Duration> cache;
68  bool is_run;
69 
70 private:
71  void init()
72  {
73  st = Clock::now();
74  laps.clear();
75  cache.clear();
76  }
77 
78  Duration d_accumulate(std::vector<Duration> const& ds, uint end) const
79  {
80  return std::accumulate(ds.begin(), ds.begin() + end, Duration(), [&](Duration const& sum, Duration const& v){
81  return sum + v;
82  });
83  }
84 
85 public:
87  {
88  init();
89  is_run = true;
90  }
91 
93  void reset()
94  {
95  init();
96  is_run = false;
97  }
98 
100  void stop()
101  {
102  if (is_run) cache.push_back(Clock::now() - st);
103  is_run = false;
104  }
105 
107  void restart()
108  {
109  st = Clock::now();
110  is_run = true;
111  }
112 
114  void save()
115  {
116  if (is_run){
117  auto now = Clock::now();
118  cache.push_back(now - st);
119  st = std::move(now);
120  }
121 
122  laps.push_back(d_accumulate(cache, cache.size()));
123  cache.clear();
124  }
125 
126 
128  uint get_count() const{ return laps.size(); }
129 
130 
132 
135  template<class TimeUnit = std::chrono::milliseconds>
137  {
138  return std::chrono::duration_cast<TimeUnit>(d_accumulate(laps, laps.size())).count();
139  }
140 
142 
145  template<class TimeUnit = std::chrono::milliseconds>
146  auto get_split_time(uint index) ->Maybe<long>
147  {
148  return index < laps.size()
149  ? Just<long>(std::chrono::duration_cast<TimeUnit>(d_accumulate(laps, index+1)).count())
150  : Nothing(-1l);
151  }
152 
154 
157  template<class TimeUnit = std::chrono::milliseconds>
158  auto get_lap_time(uint index) ->Maybe<long>
159  {
160  return index < laps.size()
161  ? Just<long>(std::chrono::duration_cast<TimeUnit>(laps[index]).count())
162  : Nothing(-1l);
163  }
164 };
165 
166 }
167 
168 #endif
uint get_count() const
記録したデータ数を取得
Definition: time_watch.hpp:128
タイムウォッチ
Definition: time_watch.hpp:60
auto get_split_time(uint index) -> Maybe< long >
 指定した区間までのトータル時間(スプリットタイム)を取得
Definition: time_watch.hpp:146
auto end(C &&c) -> std::move_iterator< typename RC::iterator >
auto Nothing() -> typename impl::SameIf< T, void, typename boost::none_t, Maybe< T >>::type
値コンストラクタ
Definition: maybe.hpp:67
auto get_lap_time(uint index) -> Maybe< long >
 指定した区間の時間(ラップタイム)を取得
Definition: time_watch.hpp:158
long get_total_time()
 全区間のトータルの時間を取得
Definition: time_watch.hpp:136
void save()
時間保存
Definition: time_watch.hpp:114
void stop()
停止
Definition: time_watch.hpp:100
void reset()
初期化して停止
Definition: time_watch.hpp:93
void restart()
停止解除
Definition: time_watch.hpp:107
Definition: array.hpp:15
auto sum(C const &data) -> typename impl::SameIf< R, void, typename impl::container_traits< C >::value_type, R >::type
総和