]> git.lyx.org Git - lyx.git/blob - boost/boost/compose.hpp
complie fix
[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 #ifndef BOOST_COMPOSE_HPP
15 #define BOOST_COMPOSE_HPP
16
17 #include <functional>
18
19 namespace boost {
20
21 /**********************************************************
22  * type nullary_function
23  * - as supplement to unary_function and binary_function
24  **********************************************************/
25 template <class Result>
26 struct nullary_function {
27     typedef Result result_type;
28 };
29
30 /**********************************************************
31  * ptr_fun for functions with no argument
32  **********************************************************/
33 template <class Result>
34 class pointer_to_nullary_function : public nullary_function<Result>
35 {
36   protected:
37     Result (*ptr)();
38   public:
39     pointer_to_nullary_function() {
40     }
41     explicit pointer_to_nullary_function(Result (*x)()) : ptr(x) {
42     }
43     Result operator()() const { 
44         return ptr();
45     }
46 };
47
48 template <class Result>
49 inline pointer_to_nullary_function<Result> ptr_fun(Result (*x)())
50 {
51   return pointer_to_nullary_function<Result>(x);
52 }
53
54 /*********** compose_f_gx_t and compose_f_gx **********************/
55
56 /* class for the compose_f_gx adapter
57  */
58 template <class OP1, class OP2>
59 class compose_f_gx_t
60  : public std::unary_function<typename OP2::argument_type,
61                               typename OP1::result_type>
62 {
63   private:
64     OP1 op1;    // process: op1(op2(x))
65     OP2 op2;
66   public:
67     // constructor
68     compose_f_gx_t(const OP1& o1, const OP2& o2)
69      : op1(o1), op2(o2) {
70     }
71
72     // function call
73     typename OP1::result_type
74     operator()(const typename OP2::argument_type& x) const {
75         return op1(op2(x));
76     }
77 };
78
79 /* convenience functions for the compose_f_gx adapter
80  */
81 template <class OP1, class OP2>
82 inline compose_f_gx_t<OP1,OP2>
83 compose_f_gx (const OP1& o1, const OP2& o2) {
84     return compose_f_gx_t<OP1,OP2>(o1,o2);
85 }
86
87 /*********** compose_f_gx_hx_t and compose_f_gx_hx **********************/
88
89 /* class for the compose_f_gx_hx adapter
90  */
91 template <class OP1, class OP2, class OP3>
92 class compose_f_gx_hx_t
93  : public std::unary_function<typename OP2::argument_type,
94                               typename OP1::result_type>
95 {
96   private:
97     OP1 op1;    // process: op1(op2(x),op3(x))
98     OP2 op2;
99     OP3 op3;
100   public:
101     // constructor
102     compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
103      : op1(o1), op2(o2), op3(o3) {
104     }
105
106     // function call
107     typename OP1::result_type
108     operator()(const typename OP2::argument_type& x) const {
109         return op1(op2(x),op3(x));
110     }
111 };
112
113 /* convenience functions for the compose_f_gx_hx adapter
114  */
115 template <class OP1, class OP2, class OP3>
116 inline compose_f_gx_hx_t<OP1,OP2,OP3>
117 compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3) {
118     return compose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
119 }
120
121 /*********** compose_f_gxy_t and compose_f_gxy **********************/
122
123 /* class for the compose_f_gxy adapter
124  */
125 template <class OP1, class OP2>
126 class compose_f_gxy_t
127  : public std::binary_function<typename OP2::first_argument_type,
128                                typename OP2::second_argument_type,
129                                typename OP1::result_type>
130 {
131   private:
132     OP1 op1;    // process: op1(op2(x,y))
133     OP2 op2;
134   public:
135     // constructor
136     compose_f_gxy_t (const OP1& o1, const OP2& o2)
137      : op1(o1), op2(o2) {
138     }
139
140     // function call
141     typename OP1::result_type
142     operator()(const typename OP2::first_argument_type& x,
143                const typename OP2::second_argument_type& y) const {
144         return op1(op2(x,y));
145     }
146 };
147
148 /* convenience function for the compose_f_gxy adapter
149  */
150 template <class OP1, class OP2>
151 inline compose_f_gxy_t<OP1,OP2>
152 compose_f_gxy (const OP1& o1, const OP2& o2) {
153     return compose_f_gxy_t<OP1,OP2>(o1,o2);
154 }
155
156 /*********** compose_f_gx_hy_t and compose_f_gx_hy **********************/
157
158 /* class for the compose_f_gx_hy adapter
159  */
160 template <class OP1, class OP2, class OP3>
161 class compose_f_gx_hy_t
162  : public std::binary_function<typename OP2::argument_type,
163                                typename OP3::argument_type,
164                                typename OP1::result_type>
165 {
166   private:
167     OP1 op1;    // process: op1(op2(x),op3(y))
168     OP2 op2;
169     OP3 op3;
170   public:
171     // constructor
172     compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3)
173      : op1(o1), op2(o2), op3(o3) {
174     }
175
176     // function call
177     typename OP1::result_type
178     operator()(const typename OP2::argument_type& x,
179                const typename OP3::argument_type& y) const {
180         return op1(op2(x),op3(y));
181     }
182 };
183
184 /* convenience function for the compose_f_gx_hy adapter
185  */
186 template <class OP1, class OP2, class OP3>
187 inline compose_f_gx_hy_t<OP1,OP2,OP3>
188 compose_f_gx_hy (const OP1& o1, const OP2& o2, const OP3& o3) {
189     return compose_f_gx_hy_t<OP1,OP2,OP3>(o1,o2,o3);
190 }
191
192 /*********** compose_f_g_t and compose_f_g **********************/
193
194 /* class for the compose_f_g adapter
195  */
196 template <class OP1, class OP2>
197 class compose_f_g_t
198  : public boost::nullary_function<typename OP1::result_type>
199 {
200   private:
201     OP1 op1;    // process: op1(op2())
202     OP2 op2;
203   public:
204     // constructor
205     compose_f_g_t(const OP1& o1, const OP2& o2)
206      : op1(o1), op2(o2) {
207     }
208
209     // function call
210     typename OP1::result_type
211     operator()() const {
212         return op1(op2());
213     }
214 };
215
216 /* convenience functions for the compose_f_g adapter
217  */
218 template <class OP1, class OP2>
219 inline compose_f_g_t<OP1,OP2>
220 compose_f_g (const OP1& o1, const OP2& o2) {
221     return compose_f_g_t<OP1,OP2>(o1,o2);
222 }
223
224 } /* namespace boost */
225
226 #endif /*BOOST_COMPOSE_HPP*/