URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2007-09-07 Matthieu Garrigues <garrigues.matthieu(a)lrde.epita.fr>
add behavior to value types
* mln/value/int_u.hh: add interop to prop
* mln/value/props.hh: add interop to prop
* sandbox/garrigues/value_behavior.cc: try to implement behaviors
mln/value/int_u.hh | 1
mln/value/props.hh | 1
sandbox/garrigues/value_behavior.cc | 103 ++++++++++++++++++++++++++++++++++++
3 files changed, 105 insertions(+)
Index: trunk/milena/mln/value/props.hh
===================================================================
--- trunk/milena/mln/value/props.hh (revision 1084)
+++ trunk/milena/mln/value/props.hh (revision 1085)
@@ -31,6 +31,7 @@
/*! \file mln/value/props.hh
*
* \brief Define properties of value types.
+ * FIXME : add interop typedef in each props
*/
# include <climits>
Index: trunk/milena/mln/value/int_u.hh
===================================================================
--- trunk/milena/mln/value/int_u.hh (revision 1084)
+++ trunk/milena/mln/value/int_u.hh (revision 1085)
@@ -103,6 +103,7 @@
static const unsigned nbits = n;
typedef data_kind kind;
typedef float sum;
+ typedef int interop;
};
Index: trunk/milena/sandbox/garrigues/value_behavior.cc
===================================================================
--- trunk/milena/sandbox/garrigues/value_behavior.cc (revision 0)
+++ trunk/milena/sandbox/garrigues/value_behavior.cc (revision 1085)
@@ -0,0 +1,103 @@
+# include <mln/value/int_u8.hh>
+# include <mln/value/props.hh>
+
+using mln::value::props;
+
+/// Get the iterop type of a value type
+# define mln_interop(I) typename mln::value::props<I>::interop
+
+// value_type behavior
+template<typename V, template<typename> class B >
+struct behave : V
+{
+ typedef B<V> behavior;
+ typedef V value;
+
+ behave(const typename behavior::to_compute v)
+ {
+ this->v_ = behavior::compute(v);
+ }
+
+ V&
+ operator= (const typename behavior::to_compute& v)
+ {
+ this->v_ = behavior::compute(v);
+ }
+
+ V&
+ operator+= (const typename behavior::to_compute& v)
+ {
+ this->v_ = behavior::compute(this->v_ + v);
+ }
+
+ V&
+ operator-= (const typename behavior::to_compute& v)
+ {
+ this->v_ = behavior::compute(this->v_ - v);
+ }
+
+};
+
+template<typename V>
+struct saturated
+{
+ typedef V val;
+
+ typedef mln_interop(val) to_compute;
+
+ static val compute(to_compute n)
+ {
+ if (props<val>::min() > n)
+ return props<val>::min();
+ if (props<val>::max() < n)
+ return props<val>::max();
+ return n;
+ }
+
+};
+
+// template<typename V>
+// struct round
+// {
+// typedef V val;
+// // to_compute ? int or float?
+// typedef mln_interop(val) to_compute;
+// static val compute(to_compute n)
+// {
+// if (props<val>::min() > n)
+// return props<val>::min();
+// if (props<val>::max() < n)
+// return props<val>::max();
+// return n;
+// }
+
+// };
+
+int main()
+{
+
+ typedef behave<mln::value::int_u8, saturated> my_u8_sat;
+ typedef behave<mln::value::int_u<3>, saturated> my_u3_sat;
+
+ my_u8_sat i=259;
+ std::cout << i << std::endl;
+ i += 10;
+ std::cout << i << std::endl;
+
+ i -= 5;
+ std::cout << i << std::endl;
+
+ i -= 500;
+ std::cout << i << std::endl;
+ std::cout << std::endl;
+
+
+ my_u3_sat j = -5000;
+ std::cout << j << std::endl;
+ j = 2;
+ std::cout << j << std::endl;
+ j = 3;
+ std::cout << j << std::endl;
+ j = 11;
+ std::cout << j << std::endl;
+}