Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 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_MORPHO_ATTRIBUTE_SUM_HH 00027 # define MLN_MORPHO_ATTRIBUTE_SUM_HH 00028 00032 00033 # include <mln/accu/internal/base.hh> 00034 00035 # include <mln/trait/value_.hh> // For mln_sum. 00036 # include <mln/value/builtin/all.hh> // In the case of summing builtin values. 00037 # include <mln/literal/zero.hh> // For initialization. 00038 00039 # include <mln/util/pix.hh> 00040 00041 namespace mln 00042 { 00043 00044 00045 // Forward declaration. 00046 00047 namespace morpho { 00048 namespace attribute { 00049 template <typename I, typename S> class sum; 00050 } 00051 } 00052 00053 00054 // Traits. 00055 00056 namespace trait 00057 { 00058 00059 template <typename I, typename S> 00060 struct accumulator_< morpho::attribute::sum<I,S> > 00061 { 00062 typedef accumulator::has_untake::yes has_untake; 00063 typedef accumulator::has_set_value::yes has_set_value; 00064 typedef accumulator::has_stop::no has_stop; 00065 typedef accumulator::when_pix::use_v when_pix; 00066 }; 00067 00068 } // end of namespace mln::trait 00069 00070 00071 namespace morpho 00072 { 00073 00074 namespace attribute 00075 { 00076 00078 00079 template <typename I, typename S = mln_sum(mln_value(I))> 00080 class sum : public mln::accu::internal::base< S, sum<I,S> > 00081 { 00082 typedef mln::accu::internal::base< S, sum<I> > super_; 00083 public: 00084 00085 typedef mln_value(I) argument; 00086 00087 sum(); 00088 00091 void init(); 00092 00093 void take(const argument& v); 00094 void take(const util::pix<I>& px); 00095 void take(const sum<I,S>& other); 00096 00097 void take_as_init_(const argument& v); 00098 void take_as_init_(const util::pix<I>& px); 00100 00102 S to_result() const; 00103 00106 bool is_valid() const; 00107 00109 void untake(const argument& v); 00110 void untake(const util::pix<I>& px); 00111 00113 void set_value(const argument& v); 00114 void set_value(const util::pix<I>& px); 00115 00116 00117 protected: 00118 00120 S s_; 00121 }; 00122 00123 00124 00125 # ifndef MLN_INCLUDE_ONLY 00126 00127 template <typename I, typename S> 00128 inline 00129 sum<I,S>::sum() 00130 { 00131 init(); 00132 } 00133 00134 template <typename I, typename S> 00135 inline 00136 void 00137 sum<I,S>::init() 00138 { 00139 s_ = literal::zero; 00140 } 00141 00142 // take. 00143 00144 template <typename I, typename S> 00145 inline 00146 void 00147 sum<I,S>::take(const argument& v) 00148 { 00149 s_ += v; 00150 } 00151 00152 template <typename I, typename S> 00153 inline 00154 void 00155 sum<I,S>::take(const util::pix<I>& px) 00156 { 00157 take(px.v()); 00158 } 00159 00160 template <typename I, typename S> 00161 inline 00162 void 00163 sum<I,S>::take(const sum<I,S>& other) 00164 { 00165 s_ += other.s_; 00166 } 00167 00168 // take_as_init_. 00169 00170 template <typename I, typename S> 00171 inline 00172 void 00173 sum<I,S>::take_as_init_(const argument& v) 00174 { 00175 s_ = v; 00176 } 00177 00178 template <typename I, typename S> 00179 inline 00180 void 00181 sum<I,S>::take_as_init_(const util::pix<I>& px) 00182 { 00183 take_as_init_(px.v()); 00184 } 00185 00186 template <typename I, typename S> 00187 inline 00188 void 00189 sum<I,S>::untake(const argument& v) 00190 { 00191 s_ -= v; 00192 } 00193 00194 template <typename I, typename S> 00195 inline 00196 void 00197 sum<I,S>::untake(const util::pix<I>& px) 00198 { 00199 untake(px.v()); 00200 } 00201 00202 template <typename I, typename S> 00203 inline 00204 void 00205 sum<I,S>::set_value(const argument& v) 00206 { 00207 s_ = v; 00208 } 00209 00210 template <typename I, typename S> 00211 inline 00212 void 00213 sum<I,S>::set_value(const util::pix<I>& px) 00214 { 00215 set_value(px.v()); 00216 } 00217 00218 template <typename I, typename S> 00219 inline 00220 S 00221 sum<I,S>::to_result() const 00222 { 00223 return s_; 00224 } 00225 00226 template <typename I, typename S> 00227 inline 00228 bool 00229 sum<I,S>::is_valid() const 00230 { 00231 return true; 00232 } 00233 00234 # endif // ! MLN_INCLUDE_ONLY 00235 00236 } // end of namespace mln::morpho::attribute 00237 00238 } // end of namespace mln::morpho 00239 00240 } // end of namespace mln 00241 00242 00243 #endif // ! MLN_MORPHO_ATTRIBUTE_SUM_HH