]> git.lyx.org Git - lyx.git/blob - boost/boost/compose.hpp
LFUN_ESCAPE gets ReadOnly (fix bug 1142)
[lyx.git] / boost / boost / compose.hpp
1 /* supplementing compose function objects
2  * Fri Jul 16 21:01:58 MEST 1999
3  */
4 /* The following code example is taken from the book
5  * "The C++ Standard Library - A Tutorial and Reference"
6  * by Nicolai M. Josuttis, Addison-Wesley, 1999
7  *
8  * (C) Copyright Nicolai M. Josuttis 1999.
9  * Permission to copy, use, modify, sell and distribute this software
10  * is granted provided this copyright notice appears in all copies.
11  * This software is provided "as is" without express or implied
12  * warranty, and with no claim as to its suitability for any purpose.
13  */
14
15 // See http://www.boost.org/libs/compose for Documentation.
16
17 #ifndef BOOST_COMPOSE_HPP
18 #define BOOST_COMPOSE_HPP
19
20 #include <functional>
21
22 namespace boost {
23
24 /**********************************************************
25  * type nullary_function
26  * - as supplement to unary_function and binary_function
27  **********************************************************/
28 template <class Result>
29 struct nullary_function {
30     typedef Result result_type;
31 };
32
33 /**********************************************************
34  * ptr_fun for functions with no argument
35  **********************************************************/
36 template <class Result>
37 class pointer_to_nullary_function : public nullary_function<Result>
38 {
39   protected:
40     Result (*ptr)();
41   public:
42     pointer_to_nullary_function() {
43     }
44     explicit pointer_to_nullary_function(Result (*x)()) : ptr(x) {
45     }
46     Result operator()() const { 
47         return ptr();
48     }
49 };
50
51 template <class Result>
52 inline pointer_to_nullary_function<Result> ptr_fun(Result (*x)())
53 {
54   return pointer_to_nullary_function<Result>(x);
55 }
56
57 /*********** compose_f_gx_t and compose_f_gx **********************/
58
59 /* class for the compose_f_gx adapter
60  */
61 template <class OP1, class OP2>
62 class compose_f_gx_t
63  : public std::unary_function<typename OP2::argument_type,
64                               typename OP1::result_type>
65 {
66   private:
67     OP1 op1;    // process: op1(op2(x))
68     OP2 op2;
69   public:
70     // constructor
71     compose_f_gx_t(const OP1& o1, const OP2& o2)
72      : op1(o1), op2(o2) {
73     }
74
75     // function call
76     typename OP1::result_type
77     operator()(const typename OP2::argument_type& x) const {
78         return op1(op2(x));
79     }
80 };
81
82 /* convenience functions for the compose_f_gx adapter
83  */
84 template <class OP1, class OP2>
85 inline compose_f_gx_t<OP1,OP2>
86 compose_f_gx (const OP1& o1, const OP2& o2) {
87     return compose_f_gx_t<OP1,OP2>(o1,o2);
88 }
89
90 /*********** compose_f_gx_hx_t and compose_f_gx_hx **********************/
91
92 /* class for the compose_f_gx_hx adapter
93  */
94 template <class OP1, class OP2, class OP3>
95 class compose_f_gx_hx_t
96  : public std::unary_function<typename OP2::argument_type,
97                               typename OP1::result_type>
98 {
99   private:
100     OP1 op1;    // process: op1(op2(x),op3(x))
101     OP2 op2;
102     OP3 op3;
103   public:
104     // constructor
105     compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
106      : op1(o1), op2(o2), op3(o3) {
107     }
108
109     // function call
110     typename OP1::result_type
111     operator()(const typename OP2::argument_type& x) const {
112         return op1(op2(x),op3(x));
113     }
114 };
115
116 /* convenience functions for the compose_f_gx_hx adapter
117  */
118 template <class OP1, class OP2, class OP3>
119 inline compose_f_gx_hx_t<OP1,OP2,OP3>
120 compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3) {
121     return compose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
122 }
123
124 /*********** compose_f_gxy_t and compose_f_gxy **********************/
125
126 /* class for the compose_f_gxy adapter
127  */
128 template <class OP1, class OP2>
129 class compose_f_gxy_t
130  : public std::binary_function<typename OP2::first_argument_type,
131                                typename OP2::second_argument_type,
132                                typename OP1::result_type>
133 {
134   private:
135     OP1 op1;    // process: op1(op2(x,y))
136     OP2 op2;
137   public:
138     // constructor
139     compose_f_gxy_t (const OP1& o1, const OP2& o2)
140      : op1(o1), op2(o2) {
141     }
142
143     // function call
144     typename OP1::result_type
145     operator()(const typename OP2::first_argument_type& x,
146                const typename OP2::second_argument_type& y) const {
147         return op1(op2(x,y));
148     }
149 };
150
151 /* convenience function for the compose_f_gxy adapter
152  */
153 template <class OP1, class OP2>
154 inline compose_f_gxy_t<OP1,OP2>
155 compose_f_gxy (const OP1& o1, const OP2& o2) {
156     return compose_f_gxy_t<OP1,OP2>(o1,o2);
157 }
158
159 /*********** compose_f_gx_hy_t and compose_f_gx_hy **********************/
160
161 /* class for the compose_f_gx_hy adapter
162  */
163 template <class OP1, class OP2, class OP3>
164 class compose_f_gx_hy_t
165  : public std::binary_function<typename OP2::argument_type,
166                                typename OP3::argument_type,
167                                typename OP1::result_type>
168 {
169   private:
170     OP1 op1;    // process: op1(op2(x),op3(y))
171     OP2 op2;
172     OP3 op3;
173   public:
174     // constructor
175     compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3)
176      : op1(o1), op2(o2), op3(o3) {
177     }
178
179     // function call
180     typename OP1::result_type
181     operator()(const typename OP2::argument_type& x,
182                const typename OP3::argument_type& y) const {
183         return op1(op2(x),op3(y));
184     }
185 };
186
187 /* convenience function for the compose_f_gx_hy adapter
188  */
189 template <class OP1, class OP2, class OP3>
190 inline compose_f_gx_hy_t<OP1,OP2,OP3>
191 compose_f_gx_hy (const OP1& o1, const OP2& o2, const OP3& o3) {
192     return compose_f_gx_hy_t<OP1,OP2,OP3>(o1,o2,o3);
193 }
194
195 /*********** compose_f_g_t and compose_f_g **********************/
196
197 /* class for the compose_f_g adapter
198  */
199 template <class OP1, class OP2>
200 class compose_f_g_t
201  : public boost::nullary_function<typename OP1::result_type>
202 {
203   private:
204     OP1 op1;    // process: op1(op2())
205     OP2 op2;
206   public:
207     // constructor
208     compose_f_g_t(const OP1& o1, const OP2& o2)
209      : op1(o1), op2(o2) {
210     }
211
212     // function call
213     typename OP1::result_type
214     operator()() const {
215         return op1(op2());
216     }
217 };
218
219 /* convenience functions for the compose_f_g adapter
220  */
221 template <class OP1, class OP2>
222 inline compose_f_g_t<OP1,OP2>
223 compose_f_g (const OP1& o1, const OP2& o2) {
224     return compose_f_g_t<OP1,OP2>(o1,o2);
225 }
226
227 } /* namespace boost */
228
229 #endif /*BOOST_COMPOSE_HPP*/