]> git.lyx.org Git - lyx.git/blob - src/support/lyxfunctional.h
the mkfifo change, make it compile on egcs
[lyx.git] / src / support / lyxfunctional.h
1 // -*- C++ -*-
2
3 #ifndef LYX_FUNCTIONAL_H
4 #define LYX_FUNCTIONAL_H
5
6 //namespace lyx {
7
8 template<class R, class C, class A>
9 class class_fun_t {
10 public:
11         class_fun_t(C & ct, R(C::*p)(A)) 
12                 : c(ct), cmf(p) {}
13         R operator()(A & a) const {
14                 return (c.*cmf)(a);
15         }
16 private:
17         C & c;
18         R(C::*cmf)(A);
19 };
20
21
22 template <class R, class C, class A> class_fun_t<R, C, A>
23 class_fun(C & c, R(C::*f)(A))
24 {
25         return class_fun_t<R, C, A>(c, f);
26 }
27
28
29 template <class Cont, class Type, class MemRet>
30 class back_insert_fun_iterator {
31 protected:
32         Cont & container;
33         MemRet(Type::*pmf)();
34 public:
35         back_insert_fun_iterator(Cont & x, MemRet(Type::*p)())
36                 : container(x), pmf(p) {}
37
38         back_insert_fun_iterator &
39         operator=(Type * val) {
40                 container.push_back((val->*pmf)());
41                 return *this;
42         }
43
44         back_insert_fun_iterator &
45         operator=(Type & val) {
46                 container.push_back((val.*pmf)());
47                 return *this;
48         }
49
50         back_insert_fun_iterator & operator*() {
51                 return *this;
52         }
53         back_insert_fun_iterator & operator++() { // prefix ++
54                 return *this;
55         }
56         back_insert_fun_iterator & operator++(int) { // postfix ++
57                 return *this;
58         }
59 };
60
61
62 template <class Cont, class Type, class MemRet>
63 back_insert_fun_iterator<Cont, Type, MemRet>
64 back_inserter_fun(Cont & cont, MemRet(Type::*p)())
65 {
66         return back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
67 }
68
69
70 template <class R, class C, class A>
71 class compare_memfun_t {
72 public:
73         compare_memfun_t(R(C::*p)(), A const & a)
74                 : pmf(p), arg(a) {}
75         bool operator()(C * c) {
76                 return (c->*pmf)() == arg;
77         }
78         bool operator()(C & c) {
79                 return (c.*pmf)() == arg;
80         }
81 private:
82         R(C::*pmf)();
83         A const & arg;
84 };
85
86
87 template <class R, class C, class A>
88 compare_memfun_t<R, C, A>
89 compare_memfun(R(C::*p)(), A const & a)
90 {
91         return compare_memfun_t<R, C, A>(p, a);
92 }
93
94         
95 // Functors used in the template.
96
97 ///
98 template<typename T1, typename T2>
99 class equal_1st_in_pair {
100 public:
101         ///
102         equal_1st_in_pair(T1 const & value) : value_(value) {}
103         ///
104         typedef std::pair<T1, T2> pair_type;
105         ///
106         bool operator() (pair_type const & p) const {
107                 return p.first == value_;
108         }
109 private:
110         ///
111         T1 const & value_;
112 };
113
114
115 ///
116 template<typename T1, typename T2>
117 class equal_2nd_in_pair {
118 public:
119         ///
120         equal_2nd_in_pair(T2 const & value) : value_(value) {}
121         ///
122         typedef std::pair<T1, T2> pair_type;
123         ///
124         bool operator() (pair_type const & p) const {
125                 return p.second == value_;
126         }
127 private:
128         ///
129         T2 const & value_;
130 };
131
132
133 // }  // end of namespace lyx
134 #endif