From de6ec48d80879ce9279b3e13c24787e9054d942f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20K=C3=BCmmel?= Date: Tue, 18 Sep 2012 09:10:47 +0200 Subject: remove some code duplication --- src/build.cc | 5 +++-- src/build.h | 28 ++++++++++++---------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/build.cc b/src/build.cc index 5a0c3b3..7310c3d 100644 --- a/src/build.cc +++ b/src/build.cc @@ -18,6 +18,7 @@ #include #include + #ifdef _WIN32 #include #else @@ -180,14 +181,14 @@ string BuildStatus::FormatProgressStatus( // Overall finished edges per second. case 'o': overall_rate_.UpdateRate(finished_edges_); - overall_rate_.snprinfRate(buf, "%.1f"); + snprinfRate(overall_rate_.rate(), buf, "%.1f"); out += buf; break; // Current rate, average over the last '-j' jobs. case 'c': current_rate_.UpdateRate(finished_edges_); - current_rate_.snprinfRate(buf, "%.1f"); + snprinfRate(current_rate_.rate(), buf, "%.1f"); out += buf; break; diff --git a/src/build.h b/src/build.h index 0902a4c..c94c8cc 100644 --- a/src/build.h +++ b/src/build.h @@ -200,31 +200,33 @@ struct BuildStatus { /// The custom progress status format to use. const char* progress_status_format_; + template + void snprinfRate(double rate, T buf, const char* format) const { + if (rate == -1) snprintf(buf, sizeof(buf), "?"); + else snprintf(buf, sizeof(buf), format, rate); + } + struct RateInfo { RateInfo() : rate_(-1) {} void Restart() { stopwatch_.Restart(); } + double rate() { return rate_; } void UpdateRate(int edges) { if (edges && stopwatch_.Elapsed()) rate_ = edges / stopwatch_.Elapsed(); } - template - void snprinfRate(T buf, const char* format) { - if (rate_ == -1) snprintf(buf, sizeof(buf), "?"); - else snprintf(buf, sizeof(buf), format, rate_); - } - private: - Stopwatch stopwatch_; double rate_; + Stopwatch stopwatch_; }; struct SlidingRateInfo { - SlidingRateInfo(int n) : N(n), last_update_(-1), rate_(-1) {} + SlidingRateInfo(int n) : rate_(-1), N(n), last_update_(-1) {} void Restart() { stopwatch_.Restart(); } + double rate() { return rate_; } void UpdateRate(int update_hint) { if (update_hint == last_update_) @@ -238,18 +240,12 @@ struct BuildStatus { rate_ = times_.size() / (times_.back() - times_.front()); } - template - void snprinfRate(T buf, const char* format) { - if (rate_ == -1) snprintf(buf, sizeof(buf), "?"); - else snprintf(buf, sizeof(buf), format, rate_); - } - private: + double rate_; + Stopwatch stopwatch_; const size_t N; std::queue times_; - Stopwatch stopwatch_; int last_update_; - double rate_; }; mutable RateInfo overall_rate_; -- cgit v0.12