foreach.hpp 3.2 KB

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