EvolvingObjects
|
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 00042 template <class Procedure> 00043 class eoProcedureCounter : public Procedure, public eoValueParam<unsigned long> 00044 { 00045 public: 00046 00047 eoProcedureCounter(Procedure& _proc, std::string _name = "proc_counter") 00048 : eoValueParam<unsigned long>(0, _name), proc(_proc) {} 00049 00066 typename Procedure::result_type operator()(void) 00067 { 00068 value()++; 00069 #ifdef _MSC_VER 00070 proc(); 00071 #else 00072 return proc(); 00073 #endif 00074 } 00075 00076 private : 00077 00078 Procedure& proc; 00079 }; 00080 00096 template <class UnaryFunctor> 00097 class eoUnaryFunctorCounter : public UnaryFunctor, public eoValueParam<unsigned long> 00098 { 00099 public: 00100 eoUnaryFunctorCounter(UnaryFunctor& _func, std::string _name = "uf_counter") 00101 : eoValueParam<unsigned long>(0, _name), func(_func) {} 00102 00119 typename UnaryFunctor::result_type operator() 00120 (typename UnaryFunctor::first_argument_type _arg1) 00121 { 00122 value()++; 00123 #ifdef _MSC_VER 00124 func(_arg1); 00125 #else 00126 return func(_arg1); 00127 #endif 00128 } 00129 00130 private : 00131 00132 UnaryFunctor& func; 00133 }; 00134 00143 template <class BinaryFunctor> 00144 class eoBinaryFunctorCounter : public BinaryFunctor, public eoValueParam<unsigned long> 00145 { 00146 public: 00147 eoBinaryFunctorCounter(BinaryFunctor& _func, std::string _name = "proc_counter") 00148 : eoValueParam<unsigned long>(0, _name), func(_func) {} 00149 00167 typename BinaryFunctor::result_type operator() 00168 (typename BinaryFunctor::first_argument_type _arg1, 00169 typename BinaryFunctor::second_argument_type _arg2) 00170 { 00171 value()++; 00172 #ifdef _MSC_VER 00173 func(_arg1, _arg2); 00174 #else 00175 return func(_arg1, _arg2); 00176 #endif 00177 } 00178 00179 private : 00180 00181 BinaryFunctor& func; 00182 }; 00183 00197 template <class Procedure> 00198 eoProcedureCounter<Procedure>& make_counter(eoFunctorBase::procedure_tag, Procedure& _proc, eoFunctorStore& store, std::string _name = "proc_counter") 00199 { 00200 eoProcedureCounter<Procedure>* result = new eoProcedureCounter<Procedure>(_proc, _name); 00201 store.storeFunctor(result); 00202 return *result; 00203 } 00204 00205 template <class UnaryFunctor> 00206 eoUnaryFunctorCounter<UnaryFunctor>& make_counter(eoFunctorBase::unary_function_tag, UnaryFunctor& _proc, eoFunctorStore& store, std::string _name = "uf_counter") 00207 { 00208 eoUnaryFunctorCounter<UnaryFunctor>* result = new eoUnaryFunctorCounter<UnaryFunctor>(_proc, _name); 00209 store.storeFunctor(result); 00210 return *result; 00211 } 00212 00213 template <class BinaryFunctor> 00214 eoBinaryFunctorCounter<BinaryFunctor>& make_counter(eoFunctorBase::binary_function_tag, BinaryFunctor& _proc, eoFunctorStore& store, std::string _name = "uf_counter") 00215 { 00216 eoBinaryFunctorCounter<BinaryFunctor>* result = new eoBinaryFunctorCounter<BinaryFunctor>(_proc, _name); 00217 store.storeFunctor(result); 00218 return *result; 00219 } 00220 00221 #endif