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