1 |
// Foreach for STL |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
/* Menes - C++ High-Level Utility Library |
8 |
* Copyright (C) 2004 Jay Freeman (saurik) |
9 |
*/ |
10 |
|
11 |
/* |
12 |
* Redistribution and use in source and binary |
13 |
* forms, with or without modification, are permitted |
14 |
* provided that the following conditions are met: |
15 |
* |
16 |
* 1. Redistributions of source code must retain the |
17 |
* above copyright notice, this list of conditions |
18 |
* and the following disclaimer. |
19 |
* 2. Redistributions in binary form must reproduce the |
20 |
* above copyright notice, this list of conditions |
21 |
* and the following disclaimer in the documentation |
22 |
* and/or other materials provided with the |
23 |
* distribution. |
24 |
* 3. The name of the author may not be used to endorse |
25 |
* or promote products derived from this software |
26 |
* without specific prior written permission. |
27 |
* |
28 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' |
29 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
30 |
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
31 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
32 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
33 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
34 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
35 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
36 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
37 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
38 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
39 |
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
40 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
41 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
42 |
*/ |
43 |
|
44 |
#ifndef _foreach_hpp_ |
45 |
#define _foreach_hpp_ |
46 |
|
47 |
#define _forever for (;;) |
48 |
|
49 |
#define _forall(type, item, begin, end) \ |
50 |
if (size_t _index = 0); \ |
51 |
else for (type item(begin), _end(end); item != _end; ++item, ++_index) |
52 |
|
53 |
#define _rforall(type, item, begin, end) \ |
54 |
for (type item(end), _begin(begin); item != _begin && (--item, true); ) |
55 |
|
56 |
#define _repeat(count) \ |
57 |
for (unsigned _index(0), _end(count); _index != _end; ++_index) |
58 |
|
59 |
template <typename List_, bool noop = true> |
60 |
struct StrictIterator; |
61 |
|
62 |
template <typename List_> |
63 |
struct StrictIterator<List_, true> { |
64 |
typedef typename List_::iterator Result; |
65 |
}; |
66 |
|
67 |
template <typename List_> |
68 |
struct StrictIterator<const List_, true> { |
69 |
typedef typename List_::const_iterator Result; |
70 |
}; |
71 |
|
72 |
template <typename List_, bool noop = true> |
73 |
struct ListTraits; |
74 |
|
75 |
template <typename List_> |
76 |
struct ListTraits<List_, true> |
77 |
{ |
78 |
typedef typename StrictIterator<List_>::Result Iterator; |
79 |
|
80 |
static inline Iterator Begin(List_ &arg) { |
81 |
return arg.begin(); |
82 |
} |
83 |
|
84 |
static inline Iterator End(List_ &arg) { |
85 |
return arg.end(); |
86 |
} |
87 |
}; |
88 |
|
89 |
#define _foreach_(type, item, set, forall, _typename) \ |
90 |
for (bool _stop(true); _stop; ) \ |
91 |
for (type &_set = set; _stop; _stop = false) \ |
92 |
forall (_typename ListTraits< type >::Iterator, item, ListTraits< type >::Begin(_set), ListTraits< type >::End(_set)) |
93 |
|
94 |
#define _foreach(type, item, set) \ |
95 |
_foreach_(type, item, set, _forall, ) |
96 |
|
97 |
#define _rforeach(type, item, set) \ |
98 |
_foreach_(type, item, set, _rforall, ) |
99 |
|
100 |
#define _tforeach(type, item, set) \ |
101 |
_foreach_(type, item, set, _forall, typename) |
102 |
|
103 |
#define _rtforeach(type, item, set) \ |
104 |
_foreach_(type, item, set, _rforall, typename) |
105 |
|
106 |
#endif//_foreach_hpp_ |