]> git.lyx.org Git - features.git/blob - src/support/lyxfunctional.h
fix parse error in FormPreferences, make LyX be able to compile with gcc 2.97, use...
[features.git] / src / support / lyxfunctional.h
1 // -*- C++ -*-
2
3 #ifndef LYX_FUNCTIONAL_H
4 #define LYX_FUNCTIONAL_H
5
6 #include <iterator>
7
8 //namespace lyx {
9
10 template<class R, class C, class A>
11 class class_fun_t {
12 public:
13         class_fun_t(C & ct, R(C::*p)(A)) 
14                 : c(ct), cmf(p) {}
15         R operator()(A & a) const {
16                 return (c.*cmf)(a);
17         }
18 private:
19         C & c;
20         R(C::*cmf)(A);
21 };
22
23
24 template<class C, class A>
25 class void_class_fun_t {
26 public:
27         void_class_fun_t(C & ct, void(C::*p)(A))
28                 : c(ct), cmf(p) {}
29         void operator()(A & a) const {
30                 (c.*cmf)(a);
31         }
32 private:
33         C & c;
34         void(C::*cmf)(A);
35 };
36
37        
38 template <class R, class C, class A> class_fun_t<R, C, A>
39 class_fun(C & c, R(C::*f)(A))
40 {
41         return class_fun_t<R, C, A>(c, f);
42 }
43
44
45 template <class C, class A> void_class_fun_t<C, A>
46 class_fun(C & c, void(C::*f)(A))
47 {
48         return void_class_fun_t<C, A>(c, f);
49 }
50
51
52 template <class Cont, class Type, class MemRet>
53 class back_insert_fun_iterator {
54 protected:
55         Cont * container;
56         MemRet(Type::*pmf)();
57 public:
58         typedef Cont container_type;
59         typedef std::output_iterator_tag iterator_category;
60         typedef void value_type;
61         typedef void difference_type;
62         typedef void pointer;
63         typedef void reference;
64        
65         back_insert_fun_iterator(Cont & x, MemRet(Type::*p)())
66                 : container(&x), pmf(p) {}
67
68         back_insert_fun_iterator &
69         operator=(Type * val) {
70                 container->push_back((val->*pmf)());
71                 return *this;
72         }
73
74         back_insert_fun_iterator &
75         operator=(Type & val) {
76                 container->push_back((val.*pmf)());
77                 return *this;
78         }
79
80         back_insert_fun_iterator & operator*() {
81                 return *this;
82         }
83         back_insert_fun_iterator & operator++() { // prefix ++
84                 return *this;
85         }
86         back_insert_fun_iterator & operator++(int) { // postfix ++
87                 return *this;
88         }
89 };
90
91
92 template <class Cont, class Type, class MemRet>
93 class const_back_insert_fun_iterator {
94 protected:
95         Cont * container;
96         MemRet(Type::*pmf)() const;
97 public:
98         typedef Cont container_type;
99         typedef std::output_iterator_tag iterator_category;
100         typedef void value_type;
101         typedef void difference_type;
102         typedef void pointer;
103         typedef void reference;
104         
105         const_back_insert_fun_iterator(Cont & x, MemRet(Type::*p)() const)
106                 : container(&x), pmf(p) {}
107         
108         ~const_back_insert_fun_iterator() {}
109       
110         const_back_insert_fun_iterator &
111         operator=(Type const * val) {
112                 container->push_back((val->*pmf)());
113                 return *this;
114         }
115
116         const_back_insert_fun_iterator &
117         operator=(Type const & val) {
118                 container->push_back((val.*pmf)());
119                 return *this;
120         }
121
122         const_back_insert_fun_iterator & operator*() {
123                 return *this;
124         }
125         const_back_insert_fun_iterator & operator++() { // prefix ++
126                 return *this;
127         }
128         const_back_insert_fun_iterator & operator++(int) { // postfix ++
129                 return *this;
130         }
131 };
132
133
134 template <class Cont, class Type, class MemRet>
135 back_insert_fun_iterator<Cont, Type, MemRet>
136 back_inserter_fun(Cont & cont, MemRet(Type::*p)())
137 {
138         return back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
139 }
140
141
142 template <class Cont, class Type, class MemRet>
143 const_back_insert_fun_iterator<Cont, Type, MemRet>
144 back_inserter_fun(Cont & cont, MemRet(Type::*p)() const)
145 {
146         return const_back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
147 }
148
149
150 template <class R, class C, class A>
151 class compare_memfun_t {
152 public:
153         compare_memfun_t(R(C::*p)(), A const & a)
154                 : pmf(p), arg(a) {}
155         bool operator()(C * c) {
156                 return (c->*pmf)() == arg;
157         }
158         bool operator()(C & c) {
159                 return (c.*pmf)() == arg;
160         }
161 private:
162         R(C::*pmf)();
163         A const & arg;
164 };
165
166
167 template <class R, class C, class A>
168 class const_compare_memfun_t {
169 public:
170         const_compare_memfun_t(R(C::*p)() const, A const & a)
171                 : pmf(p), arg(a) {}
172         bool operator()(C const * c) {
173                 return (c->*pmf)() == arg;
174         }
175         bool operator()(C const & c) {
176                 return (c.*pmf)() == arg;
177         }
178 private:
179         R(C::*pmf)() const;
180         A const & arg;
181 };
182
183
184 template <class R, class C, class A>
185 compare_memfun_t<R, C, A>
186 compare_memfun(R(C::*p)(), A const & a)
187 {
188         return compare_memfun_t<R, C, A>(p, a);
189 }
190
191
192 template <class R, class C, class A>
193 const_compare_memfun_t<R, C, A>
194 compare_memfun(R(C::*p)() const, A const & a)
195 {
196         return const_compare_memfun_t<R, C, A>(p, a);
197 }
198
199         
200 // Functors used in the template.
201
202 ///
203 template<typename T1, typename T2>
204 class equal_1st_in_pair {
205 public:
206         ///
207         equal_1st_in_pair(T1 const & value) : value_(value) {}
208         ///
209         typedef std::pair<T1, T2> pair_type;
210         ///
211         bool operator() (pair_type const & p) const {
212                 return p.first == value_;
213         }
214 private:
215         ///
216         T1 const & value_;
217 };
218
219
220 ///
221 template<typename T1, typename T2>
222 class equal_2nd_in_pair {
223 public:
224         ///
225         equal_2nd_in_pair(T2 const & value) : value_(value) {}
226         ///
227         typedef std::pair<T1, T2> pair_type;
228         ///
229         bool operator() (pair_type const & p) const {
230                 return p.second == value_;
231         }
232 private:
233         ///
234         T2 const & value_;
235 };
236
237
238 // }  // end of namespace lyx
239 #endif