$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
spe/unary.hh
1 // Copyright (C) 2007, 2008, 2009, 2011 EPITA Research and Development
2 // 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_FUN_SPE_UNARY_HH
28 # define MLN_FUN_SPE_UNARY_HH
29 
30 # include <mln/core/concept/function.hh>
31 # include <mln/trait/next/solve.hh>
32 # include <mln/trait/functions.hh>
33 
34 namespace mln
35 {
36 
37  namespace fun
38  {
39 
40  namespace spe
41  {
42 
43  // Wrapper for bijective functions
44  template <typename Fun, typename T>
45  struct lwrapper
46  {
47  typedef typename Fun::result result;
48  typedef typename Fun::argument argument;
49  typedef typename Fun::lvalue lvalue;
50  typedef lwrapper lresult;
51 
52  lwrapper(const Fun& f, T& x)
53  : x_(&x), f_(&f)
54  {
55  }
56 
57  result to_result() const
58  {
59  return (*f_)(*const_cast<const T*>(x_));
60  };
61 
62  operator result() const
63  {
64  return to_result();
65  };
66 
67  const result& operator = (const result& r) const
68  {
69  argument x(*x_);
70  f_->set(x, r);
71  *x_ = x;
72 
73  return r;
74  }
75 
76  private:
77  T *x_;
78  const Fun *f_;
79  };
80 
81  template <typename Fun>
82  struct lwrapper<Fun, typename Fun::argument>
83  {
84  typedef typename Fun::result result;
85  typedef typename Fun::argument argument;
86  typedef typename Fun::lvalue lvalue;
87  typedef lwrapper lresult;
88 
89  lwrapper(const Fun& f, argument& x)
90  : x_(&x), f_(&f)
91  {
92  }
93 
94  result to_result() const
95  {
96  return (*f_)(*const_cast<const argument*>(x_));
97  };
98 
99  operator result() const
100  {
101  return to_result();
102  };
103 
104  const result& operator = (const result& r) const
105  {
106  f_->set(*x_, r);
107 
108  return r;
109  }
110 
111  private:
112  argument *x_;
113  const Fun *f_;
114  };
115 
116  template <typename Fun, typename Any, typename T>
117  struct lwrapper<Fun, lwrapper<Any, T> >
118  {
119  typedef typename Fun::result result;
120  typedef typename Fun::argument argument;
121  typedef typename Fun::lvalue lvalue;
122  typedef lwrapper lresult;
123 
124  lwrapper(const Fun& f, const lwrapper<Any, T>& m)
125  : m_(m), f_(&f)
126  {
127  }
128 
130  {
131  return (*f_)(m_.to_result());
132  };
133 
134  operator result() const
135  {
136  return to_result();
137  };
138 
139  const result& operator = (const result& r) const
140  {
141  argument m(m_);
142  f_->set(m, r);
143  m_ = m;
144 
145  return r;
146  }
147 
148  private:
149  const lwrapper<Any, T> m_;
150  const Fun *f_;
151  };
152 
153  template <typename Fun, typename T>
154  struct unary;
155 
156  namespace impl
157  {
158 
159  template <bool param, bool set, typename Fun, typename T>
160  struct unary_impl;
161 
162  template <typename Fun, typename T>
163  struct unary_impl<false, false, Fun, T> : Function_v2v< unary<Fun, T> >
164  {
165  typedef Fun flag;
166  typedef mln_trait_nunary(Fun, T) def;
167 
168  typedef typename def::argument argument;
169  typedef typename def::result result;
170 
171  result operator () (const argument& value) const
172  {
173  return def::read(value);
174  }
175 
176  template <typename U>
177  void init(const U& value)
178  {
179  (void) value;
180  };
181 
182  };
183 
184  template <typename Fun, typename T>
185  struct unary_impl<false, true, Fun, T> : unary_impl<false, false, Fun, T>
186  {
188  typedef typename super::def::lvalue lvalue;
189 
190  template <typename U>
191  struct lresult_with
192  {
194  };
195 
197 
198  void set(lvalue l, const typename super::result& r) const
199  {
200  super::def::write(l, r);
201  }
202 
203  using super::operator ();
204 
205  lresult apply_rw(typename super::argument& value) const
206  {
207  return lresult(exact(*this), value);
208  }
209 
210  template <typename U>
211  typename lresult_with<U>::ret apply_rw(U& value) const
212  {
213  return typename lresult_with<U>::ret(exact(*this), value);
214  }
215 
216  lresult operator () (typename super::argument& value) const
217  {
218  return apply_rw(value);
219  }
220  };
221 
222  template <typename Fun, typename T>
223  struct unary_impl<true, false, Fun, T> : Function_v2v< unary<Fun, T> >
224  {
225  typedef Fun flag;
226  typedef mln_trait_nunary(Fun, T) def;
227 
228  typedef typename def::argument argument;
229  typedef typename def::result result;
230 
231  typedef mln_trait_fun_param(unary_impl) param;
232  typedef mln_trait_fun_storage(unary_impl) storage;
233 
234  result operator () (const argument& value) const
235  {
236  return def::read(this->state_, value);
237  }
238 
239  template <typename U>
240  void init(const U& value)
241  {
243  };
244 
246  {
247  return state_;
248  }
249 
250  const stored<storage>& state() const
251  {
252  return state_;
253  }
254 
255  protected:
257  };
258 
259  template <typename Fun, typename T>
260  struct unary_impl<true, true, Fun, T> : unary_impl<true, false, Fun, T>
261  {
263  typedef typename super::def::lvalue lvalue;
264 
265  template <typename U>
266  struct lresult_with
267  {
269  };
270 
272 
273  void set(lvalue l, const typename super::result& r) const
274  {
275  super::def::write(this->state(), l, r);
276  }
277 
278  using super::operator ();
279 
280  lresult apply_rw(typename super::argument& value) const
281  {
282  return lresult(exact(*this), value);
283  }
284 
285  template <typename U>
286  typename lresult_with<U>::ret apply_rw(U& value) const
287  {
288  return typename lresult_with<U>::ret(exact(*this), value);
289  }
290 
291  lresult operator () (typename super::argument& value) const
292  {
293  return apply_rw(value);
294  }
295  };
296 
297  } // end of namespace mln::fun::spe::impl
298 
299  template <typename Fun, typename T>
300  struct unary
301  : impl::unary_impl<mlc_or(mln_trait_fun_is_parametrable(mln_trait_nunary(Fun, T)), mln_trait_fun_is_parametrable(Fun))::value,
302  mln_trait_fun_is_assignable_(mln_trait_nunary(Fun, T))::value, Fun, T>
303  {
304  typedef mln_trait_nunary(Fun, T) def;
305  typedef impl::unary_impl<mlc_or(mln_trait_fun_is_parametrable(def), mln_trait_fun_is_parametrable(Fun))::value,
306  mln_trait_fun_is_assignable_(def)::value,
307  Fun,
308  T>
310 
311  unary() {}
312 
313  template <typename U>
314  unary(const U& param)
315  {
316  this->init(param);
317  }
318 
319  using super::operator();
320  };
321 
322  } // end of namespace mln::fun::spe
323 
324  } // end of namespace mln::fun
325 
326 } // end of namespace mln
327 
328 template <typename F, typename T>
329 std::ostream& operator << (std::ostream& o, const mln::fun::spe::lwrapper<F, T>& m)
330 {
331  return o << m.to_result();
332 }
333 
334 #endif // ! MLN_FUN_SPE_UNARY_HH