3 #ifndef LYX_FUNCTIONAL_H
4 #define LYX_FUNCTIONAL_H
6 /** \file lyxfunctional.h
7 \brief Convenient function objects for use with LyX
9 This is currently a small collection of small function objects for use
10 together with std::algorithms.
18 template<class R, class C, class A>
21 class_fun_t(C & ct, R(C::*p)(A))
23 R operator()(A & a) const {
32 template<class C, class A>
33 class void_class_fun_t {
35 void_class_fun_t(C & ct, void(C::*p)(A))
37 void operator()(A & a) const {
46 /// Use to call a class method with a container element.
47 /** Most easily used as a functor to std::algoritms.
50 A a; // class that have a int print(string const &) method
52 for_each(vs.begin(), vs.end(), class_fun(int, vs, &A::print);
55 template <class R, class C, class A> class_fun_t<R, C, A>
56 class_fun(C & c, R(C::*f)(A))
58 return class_fun_t<R, C, A>(c, f);
62 template <class C, class A> void_class_fun_t<C, A>
63 class_fun(C & c, void(C::*f)(A))
65 return void_class_fun_t<C, A>(c, f);
69 template <class Cont, class Type, class MemRet>
70 class back_insert_fun_iterator {
75 typedef Cont container_type;
76 typedef std::output_iterator_tag iterator_category;
77 typedef void value_type;
78 typedef void difference_type;
80 typedef void reference;
82 back_insert_fun_iterator(Cont & x, MemRet(Type::*p)())
83 : container(&x), pmf(p) {}
85 back_insert_fun_iterator &
86 operator=(Type * val) {
87 container->push_back((val->*pmf)());
91 back_insert_fun_iterator &
92 operator=(Type & val) {
93 container->push_back((val.*pmf)());
97 back_insert_fun_iterator & operator*() {
100 back_insert_fun_iterator & operator++() { // prefix ++
103 back_insert_fun_iterator & operator++(int) { // postfix ++
109 template <class Cont, class Type, class MemRet>
110 class const_back_insert_fun_iterator {
113 MemRet(Type::*pmf)() const;
115 typedef Cont container_type;
116 typedef std::output_iterator_tag iterator_category;
117 typedef void value_type;
118 typedef void difference_type;
119 typedef void pointer;
120 typedef void reference;
122 const_back_insert_fun_iterator(Cont & x, MemRet(Type::*p)() const)
123 : container(&x), pmf(p) {}
125 ~const_back_insert_fun_iterator() {}
127 const_back_insert_fun_iterator &
128 operator=(Type const * val) {
129 container->push_back((val->*pmf)());
133 const_back_insert_fun_iterator &
134 operator=(Type const & val) {
135 container->push_back((val.*pmf)());
139 const_back_insert_fun_iterator & operator*() {
142 const_back_insert_fun_iterator & operator++() { // prefix ++
145 const_back_insert_fun_iterator & operator++(int) { // postfix ++
151 template <class Cont, class Type, class MemRet>
152 back_insert_fun_iterator<Cont, Type, MemRet>
153 back_inserter_fun(Cont & cont, MemRet(Type::*p)())
155 return back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
159 template <class Cont, class Type, class MemRet>
160 const_back_insert_fun_iterator<Cont, Type, MemRet>
161 back_inserter_fun(Cont & cont, MemRet(Type::*p)() const)
163 return const_back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
167 template <class R, class C, class A>
168 class compare_memfun_t {
170 compare_memfun_t(R(C::*p)(), A const & a)
172 bool operator()(C * c) {
173 return (c->*pmf)() == arg;
175 bool operator()(C & c) {
176 return (c.*pmf)() == arg;
184 template <class R, class C, class A>
185 class const_compare_memfun_t {
187 const_compare_memfun_t(R(C::*p)() const, A const & a)
189 bool operator()(C const * c) {
190 return (c->*pmf)() == arg;
192 bool operator()(C const & c) {
193 return (c.*pmf)() == arg;
201 template <class R, class C, class A>
202 compare_memfun_t<R, C, A>
203 compare_memfun(R(C::*p)(), A const & a)
205 return compare_memfun_t<R, C, A>(p, a);
209 template <class R, class C, class A>
210 const_compare_memfun_t<R, C, A>
211 compare_memfun(R(C::*p)() const, A const & a)
213 return const_compare_memfun_t<R, C, A>(p, a);
217 // Functors used in the template.
221 class equal_1st_in_pair {
224 typedef typename T::first_type first_type;
228 equal_1st_in_pair(first_type const & value) : value_(value) {}
230 bool operator() (pair_type const & p) const {
231 return p.first == value_;
235 first_type const & value_;
241 class equal_2nd_in_pair {
244 typedef typename T::second_type second_type;
248 equal_2nd_in_pair(second_type const & value) : value_(value) {}
250 bool operator() (pair_type const & p) const {
251 return p.second == value_;
255 second_type const & value_;
258 } // end of namespace lyx