]> git.lyx.org Git - lyx.git/blob - src/support/lyxfunctional.h
* lyxfunctional.h: delete back_inserter_fun functions and helper
[lyx.git] / src / support / lyxfunctional.h
1 // -*- C++ -*-
2 /**
3  * \file lyxfunctional.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * \brief Convenient function objects for use with LyX
12  *
13  * This is currently a small collection of small function objects for use
14  * together with std::algorithms.
15  */
16
17
18 #ifndef LYX_FUNCTIONAL_H
19 #define LYX_FUNCTIONAL_H
20
21 #include <iterator>
22
23 namespace lyx {
24
25 template <class R, class C, class A>
26 class compare_memfun_t {
27 public:
28         compare_memfun_t(R(C::*p)(), A const & a)
29                 : pmf(p), arg(a) {}
30         bool operator()(C * c) {
31                 return (c->*pmf)() == arg;
32         }
33         bool operator()(C & c) {
34                 return (c.*pmf)() == arg;
35         }
36 private:
37         R(C::*pmf)();
38         A const & arg;
39 };
40
41
42 template <class R, class C, class A>
43 class const_compare_memfun_t {
44 public:
45         const_compare_memfun_t(R(C::*p)() const, A const & a)
46                 : pmf(p), arg(a) {}
47         bool operator()(C const * c) {
48                 return (c->*pmf)() == arg;
49         }
50         bool operator()(C const & c) {
51                 return (c.*pmf)() == arg;
52         }
53 private:
54         R(C::*pmf)() const;
55         A const & arg;
56 };
57
58
59 template <class R, class C, class A>
60 compare_memfun_t<R, C, A>
61 compare_memfun(R(C::*p)(), A const & a)
62 {
63         return compare_memfun_t<R, C, A>(p, a);
64 }
65
66
67 template <class R, class C, class A>
68 const_compare_memfun_t<R, C, A>
69 compare_memfun(R(C::*p)() const, A const & a)
70 {
71         return const_compare_memfun_t<R, C, A>(p, a);
72 }
73
74
75 // Functors used in the template.
76
77 ///
78 template<typename T>
79 class equal_1st_in_pair {
80 public:
81         ///
82         typedef typename T::first_type first_type;
83         ///
84         typedef T pair_type;
85         ///
86         equal_1st_in_pair(first_type const & value) : value_(value) {}
87         ///
88         bool operator() (pair_type const & p) const {
89                 return p.first == value_;
90         }
91 private:
92         ///
93         first_type const & value_;
94 };
95
96
97 ///
98 template<typename T>
99 class equal_2nd_in_pair {
100 public:
101         ///
102         typedef typename T::second_type second_type;
103         ///
104         typedef T pair_type;
105         ///
106         equal_2nd_in_pair(second_type const & value) : value_(value) {}
107         ///
108         bool operator() (pair_type const & p) const {
109                 return p.second == value_;
110         }
111 private:
112         ///
113         second_type const & value_;
114 };
115
116 }  // end of namespace lyx
117 #endif