count.hh 3.5 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
    template <typename T>
46
    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
67
68
      /// Check whether this accu is able to return a result.
      /// Always true here.
      bool is_valid() const;

69
70
    protected:
      /// The value of the counter.
71
      std::size_t count__;
72
    };
73

74
    namespace meta
75
    {
76
77
78

      /// \brief Meta accumulator for count.
      struct count : public Meta_Accumulator< count >
79
      {
80
81
82
83
84
	template <typename T>
	struct with
	{
	  typedef count_<T> ret;
	};
85
      };
86
87

    } // end of namespace mln::accu::meta
88

89
90
91

# ifndef MLN_INCLUDE_ONLY

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

99
    template <typename T>
100
    inline
101
    void
102
    count_<T>::init()
103
    {
104
      count__ = 0;
105
106
    }

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

115
    template <typename T>
116
    inline
117
    void
118
    count_<T>::take(const count_<T>& other)
119
    {
120
      count__ += other.count__;
121
122
    }

123
    template <typename T>
124
    inline
125
    std::size_t
126
    count_<T>::to_result() const
127
    {
128
      return count__;
129
130
    }

131
    template <typename T>
132
    inline
133
    void
134
    count_<T>::set_value(std::size_t c)
135
    {
136
      count__ = c;
137
138
    }

139
140
141
142
143
144
145
146
    template <typename T>
    inline
    bool
    count_<T>::is_valid() const
    {
      return true;
    }

147
148
# endif // ! MLN_INCLUDE_ONLY

149
  } // end of namespace mln::accu
150
151
152
153

} // end of namespace mln


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