* mln/world/kn/safe_cast.hh: New overloads.
* tests/world/kn/Makefile.am: New target.
* tests/world/kn/safe_cast.cc: New.
---
milena/ChangeLog | 10 ++
milena/mln/world/kn/safe_cast.hh | 112 ++++++++++++++++----
milena/tests/world/kn/Makefile.am | 2 +
milena/tests/world/kn/safe_cast.cc | 209 ++++++++++++++++++++++++++++++++++++
4 files changed, 314 insertions(+), 19 deletions(-)
create mode 100644 milena/tests/world/kn/safe_cast.cc
diff --git a/milena/ChangeLog b/milena/ChangeLog
index 9c34521..dcfadaf 100644
--- a/milena/ChangeLog
+++ b/milena/ChangeLog
@@ -1,5 +1,15 @@
2012-10-29 Guillaume Lazzara <z(a)lrde.epita.fr>
+ More safe cast overloads.
+
+ * mln/world/kn/safe_cast.hh: New overloads.
+
+ * tests/world/kn/Makefile.am: New target.
+
+ * tests/world/kn/safe_cast.cc: New.
+
+2012-10-29 Guillaume Lazzara <z(a)lrde.epita.fr>
+
* mln/world/kn/fill_0_from_1_faces.hh: Add an overload with
functions.
diff --git a/milena/mln/world/kn/safe_cast.hh b/milena/mln/world/kn/safe_cast.hh
index d4e4f5c..39309cd 100644
--- a/milena/mln/world/kn/safe_cast.hh
+++ b/milena/mln/world/kn/safe_cast.hh
@@ -31,7 +31,7 @@
#ifndef MLN_WORLD_KN_SAFE_CAST_HH
# define MLN_WORLD_KN_SAFE_CAST_HH
-# include <mln/value/int_u8.hh>
+# include <mln/value/int_u.hh>
# include <mln/value/intsub.hh>
# include <mln/value/interval.hh>
@@ -62,48 +62,85 @@ namespace mln
namespace value
{
- template <unsigned n>
- void safe_cast_(const int_u8& from, intsub<n>& to)
+ template <unsigned m, unsigned n>
+ void safe_cast_(const int_u<m>& from, intsub<n>& to)
{
- to = from.to_equiv();
+ to = from.to_interop();
}
- template <unsigned n>
- void safe_cast_(const int_u8& from, interval<intsub<n> >& to)
+ template <unsigned m, unsigned n>
+ void safe_cast_(const int_u<m>& from, interval<intsub<n> >&
to)
{
to = intsub<n>(from.to_interop());
}
- inline
- void safe_cast_(const int_u8& from, interval<int_u8>& to)
+ template <unsigned m>
+ void safe_cast_(const int_u<m>& from, interval<int_u<m> >&
to)
+ {
+ to = from;
+ }
+
+ template <unsigned m>
+ void safe_cast_(const int_u<m>& from, interval<int>& to)
{
to = from;
}
- inline
- void safe_cast_(const int_u8& from, int& to)
+ template <unsigned m>
+ void safe_cast_(const int_u<m>& from, int& to)
{
to = from.to_interop();
}
+ void safe_cast_(const interval<int>& from, int& to)
+ {
+ if (!from.is_degenerated())
+ std::abort();
+ to = from.first();
+ }
+
+ template <unsigned m>
+ void safe_cast_(const interval<int>& from, int_u<m>& to)
+ {
+ if (!from.is_degenerated())
+ std::abort();
+ to = from.first();
+ }
+
template <unsigned n>
- void safe_cast_(const interval<intsub<n> >& from, int_u8& to)
+ void safe_cast_(const interval<int>& from, intsub<n>& to)
+ {
+ if (!from.is_degenerated())
+ std::abort();
+ to = from.first();
+ }
+
+ template <unsigned n, unsigned m>
+ void safe_cast_(const interval<intsub<n> >& from, int_u<m>&
to)
{
if (!from.is_degenerated())
std::abort();
to = intsub<n>(from.first());
}
- inline
- void safe_cast_(const interval<int_u8>& from, int_u8& to)
+ template <unsigned m>
+ void safe_cast_(const interval<int_u<m> >& from, int_u<m>&
to)
{
if (!from.is_degenerated())
std::abort();
to = from.first();
}
- template <unsigned n>
- void safe_cast_(const interval<int_u8>& from, intsub<n>& to)
+ template <unsigned m, unsigned n>
+ void safe_cast_(const interval<int_u<m> >& from, intsub<n>&
to)
+ {
+ if (!from.is_degenerated())
+ std::abort();
+ to = from.first().to_interop();
+ }
+
+ template <unsigned m>
+ void safe_cast_(const interval<int_u<m> >& from, int& to)
{
if (!from.is_degenerated())
std::abort();
@@ -132,13 +169,25 @@ namespace mln
to = interval<intsub<n> >(from);
}
- inline
- void safe_cast_(const int& from, int_u8& to)
+ void safe_cast_(const int& from, interval<int>& to)
+ {
+ to = interval<int>(from);
+ }
+
+ template <unsigned m>
+ void safe_cast_(const int& from, int_u<m>& to)
{
to = from;
}
template <unsigned n>
+ void safe_cast_(const int& from, intsub<n>& to)
+ {
+ to = from;
+ }
+
+
+ template <unsigned n>
void safe_cast_(const intsub<n>& from, intsub<2*n>& to)
{
to = static_cast<intsub<2*n> >(from.to_int());
@@ -150,12 +199,37 @@ namespace mln
to = static_cast<intsub<n/2> >(from.to_int());
}
- template <unsigned n>
- void safe_cast_(const intsub<n>& from, int_u8& to)
+ template <unsigned n, unsigned m>
+ void safe_cast_(const intsub<n>& from, int_u<m>& to)
{
to = from.to_interop();
}
+ template <unsigned n>
+ void safe_cast_(const intsub<n>& from, int& to)
+ {
+ to = from;
+ }
+
+ template <unsigned n, unsigned m>
+ void safe_cast_(const intsub<n>& from, interval<int_u<m> >&
to)
+ {
+ to = interval<int_u<m> >(from.to_interop());
+ }
+
+ template <unsigned n>
+ void safe_cast_(const intsub<n>& from, interval<int>& to)
+ {
+ to = interval<int>(from.to_interop());
+ }
+
+ template <unsigned n>
+ void safe_cast_(const intsub<n>& from, interval<intsub<n>
>& to)
+ {
+ to = interval<intsub<n> >(from);
+ }
+
+
} // end of namespace mln::value
diff --git a/milena/tests/world/kn/Makefile.am b/milena/tests/world/kn/Makefile.am
index 84b0b2f..d7d2fbe 100644
--- a/milena/tests/world/kn/Makefile.am
+++ b/milena/tests/world/kn/Makefile.am
@@ -37,6 +37,7 @@ check_PROGRAMS = \
is_1_face_horizontal \
is_2_face \
level \
+ safe_cast \
saturate \
un_immerse
@@ -56,6 +57,7 @@ is_1_face_vertical_SOURCES = is_1_face_vertical.cc
is_1_face_horizontal_SOURCES = is_1_face_horizontal.cc
is_2_face_SOURCES = is_2_face.cc
level_SOURCES = level.cc
+safe_cast_SOURCES = safe_cast.cc
saturate_SOURCES = saturate.cc
un_immerse_SOURCES = un_immerse.cc
diff --git a/milena/tests/world/kn/safe_cast.cc b/milena/tests/world/kn/safe_cast.cc
new file mode 100644
index 0000000..7c5be20
--- /dev/null
+++ b/milena/tests/world/kn/safe_cast.cc
@@ -0,0 +1,209 @@
+// Copyright (C) 2012 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.
+
+/// \file
+
+#include <mln/value/int_u8.hh>
+#include <mln/value/interval.hh>
+#include <mln/value/intsub.hh>
+#include <mln/world/kn/safe_cast.hh>
+
+int main()
+{
+ using namespace mln;
+ using namespace mln::value;
+ using namespace mln::world;
+
+ // int -> int_u<8>
+ {
+ int i = 2;
+ int_u<8> j = kn::safe_cast_to<int_u<8> >(i);
+ mln_assertion(j == 2);
+ }
+
+ // int -> intsub<2>
+ {
+ int i = 2;
+ intsub<2> j = kn::safe_cast_to<intsub<2> >(i);
+ mln_assertion(j == 2);
+ }
+
+
+ // int_u<8> -> int
+ {
+ int_u<8> i = 2;
+ int j = kn::safe_cast_to<int>(i);
+ mln_assertion(j == 2);
+ }
+
+ // int_u<8> -> intsub<2>
+ {
+ int_u<8> i = 2;
+ intsub<2> j = kn::safe_cast_to<intsub<2> >(i);
+ mln_assertion(j == 2);
+ }
+
+ // intsub<2> -> int
+ {
+ intsub<2> i = 2;
+ int j = kn::safe_cast_to<int>(i);
+ mln_assertion(j == 2);
+ }
+
+ // intsub<2> -> int_u<8>
+ {
+ intsub<2> i = 2;
+ int_u<8> j = kn::safe_cast_to<int_u<8> >(i);
+ mln_assertion(j == 2);
+ }
+
+ // int -> interval<int>
+ {
+ int i = 2;
+ interval<int> j = kn::safe_cast_to<interval<int> >(i);
+ mln_assertion(j.is_degenerated() && j.first() == 2);
+ }
+
+ // int_u -> interval<int>
+ {
+ int_u<8> i = 2;
+ interval<int> j = kn::safe_cast_to<interval<int> >(i);
+ mln_assertion(j.is_degenerated() && j.first() == 2);
+ }
+
+ // int_u -> interval<int>
+ {
+ intsub<2> i = 2;
+ interval<int> j = kn::safe_cast_to<interval<int> >(i);
+ mln_assertion(j.is_degenerated() && j.first() == 2);
+ }
+
+ // int -> interval<int_u<8> >
+ {
+ int i = 2;
+ interval<int_u<8> > j = kn::safe_cast_to<interval<int_u<8>
> >(i);
+ mln_assertion(j.is_degenerated() && j.first() == 2);
+ }
+
+ // int_u<8> -> interval<int_u<8> >
+ {
+ int_u<8> i = 2;
+ interval<int_u<8> > j = kn::safe_cast_to<interval<int_u<8>
> >(i);
+ mln_assertion(j.is_degenerated() && j.first() == 2);
+ }
+
+ // intsub<2> -> interval<int_u<8> >
+ {
+ intsub<2> i = 2;
+ interval<int_u<8> > j = kn::safe_cast_to<interval<int_u<8>
> >(i);
+ mln_assertion(j.is_degenerated() && j.first() == 2);
+ }
+
+
+ // int -> interval<intsub<2> >
+ {
+ int i = 2;
+ interval<intsub<2> > j = kn::safe_cast_to<interval<intsub<2>
> >(i);
+ mln_assertion(j.is_degenerated() && j.first() == 2);
+ }
+
+ // int_u<8> -> interval<intsub<2> >
+ {
+ int_u<8> i = 2;
+ interval<intsub<2> > j = kn::safe_cast_to<interval<intsub<2>
> >(i);
+ mln_assertion(j.is_degenerated() && j.first() == 2);
+ }
+
+ // intsub<2> -> interval<intsub<2> >
+ {
+ intsub<2> i = 2;
+ interval<intsub<2> > j = kn::safe_cast_to<interval<intsub<2>
> >(i);
+ mln_assertion(j.is_degenerated() && j.first() == 2);
+ }
+
+ // interval<int> -> int
+ {
+ interval<int> i(2,2);
+ int j = kn::safe_cast_to<int>(i);
+ mln_assertion(j == 2);
+ }
+
+ // interval<int> -> int_u<8>
+ {
+ interval<int> i(2,2);
+ int_u<8> j = kn::safe_cast_to<int_u<8> >(i);
+ mln_assertion(j == 2);
+ }
+
+ // interval<int> -> int_sub<2>
+ {
+ interval<int> i(2,2);
+ intsub<2> j = kn::safe_cast_to<intsub<2> >(i);
+ mln_assertion(j == 2);
+ }
+
+ // interval<int_u<8> > -> int
+ {
+ interval<int_u<8> > i(2,2);
+ int j = kn::safe_cast_to<int>(i);
+ mln_assertion(j == 2);
+ }
+
+ // interval<int_u<8> > -> int_u<8>
+ {
+ interval<int_u<8> > i(2,2);
+ int_u<8> j = kn::safe_cast_to<int_u<8> >(i);
+ mln_assertion(j == 2);
+ }
+
+ // interval<int_u<8> > -> int_sub<2>
+ {
+ interval<int_u<8> > i(2,2);
+ intsub<2> j = kn::safe_cast_to<intsub<2> >(i);
+ mln_assertion(j == 2);
+ }
+
+ // interval<intsub<2> > -> int
+ {
+ interval<intsub<2> > i(2,2);
+ int j = kn::safe_cast_to<int>(i);
+ mln_assertion(j == 2);
+ }
+
+ // interval<intsub<2> > -> int_u<8>
+ {
+ interval<intsub<2> > i(2,2);
+ int_u<8> j = kn::safe_cast_to<int_u<8> >(i);
+ mln_assertion(j == 2);
+ }
+
+ // interval<intsub<2> > -> int_sub<2>
+ {
+ interval<intsub<2> > i(2,2);
+ intsub<2> j = kn::safe_cast_to<intsub<2> >(i);
+ mln_assertion(j == 2);
+ }
+
+}
--
1.7.2.5