ForEach.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/Vector.h"
  24. #include "../Container/List.h"
  25. #include "../Container/HashSet.h"
  26. #include "../Container/HashMap.h"
  27. #include <algorithm>
  28. // VS2010+ and other compilers: use std::begin(), std::end() & range based for
  29. // C++11 features need to be enabled
  30. #if !defined(_MSC_VER) || _MSC_VER > 1600
  31. #define foreach(VAL, VALS) for (VAL : VALS)
  32. // Fallback solution for VS2010. Will have problem with break statement.
  33. // See https://github.com/urho3d/Urho3D/issues/561
  34. #else
  35. namespace Atomic
  36. {
  37. template<typename T>
  38. struct false_wrapper {
  39. false_wrapper(const T& value) : value(value) { }
  40. operator bool() const { return false; }
  41. T value;
  42. };
  43. template<typename T>
  44. false_wrapper<T> make_false_wrapper(const T& value) {
  45. return false_wrapper<T>(value);
  46. }
  47. // vector support functions
  48. template <class T>
  49. Atomic::RandomAccessIterator<T> Begin(Atomic::Vector<T> &v) {
  50. return v.Begin();
  51. }
  52. template <class T>
  53. Atomic::RandomAccessIterator<T> Begin(Atomic::Vector<T> *v) {
  54. return v->Begin();
  55. }
  56. template <class T>
  57. Atomic::RandomAccessConstIterator<T> Begin(const Atomic::Vector<T> &v) {
  58. return v.Begin();
  59. }
  60. template <class T>
  61. Atomic::RandomAccessConstIterator<T> Begin(const Atomic::Vector<T> *v) {
  62. return v->Begin();
  63. }
  64. template <class T>
  65. Atomic::RandomAccessIterator<T> End(Atomic::Vector<T> &v) {
  66. return v.End();
  67. }
  68. template <class T>
  69. Atomic::RandomAccessIterator<T> End(Atomic::Vector<T> *v) {
  70. return v->End();
  71. }
  72. template <class T>
  73. Atomic::RandomAccessConstIterator<T> End(const Atomic::Vector<T> &v) {
  74. return v.End();
  75. }
  76. template <class T>
  77. Atomic::RandomAccessConstIterator<T> End(const Atomic::Vector<T> *v) {
  78. return v->End();
  79. }
  80. // podvector support functions
  81. template <class T>
  82. Atomic::RandomAccessIterator<T> Begin(Atomic::PODVector<T> &v) {
  83. return v.Begin();
  84. }
  85. template <class T>
  86. Atomic::RandomAccessIterator<T> Begin(Atomic::PODVector<T> *v) {
  87. return v->Begin();
  88. }
  89. template <class T>
  90. Atomic::RandomAccessConstIterator<T> Begin(const Atomic::PODVector<T> &v) {
  91. return v.Begin();
  92. }
  93. template <class T>
  94. Atomic::RandomAccessConstIterator<T> Begin(const Atomic::PODVector<T> *v) {
  95. return v->Begin();
  96. }
  97. template <class T>
  98. Atomic::RandomAccessIterator<T> End(Atomic::PODVector<T> &v) {
  99. return v.End();
  100. }
  101. template <class T>
  102. Atomic::RandomAccessIterator<T> End(Atomic::PODVector<T> *v) {
  103. return v->End();
  104. }
  105. template <class T>
  106. Atomic::RandomAccessConstIterator<T> End(const Atomic::PODVector<T> &v) {
  107. return v.End();
  108. }
  109. template <class T>
  110. Atomic::RandomAccessConstIterator<T> End(const Atomic::PODVector<T> *v) {
  111. return v->End();
  112. }
  113. }
  114. #define foreach(VAL, VALS) \
  115. if (const auto& _foreach_begin = Atomic::make_false_wrapper(Atomic::Begin(VALS))) { } else \
  116. if (const auto& _foreach_end = Atomic::make_false_wrapper(Atomic::End(VALS))) { } else \
  117. for (auto it = _foreach_begin.value; it != _foreach_end.value; ++it) \
  118. if (bool _foreach_flag = false) { } else \
  119. for (VAL = *it; !_foreach_flag; _foreach_flag = true)
  120. #endif