QxForeach.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifdef _QX_ENABLE_BOOST
  32. #ifndef _QX_FOREACH_H_
  33. #define _QX_FOREACH_H_
  34. #ifdef _MSC_VER
  35. #pragma once
  36. #endif
  37. /*!
  38. * \file QxForeach.h
  39. * \author Lionel Marty
  40. * \ingroup QxCollection
  41. * \brief foreach-style (based on BOOST_FOREACH macro) to iterate over all stl, boost and Qt containers + qx::QxCollection<Key, Value> QxOrm library container
  42. *
  43. * Quick sample using QX_FOREACH (or _foreach) macro :
  44. * \code
  45. // iterate over drugs container using QxOrm '_foreach' keyword
  46. _foreach(drug_ptr p, lstDrugs)
  47. { qDebug() << qPrintable(p->name) << " " << qPrintable(p->desc); }
  48. * \endcode
  49. *
  50. * \note QxOrm library provides also other macros to iterator over all containers : _foreach_reverse, _foreach_if, _foreach_reverse_if
  51. */
  52. #include <boost/foreach.hpp>
  53. #ifndef BOOST_FOREACH_ID
  54. #define BOOST_FOREACH_ID(x) x
  55. #endif
  56. #include <QxTraits/is_qx_collection.h>
  57. namespace qx {
  58. namespace foreach {
  59. template<typename T, typename C, bool is_qx_collection = false>
  60. struct qx_deref_boost_or_qx
  61. {
  62. typedef typename boost::foreach_detail_::foreach_reference<T, C>::type type;
  63. };
  64. template<typename T, typename C>
  65. struct qx_deref_boost_or_qx<T, C, true>
  66. {
  67. typedef typename T::type_pair_key_value::second_type type;
  68. };
  69. template<typename T, typename C, bool is_qx_collection = false>
  70. struct qx_deref_deduce
  71. {
  72. static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, false>::type
  73. deref(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
  74. { return boost::foreach_detail_::deref(cur, ptmp); }
  75. static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, false>::type
  76. rderef(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
  77. { return boost::foreach_detail_::rderef(cur, ptmp); }
  78. };
  79. template<typename T, typename C>
  80. struct qx_deref_deduce<T, C, true>
  81. {
  82. static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, true>::type
  83. deref(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
  84. { return boost::foreach_detail_::deref(cur, ptmp).second; }
  85. static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, true>::type
  86. rderef(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
  87. { return boost::foreach_detail_::rderef(cur, ptmp).second; }
  88. };
  89. struct qx_deref
  90. {
  91. template<typename T, typename C>
  92. static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, qx::trait::is_qx_collection<T>::value>::type
  93. deref(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
  94. { return qx::foreach::qx_deref_deduce<T, C, qx::trait::is_qx_collection<T>::value>::deref(cur, ptmp); }
  95. template<typename T, typename C>
  96. static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, qx::trait::is_qx_collection<T>::value>::type
  97. deref_reverse(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
  98. { return qx::foreach::qx_deref_deduce<T, C, qx::trait::is_qx_collection<T>::value>::rderef(cur, ptmp); }
  99. };
  100. } // namespace foreach
  101. } // namespace qx
  102. #define QX_FOREACH_DEREF(COL) \
  103. qx::foreach::qx_deref::deref(BOOST_FOREACH_ID(_foreach_cur), BOOST_FOREACH_TYPEOF(COL))
  104. #define QX_FOREACH_DEREF_REVERSE(COL) \
  105. qx::foreach::qx_deref::deref_reverse(BOOST_FOREACH_ID(_foreach_cur), BOOST_FOREACH_TYPEOF(COL))
  106. #define QX_FOREACH(VAR, COL) \
  107. BOOST_FOREACH_PREAMBLE() \
  108. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else \
  109. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_BEGIN(COL)) {} else \
  110. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_END(COL)) {} else \
  111. for (bool BOOST_FOREACH_ID(_foreach_continue) = true; \
  112. BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_DONE(COL); \
  113. BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_NEXT(COL) : (void)0) \
  114. if (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else \
  115. for (VAR = QX_FOREACH_DEREF(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)
  116. #define QX_FOREACH_REVERSE(VAR, COL) \
  117. BOOST_FOREACH_PREAMBLE() \
  118. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else \
  119. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_RBEGIN(COL)) {} else \
  120. if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_REND(COL)) {} else \
  121. for (bool BOOST_FOREACH_ID(_foreach_continue) = true; \
  122. BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_RDONE(COL); \
  123. BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_RNEXT(COL) : (void)0) \
  124. if (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else \
  125. for (VAR = QX_FOREACH_DEREF_REVERSE(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)
  126. #ifdef _foreach
  127. #undef _foreach
  128. #endif // _foreach
  129. #ifdef _foreach_reverse
  130. #undef _foreach_reverse
  131. #endif // _foreach_reverse
  132. #ifdef _foreach_if
  133. #undef _foreach_if
  134. #endif // _foreach_if
  135. #ifdef _foreach_reverse_if
  136. #undef _foreach_reverse_if
  137. #endif // _foreach_reverse_if
  138. #define _foreach QX_FOREACH
  139. #define _foreach_reverse QX_FOREACH_REVERSE
  140. #define _foreach_if(VAR, COL, COND) _foreach(VAR, COL) if (COND)
  141. #define _foreach_reverse_if(VAR, COL, COND) _foreach_reverse(VAR, COL) if (COND)
  142. #endif // _QX_FOREACH_H_
  143. #endif // _QX_ENABLE_BOOST