![]() |
QxOrm
1.4.3
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** http://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 #ifndef _QX_NESTED_MODEL_H_ 00033 #define _QX_NESTED_MODEL_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <boost/static_assert.hpp> 00047 #include <boost/mpl/if.hpp> 00048 #include <boost/mpl/logical.hpp> 00049 #include <boost/type_traits/is_pointer.hpp> 00050 #include <boost/type_traits/is_same.hpp> 00051 #include <boost/type_traits/is_base_of.hpp> 00052 00053 #include <QxConvert/QxConvert.h> 00054 00055 #include <QxCollection/QxCollection.h> 00056 00057 #include <QxTraits/is_qx_registered.h> 00058 #include <QxTraits/is_container.h> 00059 #include <QxTraits/is_smart_ptr.h> 00060 #include <QxTraits/get_base_class.h> 00061 #include <QxTraits/get_class_name_primitive.h> 00062 #include <QxTraits/construct_ptr.h> 00063 #include <QxTraits/generic_container.h> 00064 #include <QxTraits/is_valid_primary_key.h> 00065 00066 #include <QxSerialize/QxClone.h> 00067 00068 #include <QxModelView/IxModel.h> 00069 #include <QxModelView/QxModel.h> 00070 00071 namespace qx { 00072 namespace model_view { 00073 00074 template <class T> 00075 qx::IxModel * create_nested_model(qx::IxModel * pParent, const QModelIndex & idxParent, T & t); 00076 00077 template <class T> 00078 void sync_nested_model(qx::IxModel * pModel, T & t); 00079 00080 } // namespace model_view 00081 } // namespace qx 00082 00083 namespace qx { 00084 namespace model_view { 00085 namespace detail { 00086 00087 template <class T> 00088 struct QxNestedModel; 00089 00090 template <class T, bool bIsQObject /* = false */> 00091 struct QxNestedModel_Helper 00092 { 00093 00094 static qx_shared_ptr<T> clone(T & t) 00095 { qx_shared_ptr<T> p; p.reset(new T()); (* p) = t; return p; } 00096 00097 static void synchronize(T & t1, T & t2) 00098 { t1 = t2; } 00099 00100 }; 00101 00102 template <class T> 00103 struct QxNestedModel_Helper<T, true> 00104 { 00105 00106 static qx_shared_ptr<T> clone(T & t) 00107 { qx_shared_ptr<T> p; p.reset(qx::clone_to_nude_ptr(t)); qAssert(p); return p; } 00108 00109 static void synchronize(T & t1, T & t2) 00110 { 00111 qx::IxClass * pClass = qx::QxClass<T>::getSingleton(); 00112 qx::IxDataMemberX * pDataMemberX = (pClass ? pClass->getDataMemberX() : NULL); 00113 for (long l = 0; (pDataMemberX && (l < pDataMemberX->count_WithDaoStrategy())); l++) 00114 { 00115 qx::IxDataMember * pDataMember = pDataMemberX->get_WithDaoStrategy(l); if (! pDataMember) { continue; } 00116 QVariant value = pDataMember->toVariant(& t2); 00117 pDataMember->fromVariant((& t1), value); 00118 } 00119 } 00120 00121 }; 00122 00123 template <class T> 00124 struct QxNestedModel_Generic 00125 { 00126 00127 typedef typename qx::QxModel<T>::type_collection type_collection; 00128 typedef typename qx::QxModel<T>::type_primary_key type_primary_key; 00129 typedef typename qx::QxModel<T>::type_ptr type_ptr; 00130 00131 enum { is_valid = qx::trait::is_qx_registered<T>::value }; 00132 00133 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00134 { 00135 BOOST_STATIC_ASSERT(is_valid); 00136 qx::QxModel<T> * pModel = new qx::QxModel<T>(pParent); 00137 pModel->setParentModel(pParent); 00138 type_collection & model = pModel->m_model; 00139 long & idx = pModel->m_lManualInsertIndex; 00140 type_primary_key key; 00141 00142 pModel->beginInsertRows(idxParent, 0, 0); 00143 type_ptr ptr = qx::model_view::detail::QxNestedModel_Helper<T, boost::is_base_of<QObject, T>::value>::clone(t); 00144 qx::IxDataMember * pDataMemberId = pModel->m_pDataMemberId; 00145 if (! pDataMemberId) { qAssert(false); pModel->endInsertRows(); return pModel; } 00146 QVariant value = pDataMemberId->toVariant(& t); 00147 if (! qx::trait::is_valid_primary_key(value)) 00148 { idx--; value = QVariant(static_cast<qlonglong>(idx)); } 00149 qx::cvt::from_variant(value, key); 00150 model.insert(0, key, ptr); 00151 pModel->endInsertRows(); 00152 return pModel; 00153 } 00154 00155 static inline void synchronize(qx::IxModel * pModel, T & t) 00156 { 00157 if (! pModel) { qAssert(false); return; } 00158 qx::QxModel<T> * pModelTyped = static_cast<qx::QxModel<T> *>(pModel); 00159 type_collection & model = pModelTyped->m_model; 00160 if (model.count() <= 0) { return; } 00161 type_ptr ptr = model.getByIndex(0); 00162 if (! ptr) { return; } 00163 qx::model_view::detail::QxNestedModel_Helper<T, boost::is_base_of<QObject, T>::value>::synchronize(t, (* ptr)); 00164 } 00165 00166 }; 00167 00168 template <class T> 00169 struct QxNestedModel_Container 00170 { 00171 00172 typedef qx::trait::generic_container<T> type_generic_container; 00173 typedef typename type_generic_container::type_value_qx type_data; 00174 typedef typename type_generic_container::type_item type_item; 00175 typedef typename qx::QxModel<type_data>::type_collection type_collection; 00176 typedef typename qx::QxModel<type_data>::type_primary_key type_primary_key; 00177 typedef typename qx::QxModel<type_data>::type_ptr type_ptr; 00178 00179 enum { is_valid = qx::trait::is_qx_registered<type_data>::value }; 00180 00181 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00182 { 00183 int iCurrRow = 0; 00184 BOOST_STATIC_ASSERT(is_valid); 00185 qx::QxModel<type_data> * pModel = new qx::QxModel<type_data>(pParent); 00186 pModel->setParentModel(pParent); 00187 long lCount = static_cast<long>(type_generic_container::size(t)); 00188 if (lCount <= 0) { return pModel; } 00189 00190 pModel->beginInsertRows(idxParent, 0, (lCount - 1)); 00191 for (typename T::iterator it = t.begin(); it != t.end(); ++it) 00192 { insertItem(pModel, (* it), iCurrRow); iCurrRow++; } 00193 pModel->endInsertRows(); 00194 return pModel; 00195 } 00196 00197 template <typename U> 00198 static inline bool insert(qx::IxModel * pModel, U & item, int iRow) 00199 { 00200 if (! pModel) { qAssert(false); return false; } 00201 qx::QxModel<U> * pModelWrk = static_cast<qx::QxModel<U> *>(pModel); 00202 type_collection & model = pModelWrk->m_model; 00203 long & idx = pModelWrk->m_lManualInsertIndex; 00204 type_primary_key key; 00205 00206 type_ptr ptr = qx::model_view::detail::QxNestedModel_Helper<U, boost::is_base_of<QObject, U>::value>::clone(item); 00207 qx::IxDataMember * pDataMemberId = pModelWrk->m_pDataMemberId; 00208 if (! pDataMemberId) { qAssert(false); return false; } 00209 QVariant value = pDataMemberId->toVariant(& item); 00210 if (! qx::trait::is_valid_primary_key(value)) 00211 { idx--; value = QVariant(static_cast<qlonglong>(idx)); } 00212 qx::cvt::from_variant(value, key); 00213 model.insert(iRow, key, ptr); 00214 return true; 00215 } 00216 00217 static inline void synchronize(qx::IxModel * pModel, T & t) 00218 { 00219 if (! pModel) { qAssert(false); return; } 00220 type_generic_container::clear(t); 00221 qx::QxModel<type_data> * pModelTyped = static_cast<qx::QxModel<type_data> *>(pModel); 00222 type_collection & model = pModelTyped->m_model; 00223 00224 for (long l = 0; l < model.count(); l++) 00225 { 00226 type_ptr ptr = model.getByIndex(l); if (! ptr) { continue; } 00227 type_item item = type_generic_container::createItem(); 00228 type_data & item_val = item.value_qx(); 00229 qx::model_view::detail::QxNestedModel_Helper<type_data, boost::is_base_of<QObject, type_data>::value>::synchronize(item_val, (* ptr)); 00230 QVariant vKey = QVariant(static_cast<qlonglong>(l)); 00231 qx::cvt::from_variant(vKey, item.key()); 00232 type_generic_container::insertItem(t, item); 00233 } 00234 } 00235 00236 private: 00237 00238 template <typename U> 00239 static inline bool insertItem(qx::IxModel * pModel, U & item, int iRow) 00240 { return insertItem_Helper<U, boost::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::insert(pModel, item, iRow); } 00241 00242 template <typename U, bool bIsPointer /* = true */> 00243 struct insertItem_Helper 00244 { 00245 static inline bool insert(qx::IxModel * pModel, U & item, int iRow) 00246 { return (item ? qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, (* item), iRow) : true); } 00247 }; 00248 00249 template <typename U1, typename U2> 00250 struct insertItem_Helper<std::pair<U1, U2>, false> 00251 { 00252 static inline bool insert(qx::IxModel * pModel, std::pair<U1, U2> & item, int iRow) 00253 { return qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, item.second, iRow); } 00254 }; 00255 00256 template <typename U1, typename U2> 00257 struct insertItem_Helper<const std::pair<U1, U2>, false> 00258 { 00259 static inline bool insert(qx::IxModel * pModel, const std::pair<U1, U2> & item, int iRow) 00260 { return qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, item.second, iRow); } 00261 }; 00262 00263 template <typename U1, typename U2> 00264 struct insertItem_Helper<QPair<U1, U2>, false> 00265 { 00266 static inline bool insert(qx::IxModel * pModel, QPair<U1, U2> & item, int iRow) 00267 { return qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, item.second, iRow); } 00268 }; 00269 00270 template <typename U1, typename U2> 00271 struct insertItem_Helper<const QPair<U1, U2>, false> 00272 { 00273 static inline bool insert(qx::IxModel * pModel, const QPair<U1, U2> & item, int iRow) 00274 { return qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, item.second, iRow); } 00275 }; 00276 00277 template <typename U> 00278 struct insertItem_Helper<U, false> 00279 { 00280 enum { is_same_type = boost::is_same<qx::model_view::detail::QxNestedModel_Container<T>::type_data, U>::value }; 00281 static bool insert(qx::IxModel * pModel, U & item, int iRow) 00282 { BOOST_STATIC_ASSERT(is_same_type); return qx::model_view::detail::QxNestedModel_Container<T>::insert(pModel, item, iRow); } 00283 }; 00284 00285 }; 00286 00287 template <class T> 00288 struct QxNestedModel_Ptr 00289 { 00290 00291 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00292 { return (t ? create_Helper(pParent, idxParent, (* t)) : create_NullHelper(pParent, idxParent)); } 00293 00294 static inline void synchronize(qx::IxModel * pModel, T & t) 00295 { if (! t) { qx::trait::construct_ptr<T>::get(t); }; if (t) { qx::model_view::sync_nested_model(pModel, (* t)); } } 00296 00297 private: 00298 00299 template <class U> 00300 static inline qx::IxModel * create_Helper(qx::IxModel * pParent, const QModelIndex & idxParent, U & u) 00301 { return qx::model_view::detail::QxNestedModel<U>::create(pParent, idxParent, u); } 00302 00303 static inline qx::IxModel * create_NullHelper(qx::IxModel * pParent, const QModelIndex & idxParent) 00304 { 00305 T t; qx::trait::construct_ptr<T>::get(t); 00306 if (! t) { qAssert(false); return NULL; } 00307 qx::IxModel * pModel = qx::model_view::create_nested_model(pParent, idxParent, (* t)); 00308 if (pModel) { pModel->clear(); } qAssert(pModel != NULL); 00309 return pModel; 00310 } 00311 00312 }; 00313 00314 template <class T> 00315 struct QxNestedModel 00316 { 00317 00318 typedef typename boost::mpl::if_c< boost::is_pointer<T>::value, qx::model_view::detail::QxNestedModel_Ptr<T>, qx::model_view::detail::QxNestedModel_Generic<T> >::type type_model_view_1; 00319 typedef typename boost::mpl::if_c< qx::trait::is_smart_ptr<T>::value, qx::model_view::detail::QxNestedModel_Ptr<T>, type_model_view_1 >::type type_model_view_2; 00320 typedef typename boost::mpl::if_c< qx::trait::is_container<T>::value, qx::model_view::detail::QxNestedModel_Container<T>, type_model_view_2 >::type type_model_view_3; 00321 00322 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00323 { return type_model_view_3::create(pParent, idxParent, t); } 00324 00325 static inline void synchronize(qx::IxModel * pModel, T & t) 00326 { type_model_view_3::synchronize(pModel, t); } 00327 00328 }; 00329 00330 } // namespace detail 00331 } // namespace model_view 00332 } // namespace qx 00333 00334 namespace qx { 00335 namespace model_view { 00336 00344 template <class T> 00345 qx::IxModel * create_nested_model(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00346 { return qx::model_view::detail::QxNestedModel<T>::create(pParent, idxParent, t); } 00347 00348 template <class T, class U> 00349 qx::IxModel * create_nested_model_with_type(qx::IxModel * pParent, const QModelIndex & idxParent, T & t, U * dummy) 00350 { 00351 Q_UNUSED(dummy); 00352 BOOST_STATIC_ASSERT((boost::is_base_of<qx::IxModel, U>::value)); 00353 qx::IxModel * pModel = qx::model_view::create_nested_model(pParent, idxParent, t); 00354 if (! pModel) { return NULL; } 00355 U * pOther = new U(pModel, pParent); 00356 delete pModel; 00357 return pOther; 00358 } 00359 00360 template <class T> 00361 void sync_nested_model(qx::IxModel * pModel, T & t) 00362 { qx::model_view::detail::QxNestedModel<T>::synchronize(pModel, t); } 00363 00364 } // namespace model_view 00365 } // namespace qx 00366 00367 #endif // _QX_NESTED_MODEL_H_