![]() |
QxOrm
1.4.2
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_GENERIC_CONTAINER_H_ 00033 #define _QX_GENERIC_CONTAINER_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <boost/type_traits/is_pointer.hpp> 00047 00048 #include <boost/unordered_set.hpp> 00049 #include <boost/unordered_map.hpp> 00050 00051 #include <QxTraits/is_smart_ptr.h> 00052 #include <QxTraits/remove_attr.h> 00053 #include <QxTraits/remove_smart_ptr.h> 00054 #include <QxTraits/construct_ptr.h> 00055 00056 namespace qx { 00057 namespace trait { 00058 00059 class no_type 00060 { private: void dummy() const { ; } }; 00061 00066 template <class T> 00067 struct generic_container 00068 { typedef no_type type_item; typedef no_type type_key; typedef no_type type_value; typedef no_type type_value_qx; typedef no_type type_iterator; }; 00069 00070 template <typename Key, typename Value> 00071 struct generic_container_item 00072 { 00073 00074 typedef Key type_key; 00075 typedef Value type_value; 00076 typedef typename qx::trait::remove_attr<Value>::type type_value_qx_tmp; 00077 typedef typename qx::trait::remove_smart_ptr<type_value_qx_tmp>::type type_value_qx; 00078 00079 enum { is_key_pointer = (boost::is_pointer<type_key>::value || qx::trait::is_smart_ptr<type_key>::value) }; 00080 enum { is_value_pointer = (boost::is_pointer<type_value>::value || qx::trait::is_smart_ptr<type_value>::value) }; 00081 00082 private: 00083 00084 std::pair<type_key, type_value> m_pair; 00085 00086 public: 00087 00088 generic_container_item() { ; } 00089 generic_container_item(const Key & key, const Value & value) { m_pair = std::make_pair(key, value); } 00090 ~generic_container_item() { ; } 00091 00092 inline type_key & key() { return m_pair.first; } 00093 inline type_value & value() { return m_pair.second; } 00094 inline const type_key & key() const { return m_pair.first; } 00095 inline const type_value & value() const { return m_pair.second; } 00096 inline type_value_qx & value_qx() { return value_qx_Helper<is_value_pointer, type_value, type_value_qx, 0>::get(m_pair.second); } 00097 inline const type_value_qx & value_qx() const { return value_qx_Helper<is_value_pointer, type_value, type_value_qx, 0>::get(m_pair.second); } 00098 00099 inline void key(const Key & key) { m_pair.first = key; } 00100 inline void value(const Value & value) { m_pair.second = value; } 00101 00102 static inline type_key newKey() { return new_Helper<is_key_pointer, type_key, 0>::get(); } 00103 static inline type_value newValue() { return new_Helper<is_value_pointer, type_value, 0>::get(); } 00104 00105 private: 00106 00107 template <bool bIsPointer /* = true */, typename T, int dummy> 00108 struct new_Helper 00109 { static inline T get() { T t; qx::trait::construct_ptr<T>::get(t); return t; } }; 00110 00111 template <typename T, int dummy> 00112 struct new_Helper<false, T, dummy> 00113 { static inline T get() { return T(); } }; 00114 00115 template <bool bIsPointer /* = true */, typename T, typename U, int dummy> 00116 struct value_qx_Helper 00117 { static inline U & get(T & t) { return (* t); } }; 00118 00119 template <typename T, typename U, int dummy> 00120 struct value_qx_Helper<false, T, U, dummy> 00121 { static inline U & get(T & t) { return t; } }; 00122 00123 }; 00124 00125 } // namespace trait 00126 } // namespace qx 00127 00128 #define QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(TypeContainer, TypeKey, TypeValue) \ 00129 typedef qx::trait::generic_container_item< TypeKey, TypeValue > type_item; \ 00130 typedef typename type_item::type_key type_key; \ 00131 typedef typename type_item::type_value type_value; \ 00132 typedef typename type_item::type_value_qx type_value_qx; \ 00133 typedef typename TypeContainer::iterator type_iterator; 00134 00135 namespace qx { 00136 namespace trait { 00137 namespace detail { 00138 00139 template <typename Container, typename Item> 00140 struct generic_container_base 00141 { 00142 00143 QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(Container, qx::trait::no_type, Item) 00144 00145 static inline long size(const Container & t) { return static_cast<long>(t.size()); } 00146 static inline void clear(Container & t) { t.clear(); } 00147 static inline void reserve(Container & t, long l) { t.reserve(l); } 00148 static inline type_item createItem() { return type_item(type_item::newKey(), type_item::newValue()); } 00149 static inline Item * insertItem(Container & t, type_item & item) { t.push_back(item.value()); return (& t.back()); } 00150 static inline type_iterator end(Container & t) { return t.end(); } 00151 00152 static inline type_iterator begin(Container & t, type_item & item) 00153 { if (t.size() <= 0) { return t.end(); }; item.value(* t.begin()); return t.begin(); } 00154 00155 static inline type_iterator next(Container & t, type_iterator itr, type_item & item) 00156 { itr++; if (itr == t.end()) { return t.end(); }; item.value(* itr); return itr; } 00157 00158 }; 00159 00160 template <typename Container, typename Item> 00161 struct generic_container_base_without_reserve 00162 { 00163 00164 QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(Container, qx::trait::no_type, Item) 00165 00166 static inline long size(const Container & t) { return static_cast<long>(t.size()); } 00167 static inline void clear(Container & t) { t.clear(); } 00168 static inline void reserve(Container & t, long l) { Q_UNUSED(t); Q_UNUSED(l); } 00169 static inline type_item createItem() { return type_item(type_item::newKey(), type_item::newValue()); } 00170 static inline Item * insertItem(Container & t, type_item & item) { t.push_back(item.value()); return (& t.back()); } 00171 static inline type_iterator end(Container & t) { return t.end(); } 00172 00173 static inline type_iterator begin(Container & t, type_item & item) 00174 { if (t.size() <= 0) { return t.end(); }; item.value(* t.begin()); return t.begin(); } 00175 00176 static inline type_iterator next(Container & t, type_iterator itr, type_item & item) 00177 { itr++; if (itr == t.end()) { return t.end(); }; item.value(* itr); return itr; } 00178 00179 }; 00180 00181 template <typename Container, typename Item> 00182 struct generic_container_base_set 00183 { 00184 00185 QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(Container, qx::trait::no_type, Item) 00186 00187 static inline long size(const Container & t) { return static_cast<long>(t.size()); } 00188 static inline void clear(Container & t) { t.clear(); } 00189 static inline void reserve(Container & t, long l) { Q_UNUSED(t); Q_UNUSED(l); } 00190 static inline type_item createItem() { return type_item(type_item::newKey(), type_item::newValue()); } 00191 static inline Item * insertItem(Container & t, type_item & item) { return const_cast<Item *>(& (* (t.insert(item.value()).first))); } 00192 static inline type_iterator end(Container & t) { return t.end(); } 00193 00194 static inline type_iterator begin(Container & t, type_item & item) 00195 { if (t.size() <= 0) { return t.end(); }; item.value(* t.begin()); return t.begin(); } 00196 00197 static inline type_iterator next(Container & t, type_iterator itr, type_item & item) 00198 { itr++; if (itr == t.end()) { return t.end(); }; item.value(* itr); return itr; } 00199 00200 }; 00201 00202 template <typename Container, typename Item> 00203 struct generic_container_base_multi_set 00204 { 00205 00206 QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(Container, qx::trait::no_type, Item) 00207 00208 static inline long size(const Container & t) { return static_cast<long>(t.size()); } 00209 static inline void clear(Container & t) { t.clear(); } 00210 static inline void reserve(Container & t, long l) { Q_UNUSED(t); Q_UNUSED(l); } 00211 static inline type_item createItem() { return type_item(type_item::newKey(), type_item::newValue()); } 00212 static inline Item * insertItem(Container & t, type_item & item) { return const_cast<Item *>(& (* (t.insert(item.value())))); } 00213 static inline type_iterator end(Container & t) { return t.end(); } 00214 00215 static inline type_iterator begin(Container & t, type_item & item) 00216 { if (t.size() <= 0) { return t.end(); }; item.value(* t.begin()); return t.begin(); } 00217 00218 static inline type_iterator next(Container & t, type_iterator itr, type_item & item) 00219 { itr++; if (itr == t.end()) { return t.end(); }; item.value(* itr); return itr; } 00220 00221 }; 00222 00223 template <typename Container, typename Key, typename Value> 00224 struct generic_container_base_key_value_std_style 00225 { 00226 00227 QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(Container, Key, Value) 00228 00229 static inline long size(const Container & t) { return static_cast<long>(t.size()); } 00230 static inline void clear(Container & t) { t.clear(); } 00231 static inline void reserve(Container & t, long l) { t.reserve(l); } 00232 static inline type_item createItem() { return type_item(type_item::newKey(), type_item::newValue()); } 00233 static inline Value * insertItem(Container & t, type_item & item) { return (& (t.insert(std::make_pair(item.key(), item.value())).first->second)); } 00234 static inline type_iterator end(Container & t) { return t.end(); } 00235 00236 static inline type_iterator begin(Container & t, type_item & item) 00237 { if (t.size() <= 0) { return t.end(); }; item.value(* t.begin().second); item.key(* t.begin().first); return t.begin(); } 00238 00239 static inline type_iterator next(Container & t, type_iterator itr, type_item & item) 00240 { itr++; if (itr == t.end()) { return t.end(); }; item.value(* itr.second); item.key(* itr.first); return itr; } 00241 00242 }; 00243 00244 template <typename Container, typename Key, typename Value> 00245 struct generic_container_base_key_value_multi_std_style 00246 { 00247 00248 QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(Container, Key, Value) 00249 00250 static inline long size(const Container & t) { return static_cast<long>(t.size()); } 00251 static inline void clear(Container & t) { t.clear(); } 00252 static inline void reserve(Container & t, long l) { t.reserve(l); } 00253 static inline type_item createItem() { return type_item(type_item::newKey(), type_item::newValue()); } 00254 static inline Value * insertItem(Container & t, type_item & item) { return (& (t.insert(std::make_pair(item.key(), item.value()))->second)); } 00255 static inline type_iterator end(Container & t) { return t.end(); } 00256 00257 static inline type_iterator begin(Container & t, type_item & item) 00258 { if (t.size() <= 0) { return t.end(); }; item.value(* t.begin().second); item.key(* t.begin().first); return t.begin(); } 00259 00260 static inline type_iterator next(Container & t, type_iterator itr, type_item & item) 00261 { itr++; if (itr == t.end()) { return t.end(); }; item.value(* itr.second); item.key(* itr.first); return itr; } 00262 00263 }; 00264 00265 template <typename Container, typename Key, typename Value> 00266 struct generic_container_base_key_value_qt_style 00267 { 00268 00269 QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(Container, Key, Value) 00270 00271 static inline long size(const Container & t) { return static_cast<long>(t.size()); } 00272 static inline void clear(Container & t) { t.clear(); } 00273 static inline void reserve(Container & t, long l) { t.reserve(l); } 00274 static inline type_item createItem() { return type_item(type_item::newKey(), type_item::newValue()); } 00275 static inline Value * insertItem(Container & t, type_item & item) { return (& (t.insert(item.key(), item.value()).value())); } 00276 static inline type_iterator end(Container & t) { return t.end(); } 00277 00278 static inline type_iterator begin(Container & t, type_item & item) 00279 { if (t.size() <= 0) { return t.end(); }; item.value(* t.begin().value()); item.key(* t.begin().key()); return t.begin(); } 00280 00281 static inline type_iterator next(Container & t, type_iterator itr, type_item & item) 00282 { itr++; if (itr == t.end()) { return t.end(); }; item.value(* itr.value()); item.key(* itr.key()); return itr; } 00283 00284 }; 00285 00286 } // namespace detail 00287 00288 template <typename T> 00289 struct generic_container< std::vector<T> > : public qx::trait::detail::generic_container_base< std::vector<T>, T > 00290 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(std::vector, T), qx::trait::no_type, T) }; 00291 00292 template <typename T> 00293 struct generic_container< std::list<T> > : public qx::trait::detail::generic_container_base_without_reserve< std::list<T>, T > 00294 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(std::list, T), qx::trait::no_type, T) }; 00295 00296 template <typename T> 00297 struct generic_container< std::set<T> > : public qx::trait::detail::generic_container_base_set< std::set<T>, T > 00298 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(std::set, T), qx::trait::no_type, T) }; 00299 00300 template <typename Key, typename Value> 00301 struct generic_container< std::map<Key, Value> > : public qx::trait::detail::generic_container_base_key_value_std_style< std::map<Key, Value>, Key, Value > 00302 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(std::map, Key, Value), Key, Value) }; 00303 00304 template <typename T> 00305 struct generic_container< boost::unordered_set<T> > : public qx::trait::detail::generic_container_base_set< boost::unordered_set<T>, T > 00306 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(boost::unordered_set, T), qx::trait::no_type, T) }; 00307 00308 template <typename T> 00309 struct generic_container< boost::unordered_multiset<T> > : public qx::trait::detail::generic_container_base_multi_set< boost::unordered_multiset<T>, T > 00310 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(boost::unordered_multiset, T), qx::trait::no_type, T) }; 00311 00312 template <typename Key, typename Value> 00313 struct generic_container< boost::unordered_map<Key, Value> > : public qx::trait::detail::generic_container_base_key_value_std_style< boost::unordered_map<Key, Value>, Key, Value > 00314 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(boost::unordered_map, Key, Value), Key, Value) }; 00315 00316 template <typename Key, typename Value> 00317 struct generic_container< boost::unordered_multimap<Key, Value> > : public qx::trait::detail::generic_container_base_key_value_multi_std_style< boost::unordered_multimap<Key, Value>, Key, Value > 00318 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(boost::unordered_multimap, Key, Value), Key, Value) }; 00319 00320 #ifdef _QX_CPP_11_CONTAINER 00321 #ifndef BOOST_NO_CXX11_STD_UNORDERED 00322 00323 template <typename T> 00324 struct generic_container< std::unordered_set<T> > : public qx::trait::detail::generic_container_base_set< std::unordered_set<T>, T > 00325 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(std::unordered_set, T), qx::trait::no_type, T) }; 00326 00327 template <typename T> 00328 struct generic_container< std::unordered_multiset<T> > : public qx::trait::detail::generic_container_base_multi_set< std::unordered_multiset<T>, T > 00329 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(std::unordered_multiset, T), qx::trait::no_type, T) }; 00330 00331 template <typename Key, typename Value> 00332 struct generic_container< std::unordered_map<Key, Value> > : public qx::trait::detail::generic_container_base_key_value_std_style< std::unordered_map<Key, Value>, Key, Value > 00333 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(std::unordered_map, Key, Value), Key, Value) }; 00334 00335 template <typename Key, typename Value> 00336 struct generic_container< std::unordered_multimap<Key, Value> > : public qx::trait::detail::generic_container_base_key_value_multi_std_style< std::unordered_multimap<Key, Value>, Key, Value > 00337 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(std::unordered_multimap, Key, Value), Key, Value) }; 00338 00339 #endif // BOOST_NO_CXX11_STD_UNORDERED 00340 #endif // _QX_CPP_11_CONTAINER 00341 00342 template <typename T> 00343 struct generic_container< QVector<T> > : public qx::trait::detail::generic_container_base< QVector<T>, T > 00344 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(QVector, T), qx::trait::no_type, T) }; 00345 00346 #if (QT_VERSION >= 0x040700) 00347 00348 template <typename T> 00349 struct generic_container< QList<T> > : public qx::trait::detail::generic_container_base< QList<T>, T > 00350 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(QList, T), qx::trait::no_type, T) }; 00351 00352 #else // (QT_VERSION >= 0x040700) 00353 00354 template <typename T> 00355 struct generic_container< QList<T> > : public qx::trait::detail::generic_container_base_without_reserve< QList<T>, T > 00356 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(QList, T), qx::trait::no_type, T) }; 00357 00358 #endif // (QT_VERSION >= 0x040700) 00359 00360 template <typename T> 00361 struct generic_container< QLinkedList<T> > : public qx::trait::detail::generic_container_base_without_reserve< QLinkedList<T>, T > 00362 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(QLinkedList, T), qx::trait::no_type, T) }; 00363 00364 template <typename T> 00365 struct generic_container< QSet<T> > : public qx::trait::detail::generic_container_base_multi_set< QSet<T>, T > 00366 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1(QSet, T), qx::trait::no_type, T) }; 00367 00368 template <typename Key, typename Value> 00369 struct generic_container< QMap<Key, Value> > : public qx::trait::detail::generic_container_base_key_value_qt_style< QMap<Key, Value>, Key, Value > 00370 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(QMap, Key, Value), Key, Value) }; 00371 00372 template <typename Key, typename Value> 00373 struct generic_container< QMultiMap<Key, Value> > : public qx::trait::detail::generic_container_base_key_value_qt_style< QMultiMap<Key, Value>, Key, Value > 00374 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(QMultiMap, Key, Value), Key, Value) }; 00375 00376 template <typename Key, typename Value> 00377 struct generic_container< QHash<Key, Value> > : public qx::trait::detail::generic_container_base_key_value_qt_style< QHash<Key, Value>, Key, Value > 00378 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(QHash, Key, Value), Key, Value) }; 00379 00380 template <typename Key, typename Value> 00381 struct generic_container< QMultiHash<Key, Value> > : public qx::trait::detail::generic_container_base_key_value_qt_style< QMultiHash<Key, Value>, Key, Value > 00382 { QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(QMultiHash, Key, Value), Key, Value) }; 00383 00384 template <typename Key, typename Value> 00385 struct generic_container< qx::QxCollection<Key, Value> > 00386 { 00387 00388 QX_TRAIT_GENERIC_CONTAINER_TYPEDEF(QX_TEMPLATE_T_P1_P2(qx::QxCollection, Key, Value), Key, Value) 00389 00390 static inline long size(const qx::QxCollection<Key, Value> & t) { return static_cast<long>(t.size()); } 00391 static inline void clear(qx::QxCollection<Key, Value> & t) { t.clear(); } 00392 static inline void reserve(qx::QxCollection<Key, Value> & t, long l) { t.reserve(l); } 00393 static inline type_item createItem() { return type_item(type_item::newKey(), type_item::newValue()); } 00394 static inline Value * insertItem(qx::QxCollection<Key, Value> & t, type_item & item) { t.insert(item.key(), item.value()); return const_cast<Value *>(& t.getByKey(item.key())); } 00395 static inline type_iterator end(qx::QxCollection<Key, Value> & t) { return t.end(); } 00396 00397 static inline type_iterator begin(qx::QxCollection<Key, Value> & t, type_item & item) 00398 { if (t.size() <= 0) { return t.end(); }; item.value(t.getByIndex(0)); item.key(t.getKeyByIndex(0)); return t.begin(); } 00399 00400 static inline type_iterator next(qx::QxCollection<Key, Value> & t, type_iterator itr, type_item & item) 00401 { itr++; if (itr == t.end()) { return t.end(); }; long l = (itr - t.begin()); item.value(t.getByIndex(l)); item.key(t.getKeyByIndex(l)); return itr; } 00402 00403 }; 00404 00405 } // namespace trait 00406 } // namespace qx 00407 00408 #endif // _QX_GENERIC_CONTAINER_H_