QxOrm  1.4.9
C++ Object Relational Mapping library
QxForeach.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://www.qxorm.com/
00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
00005 **
00006 ** This file is part of the QxOrm library
00007 **
00008 ** This software is provided 'as-is', without any express or implied
00009 ** warranty. In no event will the authors be held liable for any
00010 ** damages arising from the use of this software
00011 **
00012 ** Commercial Usage
00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
00014 ** accordance with the commercial license agreement provided with the
00015 ** Software or, alternatively, in accordance with the terms contained in
00016 ** a written agreement between you and Lionel Marty
00017 **
00018 ** GNU General Public License Usage
00019 ** Alternatively, this file may be used under the terms of the GNU
00020 ** General Public License version 3.0 as published by the Free Software
00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
00022 ** packaging of this file. Please review the following information to
00023 ** ensure the GNU General Public License version 3.0 requirements will be
00024 ** met : http://www.gnu.org/copyleft/gpl.html
00025 **
00026 ** If you are unsure which license is appropriate for your use, or
00027 ** if you have questions regarding the use of this file, please contact :
00028 ** contact@qxorm.com
00029 **
00030 ****************************************************************************/
00031 
00032 #ifdef _QX_ENABLE_BOOST
00033 #ifndef _QX_FOREACH_H_
00034 #define _QX_FOREACH_H_
00035 
00036 #ifdef _MSC_VER
00037 #pragma once
00038 #endif
00039 
00056 #include <boost/foreach.hpp>
00057 
00058 #ifndef BOOST_FOREACH_ID
00059 #define BOOST_FOREACH_ID(x) x
00060 #endif
00061 
00062 #include <QxTraits/is_qx_collection.h>
00063 
00064 namespace qx {
00065 
00066 namespace foreach {
00067 
00068 template<typename T, typename C, bool is_qx_collection = false>
00069 struct qx_deref_boost_or_qx
00070 {
00071    typedef typename boost::foreach_detail_::foreach_reference<T, C>::type type;
00072 };
00073 
00074 template<typename T, typename C>
00075 struct qx_deref_boost_or_qx<T, C, true>
00076 {
00077    typedef typename T::type_pair_key_value::second_type type;
00078 };
00079 
00080 template<typename T, typename C, bool is_qx_collection = false>
00081 struct qx_deref_deduce
00082 {
00083    static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, false>::type
00084    deref(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
00085    { return boost::foreach_detail_::deref(cur, ptmp); }
00086 
00087    static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, false>::type
00088    rderef(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
00089    { return boost::foreach_detail_::rderef(cur, ptmp); }
00090 };
00091 
00092 template<typename T, typename C>
00093 struct qx_deref_deduce<T, C, true>
00094 {
00095    static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, true>::type
00096    deref(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
00097    { return boost::foreach_detail_::deref(cur, ptmp).second; }
00098 
00099    static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, true>::type
00100    rderef(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
00101    { return boost::foreach_detail_::rderef(cur, ptmp).second; }
00102 };
00103 
00104 struct qx_deref
00105 {
00106    template<typename T, typename C>
00107    static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, qx::trait::is_qx_collection<T>::value>::type
00108    deref(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
00109    { return qx::foreach::qx_deref_deduce<T, C, qx::trait::is_qx_collection<T>::value>::deref(cur, ptmp); }
00110 
00111    template<typename T, typename C>
00112    static inline typename qx::foreach::qx_deref_boost_or_qx<T, C, qx::trait::is_qx_collection<T>::value>::type
00113    deref_reverse(boost::foreach_detail_::auto_any_t cur, boost::foreach_detail_::type2type<T, C> * ptmp)
00114    { return qx::foreach::qx_deref_deduce<T, C, qx::trait::is_qx_collection<T>::value>::rderef(cur, ptmp); }
00115 };
00116 
00117 } // namespace foreach
00118 
00119 } // namespace qx
00120 
00121 #define QX_FOREACH_DEREF(COL) \
00122    qx::foreach::qx_deref::deref(BOOST_FOREACH_ID(_foreach_cur), BOOST_FOREACH_TYPEOF(COL))
00123 
00124 #define QX_FOREACH_DEREF_REVERSE(COL) \
00125    qx::foreach::qx_deref::deref_reverse(BOOST_FOREACH_ID(_foreach_cur), BOOST_FOREACH_TYPEOF(COL))
00126 
00127 #define QX_FOREACH(VAR, COL)                                                                    \
00128    BOOST_FOREACH_PREAMBLE()                                                                     \
00129    if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else    \
00130    if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_BEGIN(COL)) {} else      \
00131    if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_END(COL)) {} else        \
00132    for (bool BOOST_FOREACH_ID(_foreach_continue) = true;                                                          \
00133          BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_DONE(COL);                                         \
00134          BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_NEXT(COL) : (void)0)                                 \
00135       if  (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else                        \
00136       for (VAR = QX_FOREACH_DEREF(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)
00137 
00138 #define QX_FOREACH_REVERSE(VAR, COL)                                                            \
00139    BOOST_FOREACH_PREAMBLE()                                                                     \
00140    if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_col) = BOOST_FOREACH_CONTAIN(COL)) {} else    \
00141    if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_cur) = BOOST_FOREACH_RBEGIN(COL)) {} else     \
00142    if (boost::foreach_detail_::auto_any_t BOOST_FOREACH_ID(_foreach_end) = BOOST_FOREACH_REND(COL)) {} else       \
00143    for (bool BOOST_FOREACH_ID(_foreach_continue) = true;                                                          \
00144          BOOST_FOREACH_ID(_foreach_continue) && !BOOST_FOREACH_RDONE(COL);                                        \
00145          BOOST_FOREACH_ID(_foreach_continue) ? BOOST_FOREACH_RNEXT(COL) : (void)0)                                \
00146       if  (boost::foreach_detail_::set_false(BOOST_FOREACH_ID(_foreach_continue))) {} else                        \
00147       for (VAR = QX_FOREACH_DEREF_REVERSE(COL); !BOOST_FOREACH_ID(_foreach_continue); BOOST_FOREACH_ID(_foreach_continue) = true)
00148 
00149 #ifdef _foreach
00150 #undef _foreach
00151 #endif // _foreach
00152 
00153 #ifdef _foreach_reverse
00154 #undef _foreach_reverse
00155 #endif // _foreach_reverse
00156 
00157 #ifdef _foreach_if
00158 #undef _foreach_if
00159 #endif // _foreach_if
00160 
00161 #ifdef _foreach_reverse_if
00162 #undef _foreach_reverse_if
00163 #endif // _foreach_reverse_if
00164 
00165 #define _foreach QX_FOREACH
00166 #define _foreach_reverse QX_FOREACH_REVERSE
00167 
00168 #define _foreach_if(VAR, COL, COND) _foreach(VAR, COL) if (COND)
00169 #define _foreach_reverse_if(VAR, COL, COND) _foreach_reverse(VAR, COL) if (COND)
00170 
00171 #endif // _QX_FOREACH_H_
00172 #endif // _QX_ENABLE_BOOST