$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
literal/ops.hh
1 // Copyright (C) 2007, 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_LITERAL_OPS_HH
27 # define MLN_LITERAL_OPS_HH
28 
35 # include <mln/core/concept/literal.hh>
36 # include <mln/trait/all.hh>
37 # include <mln/metal/equal.hh>
38 # include <mln/metal/converts_to.hh>
39 
40 
41 
42 namespace mln
43 {
44 
45 
46  namespace trait
47  {
48 
49  template < template <class, class> class Name,
50  typename L, typename O >
51  struct set_binary_< Name, mln::Literal, L, mln::Object, O >
52  {
53  typedef mln_trait_binary(Name, O, O) ret;
54  };
55 
56  template < template <class, class> class Name,
57  typename O, typename L >
58  struct set_binary_< Name, mln::Object, O, mln::Literal, L >
59  {
60  typedef mln_trait_binary(Name, O, O) ret;
61  };
62 
63  template < typename L1, typename L2 >
64  struct set_binary_< op::eq, mln::Literal, L1, mln::Literal, L2 >
65  {
66  typedef bool ret;
67  };
68 
69  template < typename L1, typename L2 >
70  struct set_binary_< op::neq, mln::Literal, L1, mln::Literal, L2 >
71  {
72  typedef bool ret;
73  };
74 
75  } // end of namespace mln::trait
76 
77 
78  // Arithmetical operators.
79 
80  template <typename O, typename L>
81  mln_trait_op_plus(O, O)
82  operator+(const Object<O>& lhs, const Literal<L>& rhs);
83 
84  template <typename L, typename O>
85  mln_trait_op_plus(O, O)
86  operator+(const Literal<L>& lhs, const Object<O>& rhs);
87 
88  template <typename O, typename L>
89  mln_trait_op_minus(O, O)
90  operator-(const Object<O>& lhs, const Literal<L>& rhs);
91 
92  template <typename L, typename O>
93  mln_trait_op_minus(O, O)
94  operator-(const Literal<L>& lhs, const Object<O>& rhs);
95 
96  template <typename O, typename L>
97  mln_trait_op_times(O, O)
98  operator*(const Object<O>& lhs, const Literal<L>& rhs);
99 
100  template <typename L, typename O>
101  mln_trait_op_times(O, O)
102  operator*(const Literal<L>& lhs, const Object<O>& rhs);
103 
104  template <typename O, typename L>
105  mln_trait_op_div(O, O)
106  operator/(const Object<O>& lhs, const Literal<L>& rhs);
107 
108  template <typename L, typename O>
109  mln_trait_op_div(O, O)
110  operator/(const Literal<L>& lhs, const Object<O>& rhs);
111 
112  template <typename O, typename L>
113  mln_trait_op_mod(O, O)
114  operator%(const Object<O>& lhs, const Literal<L>& rhs);
115 
116  template <typename L, typename O>
117  mln_trait_op_mod(O, O)
118  operator%(const Literal<L>& lhs, const Object<O>& rhs);
119 
120 
121  // Operator equal.
122 
123  template <typename O, typename L>
124  mln_trait_op_eq(O, O)
125  operator==(const Object<O>& lhs, const Literal<L>& rhs);
126 
127  template <typename L, typename O>
128  mln_trait_op_eq(O, O)
129  operator==(const Literal<L>& lhs, const Object<O>& rhs);
130 
131  template <typename L1, typename L2>
132  bool operator==(const Literal<L1>& lhs, const Literal<L2>& rhs);
133 
134  // FIXME: ...
135 
136 
137 
138 # ifndef MLN_INCLUDE_ONLY
139 
140  // FIXME: Static assertion code de-activated because when O's ctor
141  // is explicit from literal the metal program does not compile...
142 
143 
144  // Op +
145 
146  template <typename O, typename L>
147  inline
148  mln_trait_op_plus(O, O)
149  operator+(const Object<O>& lhs, const Literal<L>& rhs)
150  {
151  // mlc_converts_to(L, O)::check();
152  return exact(lhs) + O(exact(rhs));
153  }
154 
155  template <typename L, typename O>
156  inline
157  mln_trait_op_plus(O, O)
158  operator+(const Literal<L>& lhs, const Object<O>& rhs)
159  {
160  // mlc_converts_to(L, O)::check();
161  return O(exact(lhs)) + exact(rhs);
162  }
163 
164  // Op -
165 
166  template <typename O, typename L>
167  inline
168  mln_trait_op_minus(O, O)
169  operator-(const Object<O>& lhs, const Literal<L>& rhs)
170  {
171  // mlc_converts_to(L, O)::check();
172  return exact(lhs) - O(exact(rhs));
173  }
174 
175  template <typename L, typename O>
176  inline
177  mln_trait_op_minus(O, O)
178  operator-(const Literal<L>& lhs, const Object<O>& rhs)
179  {
180  // mlc_converts_to(L, O)::check();
181  return O(exact(lhs)) - exact(rhs);
182  }
183 
184  // Op *
185 
186  template <typename O, typename L>
187  inline
188  mln_trait_op_times(O, O)
189  operator*(const Object<O>& lhs, const Literal<L>& rhs)
190  {
191  // mlc_converts_to(L, O)::check();
192  return exact(lhs) * O(exact(rhs));
193  }
194 
195  template <typename L, typename O>
196  inline
197  mln_trait_op_times(O, O)
198  operator*(const Literal<L>& lhs, const Object<O>& rhs)
199  {
200  // mlc_converts_to(L, O)::check();
201  return O(exact(lhs)) * exact(rhs);
202  }
203 
204  // Op /
205 
206  template <typename O, typename L>
207  inline
208  mln_trait_op_div(O, O)
209  operator/(const Object<O>& lhs, const Literal<L>& rhs)
210  {
211  // mlc_converts_to(L, O)::check();
212  return exact(lhs) / O(exact(rhs));
213  }
214 
215  template <typename L, typename O>
216  inline
217  mln_trait_op_div(O, O)
218  operator/(const Literal<L>& lhs, const Object<O>& rhs)
219  {
220  // mlc_converts_to(L, O)::check();
221  return O(exact(lhs)) / exact(rhs);
222  }
223 
224  // Op %
225 
226  template <typename O, typename L>
227  inline
228  mln_trait_op_mod(O, O)
229  operator%(const Object<O>& lhs, const Literal<L>& rhs)
230  {
231  // mlc_converts_to(L, O)::check();
232  return exact(lhs) % O(exact(rhs));
233  }
234 
235  template <typename L, typename O>
236  inline
237  mln_trait_op_mod(O, O)
238  operator%(const Literal<L>& lhs, const Object<O>& rhs)
239  {
240  // mlc_converts_to(L, O)::check();
241  return O(exact(lhs)) % exact(rhs);
242  }
243 
244 
245  // Op ==
246 
247  template <typename O, typename L>
248  inline
249  mln_trait_op_eq(O, O)
250  operator==(const Object<O>& lhs, const Literal<L>& rhs)
251  {
252  // mlc_converts_to(L, O)::check();
253  return exact(lhs) == O(exact(rhs));
254  }
255 
256  template <typename L, typename O>
257  inline
258  mln_trait_op_eq(O, O)
259  operator==(const Literal<L>& lhs, const Object<O>& rhs)
260  {
261  // mlc_converts_to(L, O)::check();
262  return O(exact(lhs)) == exact(rhs);
263  }
264 
265  template <typename L1, typename L2>
266  inline
267  bool
268  operator==(const Literal<L1>&, const Literal<L2>&)
269  {
270  return mlc_equal(L1, L2)::value;
271  }
272 
273  // FIXME: Add less, etc.
274 
275 # endif // ! MLN_INCLUDE_ONLY
276 
277 } // end of namespace mln
278 
279 
280 #endif // ! MLN_LITERAL_OPS_HH