Chris@1256: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@1256: 
Chris@1256: /*
Chris@1256:     Sonic Visualiser
Chris@1256:     An audio file viewer and annotation editor.
Chris@1256:     Centre for Digital Music, Queen Mary, University of London.
Chris@1256:     
Chris@1256:     This program is free software; you can redistribute it and/or
Chris@1256:     modify it under the terms of the GNU General Public License as
Chris@1256:     published by the Free Software Foundation; either version 2 of the
Chris@1256:     License, or (at your option) any later version.  See the file
Chris@1256:     COPYING included with this distribution for more information.
Chris@1256: */
Chris@1256: 
Chris@1256: #ifndef HIT_COUNT_H
Chris@1256: #define HIT_COUNT_H
Chris@1256: 
Chris@1256: #include <string>
Chris@1256: #include <iostream>
Chris@1256: 
Chris@1256: /**
Chris@1256:  * Profile class for counting cache hits and the like.
Chris@1256:  */
Chris@1570: #ifndef NO_HIT_COUNTS
Chris@1570: 
Chris@1256: class HitCount
Chris@1256: {
Chris@1256: public:
Chris@1256:     HitCount(std::string name) :
Chris@1429:         m_name(name),
Chris@1429:         m_hit(0),
Chris@1429:         m_partial(0),
Chris@1429:         m_miss(0)
Chris@1256:     { }
Chris@1256:     
Chris@1256:     ~HitCount() {
Chris@1429:         using namespace std;
Chris@1429:         int total = m_hit + m_partial + m_miss;
Chris@1429:         cerr << "Hit count: " << m_name << ": ";
Chris@1429:         if (m_partial > 0) {
Chris@1429:             cerr << m_hit << " hits, " << m_partial << " partial, "
Chris@1429:                  << m_miss << " misses";
Chris@1429:         } else {
Chris@1429:             cerr << m_hit << " hits, " << m_miss << " misses";
Chris@1429:         }
Chris@1429:         if (total > 0) {
Chris@1429:             if (m_partial > 0) {
Chris@1429:                 cerr << " (" << ((m_hit * 100.0) / total) << "%, "
Chris@1429:                      << ((m_partial * 100.0) / total) << "%, "
Chris@1429:                      << ((m_miss * 100.0) / total) << "%)";
Chris@1429:             } else {
Chris@1429:                 cerr << " (" << ((m_hit * 100.0) / total) << "%, "
Chris@1429:                      << ((m_miss * 100.0) / total) << "%)";
Chris@1429:             }
Chris@1429:         }
Chris@1429:         cerr << endl;
Chris@1256:     }
Chris@1256: 
Chris@1256:     void hit() { ++m_hit; }
Chris@1256:     void partial() { ++m_partial; }
Chris@1256:     void miss() { ++m_miss; }
Chris@1256: 
Chris@1256: private:
Chris@1256:     std::string m_name;
Chris@1256:     int m_hit;
Chris@1256:     int m_partial;
Chris@1256:     int m_miss;
Chris@1256: };
Chris@1256: 
Chris@1570: #else // NO_HIT_COUNTS
Chris@1570: 
Chris@1570: class HitCount
Chris@1570: {
Chris@1570: public:
Chris@1570:     HitCount(std::string) {}
Chris@1570: 
Chris@1570:     void hit() {}
Chris@1570:     void partial() {}
Chris@1570:     void miss() {}
Chris@1570: };
Chris@1570: 
Chris@1256: #endif
Chris@1570: 
Chris@1570: #endif