00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #ifndef _CVC_lite__statistics_h
00031 #define _CVC_lite__statistics_h
00032 
00033 #include <string>
00034 #include <iostream>
00035 #include <sstream>
00036 #include <map>
00037 
00038 namespace CVCL {
00039 
00040   class Statistics; 
00041 
00042   
00043   
00044 
00045   
00046   class StatFlag {
00047   private:
00048     bool* d_flag; 
00049   public:
00050     
00051     
00052     StatFlag(bool& flag) : d_flag(&flag) { }
00053     
00054     ~StatFlag() { }
00055     
00056     operator bool() { return *d_flag; }
00057 
00058     
00059     
00060     bool operator--() { *d_flag = false; return false; }
00061     bool operator++() { *d_flag = true; return true; }
00062     
00063     bool operator--(int) { bool x=*d_flag; *d_flag=false; return x; }
00064     bool operator++(int) { bool x=*d_flag; *d_flag=true; return x; }
00065     
00066     StatFlag& operator=(bool x) { *d_flag=(x!=false); return *this; }
00067     
00068     friend bool operator==(const StatFlag& f1, const StatFlag& f2);
00069     friend bool operator!=(const StatFlag& f1, const StatFlag& f2);
00070     
00071     friend std::ostream& operator<<(std::ostream& os, const StatFlag& f);
00072   }; 
00073 
00074   inline bool operator==(const StatFlag& f1, const StatFlag& f2) {
00075     return (*f1.d_flag) == (*f2.d_flag);
00076   }
00077   inline bool operator!=(const StatFlag& f1, const StatFlag& f2) {
00078     return (*f1.d_flag) != (*f2.d_flag);
00079   }
00080   inline std::ostream& operator<<(std::ostream& os, const StatFlag& f) {
00081     if(*f.d_flag) return(os << "true");
00082     else return(os << "false");
00083   }
00084 
00085   
00086   
00087   
00088   class StatCounter {
00089   private:
00090     int* d_counter; 
00091   public:
00092     
00093     
00094     StatCounter(int& c) : d_counter(&c) { }
00095     
00096     ~StatCounter() { }
00097     
00098     
00099 
00100     operator int() { return *d_counter; }
00101 
00102     
00103     
00104     int operator--() { return --(*d_counter); }
00105     int operator++() { return ++(*d_counter); }
00106     
00107     int operator--(int) { return (*d_counter)--; }
00108     int operator++(int) { return (*d_counter)++; }
00109     
00110     StatCounter& operator=(int x) { *d_counter=x; return *this; }
00111     StatCounter& operator+=(int x) { *d_counter+=x; return *this; }
00112     StatCounter& operator-=(int x) { *d_counter-=x; return *this; }
00113     StatCounter& operator=(const StatCounter& x)
00114       { *d_counter=*x.d_counter; return *this; }
00115     StatCounter& operator-=(const StatCounter& x)
00116       { *d_counter-=*x.d_counter; return *this; }
00117     StatCounter& operator+=(const StatCounter& x)
00118       { *d_counter+=*x.d_counter; return *this; }
00119     
00120     friend bool operator==(const StatCounter& c1, const StatCounter& c2);
00121     friend bool operator!=(const StatCounter& c1, const StatCounter& c2);
00122     friend bool operator==(int c1, const StatCounter& c2);
00123     friend bool operator!=(int c1, const StatCounter& c2);
00124     friend bool operator==(const StatCounter& c1, int c2);
00125     friend bool operator!=(const StatCounter& c1, int c2);
00126     
00127     friend std::ostream& operator<<(std::ostream& os, const StatCounter& f);
00128   }; 
00129 
00130   inline bool operator==(const StatCounter& c1, const StatCounter& c2) {
00131     return (*c1.d_counter) == (*c2.d_counter);
00132   }
00133   inline bool operator!=(const StatCounter& c1, const StatCounter& c2) {
00134     return (*c1.d_counter) != (*c2.d_counter);
00135   }
00136   inline bool operator==(int c1, const StatCounter& c2) {
00137     return c1 == (*c2.d_counter);
00138   }
00139   inline bool operator!=(int c1, const StatCounter& c2) {
00140     return c1 != (*c2.d_counter);
00141   }
00142   inline bool operator==(const StatCounter& c1, int c2) {
00143     return (*c1.d_counter) == c2;
00144   }
00145   inline bool operator!=(const StatCounter& c1, int c2) {
00146     return (*c1.d_counter) != c2;
00147   }
00148   inline std::ostream& operator<<(std::ostream& os, const StatCounter& c) {
00149     return (os << *c.d_counter);
00150   }
00151 
00152   
00153 
00154   class Statistics {
00155   private:
00156     
00157     std::ostream* d_os;
00158     typedef std::map<std::string, bool> StatFlagMap;
00159     typedef std::map<std::string, int> StatCounterMap;
00160     StatFlagMap d_flags;
00161     StatCounterMap d_counters;
00162   public:
00163     
00164     Statistics() { }
00165     
00166     ~Statistics() { }
00167     
00168     
00169     StatFlag flag(const std::string& name)
00170       { return StatFlag(d_flags[name]); }
00171     StatCounter counter(const std::string& name)
00172       { return StatCounter(d_counters[name]); }
00173 
00174     
00175     std::ostream& printAll(std::ostream& os) const;
00176     friend std::ostream& operator<<(std::ostream& os,
00177                                     const Statistics& stats) {
00178       return stats.printAll(os);
00179     }
00180   }; 
00181 
00182 } 
00183 
00184 #endif