![]() |
QxOrm
1.2.7
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_COLLECTION_H_ 00033 #define _QX_COLLECTION_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #ifdef _MSC_VER 00047 #pragma warning(push) 00048 #pragma warning(disable:4996) 00049 #pragma warning(disable:4503) 00050 #endif // _MSC_VER 00051 00052 #include <boost/multi_index_container.hpp> 00053 #include <boost/multi_index/member.hpp> 00054 #include <boost/multi_index/random_access_index.hpp> 00055 #include <boost/multi_index/hashed_index.hpp> 00056 #include <boost/type_traits/is_pointer.hpp> 00057 00058 #include <QtCore/qpair.h> 00059 00060 #include <QxCollection/IxCollection.h> 00061 #include <QxCollection/QxForeach.h> 00062 00063 #include <QxCommon/QxHashValue.h> 00064 00065 #include <QxTraits/get_class_name.h> 00066 #include <QxTraits/is_smart_ptr.h> 00067 00068 namespace qx { 00069 00152 template <typename Key, typename Value> 00153 class QxCollection : public IxCollection 00154 { 00155 00156 public: 00157 00158 typedef QPair<Key, Value> type_pair_key_value; 00159 00160 protected: 00161 00162 typedef boost::multi_index::member<type_pair_key_value, Key, & type_pair_key_value::first> type_member; 00163 typedef boost::multi_index::random_access<> type_first_index; 00164 typedef boost::multi_index::hashed_unique<type_member> type_second_index; 00165 typedef boost::multi_index::indexed_by<type_first_index, type_second_index> type_indexed_by; 00166 typedef boost::multi_index::multi_index_container<type_pair_key_value, type_indexed_by> type_container; 00167 00168 typedef typename type_container::template nth_index<0>::type type_index_rand; 00169 typedef typename type_container::template nth_index<1>::type type_index_hash; 00170 00171 public: 00172 00173 typedef typename type_index_rand::iterator iterator; 00174 typedef typename type_index_rand::const_iterator const_iterator; 00175 typedef typename type_index_rand::reverse_iterator reverse_iterator; 00176 typedef typename type_index_rand::const_reverse_iterator const_reverse_iterator; 00177 00178 typedef const Key & const_reference_key; 00179 typedef const Value & const_reference_value; 00180 00181 protected: 00182 00183 type_container m_qxCollection; 00184 00185 public: 00186 00187 QxCollection(); 00188 QxCollection(const QxCollection<Key, Value> & other); 00189 virtual ~QxCollection(); 00190 00191 QxCollection<Key, Value> & operator= (const QxCollection<Key, Value> & other); 00192 bool operator== (const QxCollection<Key, Value> & other) const; 00193 bool operator!= (const QxCollection<Key, Value> & other) const; 00194 00195 inline iterator begin(); 00196 inline iterator end(); 00197 inline const_iterator begin() const; 00198 inline const_iterator end() const; 00199 00200 inline reverse_iterator rbegin(); 00201 inline reverse_iterator rend(); 00202 inline const_reverse_iterator rbegin() const; 00203 inline const_reverse_iterator rend() const; 00204 00205 inline long capacity() const; 00206 inline void reserve(long size); 00207 inline void reverse(); 00208 inline void clear(); 00209 inline long count() const; 00210 inline long size() const; 00211 inline bool contains(const Key & key) const; 00212 inline bool exist(const Key & key) const; 00213 inline bool empty() const; 00214 00215 inline bool push_back(const Key & key, const Value & value); 00216 inline bool push_front(const Key & key, const Value & value); 00217 inline bool insert(const Key & key, const Value & value); 00218 inline bool insert(long index, const Key & key, const Value & value); 00219 inline bool insert(const QxCollection<Key, Value> & other); 00220 inline bool insert(long index, const QxCollection<Key, Value> & other); 00221 inline bool replace(long index, const Key & key, const Value & value); 00222 inline bool swap(long index1, long index2); 00223 inline bool move(long indexFrom, long indexTo); 00224 00225 inline bool removeByKey(const Key & key); 00226 inline bool removeByIndex(long index); 00227 inline bool removeByIndex(long first, long last); 00228 inline bool removeFirst(); 00229 inline bool removeLast(); 00230 00231 inline const_reference_value getByKey(const Key & key) const; 00232 inline const_reference_value getByIndex(long index) const; 00233 inline const_reference_value getFirst() const; 00234 inline const_reference_value getLast() const; 00235 inline const_reference_key getKeyByIndex(long index) const; 00236 00237 inline void sortByKey(bool bAscending = true); 00238 inline void sortByValue(bool bAscending = true); 00239 00240 template <typename Compare> 00241 inline void sort(Compare comp) { m_qxCollection.template get<0>().sort(comp); } 00242 00243 private: 00244 00245 void cloneCollection(QxCollection<Key, Value> * pClone, const QxCollection<Key, Value> & pRef); 00246 bool isSameCollection(const QxCollection<Key, Value> * p1, const QxCollection<Key, Value> & p2) const; 00247 00248 template <bool bIsPointer /* = false */, int dummy> 00249 struct compareKeyValue 00250 { 00251 static inline bool compareByKeyAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.first < v2.first); } 00252 static inline bool compareByKeyDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.first > v2.first); } 00253 static inline bool compareByValueAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.second < v2.second); } 00254 static inline bool compareByValueDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.second > v2.second); } 00255 }; 00256 00257 template <int dummy> 00258 struct compareKeyValue<true, dummy> 00259 { 00260 static inline bool compareByKeyAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return ((v1.first && v2.first) ? ((* v1.first) < (* v2.first)) : false); } 00261 static inline bool compareByKeyDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return ((v1.first && v2.first) ? ((* v1.first) > (* v2.first)) : true); } 00262 static inline bool compareByValueAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return ((v1.second && v2.second) ? ((* v1.second) < (* v2.second)) : false); } 00263 static inline bool compareByValueDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return ((v1.second && v2.second) ? ((* v1.second) > (* v2.second)) : true); } 00264 }; 00265 00266 public: 00267 00268 virtual long _count() const { return this->count(); } 00269 virtual void _clear() { this->clear(); } 00270 virtual bool _remove(long index) { return this->removeByIndex(index); } 00271 virtual boost::any _at(long index) const { Value val = this->getByIndex(index); return boost::any(val); } 00272 00273 }; 00274 00275 } // namespace qx 00276 00277 #include "../../inl/QxCollection/QxCollection.inl" 00278 00279 QX_REGISTER_CLASS_NAME_TEMPLATE_2(qx::QxCollection) 00280 00281 #ifdef _MSC_VER 00282 #pragma warning(pop) 00283 #endif // _MSC_VER 00284 00285 #endif // _QX_COLLECTION_H_