1 /* supplementing compose function objects
2 * Fri Jul 16 21:01:58 MEST 1999
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
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.
15 // See http://www.boost.org/libs/compose for Documentation.
17 #ifndef BOOST_COMPOSE_HPP
18 #define BOOST_COMPOSE_HPP
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;
33 /**********************************************************
34 * ptr_fun for functions with no argument
35 **********************************************************/
36 template <class Result>
37 class pointer_to_nullary_function : public nullary_function<Result>
42 pointer_to_nullary_function() {
44 explicit pointer_to_nullary_function(Result (*x)()) : ptr(x) {
46 Result operator()() const {
51 template <class Result>
52 inline pointer_to_nullary_function<Result> ptr_fun(Result (*x)())
54 return pointer_to_nullary_function<Result>(x);
57 /*********** compose_f_gx_t and compose_f_gx **********************/
59 /* class for the compose_f_gx adapter
61 template <class OP1, class OP2>
63 : public std::unary_function<typename OP2::argument_type,
64 typename OP1::result_type>
67 OP1 op1; // process: op1(op2(x))
71 compose_f_gx_t(const OP1& o1, const OP2& o2)
76 typename OP1::result_type
77 operator()(const typename OP2::argument_type& x) const {
82 /* convenience functions for the compose_f_gx adapter
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);
90 /*********** compose_f_gx_hx_t and compose_f_gx_hx **********************/
92 /* class for the compose_f_gx_hx adapter
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>
100 OP1 op1; // process: op1(op2(x),op3(x))
105 compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
106 : op1(o1), op2(o2), op3(o3) {
110 typename OP1::result_type
111 operator()(const typename OP2::argument_type& x) const {
112 return op1(op2(x),op3(x));
116 /* convenience functions for the compose_f_gx_hx adapter
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);
124 /*********** compose_f_gxy_t and compose_f_gxy **********************/
126 /* class for the compose_f_gxy adapter
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>
135 OP1 op1; // process: op1(op2(x,y))
139 compose_f_gxy_t (const OP1& o1, const OP2& o2)
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));
151 /* convenience function for the compose_f_gxy adapter
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);
159 /*********** compose_f_gx_hy_t and compose_f_gx_hy **********************/
161 /* class for the compose_f_gx_hy adapter
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>
170 OP1 op1; // process: op1(op2(x),op3(y))
175 compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3)
176 : op1(o1), op2(o2), op3(o3) {
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));
187 /* convenience function for the compose_f_gx_hy adapter
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);
195 /*********** compose_f_g_t and compose_f_g **********************/
197 /* class for the compose_f_g adapter
199 template <class OP1, class OP2>
201 : public boost::nullary_function<typename OP1::result_type>
204 OP1 op1; // process: op1(op2())
208 compose_f_g_t(const OP1& o1, const OP2& o2)
213 typename OP1::result_type
219 /* convenience functions for the compose_f_g adapter
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);
227 } /* namespace boost */
229 #endif /*BOOST_COMPOSE_HPP*/