ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/Common/foreach.hpp
Revision: 25
Committed: 2008-02-09T01:39:22-08:00 (14 years, 4 months ago) by douglas
File size: 4774 byte(s)
Log Message:
All kinds of changes!

File Contents

# Content
1 // Truck Computer Dooom!
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 #include <cstdlib>
48
49 #define _forever for (;;)
50
51 #define _forall(type, item, begin, end) \
52 if (size_t _index = 0); \
53 else for (type item(begin), _end(end); item != _end; ++item, ++_index)
54
55 #define _rforall(type, item, begin, end) \
56 for (type item(end), _begin(begin); item != _begin && (--item, true); )
57
58 #define _repeat(count) \
59 for (unsigned _index(0), _end(count); _index != _end; ++_index)
60
61 template <unsigned Case_>
62 struct Case {
63 static char value[Case_ + 1];
64 };
65
66 typedef Case<true> Yes;
67 typedef Case<false> No;
68
69 template <typename Test_, typename Result_, size_t Size_ = sizeof (Test_)>
70 struct IfValidType
71 {
72 typedef Result_ Result;
73 };
74
75 enum Capitalization { UnderscoredLowerCase = 0, UpperCamelCase = 1 };
76
77 namespace be
78 {
79 template <typename List_>
80 typename IfValidType< typename List_::const_iterator, Case<UnderscoredLowerCase> >::Result CheckCapitalization_(List_ *);
81
82 template <typename List_>
83 typename IfValidType< typename List_::ConstIterator, Case<UpperCamelCase> >::Result CheckCapitalization_(List_ *);
84 }
85
86 template <typename List_>
87 struct ListCapitalization
88 {
89 static const Capitalization value = static_cast<Capitalization>(sizeof (be::CheckCapitalization_(reinterpret_cast<List_ *>(NULL)).value) - 1);
90 };
91
92 template <typename List_, Capitalization Capitalization_>
93 struct StrictIterator;
94
95 template <typename List_>
96 struct StrictIterator<List_, UnderscoredLowerCase>
97 {
98 typedef typename List_::iterator Result;
99 };
100
101 template <typename List_>
102 struct StrictIterator<const List_, UnderscoredLowerCase>
103 {
104 typedef typename List_::const_iterator Result;
105 };
106
107 template <typename List_>
108 struct StrictIterator<List_, UpperCamelCase>
109 {
110 typedef typename List_::Iterator Result;
111 };
112
113 template <typename List_>
114 struct StrictIterator<const List_, UpperCamelCase>
115 {
116 typedef typename List_::ConstIterator Result;
117 };
118
119 template <typename List_, Capitalization Capitalization_ = ListCapitalization<List_>::value>
120 struct ListTraits;
121
122 template <typename List_>
123 struct ListTraits<List_, UnderscoredLowerCase>
124 {
125 typedef typename StrictIterator<List_, UnderscoredLowerCase>::Result Iterator;
126
127 static inline Iterator Begin(List_ &arg)
128 {
129 return arg.begin();
130 }
131
132 static inline Iterator End(List_ &arg)
133 {
134 return arg.end();
135 }
136 };
137
138 template <typename List_>
139 struct ListTraits<List_, UpperCamelCase>
140 {
141 typedef typename StrictIterator<List_, UpperCamelCase>::Result Iterator;
142
143 static inline Iterator Begin(List_ &arg)
144 {
145 return arg.Begin();
146 }
147
148 static inline Iterator End(List_ &arg)
149 {
150 return arg.End();
151 }
152 };
153
154 #define _foreach_(type, item, set, forall, _typename) \
155 for (bool _stop(true); _stop; ) \
156 for (type &_set = set; _stop; _stop = false) \
157 forall (_typename ListTraits< type >::Iterator, item, ListTraits< type >::Begin(_set), ListTraits< type >::End(_set))
158
159 #define _foreach(type, item, set) \
160 _foreach_(type, item, set, _forall, )
161
162 #define _rforeach(type, item, set) \
163 _foreach_(type, item, set, _rforall, )
164
165 #define _tforeach(type, item, set) \
166 _foreach_(type, item, set, _forall, typename)
167
168 #define _rtforeach(type, item, set) \
169 _foreach_(type, item, set, _rforall, typename)
170
171 #endif//_foreach_hpp_

Properties

Name Value
svn:keywords Id