![]() |
QxOrm
1.2.8
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 00052 #include <QxCommon/QxStringCvt.h> 00053 00054 #include <QxCollection/QxCollection.h> 00055 00056 #include <QxTraits/is_qx_registered.h> 00057 #include <QxTraits/is_container.h> 00058 #include <QxTraits/is_smart_ptr.h> 00059 #include <QxTraits/get_base_class.h> 00060 #include <QxTraits/get_class_name_primitive.h> 00061 #include <QxTraits/construct_ptr.h> 00062 #include <QxTraits/generic_container.h> 00063 #include <QxTraits/is_valid_primary_key.h> 00064 00065 #include <QxModelView/IxModel.h> 00066 #include <QxModelView/QxModel.h> 00067 00068 namespace qx { 00069 namespace model_view { 00070 00071 template <class T> 00072 qx::IxModel * create_nested_model(qx::IxModel * pParent, const QModelIndex & idxParent, T & t); 00073 00074 } // namespace model_view 00075 } // namespace qx 00076 00077 namespace qx { 00078 namespace model_view { 00079 namespace detail { 00080 00081 template <class T> 00082 struct QxNestedModel; 00083 00084 template <class T> 00085 struct QxNestedModel_Generic 00086 { 00087 00088 enum { is_valid = qx::trait::is_qx_registered<T>::value }; 00089 00090 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00091 { 00092 typedef typename qx::QxModel<T>::type_collection type_collection; 00093 typedef typename qx::QxModel<T>::type_primary_key type_primary_key; 00094 typedef typename qx::QxModel<T>::type_ptr type_ptr; 00095 00096 BOOST_STATIC_ASSERT(is_valid); 00097 qx::QxModel<T> * pModel = new qx::QxModel<T>(pParent); 00098 pModel->setParentModel(pParent); 00099 type_collection & model = pModel->m_model; 00100 long & idx = pModel->m_lManualInsertIndex; 00101 type_primary_key key; 00102 type_ptr ptr; 00103 00104 pModel->beginInsertRows(idxParent, 0, 0); 00105 ptr = type_ptr(new T()); 00106 (* ptr) = t; 00107 qx::IxDataMember * pDataMemberId = pModel->m_pDataMemberId; 00108 if (! pDataMemberId) { qAssert(false); pModel->endInsertRows(); return pModel; } 00109 QVariant value = pDataMemberId->toVariant(& t); 00110 if (! qx::trait::is_valid_primary_key(value)) 00111 { idx--; value = QVariant(static_cast<qlonglong>(idx)); } 00112 qx::cvt::from_variant(value, key); 00113 model.insert(0, key, ptr); 00114 pModel->endInsertRows(); 00115 return pModel; 00116 } 00117 00118 }; 00119 00120 template <class T> 00121 struct QxNestedModel_Container 00122 { 00123 00124 typedef qx::trait::generic_container<T> type_generic_container; 00125 typedef typename type_generic_container::type_value_qx type_data; 00126 00127 enum { is_valid = qx::trait::is_qx_registered<type_data>::value }; 00128 00129 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00130 { 00131 int iCurrRow = 0; 00132 BOOST_STATIC_ASSERT(is_valid); 00133 qx::QxModel<type_data> * pModel = new qx::QxModel<type_data>(pParent); 00134 pModel->setParentModel(pParent); 00135 long lCount = static_cast<long>(qx::trait::generic_container<T>::size(t)); 00136 if (lCount <= 0) { return pModel; } 00137 00138 pModel->beginInsertRows(idxParent, 0, (lCount - 1)); 00139 for (typename T::iterator it = t.begin(); it != t.end(); ++it) 00140 { insertItem(pModel, (* it), iCurrRow); iCurrRow++; } 00141 pModel->endInsertRows(); 00142 return pModel; 00143 } 00144 00145 template <typename U> 00146 static inline bool insert(qx::IxModel * pModel, U & item, int iRow) 00147 { 00148 typedef typename qx::QxModel<U>::type_collection type_collection; 00149 typedef typename qx::QxModel<U>::type_primary_key type_primary_key; 00150 typedef typename qx::QxModel<U>::type_ptr type_ptr; 00151 00152 if (! pModel) { qAssert(false); return false; } 00153 qx::QxModel<U> * pModelWrk = static_cast<qx::QxModel<U> *>(pModel); 00154 type_collection & model = pModelWrk->m_model; 00155 long & idx = pModelWrk->m_lManualInsertIndex; 00156 type_primary_key key; 00157 type_ptr ptr; 00158 00159 ptr = type_ptr(new U()); 00160 (* ptr) = item; 00161 qx::IxDataMember * pDataMemberId = pModelWrk->m_pDataMemberId; 00162 if (! pDataMemberId) { qAssert(false); return false; } 00163 QVariant value = pDataMemberId->toVariant(& item); 00164 if (! qx::trait::is_valid_primary_key(value)) 00165 { idx--; value = QVariant(static_cast<qlonglong>(idx)); } 00166 qx::cvt::from_variant(value, key); 00167 model.insert(iRow, key, ptr); 00168 return true; 00169 } 00170 00171 private: 00172 00173 template <typename U> 00174 static inline bool insertItem(qx::IxModel * pModel, U & item, int iRow) 00175 { return insertItem_Helper<U, boost::is_pointer<U>::value || qx::trait::is_smart_ptr<U>::value>::insert(pModel, item, iRow); } 00176 00177 template <typename U, bool bIsPointer /* = true */> 00178 struct insertItem_Helper 00179 { 00180 static inline bool insert(qx::IxModel * pModel, U & item, int iRow) 00181 { return (item ? qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, (* item), iRow) : true); } 00182 }; 00183 00184 template <typename U1, typename U2> 00185 struct insertItem_Helper<std::pair<U1, U2>, false> 00186 { 00187 static inline bool insert(qx::IxModel * pModel, std::pair<U1, U2> & item, int iRow) 00188 { return qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, item.second, iRow); } 00189 }; 00190 00191 template <typename U1, typename U2> 00192 struct insertItem_Helper<const std::pair<U1, U2>, false> 00193 { 00194 static inline bool insert(qx::IxModel * pModel, const std::pair<U1, U2> & item, int iRow) 00195 { return qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, item.second, iRow); } 00196 }; 00197 00198 template <typename U1, typename U2> 00199 struct insertItem_Helper<QPair<U1, U2>, false> 00200 { 00201 static inline bool insert(qx::IxModel * pModel, QPair<U1, U2> & item, int iRow) 00202 { return qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, item.second, iRow); } 00203 }; 00204 00205 template <typename U1, typename U2> 00206 struct insertItem_Helper<const QPair<U1, U2>, false> 00207 { 00208 static inline bool insert(qx::IxModel * pModel, const QPair<U1, U2> & item, int iRow) 00209 { return qx::model_view::detail::QxNestedModel_Container<T>::insertItem(pModel, item.second, iRow); } 00210 }; 00211 00212 template <typename U> 00213 struct insertItem_Helper<U, false> 00214 { 00215 enum { is_same_type = boost::is_same<qx::model_view::detail::QxNestedModel_Container<T>::type_data, U>::value }; 00216 static bool insert(qx::IxModel * pModel, U & item, int iRow) 00217 { BOOST_STATIC_ASSERT(is_same_type); return qx::model_view::detail::QxNestedModel_Container<T>::insert(pModel, item, iRow); } 00218 }; 00219 00220 }; 00221 00222 template <class T> 00223 struct QxNestedModel_Ptr 00224 { 00225 00226 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00227 { return (t ? create_Helper(pParent, idxParent, (* t)) : create_NullHelper(pParent, idxParent)); } 00228 00229 private: 00230 00231 template <class U> 00232 static inline qx::IxModel * create_Helper(qx::IxModel * pParent, const QModelIndex & idxParent, U & u) 00233 { return qx::model_view::detail::QxNestedModel<U>::create(pParent, idxParent, u); } 00234 00235 static inline qx::IxModel * create_NullHelper(qx::IxModel * pParent, const QModelIndex & idxParent) 00236 { 00237 T t; qx::trait::construct_ptr<T>::get(t); 00238 if (! t) { qAssert(false); return NULL; } 00239 qx::IxModel * pModel = qx::model_view::create_nested_model(pParent, idxParent, (* t)); 00240 if (pModel) { pModel->clear(); } qAssert(pModel != NULL); 00241 return pModel; 00242 } 00243 00244 }; 00245 00246 template <class T> 00247 struct QxNestedModel 00248 { 00249 00250 static inline qx::IxModel * create(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00251 { 00252 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; 00253 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; 00254 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; 00255 00256 return type_model_view_3::create(pParent, idxParent, t); 00257 } 00258 00259 }; 00260 00261 } // namespace detail 00262 } // namespace model_view 00263 } // namespace qx 00264 00265 namespace qx { 00266 namespace model_view { 00267 00275 template <class T> 00276 qx::IxModel * create_nested_model(qx::IxModel * pParent, const QModelIndex & idxParent, T & t) 00277 { return qx::model_view::detail::QxNestedModel<T>::create(pParent, idxParent, t); } 00278 00279 } // namespace model_view 00280 } // namespace qx 00281 00282 #endif // _QX_NESTED_MODEL_H_