1 /* ResidualVM - A 3D game interpreter
2  *
3  * ResidualVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef COMMON_FOREACH_H
24 #define COMMON_FOREACH_H
25 
26 #include "common/scummsys.h"
27 
28 #if __cplusplus < 201103L
29 
30 namespace Common {
31 
32 class _Foreach_Container_Base_ {
33 public:
_Foreach_Container_Base_()34 	_Foreach_Container_Base_() : brk(1) { }
35 	mutable uint brk;
36 };
37 
38 template<class T>
39 class _Foreach_Container_ : public _Foreach_Container_Base_ {
40 public:
_Foreach_Container_(const T & co)41 	_Foreach_Container_(const T &co) : i(co.begin()), e(co.end()) { }
next()42 	void next() const {
43 		++i;
44 		brk = 1;
45 	}
end()46 	bool end() const { return i == e; }
47 
48 	mutable typename T::const_iterator i;
49 	typename T::const_iterator e;
50 };
51 
52 template<class T>
_Create_Foreach_Container_(const T & c)53 inline _Foreach_Container_<T> _Create_Foreach_Container_(const T &c) {
54 	return _Foreach_Container_<T>(c);
55 }
56 
57 template<class T>
_Get_Foreach_Container_(const _Foreach_Container_Base_ * c,const T &)58 inline const _Foreach_Container_<T> *_Get_Foreach_Container_(const _Foreach_Container_Base_ *c, const T &) {
59 	return static_cast<const _Foreach_Container_<T> *>(c);
60 }
61 
62 }
63 
64 #define foreach(var, container) \
65 for (const Common::_Foreach_Container_Base_ &_FOREACH_CONTAINER_ = Common::_Create_Foreach_Container_(container);\
66 	!Common::_Get_Foreach_Container_(&_FOREACH_CONTAINER_, container)->end(); \
67 	Common::_Get_Foreach_Container_(&_FOREACH_CONTAINER_, container)->next()) \
68 	for (var = *Common::_Get_Foreach_Container_(&_FOREACH_CONTAINER_, container)->i;\
69 		_FOREACH_CONTAINER_.brk > 0; --_FOREACH_CONTAINER_.brk)
70 
71 #else
72 
73 #define foreach(var, container) for (var : container)
74 
75 #endif
76 
77 #endif
78