00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 00025 00026 #ifndef MLN_ACCU_MATH_COUNT_HH 00027 # define MLN_ACCU_MATH_COUNT_HH 00028 00034 00035 # include <mln/accu/internal/base.hh> 00036 # include <mln/core/concept/meta_accumulator.hh> 00037 00038 00039 namespace mln 00040 { 00041 00042 // Forward declaration. 00043 namespace accu { 00044 namespace math { 00045 template <typename T> struct count; 00046 } 00047 } 00048 00049 00050 // Traits. 00051 00052 namespace trait 00053 { 00054 00055 template <typename T> 00056 struct accumulator_< accu::math::count<T> > 00057 { 00058 typedef accumulator::has_untake::yes has_untake; 00059 typedef accumulator::has_set_value::yes has_set_value; 00060 typedef accumulator::has_stop::no has_stop; 00061 typedef accumulator::when_pix::use_none when_pix; 00062 }; 00063 00064 } // end of namespace mln::trait 00065 00066 00067 namespace accu 00068 { 00069 00070 namespace meta 00071 { 00072 00073 namespace math 00074 { 00075 00077 struct count : public Meta_Accumulator< count > 00078 { 00079 template <typename T> 00080 struct with 00081 { 00082 typedef accu::math::count<T> ret; 00083 }; 00084 }; 00085 00086 } // end of namespace mln::accu::meta::math 00087 00088 } // end of namespace mln::accu::meta 00089 00090 00091 namespace math 00092 { 00093 00098 // 00099 template <typename T> 00100 struct count : public mln::accu::internal::base< unsigned , count<T> > 00101 { 00102 typedef T argument; 00103 00104 count(); 00105 00108 void init(); 00109 void take(const argument&); 00110 void take(const count<T>& other); 00111 00112 void untake(const argument&); 00113 void untake(const count<T>& other); 00114 00116 void set_value(unsigned c); 00118 00120 unsigned to_result() const; 00121 00124 bool is_valid() const; 00125 00126 protected: 00128 unsigned count_; 00129 }; 00130 00131 00132 00133 # ifndef MLN_INCLUDE_ONLY 00134 00135 template <typename T> 00136 inline 00137 count<T>::count() 00138 { 00139 init(); 00140 } 00141 00142 template <typename T> 00143 inline 00144 void 00145 count<T>::init() 00146 { 00147 count_ = 0; 00148 } 00149 00150 template <typename T> 00151 inline 00152 void 00153 count<T>::take(const argument&) 00154 { 00155 ++count_; 00156 } 00157 00158 template <typename T> 00159 inline 00160 void 00161 count<T>::untake(const argument&) 00162 { 00163 mln_precondition(count_ > 0); 00164 --count_; 00165 } 00166 00167 template <typename T> 00168 inline 00169 void 00170 count<T>::take(const count<T>& other) 00171 { 00172 count_ += other.count_; 00173 } 00174 00175 template <typename T> 00176 inline 00177 void 00178 count<T>::untake(const count<T>& other) 00179 { 00180 mln_precondition(other.count_ <= count_); 00181 count_ -= other.count_; 00182 } 00183 00184 template <typename T> 00185 inline 00186 unsigned 00187 count<T>::to_result() const 00188 { 00189 return count_; 00190 } 00191 00192 template <typename T> 00193 inline 00194 void 00195 count<T>::set_value(unsigned c) 00196 { 00197 count_ = c; 00198 } 00199 00200 template <typename T> 00201 inline 00202 bool 00203 count<T>::is_valid() const 00204 { 00205 return true; 00206 } 00207 00208 # endif // ! MLN_INCLUDE_ONLY 00209 00210 } // end of namespace mln::accu::math 00211 00212 } // end of namespace mln::accu 00213 00214 } // end of namespace mln 00215 00216 00217 #endif // ! MLN_ACCU_MATH_COUNT_HH