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 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 void_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.
220 class equal_1st_in_pair {
223 typedef typename T::first_type first_type;
227 equal_1st_in_pair(first_type const & value) : value_(value) {}
229 bool operator() (pair_type const & p) const {
230 return p.first == value_;
234 first_type const & value_;
240 class equal_2nd_in_pair {
243 typedef typename T::second_type second_type;
247 equal_2nd_in_pair(second_type const & value) : value_(value) {}
249 bool operator() (pair_type const & p) const {
250 return p.second == value_;
254 second_type const & value_;
257 } // end of namespace lyx