$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
count.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_ACCU_MATH_COUNT_HH
27 # define MLN_ACCU_MATH_COUNT_HH
28 
34 
35 # include <mln/accu/internal/base.hh>
36 # include <mln/core/concept/meta_accumulator.hh>
37 
38 
39 namespace mln
40 {
41 
42  // Forward declaration.
43  namespace accu {
44  namespace math {
45  template <typename T> struct count;
46  }
47  }
48 
49 
50  // Traits.
51 
52  namespace trait
53  {
54 
55  template <typename T>
56  struct accumulator_< accu::math::count<T> >
57  {
58  typedef accumulator::has_untake::yes has_untake;
59  typedef accumulator::has_set_value::yes has_set_value;
60  typedef accumulator::has_stop::no has_stop;
61  typedef accumulator::when_pix::use_none when_pix;
62  };
63 
64  } // end of namespace mln::trait
65 
66 
67  namespace accu
68  {
69 
70  namespace meta
71  {
72 
73  namespace math
74  {
75 
77  struct count : public Meta_Accumulator< count >
78  {
79  template <typename T>
80  struct with
81  {
83  };
84  };
85 
86  } // end of namespace mln::accu::meta::math
87 
88  } // end of namespace mln::accu::meta
89 
90 
91  namespace math
92  {
93 
98  //
99  template <typename T>
100  struct count : public mln::accu::internal::base< unsigned , count<T> >
101  {
102  typedef T argument;
103 
104  count();
105 
108  void init();
109  void take(const argument&);
110  void take(const count<T>& other);
111 
112  void untake(const argument&);
113  void untake(const count<T>& other);
114 
116  void set_value(unsigned c);
118 
120  unsigned to_result() const;
121 
124  bool is_valid() const;
125 
126  protected:
128  unsigned count_;
129  };
130 
131 
132 
133 # ifndef MLN_INCLUDE_ONLY
134 
135  template <typename T>
136  inline
138  {
139  init();
140  }
141 
142  template <typename T>
143  inline
144  void
146  {
147  count_ = 0;
148  }
149 
150  template <typename T>
151  inline
152  void
153  count<T>::take(const argument&)
154  {
155  ++count_;
156  }
157 
158  template <typename T>
159  inline
160  void
161  count<T>::untake(const argument&)
162  {
163  mln_precondition(count_ > 0);
164  --count_;
165  }
166 
167  template <typename T>
168  inline
169  void
170  count<T>::take(const count<T>& other)
171  {
172  count_ += other.count_;
173  }
174 
175  template <typename T>
176  inline
177  void
178  count<T>::untake(const count<T>& other)
179  {
180  mln_precondition(other.count_ <= count_);
181  count_ -= other.count_;
182  }
183 
184  template <typename T>
185  inline
186  unsigned
187  count<T>::to_result() const
188  {
189  return count_;
190  }
191 
192  template <typename T>
193  inline
194  void
195  count<T>::set_value(unsigned c)
196  {
197  count_ = c;
198  }
199 
200  template <typename T>
201  inline
202  bool
203  count<T>::is_valid() const
204  {
205  return true;
206  }
207 
208 # endif // ! MLN_INCLUDE_ONLY
209 
210  } // end of namespace mln::accu::math
211 
212  } // end of namespace mln::accu
213 
214 } // end of namespace mln
215 
216 
217 #endif // ! MLN_ACCU_MATH_COUNT_HH