ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/truck/Common/foreach.hpp
Revision: 10
Committed: 2007-05-07T16:54:45-07:00 (15 years, 5 months ago) by douglas
File size: 4799 byte(s)
Log Message:
Boo!

File Contents

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

Properties

Name Value
svn:keywords Common/common.hpp