3 * \file lyxfunctional.h
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
11 * \brief Convenient function objects for use with LyX
13 * This is currently a small collection of small function objects for use
14 * together with std::algorithms.
18 #ifndef LYX_FUNCTIONAL_H
19 #define LYX_FUNCTIONAL_H
25 template <class Cont, class Type, class MemRet>
26 class back_insert_fun_iterator {
31 typedef Cont container_type;
32 typedef std::output_iterator_tag iterator_category;
33 typedef void value_type;
34 typedef void difference_type;
36 typedef void reference;
38 back_insert_fun_iterator(Cont & x, MemRet(Type::*p)())
39 : container(&x), pmf(p) {}
41 back_insert_fun_iterator &
42 operator=(Type * val) {
43 container->push_back((val->*pmf)());
47 back_insert_fun_iterator &
48 operator=(Type & val) {
49 container->push_back((val.*pmf)());
53 back_insert_fun_iterator & operator*() {
56 back_insert_fun_iterator & operator++() { // prefix ++
59 back_insert_fun_iterator & operator++(int) { // postfix ++
65 template <class Cont, class Type, class MemRet>
66 class const_back_insert_fun_iterator {
69 MemRet(Type::*pmf)() const;
71 typedef Cont container_type;
72 typedef std::output_iterator_tag iterator_category;
73 typedef void value_type;
74 typedef void difference_type;
76 typedef void reference;
78 const_back_insert_fun_iterator(Cont & x, MemRet(Type::*p)() const)
79 : container(&x), pmf(p) {}
81 ~const_back_insert_fun_iterator() {}
83 const_back_insert_fun_iterator &
84 operator=(Type const * val) {
85 container->push_back((val->*pmf)());
89 const_back_insert_fun_iterator &
90 operator=(Type const & val) {
91 container->push_back((val.*pmf)());
95 const_back_insert_fun_iterator & operator*() {
98 const_back_insert_fun_iterator & operator++() { // prefix ++
101 const_back_insert_fun_iterator & operator++(int) { // postfix ++
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)())
111 return back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
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)
119 return const_back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
123 template <class R, class C, class A>
124 class compare_memfun_t {
126 compare_memfun_t(R(C::*p)(), A const & a)
128 bool operator()(C * c) {
129 return (c->*pmf)() == arg;
131 bool operator()(C & c) {
132 return (c.*pmf)() == arg;
140 template <class R, class C, class A>
141 class const_compare_memfun_t {
143 const_compare_memfun_t(R(C::*p)() const, A const & a)
145 bool operator()(C const * c) {
146 return (c->*pmf)() == arg;
148 bool operator()(C const & c) {
149 return (c.*pmf)() == arg;
157 template <class R, class C, class A>
158 compare_memfun_t<R, C, A>
159 compare_memfun(R(C::*p)(), A const & a)
161 return compare_memfun_t<R, C, A>(p, a);
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)
169 return const_compare_memfun_t<R, C, A>(p, a);
173 // Functors used in the template.
177 class equal_1st_in_pair {
180 typedef typename T::first_type first_type;
184 equal_1st_in_pair(first_type const & value) : value_(value) {}
186 bool operator() (pair_type const & p) const {
187 return p.first == value_;
191 first_type const & value_;
197 class equal_2nd_in_pair {
200 typedef typename T::second_type second_type;
204 equal_2nd_in_pair(second_type const & value) : value_(value) {}
206 bool operator() (pair_type const & p) const {
207 return p.second == value_;
211 second_type const & value_;
214 } // end of namespace lyx