counter.h

Go to the documentation of this file.
00001 /* -*- C++ -*-
00002  *
00003  * This file is a part of LEMON, a generic C++ optimization library
00004  *
00005  * Copyright (C) 2003-2006
00006  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
00007  * (Egervary Research Group on Combinatorial Optimization, EGRES).
00008  *
00009  * Permission to use, modify and distribute this software is granted
00010  * provided that this copyright notice appears in all copies. For
00011  * precise terms see the accompanying LICENSE file.
00012  *
00013  * This software is provided "AS IS" with no warranty of any kind,
00014  * express or implied, and with no claim as to its suitability for any
00015  * purpose.
00016  *
00017  */
00018 
00019 #ifndef LEMON_COUNTER_H
00020 #define LEMON_COUNTER_H
00021 
00022 #include <string>
00023 #include <iostream>
00024 
00028 
00029 namespace lemon 
00030 {
00031 
00032   template<class P> class _SubNoCounter;
00033 
00034   template<class P>
00035   class _SubCounter 
00036   {
00037     P &_parent;
00038     std::string _title;
00039     std::ostream &_os;
00040     int count;
00041   public:
00042 
00043     typedef _SubCounter<_SubCounter<P> > SubCounter;
00044     typedef _SubNoCounter<_SubCounter<P> > SubNoCounter;
00045 
00046     _SubCounter(P &parent)
00047       : _parent(parent), _title(), _os(std::cerr), count(0) {}
00048     _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
00049       : _parent(parent), _title(title), _os(os), count(0) {}
00050     _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
00051       : _parent(parent), _title(title), _os(os), count(0) {}
00052     ~_SubCounter() { 
00053       _os << _title << count <<std::endl;
00054       _parent+=count;
00055     }
00056     _SubCounter &operator++() { count++; return *this;}
00057     int operator++(int) { return count++; }
00058     _SubCounter &operator--() { count--; return *this;}
00059     int operator--(int) { return count--; }
00060     _SubCounter &operator+=(int c) { count+=c; return *this;}
00061     _SubCounter &operator-=(int c) { count-=c; return *this;}
00062     void reset(int c=0) {count=c;}
00063     operator int() {return count;}
00064   };
00065 
00066   template<class P>
00067   class _SubNoCounter 
00068   {
00069     P &_parent;
00070   public:
00071     typedef _SubNoCounter<_SubNoCounter<P> > SubCounter;
00072     typedef _SubNoCounter<_SubNoCounter<P> > SubNoCounter;
00073   
00074     _SubNoCounter(P &parent) :_parent(parent) {}
00075     _SubNoCounter(P &parent,std::string,std::ostream &) 
00076       :_parent(parent) {}
00077     _SubNoCounter(P &parent,std::string) 
00078       :_parent(parent) {}
00079     _SubNoCounter(P &parent,const char *,std::ostream &)
00080       :_parent(parent) {}
00081     _SubNoCounter(P &parent,const char *)
00082       :_parent(parent) {}
00083     ~_SubNoCounter() {}
00084     _SubNoCounter &operator++() { ++_parent; return *this;}
00085     int operator++(int) { _parent++; return 0;}
00086     _SubNoCounter &operator--() { --_parent; return *this;}
00087     int operator--(int) { _parent--; return 0;}
00088     _SubNoCounter &operator+=(int c) { _parent+=c; return *this;}
00089     _SubNoCounter &operator-=(int c) { _parent-=c; return *this;}
00090     void reset(int) {}
00091     void reset() {}
00092     operator int() {return 0;}
00093   };
00094 
00095 
00098 
00100 
00105   class Counter 
00106   {
00107     std::string _title;
00108     std::ostream &_os;
00109     int count;
00110   public:
00112 
00115     typedef _SubCounter<Counter> SubCounter;
00117 
00120     typedef _SubNoCounter<Counter> SubNoCounter;
00121 
00123     Counter() : _title(), _os(std::cerr), count(0) {}
00125     Counter(std::string title,std::ostream &os=std::cerr) 
00126       : _title(title), _os(os), count(0) {}
00128     Counter(const char *title,std::ostream &os=std::cerr)
00129       : _title(title), _os(os), count(0) {}
00131     ~Counter() {
00132       _os << _title << count <<std::endl;
00133     }
00135     Counter &operator++() { count++; return *this;}
00137     int operator++(int) { return count++;}
00139     Counter &operator--() { count--; return *this;}
00141     int operator--(int) { return count--;}
00143     Counter &operator+=(int c) { count+=c; return *this;}
00145     Counter &operator-=(int c) { count-=c; return *this;}
00147     void reset(int c=0) {count=c;}
00149     operator int() {return count;}
00150   };
00151 
00153 
00156   class NoCounter
00157   {
00158   public:
00159     typedef _SubNoCounter<NoCounter> SubCounter;
00160     typedef _SubNoCounter<NoCounter> SubNoCounter;
00161 
00162     NoCounter() {}
00163     NoCounter(std::string,std::ostream &) {}
00164     NoCounter(const char *,std::ostream &) {}
00165     NoCounter(std::string) {}
00166     NoCounter(const char *) {}
00167     NoCounter &operator++() { return *this; }
00168     int operator++(int) { return 0; }
00169     NoCounter &operator--() { return *this; }
00170     int operator--(int) { return 0; }
00171     NoCounter &operator+=(int) { return *this;}
00172     NoCounter &operator-=(int) { return *this;}
00173     void reset(int) {}
00174     void reset() {}
00175     operator int() {return 0;}
00176   };
00177 
00179 }
00180 
00181 #endif

Generated on Fri Feb 3 18:36:02 2006 for LEMON by  doxygen 1.4.6