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.
17 template <class Cont, class Type, class MemRet>
18 class back_insert_fun_iterator {
23 typedef Cont container_type;
24 typedef std::output_iterator_tag iterator_category;
25 typedef void value_type;
26 typedef void difference_type;
28 typedef void reference;
30 back_insert_fun_iterator(Cont & x, MemRet(Type::*p)())
31 : container(&x), pmf(p) {}
33 back_insert_fun_iterator &
34 operator=(Type * val) {
35 container->push_back((val->*pmf)());
39 back_insert_fun_iterator &
40 operator=(Type & val) {
41 container->push_back((val.*pmf)());
45 back_insert_fun_iterator & operator*() {
48 back_insert_fun_iterator & operator++() { // prefix ++
51 back_insert_fun_iterator & operator++(int) { // postfix ++
57 template <class Cont, class Type, class MemRet>
58 class const_back_insert_fun_iterator {
61 MemRet(Type::*pmf)() const;
63 typedef Cont container_type;
64 typedef std::output_iterator_tag iterator_category;
65 typedef void value_type;
66 typedef void difference_type;
68 typedef void reference;
70 const_back_insert_fun_iterator(Cont & x, MemRet(Type::*p)() const)
71 : container(&x), pmf(p) {}
73 ~const_back_insert_fun_iterator() {}
75 const_back_insert_fun_iterator &
76 operator=(Type const * val) {
77 container->push_back((val->*pmf)());
81 const_back_insert_fun_iterator &
82 operator=(Type const & val) {
83 container->push_back((val.*pmf)());
87 const_back_insert_fun_iterator & operator*() {
90 const_back_insert_fun_iterator & operator++() { // prefix ++
93 const_back_insert_fun_iterator & operator++(int) { // postfix ++
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)())
103 return back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
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)
111 return const_back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
115 template <class R, class C, class A>
116 class compare_memfun_t {
118 compare_memfun_t(R(C::*p)(), A const & a)
120 bool operator()(C * c) {
121 return (c->*pmf)() == arg;
123 bool operator()(C & c) {
124 return (c.*pmf)() == arg;
132 template <class R, class C, class A>
133 class const_compare_memfun_t {
135 const_compare_memfun_t(R(C::*p)() const, A const & a)
137 bool operator()(C const * c) {
138 return (c->*pmf)() == arg;
140 bool operator()(C const & c) {
141 return (c.*pmf)() == arg;
149 template <class R, class C, class A>
150 compare_memfun_t<R, C, A>
151 compare_memfun(R(C::*p)(), A const & a)
153 return compare_memfun_t<R, C, A>(p, a);
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)
161 return const_compare_memfun_t<R, C, A>(p, a);
165 // Functors used in the template.
169 class equal_1st_in_pair {
172 typedef typename T::first_type first_type;
176 equal_1st_in_pair(first_type const & value) : value_(value) {}
178 bool operator() (pair_type const & p) const {
179 return p.first == value_;
183 first_type const & value_;
189 class equal_2nd_in_pair {
192 typedef typename T::second_type second_type;
196 equal_2nd_in_pair(second_type const & value) : value_(value) {}
198 bool operator() (pair_type const & p) const {
199 return p.second == value_;
203 second_type const & value_;
206 } // end of namespace lyx