Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
mln/data/convert.hh
1 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_DATA_CONVERT_HH
27 # define MLN_DATA_CONVERT_HH
28 
34 
35 # include <mln/core/routine/duplicate.hh>
36 # include <mln/fun/v2v/convert.hh>
37 # include <mln/data/transform.hh>
38 
39 
40 namespace mln
41 {
42 
43  namespace data
44  {
45 
50  //
51  template <typename V, typename I>
52  mln_ch_value(I, V)
53  convert(const V& v, const Image<I>& input);
54 
55 
56 
57 # ifndef MLN_INCLUDE_ONLY
58 
59  namespace internal
60  {
61 
62  template <typename V, typename I>
63  inline
64  void
65  convert_tests(const V&, const Image<I>& input)
66  {
67  mln_precondition(exact(input).is_valid());
68  (void) input;
69  }
70 
71  } // using namespace mln::data::internal
72 
73  namespace impl
74  {
75 
76  namespace generic
77  {
78 
79  template <typename V, typename I>
80  inline
81  mln_ch_value(I, V)
82  convert(const V& v, const Image<I>& input)
83  {
84  trace::entering("data::impl::generic::convert");
85  internal::convert_tests(v, input);
86 
87  fun::v2v::convert<V> f;
88  mln_ch_value(I, V) output = data::transform(input, f);
89 
90  trace::exiting("data::impl::generic::convert");
91  return output;
92  }
93 
94  } // end of namespace mln::data::impl::generic
95 
96 
97  template <typename V, typename I>
98  inline
99  mln_ch_value(I,V)
100  convert_identity(const V& v, const Image<I>& input)
101  {
102  trace::entering("data::impl::convert_identity");
103  internal::convert_tests(v, input);
104 
105  mln_concrete(I) output = duplicate(input);
106 
107  trace::exiting("data::impl::convert_identity");
108  return output;
109  }
110 
111 
112  } // end of namespace mln::data::impl
113 
114  namespace internal
115  {
116 
117  template <typename V, typename I>
118  inline
119  mln_ch_value(I, V)
120  convert_dispatch(metal::bool_<true>,
121  const V& v, const Image<I>& input)
122  {
123  return impl::convert_identity(v, input);
124  }
125 
126  template <typename V, typename I>
127  inline
128  mln_ch_value(I, V)
129  convert_dispatch(metal::bool_<false>,
130  const V& v, const Image<I>& input)
131  {
132  return impl::generic::convert(v, input);
133  }
134 
135  template <typename V, typename I>
136  inline
137  mln_ch_value(I, V)
138  convert_dispatch(const V& v, const Image<I>& input)
139  {
140  enum {
141  test = mlc_equal(V, mln_value(I))::value
142  };
143  return convert_dispatch(metal::bool_<test>(),
144  v, input);
145  }
146 
147  } // end of namespace mln::data::internal
148 
149  // Facade.
150 
151  template <typename V, typename I>
152  inline
153  mln_ch_value(I, V)
154  convert(const V& v, const Image<I>& input)
155  {
156  trace::entering("data::convert");
157 
158  internal::convert_tests(v, input);
159 
160  mln_ch_value(I, V) output = internal::convert_dispatch(v, input);
161 
162  trace::exiting("data::convert");
163  return output;
164  }
165 
166 
167 # endif // ! MLN_INCLUDE_ONLY
168 
169  } // end of namespace mln::data
170 
171 } // end of namespace mln
172 
173 
174 #endif // ! MLN_DATA_CONVERT_HH