summaryrefslogtreecommitdiffstats
path: root/src/include/usr/targeting/iterators/rangefilter.H
blob: 21393bf2ace9b2b7290aa0c3c209b3b7ce1952b5 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//  IBM_PROLOG_BEGIN_TAG
//  This is an automatically generated prolog.
//
//  $Source: src/include/usr/targeting/iterators/rangefilter.H $
//
//  IBM CONFIDENTIAL
//
//  COPYRIGHT International Business Machines Corp. 2011
//
//  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 other-
//  wise divested of its trade secrets, irrespective of what has
//  been deposited with the U.S. Copyright Office.
//
//  Origin: 30
//
//  IBM_PROLOG_END

#ifndef TARG_RANGEFILTER_H
#define TARG_RANGEFILTER_H

/**
 *  @file rangefilter.H
 *
 *  @brief Interface describing an object which takes an iterator range and 
 *      allows caller to iterate through the elements which match a supplied
 *      predicate (filter)
 */

//******************************************************************************
// Includes
//******************************************************************************

// STD

// Other Host Boot Components

// Targeting Component
#include <targeting/target.H>
#include <targeting/predicates/predicatebase.H>
#include <targeting/iterators/targetiterator.H>

//******************************************************************************
// Macros
//******************************************************************************

#undef TARG_NAMESPACE
#undef TARG_CLASS
#undef TARG_FUNC

//******************************************************************************
// Interface
//******************************************************************************

namespace TARGETING
{

#define TARG_NAMESPACE "TARGETING::"

#define TARG_CLASS "RangeFilter<IteratorType>::"

/**
 *  @brief Templated class which which takes an iterator range of arbitrary type
 *      and allows caller to iterate through the elements which match a supplied
 *      predicate (filter)
 */
template<typename IteratorType>
class RangeFilter
{
    public:

        /**
         *  @var Forward the type of iterated elements to help build the 
         *      class generically
         */
        typedef typename IteratorType::value_type value_type;

        /**
         *  @var fake_bool is a pointer to a member function of RangeFilter
         *      which takes no arguments and returns void.
         *      Used to implement ability to check if range iterator is valid 
         */ 
        typedef void (RangeFilter::*fake_bool)() const;

        /**
         *  @brief Create a range object and locate first item in the range that
         *      matches the predicate (or the first if no predicate)
         *
         *  @param[in] i_begin Iterator pointing to first item in range
         *  @param[in] i_end Iterator pointing to last item in range
         *  @param[in] i_pPredicate Predicate evaluated against each pointed to
         *      item
         *
         *  @note Begin iterator must point to element prior to end iterator, 
         *      otherwise result is undefined
         */  
        RangeFilter(
            const IteratorType&  i_begin,
            const IteratorType&  i_end,
            const PredicateBase* i_pPredicate = NULL);

        /**
         *  @brief Destroy a range object (nothing to do)
         */
        ~RangeFilter()
        {
        }

        /**
         *  @brief Dummy function used to implement ability to check if range 
         *      iterator is valid 
         */ 
        operator fake_bool() const
        {   
            return (iv_current != iv_end) 
                ? &RangeFilter::notComparable : NULL;
        }

        /**
         *  @brief Pre-increment operator which advances the range's iterator
         *      to point to the next valid element
         *  
         *  @return Reference to this range filter
         */ 
        RangeFilter& operator++();

        /**
         *  @brief Dereference the range filter, returning the element pointed
         *
         *  @return The element pointed to by the range filter
         */ 
        value_type operator*() const
        {
            return *iv_current;
        }

        /**
         *  @brief Dereference the range filter, returning the element pointed
         *      for use by the dereference and call member operator
         *
         *  @return The element pointed to by the range filter
         */ 
        value_type operator->() const
        {
            return *iv_current;
        }

        /**
         *  @brief Reset the range's internal iterator to point to the first
         *      item in the range that matches the predicate (or the first if no
         *      predicate)
         */ 
        void reset();

        /**
         *  @brief Set the range's predicate filter
         *
         *  @param[in] i_pPredicate Predicate to evaluate against elements 
         *      pointed to by the range
         */  
        void setPredicate(
            const PredicateBase* i_pPredicate);

        /**
         *  @brief Assignment operator; assign one range filter to another
         *
         *  @param[in] i_rhs Range filter to assign
         */  
        RangeFilter& operator=(
            const RangeFilter& i_rhs);

