summaryrefslogtreecommitdiffstats
path: root/src/include/functional
blob: ddefe2bf9c5c779cfbdd2d93c7edfafc0d277f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/include/functional.C $                                    */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2014                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#ifndef _FUNCTIONAL_H
#define _FUNCTIONAL_H

// See C++ spec

namespace std
{
    template<typename A1, typename A2, typename R>
        struct binary_function
        {
            typedef A1 first_argument_type;   ///< type of the first argument
            typedef A2 second_argument_type;  ///< type of the second argument
            typedef R result_type;            ///< type of the return type
        };

    template<typename A, typename R>
        struct unary_function
        {
            typedef A argument_type;
            typedef R result_type;
        };

    /**
     * less template
     */
    template<typename T>
        struct less : public binary_function<T, T, bool>
    {
        /**
         * operator()
         * @param[in] x first object
         * @param[in] y second object
         * @return true if x < y otherwise false
         */
        bool operator()(const T& x, const T& y) const
        {
            return x < y;
        }
    };

    template<typename T>
        struct greater : public binary_function<T, T, bool>
    {
        /**
         * operator()
         * @param[in] x first object
         * @param[in] y second object
         * @return true if x > y otherwise false
         */
        bool operator()(const T& x, const T& y) const
            { return x > y; }
    };

// --------------- ptr_fun templates --------------- //
    template<typename A1, typename Result>
        struct pointer_to_unary_function : public unary_function<A1, Result>
    {
        explicit pointer_to_unary_function(Result (*f)(A1)) : func(f) {}
        Result operator()(A1 a1) const { return (*func)(a1); }

        protected:
            Result (*func)(A1);
    };

    template <typename A1, typename Result>
        pointer_to_unary_function<A1, Result> ptr_fun(Result (*f)(A1))
    {
        return pointer_to_unary_function<A1, Result>(f);
    }

    template<typename A1, typename A2, typename Result>
        struct pointer_to_binary_function :
            public binary_function<A1, A2, Result>
    {
        explicit pointer_to_binary_function(Result (*f)(A1, A2)) : func(f) {}
        Result operator()(A1 a1, A2 a2) const { return (*func)(a1,a2); }

        protected:
            Result (*func)(A1,A2);
    };

    template <typename A1, typename A2, typename Result>
        pointer_to_binary_function<A1, A2, Result> ptr_fun(Result (*f)(A1,A2))
    {
        return pointer_to_binary_function<A1, A2, Result>(f);
    }

// --------------- mem_fun templates --------------- //

    template<typename Result, typename X>
        struct mem_fun_t : public unary_function<X*, Result>
    {
        explicit mem_fun_t(Result (X::*f)()) : func(f) {}
        Result operator()(X* x) const { return (x->*func)(); }

        protected:
            Result (X::*func)();
    };

    template<typename Result, typename X>
        struct const_mem_fun_t : public unary_function<X*, Result>
    {
        explicit const_mem_fun_t(Result (X::*f)() const) : func(f) {}
        Result operator()(const X* x) const { return (x->*func)(); }

        protected:
            Result (X::*func)() const;
    };
        
    template<typename Result, typename X>
    mem_fun_t<Result, X> mem_fun(Result (X::*f)())
    {
        return mem_fun_t<Result, X>(f);
    }

    template<typename Result, typename X>
    const_mem_fun_t<Result, X> mem_fun(Result (X::*f)() const)
    {
        return const_mem_fun_t<Result, X>(f);
    }

// --------------- mem_fun1 templates --------------- //

    template<typename Result, typename X, typename Arg>
        struct mem_fun1_t : public binary_function<X*, Arg, Result>
    {
        explicit mem_fun1_t(Result (X::*f)(Arg)) : func(f) {}
        Result operator()(X* x, Arg a) const { return (x->*func)(a); }

        protected:
            Result (X::*func)(Arg);
    };

    template<typename Result, typename X, typename Arg>
        struct const_mem_fun1_t : public binary_function<X*, Arg, Result>
    {
        explicit const_mem_fun1_t(Result (X::*f)(Arg) const) : func(f) {}
        Result operator()(const X* x, Arg a) const { return (x->*func)(a); }

        protected:
            Result (X::*func)(Arg) const;
    };

    template<typename Result, typename X, typename Arg>
    mem_fun1_t<Result, X, Arg> mem_fun(Result (X::*f)(Arg))
    {
        return mem_fun1_t<Result, X, Arg>(f);
    }

    template<typename Result, typename X, typename Arg>
    const_mem_fun1_t<Result, X, Arg> mem_fun(Result (X::*f)(Arg) const)
    {
        return const_mem_fun1_t<Result, X, Arg>(f);
    }

// --------------- mem_fun_ref templates --------------- //

