Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
data/fill.hh
1 // Copyright (C) 2007, 2008, 2009, 2010, 2011 EPITA Research and
2 // Development Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_DATA_FILL_HH
28 # define MLN_DATA_FILL_HH
29 
35 
36 # include <mln/core/concept/function.hh>
37 # include <mln/pw/image.hh>
38 # include <mln/convert/to_fun.hh>
39 
40 # include <mln/data/fill_with_image.hh>
41 # include <mln/data/fill_with_value.hh>
42 
43 
44 namespace mln
45 {
46 
47  namespace data
48  {
49 
57  template <typename I, typename D>
58  void fill(Image<I>& ima, const D& data);
59 
60 
61 
62 # ifndef MLN_INCLUDE_ONLY
63 
64  namespace internal
65  {
66 
67  // tests
68 
69  template <typename I, typename D>
70  inline
71  void fill_tests(Image<I>& ima, const D&)
72  {
73  // Avoid a warning about an undefined variable when NDEBUG
74  // is not defined.
75  (void) ima;
76 
77  mlc_is(mln_trait_image_value_io(I), trait::image::value_io::read_write)::check();
78  mln_precondition(exact(ima).is_valid());
79  // FIXME: check for ambiguities...
80  }
81 
82  // dispatch_overload
83 
84  template <typename I>
85  void fill_dispatch_overload(I& ima, const mln_value(I)& v)
86  {
88  }
89 
90  template <typename I, typename J>
91  void fill_dispatch_overload(I& ima, const Image<J>& data)
92  {
93  mln::data::fill_with_image(ima, data);
94  }
95 
96  template <typename I, typename F>
97  void fill_dispatch_overload(I& ima, const Function<F>& f)
98  {
99  mlc_converts_to(mln_result(F), mln_value(I))::check();
101  exact(f) | ima.domain());
102  }
103 
104  template <typename I, typename R, typename A>
105  void fill_dispatch_overload(I& ima, R (*f)(A))
106  {
107  mlc_converts_to(R, mln_value(I))::check();
109  convert::to_fun(f) | ima.domain());
110  }
111 
112  template <typename I, typename V, unsigned N>
113  void fill_dispatch_overload(I& ima, V (&arr)[N])
114  {
115  mlc_converts_to(V, mln_value(I))::check();
116  mln_precondition(N == ima.nsites());
117  mln_fwd_piter(I) p(ima.domain());
118  unsigned i = 0;
119  for_all(p)
120  ima(p) = arr[i++];
121  }
122 
123  // dispatch
124 
125  template <typename I, typename D>
126  void fill_dispatch(Image<I>& ima, const D& data)
127  {
128  fill_dispatch_overload(exact(ima), exact(data));
129  }
130 
131  } // end of namespace mln::data::internal
132 
133 
134  // Facade.
135 
136  template <typename I, typename D>
137  inline
138  void fill(Image<I>& ima, const D& data)
139  {
140  trace::entering("data::fill");
141 
142  internal::fill_tests(ima, data);
143  internal::fill_dispatch(ima, data);
144 
145  trace::exiting("data::fill");
146  }
147 
148 
149 # endif // ! MLN_INCLUDE_ONLY
150 
151  } // end of namespace mln::data
152 
153 } // end of namespace mln
154 
155 
156 #endif // ! MLN_DATA_FILL_HH