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