
2006-10-25 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr> Add two-way morpher. * tests/morphers/two_way_morpher.cc: New. * tests/morphers/Makefile.am: Update. * oln/morpher/two_way.hh: New. * oln/Makefile.am: Update. * oln/core/abstract/image.hh (id_): Unconst. * oln/core/abstract/functions.hh (fun_v2w2v): New. * oln/core/traits.hh (oln_xtd_case_op_id): Fix number. (include): Add commentary. Index: tests/morphers/Makefile.am =================================================================== --- tests/morphers/Makefile.am (revision 680) +++ tests/morphers/Makefile.am (working copy) @@ -24,6 +24,7 @@ count_rw_morpher \ slice_morpher \ stack_morpher \ + two_way_morpher \ value_cast \ with_lut \ \ @@ -35,6 +36,7 @@ count_rw_morpher_SOURCES = count_rw_morpher.cc slice_morpher_SOURCES = slice_morpher.cc stack_morpher_SOURCES = stack_morpher.cc +two_way_morpher_SOURCES = two_way_morpher.cc value_cast_SOURCES = value_cast.cc with_lut_SOURCES = with_lut.cc Index: tests/morphers/two_way_morpher.cc =================================================================== --- tests/morphers/two_way_morpher.cc (revision 0) +++ tests/morphers/two_way_morpher.cc (revision 0) @@ -0,0 +1,68 @@ +// Copyright (C) 2006 EPITA Research and Development Laboratory +// +// 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. + +/// Test the two-way morpher. + +#include <cassert> + +#include <oln/core/traits.hh> +#include <oln/basics2d.hh> +#include <oln/morpher/two_way.hh> + + +struct F : public oln::abstract::fun_v2w2v< F > +{ + typedef int result_type; + + int direct(int i) const + { + return i + 1; + } + + int reverse(int i) const + { + return i - 1; + } +}; + + +int main() +{ + F fun; + + using namespace oln; + typedef image2d<int> I; + I ima(1,1); + point2d p(0,0); + + ima(p) = 50; + assert(fun(ima)(p) == 51); + + morpher::two_way<I,F> ima_bis(ima); + ima_bis(p) = 52; + assert(ima(p) == 51); +} Index: oln/core/abstract/image.hh =================================================================== --- oln/core/abstract/image.hh (revision 680) +++ oln/core/abstract/image.hh (working copy) @@ -133,7 +133,7 @@ private: - const unsigned id_; + unsigned id_; }; Index: oln/core/abstract/functions.hh =================================================================== --- oln/core/abstract/functions.hh (revision 680) +++ oln/core/abstract/functions.hh (working copy) @@ -30,11 +30,19 @@ # include <stc/any.hh> # include <oln/core/type.hh> +# include <oln/core/abstract/image.hh> namespace oln { + + // Fwd decl. + namespace morpher { + template <typename I, typename F> class two_way; + } + + namespace abstract { @@ -60,6 +68,21 @@ }; + // Value1 -> Value2 *and* Value2 -> Value1. + + template <typename E> + struct fun_v2w2v : public virtual stc::any__simple<E>, + public oln::type + { + public: + template <typename I> + oln::morpher::two_way<I, E> + operator()(oln::abstract::mutable_image<I>& input) const; + protected: + fun_v2w2v(); + }; + + // Point -> Point. template <typename E> @@ -84,6 +107,11 @@ } template <typename E> + fun_v2w2v<E>::fun_v2w2v() + { + } + + template <typename E> fun_p2p<E>::fun_p2p() { } Index: oln/core/traits.hh =================================================================== --- oln/core/traits.hh (revision 680) +++ oln/core/traits.hh (working copy) @@ -28,11 +28,12 @@ #ifndef OLN_CORE_TRAITS_HH # define OLN_CORE_TRAITS_HH - # include <mlc/assert.hh> # include <mlc/abort.hh> -# include <xtd/optraits.hh> -# include <xtd/math/ops.hh> + +# include <xtd/optraits.hh> // case #1 for C++ builtins +# include <xtd/math/ops.hh> // case #2 for xtd::fun_expr_ + # include <stc/exact.hh> # include <oln/core/type.hh> @@ -56,7 +57,7 @@ namespace xtd { - static const unsigned oln_xtd_case_op_id = 2; + static const unsigned oln_xtd_case_op_id = 3; // case #3 template <typename name, Index: oln/Makefile.am =================================================================== --- oln/Makefile.am (revision 680) +++ oln/Makefile.am (working copy) @@ -183,6 +183,7 @@ morpher/stack.hh \ morpher/thru_fun.hh \ morpher/thru_mfun.hh \ + morpher/two_way.hh \ morpher/value_cast.hh \ morpher/with_lut.hh \ \ Index: oln/morpher/two_way.hh =================================================================== --- oln/morpher/two_way.hh (revision 0) +++ oln/morpher/two_way.hh (revision 0) @@ -0,0 +1,238 @@ +// Copyright (C) 2006 EPITA Research and Development Laboratory +// +// 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. + +#ifndef OLN_MORPHER_TWO_WAY_HH +# define OLN_MORPHER_TWO_WAY_HH + +# include <oln/value/two_way.hh> +# include <oln/morpher/internal/image_value_morpher.hh> + + +namespace oln +{ + + namespace morpher + { + // Forward declaration. + template <typename Image, typename Fun> struct two_way; + + } // end of namespace oln::morpher + + + /// Super type. + template <typename Image, typename Fun> + struct set_super_type< morpher::two_way<Image, Fun> > + { + typedef morpher::two_way<Image, Fun> self_t; + typedef morpher::internal::image_value_morpher<Image, self_t> ret; + }; + + + template <typename Image, typename Fun> + struct vtypes< morpher::two_way<Image, Fun> > + { + typedef morpher::two_way<Image, Fun> self_t; + public: + typedef mlc::true_ is_computed_type; + + typedef typename Fun::result_type value_type; + typedef value::two_way<Image, Fun> lvalue_type; + }; + + template <typename Image, typename Fun> + struct single_vtype< morpher::two_way<Image, Fun>, typedef_::rvalue_type > + { + typedef typename Fun::result_type ret; + }; + + + namespace morpher + { + + /// 'Image thru Function' morpher. + template <typename Image, typename Fun> + class two_way : public internal::image_value_morpher< Image, + morpher::two_way<Image, Fun> > + { + private: + + typedef two_way<Image, Fun> self_t; + + typedef internal::image_value_morpher<Image, self_t> super_t; + using super_t::image_; + + typedef oln_rvalue(self_t) rvalue_t; + typedef oln_lvalue(self_t) lvalue_t; + typedef oln_psite(self_t) psite_t; + + public: + + two_way(Image& image, Fun fun); + two_way(Image& image); + + two_way(oln::abstract::mutable_image<Image>& image, + const oln::abstract::fun_v2w2v<Fun>& fun); + + rvalue_t impl_op_read(const psite_t& p) const; + lvalue_t impl_op_readwrite(const psite_t& p); + + protected: + + Fun fun_; + }; + + + namespace ERROR + { + + struct FIXME; + + } // end of namespace oln::morpher::ERROR + + +# ifndef OLN_INCLUDE_ONLY + + // public + + template <typename Image, typename Fun> + two_way<Image, Fun>::two_way(Image& image) : + super_t(image), + fun_() + { + } + + template <typename Image, typename Fun> + two_way<Image, Fun>::two_way(Image& image, Fun fun) : + super_t(image), + fun_(fun) + { + } + + template <typename Image, typename Fun> + two_way<Image, Fun>::two_way(oln::abstract::mutable_image<Image>& image, + const oln::abstract::fun_v2w2v<Fun>& fun) : + super_t(image.exact()), + fun_(fun.exact()) + { + } + + template <typename Image, typename Fun> + typename two_way<Image, Fun>::rvalue_t + two_way<Image, Fun>::impl_op_read(const typename two_way<Image, Fun>::psite_t& p) const + { + return fun_.direct(image_(p)); + } + + template <typename Image, typename Fun> + typename two_way<Image, Fun>::lvalue_t + two_way<Image, Fun>::impl_op_readwrite(const typename two_way<Image, Fun>::psite_t& p) + { + value::two_way<Image, Fun> tmp(image_, fun_, p); + return tmp; + } + +# endif + + } // end of namespace oln::morpher + + +# ifndef OLN_INCLUDE_ONLY + + namespace value + { + + // Ctor. + template <typename I, typename F> + two_way<I,F>::two_way(I& ima, + F fun, + const oln_psite(I)& p) + : ima_(ima), + fun_(fun), + p_(p) + { + } + + // Read. + template <typename I, typename F> + template <typename V> + two_way<I,F>::operator V() const + { + return fun_.direct(ima_(p_)); + } + + // Explicit read. + template <typename I, typename F> + typename F::result_type + two_way<I,F>::value() const + { + return fun_.direct(ima_(p_)); + } + + // Write. + + template <typename I, typename F> + template <typename V> + two_way<I,F>& + two_way<I,F>::operator=(const V& value) + { + ima_(p_) = fun_.reverse(value); + return *this; + } + + // Op <<. + template <typename I, typename F> + std::ostream& operator<<(std::ostream& ostr, + const two_way<I,F>& proxy) + { + return ostr << proxy.value(); + } + + } // end of namespace oln::value + + + namespace abstract + { + + template <typename F> + template <typename I> + oln::morpher::two_way<I, F> + fun_v2w2v<F>::operator()(oln::abstract::mutable_image<I>& input) const + { + morpher::two_way<I, F> tmp(input.exact(), this->exact()); + return tmp; + } + + } // end of namespace oln::abstract + +# endif + + + +} // end of namespace oln + + +#endif // ! OLN_MORPHER_TWO_WAY_HH