foreach.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef BOOST_FOREACH
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // A stripped down version of FOREACH for
  4. // illustration purposes. NOT FOR GENERAL USE.
  5. // For a complete implementation, see BOOST_FOREACH at
  6. // http://boost-sandbox.sourceforge.net/vault/index.php?directory=eric_niebler
  7. //
  8. // Copyright 2004 Eric Niebler.
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. // Adapted to Assimp November 29th, 2008 (Alexander Gessler).
  14. // Added code to handle both const and non-const iterators, simplified some
  15. // parts.
  16. ///////////////////////////////////////////////////////////////////////////////
  17. namespace boost {
  18. namespace foreach_detail {
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // auto_any
  21. struct auto_any_base
  22. {
  23. operator bool() const { return false; }
  24. };
  25. template<typename T>
  26. struct auto_any : auto_any_base
  27. {
  28. auto_any(T const& t) : item(t) {}
  29. mutable T item;
  30. };
  31. template<typename T>
  32. T& auto_any_cast(auto_any_base const& any)
  33. {
  34. return static_cast<auto_any<T> const&>(any).item;
  35. }
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // FOREACH helper function
  38. template<typename T>
  39. auto_any<typename T::const_iterator> begin(T const& t)
  40. {
  41. return t.begin();
  42. }
  43. template<typename T>
  44. auto_any<typename T::const_iterator> end(T const& t)
  45. {
  46. return t.end();
  47. }
  48. // iterator
  49. template<typename T>
  50. bool done(auto_any_base const& cur, auto_any_base const& end, T&)
  51. {
  52. typedef typename T::iterator iter_type;
  53. return auto_any_cast<iter_type>(cur) == auto_any_cast<iter_type>(end);
  54. }
  55. template<typename T>
  56. void next(auto_any_base const& cur, T&)
  57. {
  58. ++auto_any_cast<typename T::iterator>(cur);
  59. }
  60. template<typename T>
  61. typename T::reference deref(auto_any_base const& cur, T&)
  62. {
  63. return *auto_any_cast<typename T::iterator>(cur);
  64. }
  65. } // end foreach_detail
  66. ///////////////////////////////////////////////////////////////////////////////
  67. // FOREACH
  68. #define BOOST_FOREACH(item, container) \
  69. if(boost::foreach_detail::auto_any_base const& b = boost::foreach_detail::begin(container)) {} else \
  70. if(boost::foreach_detail::auto_any_base const& e = boost::foreach_detail::end(container)) {} else \
  71. for(;!boost::foreach_detail::done(b,e,container); boost::foreach_detail::next(b,container)) \
  72. if (bool ugly_and_unique_break = false) {} else \
  73. for(item = boost::foreach_detail::deref(b,container); !ugly_and_unique_break; ugly_and_unique_break = true)
  74. } // end boost
  75. #endif