summaryrefslogtreecommitdiffstats
path: root/src/usr/diag/prdf/common/util/UtilFunct.H
blob: 8e91af488768409a09c9d651da91421f329cd79f (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/diag/prdf/common/util/UtilFunct.H $                   */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2005,2013              */
/*                                                                        */
/* p1                                                                     */
/*                                                                        */
/* Object Code Only (OCO) source materials                                */
/* Licensed Internal Code Source Materials                                */
/* IBM HostBoot Licensed Internal Code                                    */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* Origin: 30                                                             */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

/** @file UtilFunct.H
 * Utility template classes for doing functional-style programming across
 * iterators.
 */
#ifndef __UTIL_UTILFUNCT_H
#define __UTIL_UTILFUNCT_H

#include <functional>

namespace PRDF
{

namespace Util
{
    /**
     * @class unary_input
     * Functor that will read a value from a stream; useful for "generate"
     * algorithms such as std::generate_n.
     */
    template <typename Type,
              typename Stream>
    struct unary_input :
        public std::unary_function<void, Type>
    {
        Stream & stream;

        /**
         * @fn unary_input(Stream & i)
         * Constructor
         *
         * @param i : The stream to read from.
         */
        unary_input(Stream & i) : stream(i) {};

        /**
         * @fn operator()
         * Read instance of 'Type' from stream.
         *
         * Overloaded () operator to implement the functor-style behavior.
         */
        Type operator() ()
        {
            Type t;
            stream >> t;
            return t;
        };

    };

    /**
     * @class unary_compose
     * Functor that will compose two functors as in: 'f(g(x))'.
     *
     * This template is an SGI extension to the STL, not currently in the C++
     * standard.  Since it is currently an extension, define it in our Util
     * namespace for portability.
     *
     * Note: This is a clean implementation of the template, not a copy of the
     * SGI code, based on the SGI::STL documentation.
     */
    template <typename Unary1,
              typename Unary2>
    struct unary_compose :
        public std::unary_function<typename Unary2::argument_type,
                                   typename Unary1::result_type>
    {
        Unary1 u1;
        Unary2 u2;

        /**
         * @fn unary_compose(Unary1 & i1, Unary2 & i2)
         * Constructor
         *
         * @param i1 : f functor in f(g(x)).
         * @param i2 : g functor in f(g(x)).
         */
        unary_compose(const Unary1 & i1,
                      const Unary2 & i2) : u1(i1), u2(i2) {};

        /**
         * @fn operator()
         * Execute f(g(x)).
         *
         * Overloaded () operator to implement the functor-style behavior.
         */
        typename Unary1::result_type
        operator() (typename Unary2::argument_type x)
        {
            return u1(u2(x));
        };

    };

    /**
     * @fn unary_compose
     * Utility template function to automatically create a composition functor.
     */
    template <typename Unary1,
              typename Unary2>
    inline
    unary_compose<Unary1, Unary2> compose1(const Unary1 & i1,
                                           const Unary2 & i2)
    {
        return unary_compose<Unary1, Unary2>(i1, i2);
    };

    /**
     * @class unary_null.
     * Functor that will do nothing.
     *
     * Useful in combination with compose for dropping a return from another
     * functor.
     */
    template <typename Type>
    struct unary_null :
        public std::unary_function<Type, void>
    {
        unary_null() {};
        void operator() (Type i) {};
    };

};

} // end namespace PRDF

#endif

OpenPOWER on IntegriCloud