File: eoCounter.h

package info (click to toggle)
gamera 1%3A3.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 15,912 kB
  • sloc: xml: 122,324; cpp: 50,730; python: 35,044; ansic: 258; makefile: 114; sh: 101
file content (221 lines) | stat: -rw-r--r-- 8,303 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-

//-----------------------------------------------------------------------------
// eoCounter.h
// (c) Maarten Keijzer 2000
/*
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Contact: todos@geneura.ugr.es, http://geneura.ugr.es
             mak@dhi.dk
 */
//-----------------------------------------------------------------------------

#ifndef _eoCounter_h
#define _eoCounter_h

#include <eoFunctor.h>
#include <eoFunctorStore.h>
#include <utils/eoParam.h>

/**
        Generic counter class that counts the number of times
        a procedure is used. Add a procedure through its ctor and
        use this class instead of it.

    It is derived from eoValueParam so you can add it to a monitor.

    @ingroup Utilities
*/
template <class Procedure>
class eoProcedureCounter : public Procedure, public eoValueParam<unsigned long>
{
    public:

        eoProcedureCounter(Procedure& _proc, std::string _name = "proc_counter")
            : eoValueParam<unsigned long>(0, _name), proc(_proc) {}

        /** Calls the embedded function and increments the counter

          Note for MSVC users, if this code does not compile, you are quite
          likely trying to count a function that has a non-void return type.
          Don't look at us, look at the MSVC builders. Code like "return void;"
          is perfectly legal according to the ANSI standard, but the guys at
          Microsoft didn't get to implementing it yet.

          We had two choices: assuming (and compiling ) code that returns void or code that returns non-void.
          Given that in EO most functors return void, it was chosen to support void.

          But also please let me know if you have a compiler that defines _MSC_VER (lot's of windows compilers do), but is quite
          capable of compiling return void; type of code. We'll try to change the signature then.

          You happy GNU (and other compiler) users will not have a problem with this..
        */
        typename Procedure::result_type operator()(void)
        {
            value()++;
#ifdef _MSC_VER
            proc();
#else
            return proc();
#endif
        }

    private :

        Procedure& proc;
};

/**
        Generic counter class that counts the number of times
        a unary function is used. Add a unary function through its ctor and
        use this class instead of it.

    It is derived from eoValueParam so you can add it to a monitor.

    Example: suppose you have an eoEvalFunc called myeval, to count the
    number of evaluations, just define:

  eoUnaryFunctorCounter<void, EoType> evalCounter(myeval);

  and use evalCounter now instead of myeval.
*/

template <class UnaryFunctor>
class eoUnaryFunctorCounter : public UnaryFunctor, public eoValueParam<unsigned long>
{
    public:
        eoUnaryFunctorCounter(UnaryFunctor& _func, std::string _name = "uf_counter")
            : eoValueParam<unsigned long>(0, _name), func(_func) {}

        /** Calls the embedded function and increments the counter

          Note for MSVC users, if this code does not compile, you are quite
          likely trying to count a function that has a non-void return type.
          Don't look at us, look at the MSVC builders. Code like "return void;"
          is perfectly legal according to the ANSI standard, but the guys at
          Microsoft didn't get to implementing it yet.

          We had two choices: assuming (and compiling ) code that returns void or code that returns non-void.
          Given that in EO most functors return void, it was chosen to support void.

          But also please let me know if you have a compiler that defines _MSC_VER (lot's of windows compilers do), but is quite
          capable of compiling return void; type of code. We'll try to change the signature then.

          You happy GNU (and other compiler) users will not have a problem with this.
        */
        typename UnaryFunctor::result_type operator()
            (typename UnaryFunctor::first_argument_type _arg1)
        {
            value()++;
#ifdef _MSC_VER
            func(_arg1);
#else
            return func(_arg1);
#endif
        }

    private :

        UnaryFunctor& func;
};

/**
        Generic counter class that counts the number of times
        a binary function is used. Add a binary function through its ctor and
        use this class instead of it.

    It is derived from eoValueParam so you can add it to a monitor.

*/
template <class BinaryFunctor>
class eoBinaryFunctorCounter : public BinaryFunctor, public eoValueParam<unsigned long>
{
    public:
        eoBinaryFunctorCounter(BinaryFunctor& _func, std::string _name = "proc_counter")
            : eoValueParam<unsigned long>(0, _name), func(_func) {}

        /** Calls the embedded function and increments the counter

          Note for MSVC users, if this code does not compile, you are quite
          likely trying to count a function that has a non-void return type.
          Don't look at us, look at the MSVC builders. Code like "return void;"
          is perfectly legal according to the ANSI standard, but the guys at
          Microsoft didn't get to implementing it yet.

          We had two choices: assuming (and compiling ) code that returns void or code that returns non-void.
          Given that in EO most functors return void, it was chosen to support void.


          But also please let me know if you have a compiler that defines _MSC_VER (lot's of windows compilers do), but is quite
          capable of compiling return void; type of code. We'll try to change the signature then.

          You happy GNU (and other compiler) users will not have a problem with this.
        */
        typename BinaryFunctor::result_type operator()
            (typename BinaryFunctor::first_argument_type _arg1,
             typename BinaryFunctor::second_argument_type _arg2)
        {
            value()++;
#ifdef _MSC_VER
            func(_arg1, _arg2);
#else
            return func(_arg1, _arg2);
#endif
  }

    private :

        BinaryFunctor& func;
};

/** make_counter: overloaded function to make a counter out of a function

    how it works...

    by using the xxx_function_tag structure defined in eoFunctionBase, you
    can easily create a counter from a general class (say eoEval<EOT>), by
    simply stating:

    eoEval<EOT>& myCounted = make_counter(functor_category(myEval), myEval, store)

    @see eoFunctorBase, functor_category, eoFunctorStore

*/
template <class Procedure>
eoProcedureCounter<Procedure>& make_counter(eoFunctorBase::procedure_tag, Procedure& _proc, eoFunctorStore& store, std::string _name = "proc_counter")
{
    eoProcedureCounter<Procedure>* result = new eoProcedureCounter<Procedure>(_proc, _name);
    store.storeFunctor(result);
    return *result;
}

template <class UnaryFunctor>
eoUnaryFunctorCounter<UnaryFunctor>& make_counter(eoFunctorBase::unary_function_tag, UnaryFunctor& _proc, eoFunctorStore& store, std::string _name = "uf_counter")
{
    eoUnaryFunctorCounter<UnaryFunctor>* result = new eoUnaryFunctorCounter<UnaryFunctor>(_proc, _name);
    store.storeFunctor(result);
    return *result;
}

template <class BinaryFunctor>
eoBinaryFunctorCounter<BinaryFunctor>& make_counter(eoFunctorBase::binary_function_tag, BinaryFunctor& _proc, eoFunctorStore& store, std::string _name = "uf_counter")
{
    eoBinaryFunctorCounter<BinaryFunctor>* result = new eoBinaryFunctorCounter<BinaryFunctor>(_proc, _name);
    store.storeFunctor(result);
    return *result;
}

#endif