]> git.lyx.org Git - lyx.git/blob - src/support/lyxfunctional.h
make vsnprintf work on systems without it
[lyx.git] / src / support / lyxfunctional.h
1 // -*- C++ -*-
2
3 #ifndef LYX_FUNCTIONAL_H
4 #define LYX_FUNCTIONAL_H
5
6 //namespace lyx {
7
8 template<class R, class C, class A>
9 class class_fun_t {
10 public:
11         class_fun_t(C & ct, R(C::*p)(A)) 
12                 : c(ct), cmf(p) {}
13         R operator()(A & a) const {
14                 return (c.*cmf)(a);
15         }
16 private:
17         C & c;
18         R(C::*cmf)(A);
19 };
20
21
22 template<class C, class A>
23 class void_class_fun_t {
24 public:
25         void_class_fun_t(C & ct, void(C::*p)(A))
26                 : c(ct), cmf(p) {}
27         void operator()(A & a) const {
28                 (c.*cmf)(a);
29         }
30 private:
31         C & c;
32         void(C::*cmf)(A);
33 };
34
35        
36 template <class R, class C, class A> class_fun_t<R, C, A>
37 class_fun(C & c, R(C::*f)(A))
38 {
39         return class_fun_t<R, C, A>(c, f);
40 }
41
42
43 template <class C, class A> void_class_fun_t<C, A>
44 class_fun(C & c, void(C::*f)(A))
45 {
46         return void_class_fun_t<C, A>(c, f);
47 }
48
49
50 template <class Cont, class Type, class MemRet>
51 class back_insert_fun_iterator {
52 protected:
53         Cont & container;
54         MemRet(Type::*pmf)();
55 public:
56         back_insert_fun_iterator(Cont & x, MemRet(Type::*p)())
57                 : container(x), pmf(p) {}
58
59         back_insert_fun_iterator &
60         operator=(Type * val) {
61                 container.push_back((val->*pmf)());
62                 return *this;
63         }
64
65         back_insert_fun_iterator &
66         operator=(Type & val) {
67                 container.push_back((val.*pmf)());
68                 return *this;
69         }
70
71         back_insert_fun_iterator & operator*() {
72                 return *this;
73         }
74         back_insert_fun_iterator & operator++() { // prefix ++
75                 return *this;
76         }
77         back_insert_fun_iterator & operator++(int) { // postfix ++
78                 return *this;
79         }
80 };
81
82
83 template <class Cont, class Type, class MemRet>
84 class const_back_insert_fun_iterator {
85 protected:
86         Cont & container;
87         MemRet(Type::*pmf)() const;
88 public:
89         const_back_insert_fun_iterator(Cont & x, MemRet(Type::*p)() const)
90                 : container(x), pmf(p) {}
91
92         const_back_insert_fun_iterator &
93         operator=(Type const * val) {
94                 container.push_back((val->*pmf)());
95                 return *this;
96         }
97
98         const_back_insert_fun_iterator &
99         operator=(Type const & val) {
100                 container.push_back((val.*pmf)());
101                 return *this;
102         }
103
104         const_back_insert_fun_iterator & operator*() {
105                 return *this;
106         }
107         const_back_insert_fun_iterator & operator++() { // prefix ++
108                 return *this;
109         }
110         const_back_insert_fun_iterator & operator++(int) { // postfix ++
111                 return *this;
112         }
113 };
114
115
116 template <class Cont, class Type, class MemRet>
117 back_insert_fun_iterator<Cont, Type, MemRet>
118 back_inserter_fun(Cont & cont, MemRet(Type::*p)())
119 {
120         return back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
121 }
122
123
124 template <class Cont, class Type, class MemRet>
125 const_back_insert_fun_iterator<Cont, Type, MemRet>
126 back_inserter_fun(Cont & cont, MemRet(Type::*p)() const)
127 {
128         return const_back_insert_fun_iterator<Cont, Type, MemRet>(cont, p);
129 }
130
131
132 template <class R, class C, class A>
133 class compare_memfun_t {
134 public:
135         compare_memfun_t(R(C::*p)(), A const & a)
136                 : pmf(p), arg(a) {}
137         bool operator()(C * c) {
138                 return (c->*pmf)() == arg;
139         }
140         bool operator()(C & c) {
141                 return (c.*pmf)() == arg;
142         }
143 private:
144         R(C::*pmf)();
145         A const & arg;
146 };
147
148
149 template <class R, class C, class A>
150 class const_compare_memfun_t {
151 public:
152         const_compare_memfun_t(R(C::*p)() const, A const & a)
153                 : pmf(p), arg(a) {}
154         bool operator()(C const * c) {
155                 return (c->*pmf)() == arg;
156         }
157         bool operator()(C const & c) {
158                 return (c.*pmf)() == arg;
159         }
160 private:
161         R(C::*pmf)() const;
162         A const & arg;
163 };
164
165
166 template <class R, class C, class A>
167 compare_memfun_t<R, C, A>
168 compare_memfun(R(C::*p)(), A const & a)
169 {
170         return compare_memfun_t<R, C, A>(p, a);
171 }
172
173
174 template <class R, class C, class A>
175 const_compare_memfun_t<R, C, A>
176 compare_memfun(R(C::*p)() const, A const & a)
177 {
178         return const_compare_memfun_t<R, C, A>(p, a);
179 }
180
181         
182 // Functors used in the template.
183
184 ///
185 template<typename T1, typename T2>
186 class equal_1st_in_pair {
187 public:
188         ///
189         equal_1st_in_pair(T1 const & value) : value_(value) {}
190         ///
191         typedef std::pair<T1, T2> pair_type;
192         ///
193         bool operator() (pair_type const & p) const {
194                 return p.first == value_;
195         }
196 private:
197         ///
198         T1 const & value_;
199 };
200
201
202 ///
203 template<typename T1, typename T2>
204 class equal_2nd_in_pair {
205 public:
206         ///
207         equal_2nd_in_pair(T2 const & value) : value_(value) {}
208         ///
209         typedef std::pair<T1, T2> pair_type;
210         ///
211         bool operator() (pair_type const & p) const {
212                 return p.second == value_;
213         }
214 private:
215         ///
216         T2 const & value_;
217 };
218
219
220 // }  // end of namespace lyx
221 #endif