2006-10-20 Thierry GERAUD <theo(a)tegucigalpa.lrde.epita.fr>
Add morpher based on mutators.
* tests/value/accessor.cc: New.
* tests/value/Makefile.am: Update.
* oln/morpher/thru_mfun.hh: New.
* oln/value/accessor.hh: New.
* oln/Makefile.am: Update.
* oln/morpher/thru_fun.hh: Use shortcuts.
Index: tests/value/accessor.cc
===================================================================
--- tests/value/accessor.cc (revision 0)
+++ tests/value/accessor.cc (revision 0)
@@ -0,0 +1,51 @@
+// 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 oln::value::a_*.
+#include <iostream>
+
+#include <oln/color/rgb.hh>
+
+#include <oln/basics2d.hh>
+#include <oln/value/accessor.hh>
+#include <oln/debug/print.hh>
+
+
+int main()
+{
+ using namespace oln;
+
+ typedef color::rgb_<int> rgb_t;
+ rgb_t c;
+
+ image2d<rgb_t> ima(3,3);
+ point2d p(0,0);
+
+ ima(p).red() = 51;
+ assert( a_red(ima)(p) == 51 );
+
+}
Index: tests/value/Makefile.am
===================================================================
--- tests/value/Makefile.am (revision 664)
+++ tests/value/Makefile.am (working copy)
@@ -19,9 +19,11 @@
CXXFLAGS += $(TESTS_CXXFLAGS)
check_PROGRAMS = \
+ accessor \
grey
# Value.
+accessor_SOURCES = accessor.cc
grey_SOURCES = grey.cc
Index: oln/Makefile.am
===================================================================
--- oln/Makefile.am (revision 664)
+++ oln/Makefile.am (working copy)
@@ -168,8 +168,10 @@
morpher/tags.hh \
morpher/slice.hh \
morpher/thru_fun.hh \
+ morpher/thru_mfun.hh \
morpher/value_cast.hh \
\
+ value/accessor.hh \
value/all.hh \
value/default.hh \
value/greylevel.hh \
Index: oln/morpher/thru_mfun.hh
===================================================================
--- oln/morpher/thru_mfun.hh (revision 0)
+++ oln/morpher/thru_mfun.hh (revision 0)
@@ -0,0 +1,185 @@
+// 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_THRU_MFUN
+# define OLN_MORPHER_THRU_MFUN
+
+# include <xtd/res.hh>
+# include <xtd/abstract/open_nary_fun.hh>
+# include <xtd/mexpr.hh>
+# include <oln/morpher/internal/image_value_morpher.hh>
+
+
+namespace oln
+{
+
+ namespace morpher
+ {
+ // Forward declaration.
+ template <typename Image, typename Fun> struct thru_mfun;
+
+ } // end of namespace oln::morpher
+
+
+ /// Super type.
+ template <typename Image, typename Fun>
+ struct set_super_type< morpher::thru_mfun<Image, Fun> >
+ {
+ typedef morpher::thru_mfun<Image, Fun> self_t;
+ typedef morpher::internal::image_value_morpher<Image, self_t> ret;
+ };
+
+
+ template <typename Image, typename Fun>
+ struct vtypes< morpher::thru_mfun<Image, Fun> >
+ {
+ private:
+ typedef oln_type_of(Image, rvalue) old_value_type;
+ public:
+ typedef mlc::false_ is_computed_type;
+ typedef typename xtd::res_<Fun, old_value_type>::ret value_type;
+ typedef value_type& lvalue_type;
+ };
+
+ template <typename Image, typename Fun>
+ struct single_vtype< morpher::thru_mfun<Image, Fun>, typedef_::rvalue_type
>
+ {
+ typedef morpher::thru_mfun<Image, Fun> self_t;
+ typedef oln_value(self_t) ret;
+ };
+
+
+
+ namespace morpher
+ {
+
+ /// 'Image thru Function' morpher.
+ template <typename Image, typename Fun>
+ class thru_mfun : public internal::image_value_morpher< Image,
+ morpher::thru_mfun<Image, Fun> >
+ {
+ private:
+
+ typedef thru_mfun<Image, Fun> self_t;
+ typedef internal::image_value_morpher<Image, self_t> super_t;
+
+ typedef oln_psite(self_t) psite_t;
+ typedef oln_rvalue(self_t) rvalue_t;
+ typedef oln_lvalue(self_t) lvalue_t;
+
+ public:
+
+ thru_mfun(const Image& image, const 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 AN_xtd_mutator_ONLY_WORKS_ON_AN_oln_abstract_mutable_image;
+
+ } // end of namespace oln::morpher::ERROR
+
+
+# ifndef OLN_INCLUDE_ONLY
+
+ template <typename Image, typename Fun>
+ thru_mfun<Image, Fun>::thru_mfun(const Image& image, const Fun& fun) :
+ super_t(image),
+ fun_(fun)
+ {
+ }
+
+ template <typename Image, typename Fun>
+ typename thru_mfun<Image, Fun>::rvalue_t
+ thru_mfun<Image, Fun>::impl_op_read(const typename thru_mfun<Image,
Fun>::psite_t& p) const
+ {
+ return fun_(this->image_(p));
+ }
+
+ template <typename Image, typename Fun>
+ typename thru_mfun<Image, Fun>::lvalue_t
+ thru_mfun<Image, Fun>::impl_op_readwrite(const typename thru_mfun<Image,
Fun>::psite_t& p)
+ {
+ return fun_(this->image_(p));
+ }
+
+# endif
+
+ } // end of namespace oln::morpher
+
+
+} // end of namespace oln
+
+
+
+namespace xtd
+{
+
+ // open_nary_fun_<1, Fun>::operator()(A& a) const
+ // where A is an oln::abstract::mutable_image<I>
+
+ template <typename Fun, typename A>
+ struct case_ < tag::mutable_fun_operator, mlc::pair_<Fun, A>, 1 >
+ : where_< mlc_is_a(A, oln::abstract::image) >
+ {
+ typedef stc_to_exact(A) I;
+ typedef oln::morpher::thru_mfun<I, Fun> res;
+ typedef oln::morpher::thru_mfun<I, Fun> mutable_res;
+
+ static res impl(const Fun& fun, // target
+ const oln::abstract::image<I>& ima)
+ {
+ mlc::assert_< mlc_is_a(A, oln::abstract::mutable_image),
+
oln::morpher::ERROR::AN_xtd_mutator_ONLY_WORKS_ON_AN_oln_abstract_mutable_image
>::check();
+ // FIXME: ima is const... so there may be a problem!
+ res tmp(ima.exact(), fun);
+ return tmp;
+ }
+
+ static res impl(const Fun& fun, // target
+ oln::abstract::image<I>& ima)
+ {
+ mlc::assert_< mlc_is_a(A, oln::abstract::mutable_image),
+
oln::morpher::ERROR::AN_xtd_mutator_ONLY_WORKS_ON_AN_oln_abstract_mutable_image
>::check();
+ res tmp(ima.exact(), fun);
+ return tmp;
+ }
+ };
+
+} // end of namespace xtd
+
+
+
+#endif // ! OLN_MORPHER_THRU_MFUN
Index: oln/morpher/thru_fun.hh
===================================================================
--- oln/morpher/thru_fun.hh (revision 664)
+++ oln/morpher/thru_fun.hh (working copy)
@@ -69,7 +69,7 @@
struct single_vtype< morpher::thru_fun<Image, Fun>, typedef_::rvalue_type
>
{
typedef morpher::thru_fun<Image, Fun> self_t;
- typedef oln_type_of(self_t, value) ret;
+ typedef oln_value(self_t) ret;
};
@@ -87,8 +87,8 @@
typedef thru_fun<Image, Fun> self_t;
typedef internal::image_value_morpher<Image, self_t> super_t;
- typedef oln_type_of(self_t, psite) psite_t;
- typedef oln_type_of(self_t, rvalue) rvalue_t;
+ typedef oln_psite(self_t) psite_t;
+ typedef oln_rvalue(self_t) rvalue_t;
public:
@@ -135,7 +135,7 @@
// where A is an oln::abstract::image<I>
template <typename Fun, typename A>
- struct case_ < tag::fun_operator_1, mlc::pair_<Fun, A>, 2 >
+ struct case_ < tag::fun_operator_1, mlc::pair_<Fun, A>, 1 >
: where_< mlc_is_a(A, oln::abstract::image) >
{
typedef stc_to_exact(A) I;
Index: oln/value/accessor.hh
===================================================================
--- oln/value/accessor.hh (revision 0)
+++ oln/value/accessor.hh (revision 0)
@@ -0,0 +1,56 @@
+// 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_VALUE_ACCESSOR_HH
+# define OLN_VALUE_ACCESSOR_HH
+
+#include <oln/morpher/thru_fun.hh>
+#include <xtd/accessor.hh>
+
+
+xtd_decl_accessor(value);
+xtd_decl_accessor(vec);
+
+xtd_decl_accessor(red);
+xtd_decl_accessor(green);
+xtd_decl_accessor(blue);
+
+
+namespace oln
+{
+
+ static const xtd::accessor::value_type a_value;
+ static const xtd::accessor::vec_type a_vec;
+
+ static const xtd::accessor::red_type a_red;
+ static const xtd::accessor::green_type a_green;
+ static const xtd::accessor::blue_type a_blue;
+
+} // end of namespace oln
+
+
+#endif // ! OLN_VALUE_ACCESSOR_HH