    template<typename Result, typename X>
        struct mem_fun_ref_t : public unary_function<X, Result>
    {
        explicit mem_fun_ref_t(Result (X::*f)()) : func(f) {}
        Result operator()(X& x) const { return (x.*func)(); }

        protected:
            Result (X::*func)();
    };

    template<typename Result, typename X>
        struct const_mem_fun_ref_t : public unary_function<X, Result>
    {
        explicit const_mem_fun_ref_t(Result (X::*f)() const) : func(f) {}
        Result operator()(const X& x) const { return (x.*func)(); }

        protected:
            Result (X::*func)() const;
    };
        
    template<typename Result, typename X>
    mem_fun_ref_t<Result, X> mem_fun_ref(Result (X::*f)())
    {
        return mem_fun_ref_t<Result, X>(f);
    }

    template<typename Result, typename X>
    const_mem_fun_ref_t<Result, X> mem_fun_ref(Result (X::*f)() const)
    {
        return const_mem_fun_ref_t<Result, X>(f);
    }

// --------------- mem_fun1_ref templates --------------- //

    template<typename Result, typename X, typename Arg>
        struct mem_fun1_ref_t : public binary_function<X, Arg, Result>
    {
        explicit mem_fun1_ref_t(Result (X::*f)(Arg)) : func(f) {}
        Result operator()(X& x, Arg a) const { return (x.*func)(a); }

        protected:
            Result (X::*func)(Arg);
    };

    template<typename Result, typename X, typename Arg>
        struct const_mem_fun1_ref_t : public binary_function<X, Arg, Result>
    {
        explicit const_mem_fun1_ref_t(Result (X::*f)(Arg) const) : func(f) {}
        Result operator()(const X& x, Arg a) const { return (x.*func)(a); }

        protected:
            Result (X::*func)(Arg) const;
    };

    template<typename Result, typename X, typename Arg>
    mem_fun1_ref_t<Result, X, Arg> mem_fun_ref(Result (X::*f)(Arg))
    {
        return mem_fun1_ref_t<Result, X, Arg>(f);
    }

    template<typename Result, typename X, typename Arg>
    const_mem_fun1_ref_t<Result, X, Arg> mem_fun_ref(Result (X::*f)(Arg) const)
    {
        return const_mem_fun1_ref_t<Result, X, Arg>(f);
    }

// --------------- bind1st templates --------------- //

    template<typename AdaptableBinaryFunction>
    struct binder1st : 
        public unary_function<
                    typename AdaptableBinaryFunction::second_argument_type, 
                    typename AdaptableBinaryFunction::result_type
               >
    {
        binder1st(const AdaptableBinaryFunction& F,
                  typename AdaptableBinaryFunction::first_argument_type c) :
            func(F), arg(c) {}

        typename AdaptableBinaryFunction::result_type 
            operator()(const typename 
                       AdaptableBinaryFunction::second_argument_type& x) const
        { 
            return func(arg, x);
        }

        protected:
            AdaptableBinaryFunction func;
            typename AdaptableBinaryFunction::first_argument_type arg;
    };

    template<typename AdaptableBinaryFunction, typename T>
    binder1st<AdaptableBinaryFunction>
        bind1st(const AdaptableBinaryFunction& F,
                const T& c)
    {
        return binder1st<AdaptableBinaryFunction>(F, 
                    typename AdaptableBinaryFunction::first_argument_type(c));
    };

// --------------- bind2nd templates --------------- //

    template<typename AdaptableBinaryFunction>
    struct binder2nd : 
        public unary_function<
                    typename AdaptableBinaryFunction::first_argument_type, 
                    typename AdaptableBinaryFunction::result_type
               >
    {
        binder2nd(const AdaptableBinaryFunction& F,
                  typename AdaptableBinaryFunction::second_argument_type c) :
            func(F), arg(c) {}

        typename AdaptableBinaryFunction::result_type 
            operator()(const typename 
                       AdaptableBinaryFunction::first_argument_type& x) const
        { 
            return func(x, arg);
        }

        protected:
            AdaptableBinaryFunction func;
            typename AdaptableBinaryFunction::second_argument_type arg;
    };

    template<typename AdaptableBinaryFunction, typename T>
    binder2nd<AdaptableBinaryFunction>
        bind2nd(const AdaptableBinaryFunction& F,
                const T& c)
    {
        return binder2nd<AdaptableBinaryFunction>(F, 
                    typename AdaptableBinaryFunction::second_argument_type(c));
    };


};
#endif
/* vim: set filetype=cpp : */
OpenPOWER on IntegriCloud