count.hh 3.22 KB
Newer Older
1
// Copyright (C) 2007, 2008 EPITA Research and Development Laboratory (LRDE)
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
//
// This file is part of the Olena Library.  This library is free
// software; you can redistribute it and/or modify it under the terms
// of the GNU General Public License version 2 as published by the
// Free Software Foundation.
//
// 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
// Boston, MA 02111-1307, USA.
//
// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

Thierry Geraud's avatar
Thierry Geraud committed
28
29
#ifndef MLN_ACCU_COUNT_HH
# define MLN_ACCU_COUNT_HH
30

31
32
/// \file mln/accu/count.hh
/// \brief Define an accumulator that counts.
33

34
# include <mln/accu/internal/base.hh>
35
# include <mln/core/concept/meta_accumulator.hh>
36

37
38
39
namespace mln
{

40
  namespace accu
41
42
  {

43
44
    /// \brief Generic counter accumulator class.
    /// The parameter \a T is the type to be count.
45
46
    template <typename T>
    struct count_ : public mln::accu::internal::base_< std::size_t , count_<T> >
47
    {
48
      typedef T argument;
49

50
      count_();
51

52
53
      /// Manipulators.
      /// \{
54
      void init();
55
      void take(const argument&);
56
      void take(const count_<T>& other);
57

58
      /// Force the value of the counter to \a c. 
59
      void set_value(std::size_t c);
60
      /// \}
Thierry Geraud's avatar
Thierry Geraud committed
61

62
63
      /// Get the value of the accumulator.
      std::size_t to_result() const;
64

65
66
    protected:
      /// The value of the counter.
67
      std::size_t count__;
68
    };
69
70


71
    /// \brief Meta accumulator for count.
72
73
    struct count : public Meta_Accumulator< count >
    {
74
      template <typename T>
75
76
      struct with
      {
77
	typedef count_<T> ret;
78
79
80
      };
    };

81
82
83

# ifndef MLN_INCLUDE_ONLY

84
    template <typename T>
85
    inline
86
    count_<T>::count_()
87
88
89
90
    {
      init();
    }

91
    template <typename T>
92
    inline
93
    void
94
    count_<T>::init()
95
    {
96
      count__ = 0;
97
98
    }

99
    template <typename T>
100
    inline
101
    void
102
    count_<T>::take(const argument&)
103
    {
104
      ++count__;
105
106
    }

107
    template <typename T>
108
    inline
109
    void
110
    count_<T>::take(const count_<T>& other)
111
    {
112
      count__ += other.count__;
113
114
    }

115
    template <typename T>
116
    inline
117
    std::size_t
118
    count_<T>::to_result() const
119
    {
120
      return count__;
121
122
    }

123
    template <typename T>
124
    inline
125
    void
126
    count_<T>::set_value(std::size_t c)
127
    {
128
      count__ = c;
129
130
    }

131
132
# endif // ! MLN_INCLUDE_ONLY

133
  } // end of namespace mln::accu
134
135
136
137

} // end of namespace mln


Thierry Geraud's avatar
Thierry Geraud committed
138
#endif // ! MLN_ACCU_COUNT_HH