ForEach.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Copyright (c) 2008-2014 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 "Vector.h"
  24. // Note: ForEach is not supported on all compilers, such as VS2008.
  25. namespace Urho3D {
  26. template<typename T>
  27. struct false_wrapper {
  28. false_wrapper(const T& value) : value(value) { }
  29. operator bool() const { return false; }
  30. T value;
  31. };
  32. template<typename T>
  33. false_wrapper<T> make_false_wrapper(const T& value) {
  34. return false_wrapper<T>(value);
  35. }
  36. // vector support functions
  37. template <class T>
  38. Urho3D::RandomAccessIterator<T> Begin(Urho3D::Vector<T> &v) {
  39. return v.Begin();
  40. }
  41. template <class T>
  42. Urho3D::RandomAccessIterator<T> Begin(Urho3D::Vector<T> *v) {
  43. return v->Begin();
  44. }
  45. template <class T>
  46. Urho3D::RandomAccessConstIterator<T> Begin(const Urho3D::Vector<T> &v) {
  47. return v.Begin();
  48. }
  49. template <class T>
  50. Urho3D::RandomAccessConstIterator<T> Begin(const Urho3D::Vector<T> *v) {
  51. return v->Begin();
  52. }
  53. template <class T>
  54. Urho3D::RandomAccessIterator<T> End(Urho3D::Vector<T> &v) {
  55. return v.End();
  56. }
  57. template <class T>
  58. Urho3D::RandomAccessIterator<T> End(Urho3D::Vector<T> *v) {
  59. return v->End();
  60. }
  61. template <class T>
  62. Urho3D::RandomAccessConstIterator<T> End(const Urho3D::Vector<T> &v) {
  63. return v.End();
  64. }
  65. template <class T>
  66. Urho3D::RandomAccessConstIterator<T> End(const Urho3D::Vector<T> *v) {
  67. return v->End();
  68. }
  69. // podvector support functions
  70. template <class T>
  71. Urho3D::RandomAccessIterator<T> Begin(Urho3D::PODVector<T> &v) {
  72. return v.Begin();
  73. }
  74. template <class T>
  75. Urho3D::RandomAccessIterator<T> Begin(Urho3D::PODVector<T> *v) {
  76. return v->Begin();
  77. }
  78. template <class T>
  79. Urho3D::RandomAccessConstIterator<T> Begin(const Urho3D::PODVector<T> &v) {
  80. return v.Begin();
  81. }
  82. template <class T>
  83. Urho3D::RandomAccessConstIterator<T> Begin(const Urho3D::PODVector<T> *v) {
  84. return v->Begin();
  85. }
  86. template <class T>
  87. Urho3D::RandomAccessIterator<T> End(Urho3D::PODVector<T> &v) {
  88. return v.End();
  89. }
  90. template <class T>
  91. Urho3D::RandomAccessIterator<T> End(Urho3D::PODVector<T> *v) {
  92. return v->End();
  93. }
  94. template <class T>
  95. Urho3D::RandomAccessConstIterator<T> End(const Urho3D::PODVector<T> &v) {
  96. return v.End();
  97. }
  98. template <class T>
  99. Urho3D::RandomAccessConstIterator<T> End(const Urho3D::PODVector<T> *v) {
  100. return v->End();
  101. }
  102. } // namespace Urho3D
  103. #define foreach(VAL, VALS) \
  104. if (const auto& _foreach_begin = Urho3D::make_false_wrapper(Urho3D::Begin(VALS))) { } else \
  105. if (const auto& _foreach_end = Urho3D::make_false_wrapper(Urho3D::End(VALS))) { } else \
  106. for (auto it = _foreach_begin.value; it != _foreach_end.value; ++it) \
  107. if (bool _foreach_flag = false) { } else \
  108. for (VAL = *it; !_foreach_flag; _foreach_flag = true)
  109. #define foreachv(ITER, VAL, VALS) \
  110. if (const auto& _foreach_begin = Urho3D::make_false_wrapper(Urho3D::Begin(VALS))) { } else \
  111. if (const auto& _foreach_end = Urho3D::make_false_wrapper(Urho3D::End(VALS))) { } else \
  112. if (int ITER = 0) { } else \
  113. for (auto it = _foreach_begin.value; it != _foreach_end.value; ++it) \
  114. if (bool _foreach_flag = false) { } else \
  115. for (VAL = *it; !_foreach_flag; ITER++, _foreach_flag = true)