4356: New candidates to replace value types.

https://svn.lrde.epita.fr/svn/oln/trunk/milena/sandbox Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> New candidates to replace value types. * theo/mln: New directory. * theo/mln/core, * theo/mln/core/concept, * theo/mln/walue, * theo/mln/walue/internal: New sub-directories. * theo/mln/core/concept/walue.hh: New. * theo/mln/walue/gl.cc: New. * theo/mln/walue/gl8.hh: New. * theo/mln/walue/rgb_t.hh: New. * theo/mln/walue/glf.cc: New. * theo/mln/walue/approx_equal.hh: New. * theo/mln/walue/set_encoding.hh: New. * theo/mln/walue/gl.hh: New. * theo/mln/walue/rgb_3xn.hh: New. * theo/mln/walue/rgb_3x8.cc: New. * theo/mln/walue/int_u.cc: New. * theo/mln/walue/glf.hh: New. * theo/mln/walue/gl16.hh: New. * theo/mln/walue/rgb.hh: New. * theo/mln/walue/rgb_3x8.hh: New. * theo/mln/walue/internal/rgb_base.hh: New. * theo/mln/walue/color.hh: New. * theo/mln/walue/int_u.hh: New. core/concept/walue.hh | 80 +++++++++ walue/approx_equal.hh | 98 ++++++++++++ walue/color.hh | 66 ++++++++ walue/gl.cc | 76 +++++++++ walue/gl.hh | 362 +++++++++++++++++++++++++++++++++++++++++++++ walue/gl16.hh | 50 ++++++ walue/gl8.hh | 50 ++++++ walue/glf.cc | 39 ++++ walue/glf.hh | 152 ++++++++++++++++++ walue/int_u.cc | 32 +++ walue/int_u.hh | 170 +++++++++++++++++++++ walue/internal/rgb_base.hh | 240 +++++++++++++++++++++++++++++ walue/rgb.hh | 86 ++++++++++ walue/rgb_3x8.cc | 26 +++ walue/rgb_3x8.hh | 51 ++++++ walue/rgb_3xn.hh | 98 ++++++++++++ walue/rgb_t.hh | 97 ++++++++++++ walue/set_encoding.hh | 176 +++++++++++++++++++++ 18 files changed, 1949 insertions(+) Index: theo/mln/core/concept/walue.hh --- theo/mln/core/concept/walue.hh (revision 0) +++ theo/mln/core/concept/walue.hh (revision 0) @@ -0,0 +1,80 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_CORE_CONCEPT_WALUE_HH +# define MLN_CORE_CONCEPT_WALUE_HH + +/// \file +/// \brief Definition of the concept of mln::Walue. +/// +/// \todo Activate traits. + +# include <mln/core/concept/object.hh> +// # include <mln/trait/value_.hh> + + +namespace mln +{ + + // Forward declaration. + template <typename E> struct Walue; + + // Value category flag type. + template <> + struct Walue<void> + { + typedef Object<void> super; + }; + + + /// \brief Concept for classes of values. + // + template <typename E> + struct Walue : public Object<E> + { + typedef Walue<void> category; + + // FIXME + + protected: + Walue(); + }; + + +# ifndef MLN_INCLUDE_ONLY + + template <typename E> + inline + Walue<E>::Walue() + { + // FIXME + } + +# endif // ! MLN_INCLUDE_ONLY + +} // end of namespace mln + + +#endif // ! MLN_CORE_CONCEPT_WALUE_HH Index: theo/mln/walue/gl.cc --- theo/mln/walue/gl.cc (revision 0) +++ theo/mln/walue/gl.cc (revision 0) @@ -0,0 +1,76 @@ +#include <mln/walue/gl8.hh> +#include <mln/walue/gl16.hh> +#include <mln/literal/white.hh> +#include <mln/literal/black.hh> + + + +int main() +{ + using namespace mln; + using walue::gl8; + using walue::gl16; + using walue::glf1; + using walue::set_encoding; + + { + gl8 g1 = set_encoding(127); + mln_assertion(g1.encoding() == 127); + gl8 g2 = set_encoding(128); + mln_assertion((g1 + g2) / 2 == 0.5); + } + + { + gl8 g8 = set_encoding(51); + mln_assertion(approx_equal(g8, 0.2)); + + gl16 g16 = 0.2; + mln_assertion(g16.encoding() == 13107); + mln_assertion(approx_equal(g8, g16)); + } + + { + gl8 g; + + g = 0.; + mln_assertion(g == literal::black); + + g = 1.; + mln_assertion(g == literal::white); + + g = 0.2; + mln_assertion(5 * g == literal::white); + } + + { + gl8 g1 = literal::white; + gl16 g2 = g1; + mln_assertion(g2 == literal::white); + } + + { + gl8 g1 = literal::white; + gl16 g2 = literal::white; + mln_assertion((g1 + g2) / 2 == literal::white); + // std::cout << "g1 = " << g1 << ", " << "g2 = " << g2 << std::endl; + mln_assertion(g1 <= g2 && g2 >= g1); + mln_assertion(g1 == g2); + } + + + { + using walue::gl; + gl<31> g1 = literal::white; + gl<32> g2 = literal::white; + mln_assertion(g2 == g1); + g2.set_encoding(g2.encoding() - 1); + mln_assertion(g2 < g1); + } + +// // KO: Ambiguous code do not compile :-) +// { +// walue::int_u8 i = 1; +// g = i; +// } + +} Index: theo/mln/walue/gl8.hh --- theo/mln/walue/gl8.hh (revision 0) +++ theo/mln/walue/gl8.hh (revision 0) @@ -0,0 +1,50 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_GL8_HH +# define MLN_WALUE_GL8_HH + +/// \file +/// +/// Define a 8-bit gray-level type. + +# include <mln/walue/gl.hh> + + +namespace mln +{ + + namespace walue + { + + /// 8-bit gray-level type. + typedef gl<8> gl8; + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_GL8_HH Index: theo/mln/walue/rgb_t.hh --- theo/mln/walue/rgb_t.hh (revision 0) +++ theo/mln/walue/rgb_t.hh (revision 0) @@ -0,0 +1,97 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_RGB_T_HH +# define MLN_WALUE_RGB_T_HH + +/// \file +/// \brief FIXME + +# include <mln/walue/internal/rgb_base.hh> + + +namespace mln +{ + + namespace walue + { + + // Forward declaration. + template <typename T> class rgb_t; + + + // FIXME... + + + /// \brief FIXME + // + template <typename T> + class rgb_t : public internal::rgb_base< T, T, T, + rgb_t<T> > + { + typedef internal::rgb_base< T, T, T, + rgb_t<T> > super_; + public: + + // Constructor without argument. + rgb_t(); + + rgb_t(float r, float g, float b); + + template <typename T> + rgb_t(const internal::encoding<T,T,T>& e) + : super_(set_encoding(e.t0), + set_encoding(e.t1), + set_encoding(e.t2)) + { + } + + }; + + + +# ifndef MLN_INCLUDE_ONLY + + template <typename T> + inline + rgb_t<T>::rgb_t() + { + } + + template <typename T> + inline + rgb_t<T>::rgb_t(float r, float g, float b) + : super_(r, g, b) + { + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_RGB_T_HH Index: theo/mln/walue/glf.cc --- theo/mln/walue/glf.cc (revision 0) +++ theo/mln/walue/glf.cc (revision 0) @@ -0,0 +1,39 @@ +#include <mln/walue/glf.hh> +#include <mln/walue/gl8.hh> +#include <mln/literal/white.hh> + + +int main() +{ + using namespace mln; + using walue::glf1; + using walue::glf2; + + glf1 g = walue::set_encoding(0.5); + g = g * 2; + mln_assertion(g == literal::white); + + + { + using walue::gl8; + gl8 g = literal::white; + glf1 f = g; + mln_assertion(f == g); + } + + // Tests on approx_equal. + { + glf1 g = 0.49999, g_ = 0.50001; + mln_assertion(approx_equal(g, g_) == false); + } + { + glf1 g = 0.499999, g_ = 0.500001; + mln_assertion(approx_equal(g, g_)); + mln_assertion(approx_equal(g, 0.5)); + } + { + glf1 g1 = 0.499999; + glf2 g2 = 0.500001; + mln_assertion(approx_equal(g1, g2)); + } +} Index: theo/mln/walue/approx_equal.hh --- theo/mln/walue/approx_equal.hh (revision 0) +++ theo/mln/walue/approx_equal.hh (revision 0) @@ -0,0 +1,98 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_APPROX_EQUAL_HH +# define MLN_APPROX_EQUAL_HH + +/// \file +/// +/// Define an approximative equality test. + + +namespace mln +{ + + + namespace def + { + + /// Epsilon. + const float epsilon = 1e-5; // FIXME + const float minus_epsilon = - epsilon; + const float one_plus_epsilon = 1 + epsilon; + + } // def + + + bool approx_equal(float lhs, float rhs); + bool approx_equal(float lhs, double rhs); + bool approx_equal(double lhs, float rhs); + bool approx_equal(double lhs, double rhs); + + +# ifndef MLN_INCLUDE_ONLY + + namespace internal + { + inline float my_fabs_(float f) { return f > 0.f ? f : - f; } + } + + inline + bool approx_equal(float lhs, float rhs) + { + // FIXME: Cf. boost. + float + diff = lhs > rhs ? lhs - rhs : rhs - lhs, + mean = internal::my_fabs_(lhs + rhs) / 2; + return + mean > def::epsilon ? + diff / mean < def::epsilon : + diff < def::epsilon; + } + + inline + bool approx_equal(float lhs, double rhs) + { + return approx_equal(lhs, float(rhs)); + } + + inline + bool approx_equal(double lhs, float rhs) + { + return approx_equal(float(lhs), rhs); + } + + inline + bool approx_equal(double lhs, double rhs) + { + return approx_equal(float(lhs), float(rhs)); + } + +# endif // ! MLN_INCLUDE_ONLY + +} // end of namespace mln + + +#endif // ! MLN_APPROX_EQUAL_HH Index: theo/mln/walue/set_encoding.hh --- theo/mln/walue/set_encoding.hh (revision 0) +++ theo/mln/walue/set_encoding.hh (revision 0) @@ -0,0 +1,176 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_SET_ENCODING_HH +# define MLN_WALUE_SET_ENCODING_HH + +/// \file +/// \brief Routine to set an encoding value. + +# include <mln/core/concept/walue.hh> + + +namespace mln +{ + + namespace walue + { + + namespace internal + { + + template < typename T0, typename T1, typename T2 > + struct encoding_base + { + encoding_base(const T0& t0, const T1& t1, const T2& t2) + : t0(t0), t1(t1), t2(t2) + { + } + const T0& t0; + const T1& t1; + const T2& t2; + }; + + template < typename T0, typename T1 > + struct encoding_base< T0, T1, void > + { + encoding_base(const T0& t0, const T1& t1) + : t0(t0), t1(t1) + { + } + const T0& t0; + const T1& t1; + }; + + template < typename T0 > + struct encoding_base< T0, void, void > + { + encoding_base(const T0& t0) + : t0(t0) + { + } + const T0& t0; + const T0& value() const { return t0; } + }; + + + template < typename T0, + typename T1 = void, + typename T2 = void > + struct encoding : encoding_base<T0,T1,T2> + { + + template < typename U0 > + encoding(const U0& u0) + : encoding_base<T0,T1,T2>(u0) + {} + + template < typename U0, typename U1 > + encoding(const U0& u0, const U1& u1) + : encoding_base<T0,T1,T2>(u0, u1) + {} + + template < typename U0, typename U1, typename U2 > + encoding(const U0& u0, const U1& u1, const U2& u2) + : encoding_base<T0,T1,T2>(u0, u1, u2) + {} + }; + + + } // end of namespace mln::walue::internal + + + /// \brief Set an encoding value. + // + template <typename T> + inline + internal::encoding<T,void,void> + set_encoding(const T& t) + { + internal::encoding<T,void,void> tmp(t); + return tmp; + } + + + template <typename T0, typename T1> + inline + internal::encoding<T0,T1,void> + set_encoding(const T0& t0, const T1& t1) + { + internal::encoding<T0,T1,void> tmp(t0,t1); + return tmp; + } + + template <typename T0, typename T1, typename T2> + inline + internal::encoding<T0,T1,T2> + set_encoding(const T0& t0, const T1& t1, const T2& t2) + { + internal::encoding<T0,T1,T2> tmp(t0,t1,t2); + return tmp; + } + + +# ifndef MLN_INCLUDE_ONLY + +// namespace internal +// { + +// template <typename T> +// inline +// encoding<T>::encoding(const T& t) +// : t_(t) +// { +// } + +// template <typename T> +// inline +// const T& +// encoding<T>::value() const +// { +// return t_; +// } + +// // const T& operator[](unsigned i)) const; + +// } // end of namespace mln::walue::internal + +// template <typename T> +// inline +// internal::encoding<T> +// set_encoding(const T& t) +// { +// internal::encoding<T> tmp(t); +// return tmp; +// } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_SET_ENCODING_HH Index: theo/mln/walue/gl.hh --- theo/mln/walue/gl.hh (revision 0) +++ theo/mln/walue/gl.hh (revision 0) @@ -0,0 +1,362 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_GL_HH +# define MLN_WALUE_GL_HH + +/// \file +/// +/// Define a gray-level type with discrete quantization. + +# include <mln/core/concept/walue.hh> +# include <mln/walue/int_u.hh> +# include <mln/walue/glf.hh> +# include <mln/walue/set_encoding.hh> + +// Extra usefull includes. +# include <mln/value/scalar.hh> +# include <mln/value/builtin/ops.hh> + + +namespace mln +{ + + + // Forward declarations. + namespace walue { + template <unsigned n> struct gl; + } + namespace literal { + struct white_t; + struct black_t; + } + + + namespace walue + { + + namespace internal + { + + // Max encoding with n bits. + + template <unsigned n> + struct pow_two + { + static const unsigned long max_ = 2 * pow_two<n - 1>::max_ + 1; + static float max() { return float(max_); } + }; + + template <> + struct pow_two< 1 > + { + static const unsigned long max_ = 1; + }; + + } // internal + + + + /// FIXME + // + template <unsigned n> + class gl + : public Proxy< gl<n> >, + public mln::internal::proxy_impl< glf1, gl<n> >, + private metal::bool_<(n > 1)>::check_t + { + public: + + /// Encoding associated type. + typedef typename internal::encoding_type_<n>::ret encoding_t; + + /// Constructor without argument. + gl() + { + } + + /// Copy constructor. + gl(const gl<n>& g) + { + v_ = g.v_; + } + + /// Assignment operator. + gl& operator=(const gl<n>& g) + { + v_ = g.v_; + return *this; + } + + + /// From a floating value. + gl(float f) { set_(f); } + gl& operator=(float f) { set_(f); return *this; } + + /// From a double precision floating value. + gl(double f) { set_(f); } + gl& operator=(double f) { set_(f); return *this; } + + /// From another discrete gray-level. + template <unsigned m> + gl(const gl<m>& g) { set_(g); } + template <unsigned m> + gl& operator=(const gl<m>& g) { set_(g); return *this; } + + /// From a floating encoded gray-level. + template <typename F> + gl(const glf<F>& f) { set_(f); } + template <typename F> + gl& operator=(const glf<F>& f) { set_(f); return *this; } + + // Safety. + template <typename T> + gl(T) + { + mlc_abort(T)::check(); + } + + // Safety. + template <typename T> + void operator=(T) + { + mlc_abort(T)::check(); + } + + + gl(const mln::literal::white_t&) + { + v_ = internal::pow_two<n>::max_; + } + gl& operator=(const mln::literal::white_t&) + { + v_ = internal::pow_two<n>::max_; + return *this; + } + + gl(const mln::literal::black_t&) { v_ = 0; } + gl& operator=(const mln::literal::black_t&) { v_ = 0; return *this; } + + + template <typename T> + gl(const internal::encoding<T>& e) + { + // Dev note: no need for operator==(internal::encoding). + mlc_converts_to(T, encoding_t)::check(); + mln_precondition(e.value() >= 0 && + e.value() <= internal::pow_two<n>::max_); + v_ = e.value(); + mln_postcondition(static_cast<T>(v_) == e.value()); + } + + encoding_t encoding() const + { + return v_; + } + + gl& set_encoding(encoding_t v) + { + v_ = v; + return *this; + } + + + // Give the subject (as a Proxy). + glf1 subj_() + { + return float(v_) / internal::pow_two<n>::max(); + } + + protected: + + encoding_t v_; + + private: + + void set_(float f) + { + mln_precondition(f >= def::minus_epsilon && + f <= def::one_plus_epsilon); + v_ = encoding_t(f * internal::pow_two<n>::max() + 0.5f); + } + + }; // end of class gl<n> + + + template <unsigned n> + std::ostream& + operator<<(std::ostream& ostr, const gl<n>& g) + { + return ostr << (unsigned long)(g.encoding()); + } + + + // approx_equal + + template <unsigned n> + inline + bool approx_equal(const gl<n>& lhs, const gl<n>& rhs) + { + return lhs.encoding() == rhs.encoding(); + } + + template <unsigned n, unsigned m> + inline + bool approx_equal(const gl<n>& lhs, const gl<m>& rhs) + { + if (n < m) + return lhs.encoding() == gl<n>(rhs).encoding(); + else + return approx_equal(rhs, lhs); + } + + template <unsigned n, typename F> + inline + bool approx_equal(const gl<n>& lhs, const glf<F>& rhs) + { + return lhs.encoding() == gl<n>(rhs).encoding(); + } + + template <unsigned n> + inline + bool approx_equal(const gl<n>& lhs, float rhs) + { + return lhs.encoding() == gl<n>(rhs).encoding(); + } + + template <unsigned n> + inline + bool approx_equal(const gl<n>& lhs, double rhs) + { + return lhs.encoding() == gl<n>(rhs).encoding(); + } + + // Safety. + template <unsigned n, typename T> + inline + bool approx_equal(const gl<n>& lhs, T) + { + mlc_abort(T)::check(); + return false; + } + + // With glf<F> as left hand side. + template <typename F, unsigned n> + inline + bool approx_equal(const glf<F>& lhs, const gl<n>& rhs) + { + return approx_equal(rhs, lhs); + } + + + // Operators. + + template <unsigned n> + bool operator==(const gl<n>& lhs, const gl<n>& rhs); + + template <unsigned n> + bool operator<(const gl<n>& lhs, const gl<n>& rhs); + + + template <unsigned n, unsigned m> + bool operator==(const gl<n>& lhs, const gl<m>& rhs); + + template <unsigned n, unsigned m> + bool operator<(const gl<n>& lhs, const gl<m>& rhs); + + template <unsigned n, unsigned m> + bool operator<=(const gl<n>& lhs, const gl<m>& rhs); + + +# ifndef MLN_INCLUDE_ONLY + + + // FIXME: Move definitions here... + + + // Operators. + + template <unsigned n> + inline + bool operator==(const gl<n>& lhs, const gl<n>& rhs) + { + return lhs.encoding() == rhs.encoding(); + } + + template <unsigned n> + inline + bool operator<(const gl<n>& lhs, const gl<n>& rhs) + { + return lhs.encoding() < rhs.encoding(); + } + + template <unsigned n, unsigned m> + inline + bool operator==(const gl<n>& lhs, const gl<m>& rhs) + { + typedef unsigned long long ull_t; + return + (ull_t)lhs.encoding() * (ull_t)internal::pow_two<m>::max_ == + (ull_t)rhs.encoding() * (ull_t)internal::pow_two<n>::max_; + } + + // FIXME: For low quantizations, some better code can be + // implemented. For instance + // op == { return std::abs(float(lhs) - float(rhs)) < def::epsilon; } + // op < { return float(lhs) < float(rhs) - def::epsilon; } + // op <= { return float(lhs) < float(rhs) + def::epsilon; } + + template <unsigned n, unsigned m> + inline + bool operator<(const gl<n>& lhs, const gl<m>& rhs) + { + typedef unsigned long long ull_t; + return + (ull_t)lhs.encoding() * (ull_t)internal::pow_two<m>::max_ < + (ull_t)rhs.encoding() * (ull_t)internal::pow_two<n>::max_; + } + + template <unsigned n, unsigned m> + inline + bool operator<=(const gl<n>& lhs, const gl<m>& rhs) + { + typedef unsigned long long ull_t; + return + (ull_t)lhs.encoding() * (ull_t)internal::pow_two<m>::max_ <= + (ull_t)rhs.encoding() * (ull_t)internal::pow_two<n>::max_; + } + + + // FIXME: Add specializations with float and double? + + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_GL_HH Index: theo/mln/walue/rgb_3xn.hh --- theo/mln/walue/rgb_3xn.hh (revision 0) +++ theo/mln/walue/rgb_3xn.hh (revision 0) @@ -0,0 +1,98 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_RGB_3XN_HH +# define MLN_WALUE_RGB_3XN_HH + +/// \file +/// \brief FIXME + +# include <mln/walue/internal/rgb_base.hh> +# include <mln/walue/gl.hh> + + +namespace mln +{ + + namespace walue + { + + // Forward declaration. + template <unsigned n> class rgb_3xn; + + + // FIXME... + + + /// \brief FIXME + // + template <unsigned n> + class rgb_3xn : public internal::rgb_base< gl<n>, gl<n>, gl<n>, + rgb_3xn<n> > + { + typedef internal::rgb_base< gl<n>, gl<n>, gl<n>, + rgb_3xn<n> > super_; + public: + + // Constructor without argument. + rgb_3xn(); + + rgb_3xn(float r, float g, float b); + + template <typename T> + rgb_3xn(const internal::encoding<T,T,T>& e) + : super_(set_encoding(e.t0), + set_encoding(e.t1), + set_encoding(e.t2)) + { + } + + }; + + + +# ifndef MLN_INCLUDE_ONLY + + template <unsigned n> + inline + rgb_3xn<n>::rgb_3xn() + { + } + + template <unsigned n> + inline + rgb_3xn<n>::rgb_3xn(float r, float g, float b) + : super_(r, g, b) + { + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_RGB_3XN_HH Index: theo/mln/walue/rgb_3x8.cc --- theo/mln/walue/rgb_3x8.cc (revision 0) +++ theo/mln/walue/rgb_3x8.cc (revision 0) @@ -0,0 +1,26 @@ +#include <mln/walue/rgb_3x8.hh> +#include <mln/core/var.hh> + + + +int main() +{ + using namespace mln; + using walue::rgb_3x8; + using walue::set_encoding; + + { + rgb_3x8 c(0, 0.5, 1); + std::cout << c.to_vec() << std::endl; + } + { + rgb_3x8 c = set_encoding(0, 128, 255); + std::cout << c << std::endl; + } + +// // mln_VAR(e, walue::set_encoding(0,1,2)); +// walue::internal::encoding<int> e = walue::set_encoding(0,1,2); +// std::cout << e.t0 << ' ' << e.t1 << ' ' << e.t2 << std::endl; + + +} Index: theo/mln/walue/int_u.cc --- theo/mln/walue/int_u.cc (revision 0) +++ theo/mln/walue/int_u.cc (revision 0) @@ -0,0 +1,32 @@ +#include <mln/walue/int_u.hh> +#include <mln/literal/zero.hh> + + +int main() +{ + using namespace mln; + using walue::int_u8; + + int_u8 i = 1; + mln_assertion(i == 1); + i = 2; + mln_assertion(i == 2); + + mln_assertion((i - i) == 0); + mln_assertion((i + 2 * i) == (i * 3)); + mln_assertion((+i) == i); + mln_assertion((-i) == (0 - i)); + + float f = i; + mln_assertion(f == 2); + mln_assertion(f == i); + mln_assertion((f + i) == 2 * f); + + i = literal::zero; + mln_assertion(0 == i); + + { + int_u8 i = walue::set_encoding(51); + mln_assertion(i.encoding() == 51); + } +} Index: theo/mln/walue/glf.hh --- theo/mln/walue/glf.hh (revision 0) +++ theo/mln/walue/glf.hh (revision 0) @@ -0,0 +1,152 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_GLF_HH +# define MLN_WALUE_GLF_HH + +/// \file +/// +/// Define a gray-level type with floating quantization. + +# include <mln/core/concept/proxy.hh> +# include <mln/value/scalar.hh> +# include <mln/value/builtin/ops.hh> +# include <mln/walue/set_encoding.hh> +# include <mln/walue/approx_equal.hh> + + +namespace mln +{ + + + // Forward declarations. + namespace walue { + template <typename F> struct glf; + template <unsigned n> struct gl; + } + namespace literal { + struct white_t; + struct black_t; + } + + + namespace walue + { + + /// Type aliases. + typedef glf<float> glf1; + typedef glf<double> glf2; + + + /// FIXME + // + template <typename F> + class glf + : public Proxy< glf<F> >, + public mln::internal::proxy_impl< F, glf<F> >, + private metal::or_< mlc_equal(F, float), + mlc_equal(F, double) >::check_t + { + public: + + /// Encoding associated type. + typedef F encoding_t; + + /// Constructor without argument. + glf() + { + } + + /// Constructor from a floating value. + glf(F f) + { + mln_precondition(f >= def::minus_epsilon && + f <= def::one_plus_epsilon); + v_ = f; + } + + /// Assignment from a floating value. + glf& operator=(F f) + { + mln_precondition(f >= def::minus_epsilon && + f <= def::one_plus_epsilon); + v_ = f; + return *this; + } + + + glf(const mln::literal::white_t&) { v_ = 1; } + glf& operator=(const mln::literal::white_t&) { v_ = 1; return *this; } + + glf(const mln::literal::black_t&) { v_ = 0; } + glf& operator=(const mln::literal::black_t&) { v_ = 0; return *this; } + + + template <typename T> + glf(const internal::encoding<T>& e) + { + mlc_converts_to(T, F)::check(); + v_ = e.value(); + mln_postcondition(static_cast<T>(v_) == e.value()); + } + + F encoding() const + { + return v_; + } + + glf& set_encoding(F f) + { + mln_precondition(f >= def::minus_epsilon && + f <= def::one_plus_epsilon); + v_ = f; + return *this; + } + + + // As a proxy. + F subj_() + { + return v_; + } + + protected: + + F v_; + }; + + +# ifndef MLN_INCLUDE_ONLY + + // FIXME + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_GLF_HH Index: theo/mln/walue/gl16.hh --- theo/mln/walue/gl16.hh (revision 0) +++ theo/mln/walue/gl16.hh (revision 0) @@ -0,0 +1,50 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_GL16_HH +# define MLN_WALUE_GL16_HH + +/// \file +/// +/// Define a 16-bit gray-level type. + +# include <mln/walue/gl.hh> + + +namespace mln +{ + + namespace walue + { + + /// 16-bit gray-level type. + typedef gl<16> gl16; + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_GL16_HH Index: theo/mln/walue/rgb.hh --- theo/mln/walue/rgb.hh (revision 0) +++ theo/mln/walue/rgb.hh (revision 0) @@ -0,0 +1,86 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_RGB_HH +# define MLN_WALUE_RGB_HH + +/// \file +/// \brief Definition of the Rgb abstraction. +/// +/// \todo Generalize op<< for vector-like value types. + +# include <mln/walue/color.hh> + + +namespace mln +{ + + namespace walue + { + + /// \brief Rgb abstraction. + // + template <typename E> + struct Rgb : public Color<E> + { + protected: + Rgb(); + }; + + + template <typename E> + std::ostream& + operator<<(std::ostream& ostr, const Rgb<E>& c); + + + +# ifndef MLN_INCLUDE_ONLY + + template <typename E> + inline + Rgb<E>::Rgb() + { + } + + template <typename E> + inline + std::ostream& + operator<<(std::ostream& ostr, const Rgb<E>& c_) + { + const E& c = exact(c_); + return ostr << '(' + << c.red() << ',' + << c.green() << ',' + << c.blue() << ')'; + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_RGB_HH Index: theo/mln/walue/rgb_3x8.hh --- theo/mln/walue/rgb_3x8.hh (revision 0) +++ theo/mln/walue/rgb_3x8.hh (revision 0) @@ -0,0 +1,51 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_RGB_3X8_HH +# define MLN_WALUE_RGB_3X8_HH + +/// \file +/// \brief FIXME + +# include <mln/walue/rgb_3xn.hh> + + +namespace mln +{ + + namespace walue + { + + + /// Type alias. + typedef rgb_3xn<8> rgb_3x8; + + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_RGB_3X8_HH Index: theo/mln/walue/internal/rgb_base.hh --- theo/mln/walue/internal/rgb_base.hh (revision 0) +++ theo/mln/walue/internal/rgb_base.hh (revision 0) @@ -0,0 +1,240 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_INTERNAL_RGB_BASE_HH +# define MLN_WALUE_INTERNAL_RGB_BASE_HH + +/// \file +/// \brief FIXME + +# include <mln/walue/rgb.hh> +# include <mln/algebra/vec.hh> + + +namespace mln +{ + + namespace walue + { + + namespace internal + { + + /// \brief FIXME + // + template <typename R, typename G, typename B, + typename E> + class rgb_base : public Rgb< E > + { + public: + + typedef R red_t; + R red() const; + R& red(); + + typedef G green_t; + G green() const; + G& green(); + + typedef B blue_t; + B blue() const; + B& blue(); + + protected: + + rgb_base(); + rgb_base(const R& r, const G& g, const B& b); + + R r_; + G g_; + B b_; + }; + + + /// \brief FIXME + // + template <typename T, typename E> + class rgb_base<T,T,T, E> : public Rgb< E > + { + public: + + typedef T red_t; + T red() const; + T& red(); + + typedef T green_t; + T green() const; + T& green(); + + typedef T blue_t; + T blue() const; + T& blue(); + + const algebra::vec<3,T>& to_vec() const; + + protected: + + rgb_base(); + + // FIXME: Allow input with a type U different from T... + rgb_base(const T& r, const T& g, const T& b); + + T v_[3]; + }; + + +# ifndef MLN_INCLUDE_ONLY + + template <typename R, typename G, typename B, typename E> + inline + rgb_base<R,G,B,E>::rgb_base() + { + } + + template <typename R, typename G, typename B, typename E> + inline + rgb_base<R,G,B,E>::rgb_base(const R& r, const G& g, const B& b) + : r_(r), g_(g), b_(b) + { + } + + template <typename R, typename G, typename B, typename E> + inline + R rgb_base<R,G,B,E>::red() const + { + return r_; + } + + template <typename R, typename G, typename B, typename E> + inline + R& rgb_base<R,G,B,E>::red() + { + return r_; + } + + template <typename R, typename G, typename B, typename E> + inline + G rgb_base<R,G,B,E>::green() const + { + return g_; + } + + template <typename R, typename G, typename B, typename E> + inline + G& rgb_base<R,G,B,E>::green() + { + return g_; + } + + template <typename R, typename G, typename B, typename E> + inline + B rgb_base<R,G,B,E>::blue() const + { + return b_; + } + + template <typename R, typename G, typename B, typename E> + inline + B& rgb_base<R,G,B,E>::blue() + { + return b_; + } + + // Specialization. + + template <typename T, typename E> + inline rgb_base<T,T,T,E>::rgb_base() + { + } + + template <typename T, typename E> + inline + rgb_base<T,T,T,E>::rgb_base(const T& r, const T& g, const T& b) + { + v_[0] = r; + v_[1] = g; + v_[2] = b; + } + + template <typename T, typename E> + inline + T rgb_base<T,T,T,E>::red() const + { + return v_[0]; + } + + template <typename T, typename E> + inline + T& rgb_base<T,T,T,E>::red() + { + return v_[0]; + } + + template <typename T, typename E> + inline + T rgb_base<T,T,T,E>::green() const + { + return v_[1]; + } + + template <typename T, typename E> + inline + T& rgb_base<T,T,T,E>::green() + { + return v_[1]; + } + + template <typename T, typename E> + inline + T rgb_base<T,T,T,E>::blue() const + { + return v_[2]; + } + + template <typename T, typename E> + inline + T& rgb_base<T,T,T,E>::blue() + { + return v_[2]; + } + + template <typename T, typename E> + inline + const algebra::vec<3,T>& + rgb_base<T,T,T,E>::to_vec() const + { + return *(const algebra::vec<3,T>*)(v_); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::walue::internal + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_RGB_BASE_HH Index: theo/mln/walue/color.hh --- theo/mln/walue/color.hh (revision 0) +++ theo/mln/walue/color.hh (revision 0) @@ -0,0 +1,66 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_COLOR_HH +# define MLN_WALUE_COLOR_HH + +/// \file +/// \brief Definition of the Color abstraction. + +# include <mln/core/concept/walue.hh> + + +namespace mln +{ + + namespace walue + { + + /// \brief Color abstraction. + // + template <typename E> + struct Color : public Walue<E> + { + protected: + Color(); + }; + + +# ifndef MLN_INCLUDE_ONLY + + template <typename E> + inline + Color<E>::Color() + { + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_COLOR_HH Index: theo/mln/walue/int_u.hh --- theo/mln/walue/int_u.hh (revision 0) +++ theo/mln/walue/int_u.hh (revision 0) @@ -0,0 +1,170 @@ +// Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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. + +#ifndef MLN_WALUE_INT_U_HH +# define MLN_WALUE_INT_U_HH + +/// \file +/// +/// Define a generic class for unsigned integers. + +# include <mln/core/concept/proxy.hh> +# include <mln/walue/set_encoding.hh> + + + +namespace mln +{ + + // Forward declarations. + namespace walue { + template <unsigned n> struct int_u; + } + namespace literal { + struct zero_t; + struct one_t; + } + + + namespace walue + { + + /// Type aliases. + typedef int_u<8> int_u8; + typedef int_u<16> int_u16; + + + namespace internal + { + template <unsigned n> struct encoding_type_; + + // FIXME: Revamp... + + template <> struct encoding_type_<2> { typedef unsigned char ret; }; + template <> struct encoding_type_<3> { typedef unsigned char ret; }; + template <> struct encoding_type_<4> { typedef unsigned char ret; }; + template <> struct encoding_type_<5> { typedef unsigned char ret; }; + template <> struct encoding_type_<6> { typedef unsigned char ret; }; + template <> struct encoding_type_<7> { typedef unsigned char ret; }; + template <> struct encoding_type_<8> { typedef unsigned char ret; }; + + template <> struct encoding_type_<16> { typedef unsigned short ret; }; + + template <> struct encoding_type_<31> { typedef unsigned int ret; }; + template <> struct encoding_type_<32> { typedef unsigned int ret; }; + + } // internal + + + /// Unsigned integer value class. + /// + /// The parameter is \c n the number of encoding bits. + // + template <unsigned n> + class int_u + : public Proxy< int_u<n> >, + public mln::internal::proxy_impl< unsigned, int_u<n> > + { + public: + + /// Encoding associated type. + typedef typename internal::encoding_type_<n>::ret encoding_t; + + /// Constructor without argument. + int_u() + { + } + + /// Constructor from an integer. + int_u(int i) + { + mln_precondition(i >= 0); + // FIXME: mln_precondition(unsigned(i) <= mln_max(enc_)); + v_ = static_cast<encoding_t>(i); + mln_postcondition(static_cast<int>(v_) == i); + } + + /// Assignment from an integer. + int_u<n>& operator=(int i) + { + mln_precondition(i >= 0); + // FIXME: mln_precondition(unsigned(i) <= mln_max(enc_)); + v_ = static_cast<encoding_t>(i); + mln_postcondition(static_cast<int>(v_) == i); + return *this; + } + + /// Subject as a the proxy. + unsigned subj_() + { + return v_; + } + + + int_u(const mln::literal::one_t&) { v_ = 1; } + int_u& operator=(const mln::literal::one_t&) { v_ = 1; return *this; } + + int_u(const mln::literal::zero_t&) { v_ = 0; } + int_u& operator=(const mln::literal::zero_t&) { v_ = 0; return *this; } + + + template <typename T> + int_u(const internal::encoding<T>& e) + { + mlc_converts_to(T, encoding_t)::check(); + v_ = e.value(); + mln_postcondition(static_cast<T>(v_) == e.value()); + } + + encoding_t encoding() const + { + return v_; + } + + int_u& set_encoding(encoding_t v) + { + v_ = v; + return *this; + } + + protected: + + encoding_t v_; + }; + + + +# ifndef MLN_INCLUDE_ONLY + + // FIXME + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::walue + +} // end of namespace mln + + +#endif // ! MLN_WALUE_INT_U_HH
participants (1)
-
Thierry Geraud