https://svn.lrde.epita.fr/svn/oln/trunk/milena
Index: ChangeLog
from Thierry Geraud <thierry.geraud(a)lrde.epita.fr>
Revamp value::proxy class.
* mln/core/image/imorph/labeled_image.hh: Fix missing include.
* mln/value/proxy.hh (proxy<const I>): Remove; useless.
(proxy): Change inheritance from Value to Proxy.
(operator): Remove; they are obsolete thanx to Proxy.
(ima_): Change type from & to *; late initialization is allowed.
(~proxy): De-activate static checks.
* tests/value/proxy.cc: New.
* tests/value/Makefile.am: Update.
mln/core/image/imorph/labeled_image.hh | 1
mln/value/proxy.hh | 282 +++++++++------------------------
tests/value/Makefile.am | 2
tests/value/proxy.cc | 84 +++++++++
4 files changed, 168 insertions(+), 201 deletions(-)
Index: mln/core/image/imorph/labeled_image.hh
--- mln/core/image/imorph/labeled_image.hh (revision 4333)
+++ mln/core/image/imorph/labeled_image.hh (working copy)
@@ -34,6 +34,7 @@
# include <mln/core/concept/function.hh>
# include <mln/core/internal/image_identity.hh>
# include <mln/core/site_set/box.hh>
+# include <mln/core/routine/duplicate.hh>
# include <mln/accu/pair.hh>
# include <mln/accu/nil.hh>
Index: mln/value/proxy.hh
--- mln/value/proxy.hh (revision 4333)
+++ mln/value/proxy.hh (working copy)
@@ -1,4 +1,5 @@
-// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
+// Copyright (C) 2007, 2008, 2009 EPITA Research and Development
+// Laboratory (LRDE)
//
// This file is part of Olena.
//
@@ -29,8 +30,17 @@
/// \file
///
/// Define a generic proxy class for an image pixel value.
+///
+/// \todo Maybe we should overload conversion operator with proxy<I>
+/// or... (we have to choose) maybe we should add cpy ctors (with
+/// proxy<I> and proxy<J>).
+///
+/// \todo Rename value::proxy as value::shell and fix the specific
+/// code from Fred (the 2nd parameter, namely F, is useless)
+///
+/// \todo Checks in ~proxy eventually should be re-activated.
-# include <mln/core/concept/value.hh>
+# include <mln/core/concept/proxy.hh>
# include <mln/trait/value_.hh>
# include <mln/metal/unconst.hh>
@@ -38,7 +48,7 @@
namespace mln
{
- // Fwd decl.
+ // Forward declaration.
namespace value {
template <typename I> class proxy;
}
@@ -67,12 +77,13 @@
namespace value
{
- /*! \brief Generic proxy class for an image pixel value.
- *
- * The parameter \c I is an image type.
- */
+ /// \brief Generic proxy class for an image pixel value.
+ ///
+ /// The parameter \c I is an image type.
+ //
template <typename I>
- class proxy : public Value< proxy<I> >
+ class proxy : public Proxy< proxy<I> >,
+ public mln::internal::proxy_impl< mln_value(I), proxy<I> >
{
public:
@@ -83,120 +94,68 @@
typedef mln_value(I) equiv;
/// Constructor.
+ proxy();
+
+ /// Constructor.
proxy(I& ima, const mln_psite(I)& p);
/// Destructor.
~proxy();
- /// Assignment (write access); general version.
- template <typename V>
- proxy<I>& operator=(const V& v);
+ // Assignment (write access); "natural" version.
+ proxy<I>& operator=(const mln_value(I)& v);
/// Assignment (write access); replacement for default op.
proxy<I>& operator=(const proxy<I>& rhs);
/// Assignment (write access); with other proxy.
- template <typename II>
- proxy<I>& operator=(const proxy<II>& rhs);
-
- /// Conversion (read access); general version.
- template <typename V>
- operator V() const;
-
- /// Conversion (read access); precise version.
- operator mln_value(I)() const;
-
- /// Explicit read access.
- mln_value(I) to_value() const;
-
- protected:
- I& ima_;
- mln_psite(I) p_;
- };
+ template <typename J>
+ proxy<I>& operator=(const proxy<J>& rhs);
+// /// Conversion (read access); "natural" version.
+// operator mln_value(I)() const;
- /*! \brief Generic proxy class for an image pixel value.
- *
- * The parameter \c I is an image type.
- */
- template <typename I>
- class proxy< const I > : public Value< proxy<const I> >
- {
- public:
+// /// Conversion (read access); toward a proxy.
+// template <typename J>
+// operator proxy<J>() const;
- /// Encoding associated type.
- typedef void enc; // FIXME
- /// Equivalent associated type.
- typedef mln_value(I) equiv;
+// /// Conversion (read access); general version.
+// template <typename V>
+// operator V() const;
- /// Constructor.
- proxy(const I& ima, const mln_psite(I)& p);
- /// Destructor.
- ~proxy();
+ /// Explicit read access.
+ mln_value(I) to_value() const;
- /// Conversion (read access); general version.
- template <typename V>
- operator V() const;
- /// Conversion (read access); precise version.
- operator mln_value(I)() const;
+ // As a proxy:
- /// Explicit read access.
- mln_value(I) to_value() const;
+ /// Return the proxy subject.
+ mln_value(I) subj_();
protected:
- const I& ima_;
+ I* ima_;
mln_psite(I) p_;
};
- template <typename I>
- bool operator==(const proxy<I>& lhs, const mln_value(I)& rhs);
-
- template <typename I>
- bool operator==(const mln_value(I)& lhs, const proxy<I>& rhs);
-
- template <typename I, typename J>
- bool operator==(const proxy<I>& lhs, const proxy<J>& rhs);
-
-
- template <typename I>
- bool operator<(const proxy<I>& lhs, const mln_value(I)& rhs);
-
- template <typename I>
- bool operator<(const mln_value(I)& lhs, const proxy<I>& rhs);
-
- template <typename I, typename J>
- bool operator<(const proxy<I>& lhs, const proxy<J>& rhs);
-
-
- // FIXME: Ops such as +=, etc.
-
-
-
- /*! \brief Print a value proxy \p x into the output stream \p ostr.
- *
- * \param[in,out] ostr An output stream.
- * \param[in] x A value proxy.
- *
- * \return The modified output stream \p ostr.
- */
- template <typename I>
- std::ostream& operator<<(std::ostream& ostr, const proxy<I>&
x);
-
# ifndef MLN_INCLUDE_ONLY
- // proxy<I>
+ template <typename I>
+ inline
+ proxy<I>::proxy()
+ : ima_(0)
+ {
+ }
template <typename I>
inline
proxy<I>::proxy(I& ima, const mln_psite(I)& p)
- : ima_(ima),
+ : ima_(&ima),
p_(p)
{
}
@@ -205,19 +164,19 @@
inline
proxy<I>::~proxy()
{
- mln_rvalue(I) (I::*mr)(const mln_psite(I)&) const = & I::read_;
- mr = 0;
- void (I::*mw)(const mln_psite(I)&, const mln_value(I)&) = & I::write_;
- mw = 0;
+// mln_rvalue(I) (I::*mr)(const mln_psite(I)&) const = & I::read_;
+// mr = 0;
+// void (I::*mw)(const mln_psite(I)&, const mln_value(I)&) = &
I::write_;
+// mw = 0;
}
template <typename I>
- template <typename V>
inline
proxy<I>&
- proxy<I>::operator=(const V& v)
+ proxy<I>::operator=(const mln_value(I)& v)
{
- ima_.write_(p_, v);
+ mln_precondition(ima_ != 0);
+ ima_->write_(p_, v);
return *this;
}
@@ -226,138 +185,59 @@
proxy<I>&
proxy<I>::operator=(const proxy<I>& rhs)
{
+ mln_precondition(ima_ != 0);
+ if (&rhs == this)
+ return *this; // No-op.
this->operator=(rhs.to_value());
return *this;
}
template <typename I>
- template <typename II>
+ template <typename J>
inline
proxy<I>&
- proxy<I>::operator=(const proxy<II>& rhs)
+ proxy<I>::operator=(const proxy<J>& rhs)
{
+ mln_precondition(ima_ != 0);
this->operator=(rhs.to_value());
return *this;
}
- template <typename I>
- template <typename V>
- inline
- proxy<I>::operator V() const
- {
- return ima_.read_(p_);
- }
-
- template <typename I>
- inline
- proxy<I>::operator mln_value(I)() const
- {
- return ima_.read_(p_);
- }
+// template <typename I>
+// template <typename V>
+// inline
+// proxy<I>::operator V() const
+// {
+// mln_precondition(ima_ != 0);
+// return ima_->read_(p_);
+// }
+
+// template <typename I>
+// inline
+// proxy<I>::operator mln_value(I)() const
+// {
+// mln_precondition(ima_ != 0);
+// return ima_->read_(p_);
+// }
template <typename I>
inline
mln_value(I)
proxy<I>::to_value() const
{
- return ima_.read_(p_);
- }
-
- // proxy<const I>
-
- template <typename I>
- inline
- proxy<const I>::proxy(const I& ima, const mln_psite(I)& p)
- : ima_(ima),
- p_(p)
- {
- }
-
- template <typename I>
- inline
- proxy<const I>::~proxy()
- {
- mln_rvalue(I) (I::*mr)(const mln_psite(I)&) const = & I::read_;
- mr = 0;
- }
-
- template <typename I>
- template <typename V>
- inline
- proxy<const I>::operator V() const
- {
- return ima_.read_(p_);
- }
-
- template <typename I>
- inline
- proxy<const I>::operator mln_value(I)() const
- {
- return ima_.read_(p_);
+ mln_precondition(ima_ != 0);
+ return ima_->read_(p_);
}
template <typename I>
inline
mln_value(I)
- proxy<const I>::to_value() const
- {
- return ima_.read_(p_);
- }
-
- // operator <<
-
- template <typename I>
- inline
- std::ostream& operator<<(std::ostream& ostr, const proxy<I>&
x)
+ proxy<I>::subj_()
{
- return ostr << x.to_value();
+ mln_precondition(ima_ != 0);
+ return ima_->read_(p_);
}
- // operator ==
-
- template <typename I>
- inline
- bool operator==(const proxy<I>& lhs, const mln_value(I)& rhs)
- {
- return lhs.to_value() == rhs;
- }
-
- template <typename I>
- inline
- bool operator==(const mln_value(I)& lhs, const proxy<I>& rhs)
- {
- return lhs == rhs.to_value();
- }
-
- template <typename I, typename J>
- inline
- bool operator==(const proxy<I>& lhs, const proxy<J>& rhs)
- {
- return lhs.to_value() == rhs.to_value();
- }
-
- // operator <
-
- template <typename I>
- inline
- bool operator<(const proxy<I>& lhs, const mln_value(I)& rhs)
- {
- return lhs.to_value() < rhs;
- }
-
- template <typename I>
- inline
- bool operator<(const mln_value(I)& lhs, const proxy<I>& rhs)
- {
- return lhs < rhs.to_value();
- }
-
- template <typename I, typename J>
- inline
- bool operator<(const proxy<I>& lhs, const proxy<J>& rhs)
- {
- return lhs.to_value() < rhs.to_value();
- }
# endif // ! MLN_INCLUDE_ONLY
Index: tests/value/proxy.cc
--- tests/value/proxy.cc (revision 0)
+++ tests/value/proxy.cc (revision 0)
@@ -0,0 +1,84 @@
+// 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.
+
+#include <map>
+#include <mln/value/proxy.hh>
+
+
+template <typename T>
+struct test
+{
+ // Interface required by mln::value::proxy.
+
+ typedef int psite;
+ typedef T value;
+
+ void write_(const psite& p, const value& v)
+ {
+ // std::cout << "write value " << v << " at "
<< p << std::endl;
+ data_[p] = v;
+ }
+
+ value read_(const psite& p) const
+ {
+ // std::cout << "read at " << p << " -> value
" << data_[p] << std::endl;
+ return data_[p];
+ }
+
+ mutable std::map<psite,value> data_;
+
+ // Some extra interface of an Image.
+
+ value operator()(const psite& p) const
+ {
+ return read_(p); // Read only!
+ }
+
+ mln::value::proxy<test> operator()(const psite& p)
+ {
+ mln::value::proxy<test> prx(*this, p);
+ return prx;
+ }
+};
+
+
+
+int main()
+{
+ using namespace mln;
+
+ test<float> ima;
+
+ int p = 3;
+
+ ima(p) = 8.5;
+ ima(p) = 6 * ima(p);
+ mln_assertion(ima(p) == 51);
+
+ test<int> ima_;
+ ima_(p) = 51;
+ ima_(p) = ima_(p);
+ mln_assertion(ima_(p) == 51);
+}
Index: tests/value/Makefile.am
--- tests/value/Makefile.am (revision 4333)
+++ tests/value/Makefile.am (working copy)
@@ -33,6 +33,7 @@
int_u8 \
interop \
label \
+ proxy \
rgb8 \
scalar \
set \
@@ -55,6 +56,7 @@
int_u8_SOURCES = int_u8.cc
interop_SOURCES = interop.cc
label_SOURCES = label.cc
+proxy_SOURCES = proxy.cc
rgb8_SOURCES = rgb8.cc
scalar_SOURCES = scalar.cc
set_SOURCES = set.cc