3 #ifndef LYX_FUNCTIONAL_H
4 #define LYX_FUNCTIONAL_H
6 /** \file lyxfunctional.h
7 \brief Convenient function objects for use with LyX
8 This is currently a small collection of small function objects for use
9 together with std::algorithms.
17 template<class R, class C, class A>
20 class_fun_t(C & ct, R(C::*p)(A))
22 R operator()(A & a) const {
31 template<class C, class A>
32 class void_class_fun_t {
34 void_class_fun_t(C & ct, void(C::*p)(A))
36 void operator()(A & a) const {
45 /// Use to call a class method with a container element.
46 /** Most easily used as a functor to std::algoritms.
49 A a; // class that have a int print(string const &) method
51 for_each(vs.begin(), vs.end(), class_fun(int, vs, &A::print);
54 template <class R, class C, class A> class_fun_t<R, C, A>
55 class_fun(C & c, R(C::*f)(A))
57 return class_fun_t<R, C, A>(c, f);
61 template <class C, class A> void_class_fun_t<C, A>
62 class_fun(C & c, void(C::*f)(A))
64 return void_class_fun_t<C, A>(c, f);
68 template <class Cont, class Type, class MemRet>
69 class back_insert_fun_iterator {
74 typedef Cont container_type;
75 typedef std::output_iterator_tag iterator_category;
76 typedef void value_type;
77 typedef void difference_type;
79 typedef void reference;
81 back_insert_fun_iterator(Cont & x, MemRet(Type::*p)())
82 : container(&x), pmf(p) {}
84 back_insert_fun_iterator &
85 operator=(Type * val) {
86 container->push_back((val->*pmf)());
90 back_insert_fun_iterator &
91 operator=(Type & val) {
92 container->push_back((val.*pmf)());
96 back_insert_fun_iterator & operator*() {
99 back_insert_fun_iterator & operator++() { // prefix ++
102 back_insert_fun_iterator & operator++(int) { // postfix ++
108 template <class Cont, class Type, class MemRet>
109 class const_back_insert_fun_iterator {
112 MemRet(Type::*pmf)() const;
114 typedef Cont container_type;
115 typedef std::output_iterator_tag iterator_category;
116 typedef void value_type;
117 typedef void difference_type;
118 typedef void pointer;
119 typedef void reference;
121 const_back_insert_fun_iterator(Cont & x, MemRet(Type::*p)() const)
122 : container(&x), pmf(p) {}
124 ~const_back_insert_fun_iterator() {}
126 const_back_insert_fun_iterator &
127 operator=(Type const * val) {
128 container->push_back((val->*pmf)());
132 const_back_insert_fun_iterator &
133 operator=(Type const & val) {
134 container->push_back((val.*pmf)());
138 const_back_insert_fun_iterator & operator*() {
141 const_back_insert_fun_iterator & operator++() { // prefix ++
144 const_back_insert_fun_iterator & operator++(int) { // postfix ++
150 template <class Cont, class Type, class MemRet>
151 back_insert_fun_iterator<Cont, Type, MemRet>
152 back_inserter_fun(Cont & cont, MemRet(Type::*p)())
154 return back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
158 template <class Cont, class Type, class MemRet>
159 const_back_insert_fun_iterator<Cont, Type, MemRet>
160 back_inserter_fun(Cont & cont, MemRet(Type::*p)() const)
162 return const_back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
166 template <class R, class C, class A>
167 class compare_memfun_t {
169 compare_memfun_t(R(C::*p)(), A const & a)
171 bool operator()(C * c) {
172 return (c->*pmf)() == arg;
174 bool operator()(C & c) {
175 return (c.*pmf)() == arg;
183 template <class R, class C, class A>
184 class const_compare_memfun_t {
186 const_compare_memfun_t(R(C::*p)() const, A const & a)
188 bool operator()(C const * c) {
189 return (c->*pmf)() == arg;
191 bool operator()(C const & c) {
192 return (c.*pmf)() == arg;
200 template <class R, class C, class A>
201 compare_memfun_t<R, C, A>
202 compare_memfun(R(C::*p)(), A const & a)
204 return compare_memfun_t<R, C, A>(p, a);
208 template <class R, class C, class A>
209 const_compare_memfun_t<R, C, A>
210 compare_memfun(R(C::*p)() const, A const & a)
212 return const_compare_memfun_t<R, C, A>(p, a);
216 // Functors used in the template.
219 template<typename T1, typename T2>
220 class equal_1st_in_pair {
223 equal_1st_in_pair(T1 const & value) : value_(value) {}
225 typedef std::pair<T1, T2> pair_type;
227 bool operator() (pair_type const & p) const {
228 return p.first == value_;
237 template<typename T1, typename T2>
238 class equal_2nd_in_pair {
241 equal_2nd_in_pair(T2 const & value) : value_(value) {}
243 typedef std::pair<T1, T2> pair_type;
245 bool operator() (pair_type const & p) const {
246 return p.second == value_;
254 // } // end of namespace lyx