timer.h

Go to the documentation of this file.
00001 #ifndef  __Timer_h
00002 #define  __Timer_h
00003 
00004 #include  <map>
00005 #include  <iostream>
00006 
00007 #include  "stopwatch.h"
00008 #include  "gascoigne.h"
00009 #include  <string>
00010 #include  <cassert>
00011 
00012 /*-----------------------------------------*/
00013 
00014 namespace Gascoigne
00015 {
00016 class Timer
00017 {
00018 protected:
00019 
00020   typedef std::map<std::string,StopWatch>::const_iterator  const_iterator;
00021   typedef std::map<std::string,StopWatch>::iterator        iterator;
00022 
00023   std::map<std::string,StopWatch>  mstpw;
00024 
00025 public:
00026  
00027   Timer();
00028   Timer(const Timer& T)
00029     {
00030       const_iterator p = T.begin();
00031       for(;p!=T.end();p++)
00032         {
00033           mstpw.insert(*p);
00034         }
00035     }
00036 
00037   void init(const Timer& T)
00038     {
00039       assert(0);
00040       for(const_iterator p = T.begin(); p!= T.end();p++)      add(p->first);
00041     }
00042 
00043   void add(const Timer& T)
00044     {
00045       for(iterator p = mstpw.begin(); p!= mstpw.end();p++)
00046         {
00047           double s = T.read(p->first);
00048           p->second.add(s);
00049         }
00050     }
00051   
00052   void add(const std::string& s) { mstpw.insert(std::make_pair(s,StopWatch() ));}
00053 
00054   const StopWatch& Get(const std::string& s) const
00055     {
00056       const_iterator p =  mstpw.find(s);
00057       if(p==mstpw.end()) {std::cerr << "no such StopWatch: "<<s<<std::endl; abort();}
00058       return p->second;
00059     }
00060 
00061   const_iterator begin() const {return mstpw.begin();}
00062   const_iterator end  () const {return mstpw.end  ();}
00063   void start(const std::string& s)
00064     {
00065       iterator p = mstpw.find(s);
00066       if(p==mstpw.end())
00067         {
00068           std::cerr << "Timer::start()\n";
00069           std::cerr << "no such StopWatch: " << s << std::endl; abort();
00070         }
00071       p->second.start();
00072     }
00073   void stop(const std::string& s)
00074     {
00075       iterator p = mstpw.find(s);
00076       if(p==mstpw.end())
00077         {
00078           std::cerr << "Timer::stop()\n";
00079           std::cerr << "no such StopWatch: " << s << std::endl; abort();
00080         }
00081       p->second.stop();
00082     }
00083   double read(const std::string& s) const
00084     {
00085       const_iterator p = mstpw.find(s);
00086       if(p==mstpw.end())
00087         {
00088           std::cerr << "Timer::read()\n";
00089           std::cerr << "no such StopWatch: " << s << std::endl; abort();
00090         }
00091       return p->second.read();
00092     }
00093   StopWatch total() const 
00094     {
00095       StopWatch s;
00096       const_iterator p = mstpw.begin();
00097       while(p!=mstpw.end()) {s.add(p++->second.read());}
00098       return s;
00099     }
00100   friend std::ostream& operator<<(std::ostream& os, const Timer& T);
00101 };
00102 }
00103 
00104 
00105 #endif

Generated on Thu Sep 14 10:34:35 2006 for Gascoigne by  doxygen 1.4.7