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.
14 #ifndef BOOST_COMPOSE_HPP
15 #define BOOST_COMPOSE_HPP
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;
30 /**********************************************************
31 * ptr_fun for functions with no argument
32 **********************************************************/
33 template <class Result>
34 class pointer_to_nullary_function : public nullary_function<Result>
39 pointer_to_nullary_function() {
41 explicit pointer_to_nullary_function(Result (*x)()) : ptr(x) {
43 Result operator()() const {
48 template <class Result>
49 inline pointer_to_nullary_function<Result> ptr_fun(Result (*x)())
51 return pointer_to_nullary_function<Result>(x);
54 /*********** compose_f_gx_t and compose_f_gx **********************/
56 /* class for the compose_f_gx adapter
58 template <class OP1, class OP2>
60 : public std::unary_function<typename OP2::argument_type,
61 typename OP1::result_type>
64 OP1 op1; // process: op1(op2(x))
68 compose_f_gx_t(const OP1& o1, const OP2& o2)
73 typename OP1::result_type
74 operator()(const typename OP2::argument_type& x) const {
79 /* convenience functions for the compose_f_gx adapter
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);
87 /*********** compose_f_gx_hx_t and compose_f_gx_hx **********************/
89 /* class for the compose_f_gx_hx adapter
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>
97 OP1 op1; // process: op1(op2(x),op3(x))
102 compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
103 : op1(o1), op2(o2), op3(o3) {
107 typename OP1::result_type
108 operator()(const typename OP2::argument_type& x) const {
109 return op1(op2(x),op3(x));
113 /* convenience functions for the compose_f_gx_hx adapter
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);
121 /*********** compose_f_gxy_t and compose_f_gxy **********************/
123 /* class for the compose_f_gxy adapter
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>
132 OP1 op1; // process: op1(op2(x,y))
136 compose_f_gxy_t (const OP1& o1, const OP2& o2)
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));
148 /* convenience function for the compose_f_gxy adapter
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);
156 /*********** compose_f_gx_hy_t and compose_f_gx_hy **********************/
158 /* class for the compose_f_gx_hy adapter
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>
167 OP1 op1; // process: op1(op2(x),op3(y))
172 compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3)
173 : op1(o1), op2(o2), op3(o3) {
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));
184 /* convenience function for the compose_f_gx_hy adapter
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);
192 /*********** compose_f_g_t and compose_f_g **********************/
194 /* class for the compose_f_g adapter
196 template <class OP1, class OP2>
198 : public boost::nullary_function<typename OP1::result_type>
201 OP1 op1; // process: op1(op2())
205 compose_f_g_t(const OP1& o1, const OP2& o2)
210 typename OP1::result_type
216 /* convenience functions for the compose_f_g adapter
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);
224 } /* namespace boost */
226 #endif /*BOOST_COMPOSE_HPP*/