From 01ad980e5595816d57bc9aa6aa451914b6fd2d33 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 13 Nov 2012 14:38:30 +0100 Subject: [PATCH] Pimpl TimeStat class --- include/yazpp/timestat.h | 7 ++----- src/timestat.cpp | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/include/yazpp/timestat.h b/include/yazpp/timestat.h index d0da2b1..2d86268 100644 --- a/include/yazpp/timestat.h +++ b/include/yazpp/timestat.h @@ -29,7 +29,6 @@ #ifndef YAZPP_TIMESTAT_H #define YAZPP_TIMESTAT_H -#include #include namespace yazpp_1 { @@ -40,10 +39,8 @@ namespace yazpp_1 { void add_bytes(int m); int get_total(); private: - time_t m_sec; // time of most recent bucket - int *m_bucket; - int m_ptr; - int m_size; + class Rep; + Rep *m_p; }; } diff --git a/src/timestat.cpp b/src/timestat.cpp index f06dd55..316e1f4 100644 --- a/src/timestat.cpp +++ b/src/timestat.cpp @@ -29,20 +29,31 @@ #include #endif #include +#include using namespace yazpp_1; +class TimeStat::Rep { + friend class TimeStat; + time_t sec; // time of most recent bucket + int *bucket; + int ptr; + int size; +}; + TimeStat::TimeStat(int sz) { - m_sec = 0; - m_size = sz; - m_bucket = new int[m_size]; - m_ptr = 0; + m_p = new Rep; + m_p->sec = 0; + m_p->size = sz; + m_p->bucket = new int[m_p->size]; + m_p->ptr = 0; } TimeStat::~TimeStat() { - delete [] m_bucket; + delete [] m_p->bucket; + delete m_p; } int TimeStat::get_total() @@ -50,8 +61,8 @@ int TimeStat::get_total() add_bytes(0); int bw = 0; int i; - for (i = 0; isize; i++) + bw += m_p->bucket[i]; return bw; } @@ -59,20 +70,20 @@ void TimeStat::add_bytes(int b) { time_t now = time(0); - if (now >= m_sec) + if (now >= m_p->sec) { - int d = now - m_sec; - if (d > m_size) - d = m_size; + int d = now - m_p->sec; + if (d > m_p->size) + d = m_p->size; while (--d >= 0) { - if (++m_ptr == m_size) - m_ptr = 0; - m_bucket[m_ptr] = 0; + if (++m_p->ptr == m_p->size) + m_p->ptr = 0; + m_p->bucket[m_p->ptr] = 0; } - m_bucket[m_ptr] += b; + m_p->bucket[m_p->ptr] += b; } - m_sec = now; + m_p->sec = now; } /* -- 1.7.10.4