        /**
         *  @brief Copy constructor; create range filter using another range
         *      filter as a prototype for it
         *
         *  @param[in] i_rhs Range filter to assign on create
         */ 
        RangeFilter(
            const RangeFilter& i_rhs);

    private:

        /**
         *  @brief Update internal cursor to point to the next element of the 
         *      underlying range
         *
         *  @note If the internal cursor is at the end of the range, then do 
         *      nothing
         */  
        void advance();

        /**
         *  @brief Update internal cursor to point to the next element of the 
         *      underlying range only if not past the end of the range and the
         *      predicate doesn't match the current item
         *
         *  @note If the internal cursor is at the end of the range, then do 
         *      nothing
         */   
        void advanceIfNoMatch();

        /**
         *  @brief Dummy function used to implement ability to check if range 
         *      iterator is valid 
         */
        void notComparable() const 
        { 
        }

        IteratorType         iv_current;    ///< Iterator pointing to the
                                            ///< current element
        IteratorType         iv_begin;      ///< Iterator pointing to first
                                            ///< element in range
        IteratorType         iv_end;        ///< Iterator to past the end
                                            ///< element in range
        const PredicateBase* iv_pPredicate; ///< Range filter
};

typedef RangeFilter<TargetIterator>      TargetRangeFilter;
typedef RangeFilter<ConstTargetIterator> ConstTargetRangeFilter;

//******************************************************************************
// RangeFilter::RangeFilter 
//******************************************************************************

template<typename IteratorType>
RangeFilter<IteratorType>::RangeFilter(
    const IteratorType&        i_begin,
    const IteratorType&        i_end,
    const PredicateBase* const i_pPredicate)
: iv_current(i_begin),
  iv_begin(i_begin), 
  iv_end(i_end),
  iv_pPredicate(i_pPredicate)
{
    advanceIfNoMatch();
}

//******************************************************************************
// RangeFilter::operator= 
//******************************************************************************

template<typename IteratorType>
RangeFilter<IteratorType>& RangeFilter<IteratorType>::operator=(
    const RangeFilter& i_rhs)
{
    iv_current = i_rhs.iv_current;
    iv_begin = i_rhs.iv_begin;
    iv_end = i_rhs.iv_end;
    iv_pPredicate = i_rhs.iv_pPredicate;
}

//******************************************************************************
// RangeFilter::RangeFilter (copy constructor)
//******************************************************************************

template<typename IteratorType>
RangeFilter<IteratorType>::RangeFilter(
    const RangeFilter& i_rhs)
: iv_current(i_rhs.iv_current),
  iv_begin(i_rhs.iv_begin),
  iv_end(i_rhs.iv_end),
  iv_pPredicate(i_rhs.iv_pPredicate)
{
}

//******************************************************************************
// RangeFilter::setPredicate
//******************************************************************************

template<typename IteratorType>
void RangeFilter<IteratorType>::setPredicate(
    const PredicateBase* const i_pPredicate)
{
    iv_pPredicate = i_pPredicate;
    advanceIfNoMatch();
}

//******************************************************************************
// RangeFilter::reset
//******************************************************************************

template<typename IteratorType>
void RangeFilter<IteratorType>::reset()
{
    iv_current = iv_begin;
    advanceIfNoMatch();
}

//******************************************************************************
// RangeFilter::operator++
//******************************************************************************

template<typename IteratorType>
RangeFilter<IteratorType>& RangeFilter<IteratorType>::operator++()
{
    advance();
    return *this;
}

/**
 *  @brief Disable meaningless "bool" comparisons that can occur when 
 *      evaluating a range as a bool by forcing a compilation failure.
 *      Function interfaces not documented since they are not used. 
 */
template <typename T> 
bool operator!=(const TargetRangeFilter& i_lhs, const T& i_rhs) 
{
    i_lhs.notComparable();	
    return false;	
} 

template <typename T>
bool operator==(const TargetRangeFilter& i_lhs, const T& i_rhs) 
{
    i_lhs.notComparable();
    return false;		
}

template <typename T> 
bool operator!=(const ConstTargetRangeFilter& i_lhs, const T& i_rhs) 
{
    i_lhs.notComparable();	
    return false;	
} 

template <typename T>
bool operator==(const ConstTargetRangeFilter& i_lhs, const T& i_rhs) 
{
    i_lhs.notComparable();
    return false;		
}

#undef TARG_CLASS
#undef TARG_NAMESPACE

} // End namespace TARGETING

#endif

OpenPOWER on IntegriCloud