stlab.adobe.com Adobe Systems Incorporated
functional.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 #ifndef ADOBE_FUNCTIONAL_HPP
10 #define ADOBE_FUNCTIONAL_HPP
11 
12 #include <adobe/config.hpp>
13 
14 #include <functional>
15 #include <utility>
16 
17 #include <boost/compressed_pair.hpp>
18 #include <boost/tuple/tuple.hpp>
19 
22 #include <adobe/utility/pair.hpp>
23 
24 /*************************************************************************************************/
25 
26 namespace adobe {
27 
28 /*************************************************************************************************/
29 
35 /*
36  REVISIT (sparent) : Documentation for delete_ptr moved to adobe/functional.hpp because doxygen
37  1.4.3 cannot handle the template specializations.
38 */
39 
165 template < typename F, // models UnaryFunction
166  typename G> // models UnaryFunction -> argument_type(F)
168 {
169  typedef typename F::result_type result_type;
170 
172  unary_compose(F f, G g) : data_m(f, g) { }
173 
174  template <typename U> // U models Regular
175  result_type operator()(const U& x) const { return data_m.first()(data_m.second()(x)); }
176 
177  template <typename U> // U models Regular
178  result_type operator()(U& x) const { return data_m.first()(data_m.second()(x)); }
179 
180  private:
181  boost::compressed_pair<F, G> data_m;
182 };
183 
184 /*************************************************************************************************/
185 
186 template < typename F, // models UnaryFunction
187  typename G> // models UnaryFunction -> argument_type(F)
189 
190 /*************************************************************************************************/
191 
192 template < typename F, // models BinaryFunction
193  typename G, // models UnaryFunction -> argument_type(F, 0);
194  typename H = G> // models UnaryFunction -> argument_type(F, 1);
196 {
197  typedef typename F::result_type result_type;
198 
199  template <typename T, typename U> // models Regular
200  result_type operator()(const T& x, const U& y) const { return f(g(x), h(y)); }
201 
202  template <typename T, typename U> // models Regular
203  result_type operator()(T& x, U& y) const { return f(g(x), h(y)); }
204 
205  F f;
206  G g;
207  H h;
208 };
209 
210 /*************************************************************************************************/
211 
212 template <int N, typename T> // T is boost::tuple<>
213 struct element
214 {
215  typedef typename boost::tuples::element<N, T>::type type;
216 };
217 
218 template <typename T1, typename T2>
219 struct element<0, std::pair<T1, T2> >
220 {
221  typedef typename std::pair<T1, T2>::first_type type;
222 };
223 
224 template <typename T1, typename T2>
225 struct element<1, std::pair<T1, T2> >
226 {
227  typedef typename std::pair<T1, T2>::second_type type;
228 };
229 
230 template <typename T1, typename T2>
231 struct element<0, pair<T1, T2> >
232 {
233  typedef typename pair<T1, T2>::first_type type;
234 };
235 
236 template <typename T1, typename T2>
237 struct element<1, pair<T1, T2> >
238 {
239  typedef typename pair<T1, T2>::second_type type;
240 };
241 
242 /*************************************************************************************************/
243 
244 template <int N, typename T> // T is pair or tuple
245 struct get_element : std::unary_function<T, typename element<N, T>::type>
246 {
247  typename element<N, T>::type& operator()(T& x) const
248  { return boost::get<N>(x); }
249 
250  const typename element<N, T>::type& operator()(const T& x) const
251  { return boost::get<N>(x); }
252 };
253 
254 /*************************************************************************************************/
255 
256 template <typename T1, typename T2> // T is pair or tuple
257 struct get_element<0, std::pair<T1, T2> > :
258  std::unary_function<std::pair<T1, T2>, typename std::pair<T1, T2>::first_type>
259 {
260  typedef std::pair<T1, T2> argument_type;
261  typedef typename argument_type::first_type result_type;
262 
264  { return x.first; }
265 
266  const result_type& operator()(const argument_type& x) const
267  { return x.first; }
268 };
269 
270 /*************************************************************************************************/
271 
272 template <typename T1, typename T2> // T is pair or tuple
273 struct get_element<0, pair<T1, T2> > :
274  std::unary_function<pair<T1, T2>, typename pair<T1, T2>::first_type>
275 {
278 
280  { return x.first; }
281 
282  const result_type& operator()(const argument_type& x) const
283  { return x.first; }
284 };
285 
286 /*************************************************************************************************/
287 
288 template <typename T1, typename T2> // T is pair or tuple
289 struct get_element<1, std::pair<T1, T2> > :
290  std::unary_function<std::pair<T1, T2>, typename std::pair<T1, T2>::second_type>
291 {
292  typedef std::pair<T1, T2> argument_type;
293  typedef typename argument_type::second_type result_type;
294 
296  { return x.second; }
297 
298  const result_type& operator()(const argument_type& x) const
299  { return x.second; }
300 };
301 
302 /*************************************************************************************************/
303 
304 template <typename T1, typename T2> // T is pair or tuple
305 struct get_element<1, pair<T1, T2> > :
306  std::unary_function<pair<T1, T2>, typename pair<T1, T2>::second_type>
307 {
310 
312  { return x.second; }
313 
314  const result_type& operator()(const argument_type& x) const
315  { return x.second; }
316 };
317 
318 /*************************************************************************************************/
319 
320 template <typename T>
321 struct always_true : std::unary_function<T, bool>
322 {
323  bool operator()(const T&) const { return true; }
324 };
325 
326 /*************************************************************************************************/
327 
328 template <class Result>
330 {
331  typedef Result result_type;
332 };
333 
334 /*************************************************************************************************/
335 
336 
337 template <typename T>
339 #if !defined(ADOBE_NO_DOCUMENTATION)
340 : generator_t<T>
341 #endif
342 {
343  explicit sequence_t(const T& x) : data_m(x) { }
344  T operator () () { return data_m++; }
345 private:
346  T data_m;
347 };
348 
349 /*************************************************************************************************/
350 
351 template <class T, typename R, class Compare>
352 struct compare_members_t : std::binary_function<T, T, bool>
353 {
354  compare_members_t(R T::* member, Compare compare) :
355  compare_m(compare),
356  member_m(member)
357  { }
358 
359  bool operator () (const T& x, const T& y) const
360  { return compare_m(x.*member_m, y.*member_m); }
361 
362  bool operator () (const T& x, const R& y) const
363  { return compare_m(x.*member_m, y); }
364 
365  bool operator () (const R& x, const T& y) const
366  { return compare_m(x, y.*member_m); }
367 
368  private:
369 
370 /*
371  REVISIT (sparent) : This could probably use an empty member optimization.
372 */
373 
374  Compare compare_m;
375  R T::* member_m;
376 };
377 
378 template <class T, typename R>
380 {
381  return compare_members_t<T, R, std::less<R> >(member, std::less<R>() );
382 }
383 
384 template <class T, typename R, class Compare>
385 compare_members_t<T, R, Compare> compare_members(R T::* member, Compare compare)
386 {
387  return compare_members_t<T, R, Compare>(member, compare);
388 }
389 
390 /*************************************************************************************************/
391 
392 template <class T, typename R>
393 struct mem_data_t : std::unary_function<T, R&>
394 {
396 
397  explicit mem_data_t(R T::* member) :
398  member_m(member)
399  { }
400 
401  R& operator () (T& x) const
402  { return x.*member_m; }
403 
404  const R& operator () (const T& x) const
405  { return x.*member_m; }
406 
407  private:
408  R T::* member_m;
409 };
410 
411 template <class T, typename R>
412 struct mem_data_t<const T, R> : std::unary_function<T, const R&>
413 {
414  explicit mem_data_t(R T::* member) :
415  member_m(member)
416  { }
417 
418  const R& operator () (const T& x) const
419  { return x.*member_m; }
420 
421  private:
422  R T::* member_m;
423 };
424 
425 template <class T, typename R>
427 {
428  return mem_data_t<T, R>(member);
429 }
430 
431 /*************************************************************************************************/
432 
433 template <typename O> // O models StrictWeakOrdering
434 struct equivalent : std::binary_function<typename O::first_argument_type,
435  typename O::second_argument_type,
436  bool>
437 {
438  public:
439  explicit equivalent(const O& x) : o_m(x) { }
440 
441  bool operator()( const typename O::first_argument_type& x,
442  const typename O::second_argument_type& y) const
443  { return !o_m(x, y) && !o_m(y, x); }
444  private:
445  O o_m;
446 };
447 
448 /*************************************************************************************************/
449 
450 template <class F> // F models a BinaryFunction
451 struct transposer : std::binary_function<typename F::second_argument_type,
452  typename F::first_argument_type,
453  typename F::result_type>
454 {
455  typedef typename F::second_argument_type first_argument_type;
456  typedef typename F::first_argument_type second_argument_type;
457  typedef typename F::result_type result_type;
458 
459  F fun;
460 
461  transposer(const F& f) :
462  fun(f)
463  { }
464 
466  {
467  return fun(y, x);
468  }
469 };
470 
471 template <typename F> // F models BinaryFunction
473 {
474  return transposer<F>(f);
475 }
476 
478 
479 /*************************************************************************************************/
480 
481 } // namespace adobe
482 
483 /*************************************************************************************************/
484 
485 #endif
486 
487 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google