# eoCounter.h

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 //-----------------------------------------------------------------------------
    00004 // eoCounter.h
    00005 // (c) Maarten Keijzer 2000
    00006 /*
    00007     This library is free software; you can redistribute it and/or
    00008     modify it under the terms of the GNU Lesser General Public
    00009     License as published by the Free Software Foundation; either
    00010     version 2 of the License, or (at your option) any later version.
    00011
    00012     This library is distributed in the hope that it will be useful,
    00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015     Lesser General Public License for more details.
    00016
    00017     You should have received a copy of the GNU Lesser General Public
    00018     License along with this library; if not, write to the Free Software
    00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00020
    00021     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
    00022              mak@dhi.dk
    00023  */
    00024 //-----------------------------------------------------------------------------
    00025
    00026 #ifndef _eoCounter_h
    00027 #define _eoCounter_h
    00028
    00029 #include <eoFunctor.h>
    00030 #include <eoFunctorStore.h>
    00031 #include <utils/eoParam.h>
    00032
    00040 template <class Procedure>
    00041 class eoProcedureCounter : public Procedure, public eoValueParam<unsigned long>
    00042 {
    00043     public:
    00044
    00045         eoProcedureCounter(Procedure& _proc, std::string _name = "proc_counter")
    00046             : eoValueParam<unsigned long>(0, _name), proc(_proc) {}
    00047
    00064         typename Procedure::result_type operator()(void)
    00065         {
    00066             value()++;
    00067 #ifdef _MSC_VER
    00068             proc();
    00069 #else
    00070             return proc();
    00071 #endif
    00072         }
    00073
    00074     private :
    00075
    00076         Procedure& proc;
    00077 };
    00078
    00094 template <class UnaryFunctor>
    00095 class eoUnaryFunctorCounter : public UnaryFunctor, public eoValueParam<unsigned long>
    00096 {
    00097     public:
    00098         eoUnaryFunctorCounter(UnaryFunctor& _func, std::string _name = "uf_counter")
    00099             : eoValueParam<unsigned long>(0, _name), func(_func) {}
    00100
    00117         typename UnaryFunctor::result_type operator()
    00118             (typename UnaryFunctor::first_argument_type _arg1)
    00119         {
    00120             value()++;
    00121 #ifdef _MSC_VER
    00122             func(_arg1);
    00123 #else
    00124             return func(_arg1);
    00125 #endif
    00126         }
    00127
    00128     private :
    00129
    00130         UnaryFunctor& func;
    00131 };
    00132
    00141 template <class BinaryFunctor>
    00142 class eoBinaryFunctorCounter : public BinaryFunctor, public eoValueParam<unsigned long>
    00143 {
    00144     public:
    00145         eoBinaryFunctorCounter(BinaryFunctor& _func, std::string _name = "proc_counter")
    00146             : eoValueParam<unsigned long>(0, _name), func(_func) {}
    00147
    00165         typename BinaryFunctor::result_type operator()
    00166             (typename BinaryFunctor::first_argument_type _arg1,
    00167              typename BinaryFunctor::second_argument_type _arg2)
    00168         {
    00169             value()++;
    00170 #ifdef _MSC_VER
    00171             func(_arg1, _arg2);
    00172 #else
    00173             return func(_arg1, _arg2);
    00174 #endif
    00175   }
    00176
    00177     private :
    00178
    00179         BinaryFunctor& func;
    00180 };
    00181
    00195 template <class Procedure>
    00196 eoProcedureCounter<Procedure>& make_counter(eoFunctorBase::procedure_tag, Procedure& _proc, eoFunctorStore& store, std::string _name = "proc_counter")
    00197 {
    00198     eoProcedureCounter<Procedure>* result = new eoProcedureCounter<Procedure>(_proc, _name);
    00199     store.storeFunctor(result);
    00200     return *result;
    00201 }
    00202
    00203 template <class UnaryFunctor>
    00204 eoUnaryFunctorCounter<UnaryFunctor>& make_counter(eoFunctorBase::unary_function_tag, UnaryFunctor& _proc, eoFunctorStore& store, std::string _name = "uf_counter")
    00205 {
    00206     eoUnaryFunctorCounter<UnaryFunctor>* result = new eoUnaryFunctorCounter<UnaryFunctor>(_proc, _name);
    00207     store.storeFunctor(result);
    00208     return *result;
    00209 }
    00210
    00211 template <class BinaryFunctor>
    00212 eoBinaryFunctorCounter<BinaryFunctor>& make_counter(eoFunctorBase::binary_function_tag, BinaryFunctor& _proc, eoFunctorStore& store, std::string _name = "uf_counter")
    00213 {
    00214     eoBinaryFunctorCounter<BinaryFunctor>* result = new eoBinaryFunctorCounter<BinaryFunctor>(_proc, _name);
    00215     store.storeFunctor(result);
    00216     return *result;
    00217 }
    00218
    00219 #endif