![]() |
QxOrm
1.4.4
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 <QxCollection/IxCollection.h> 00053 #include <QxCollection/QxForeach.h> 00054 00055 #include <QxCommon/QxHashValue.h> 00056 00057 #include <QxTraits/get_class_name.h> 00058 #include <QxTraits/is_smart_ptr.h> 00059 00060 namespace qx { 00061 00144 template <typename Key, typename Value> 00145 class QxCollection : public IxCollection 00146 { 00147 00148 public: 00149 00150 typedef QPair<Key, Value> type_pair_key_value; 00151 00152 protected: 00153 00154 typedef QList<type_pair_key_value> type_list_pair_key_value; 00155 typedef QHash<Key, long> type_hash_position; 00156 00157 public: 00158 00159 typedef typename type_list_pair_key_value::iterator iterator; 00160 typedef typename type_list_pair_key_value::const_iterator const_iterator; 00161 00162 #if (QT_VERSION >= 0x050600) 00163 typedef typename type_list_pair_key_value::reverse_iterator reverse_iterator; 00164 typedef typename type_list_pair_key_value::const_reverse_iterator const_reverse_iterator; 00165 #endif // (QT_VERSION >= 0x050600) 00166 00167 typedef const Key & const_reference_key; 00168 typedef const Value & const_reference_value; 00169 00170 protected: 00171 00172 type_list_pair_key_value m_list; 00173 type_hash_position m_hash; 00174 bool m_batch; 00175 00176 public: 00177 00178 QxCollection(); 00179 QxCollection(const QxCollection<Key, Value> & other); 00180 virtual ~QxCollection(); 00181 00182 QxCollection<Key, Value> & operator= (const QxCollection<Key, Value> & other); 00183 bool operator== (const QxCollection<Key, Value> & other) const; 00184 bool operator!= (const QxCollection<Key, Value> & other) const; 00185 00186 iterator begin(); 00187 iterator end(); 00188 const_iterator begin() const; 00189 const_iterator end() const; 00190 00191 #if (QT_VERSION >= 0x050600) 00192 reverse_iterator rbegin(); 00193 reverse_iterator rend(); 00194 const_reverse_iterator rbegin() const; 00195 const_reverse_iterator rend() const; 00196 #endif // (QT_VERSION >= 0x050600) 00197 00198 void reserve(long size); 00199 void reverse(); 00200 void clear(); 00201 long count() const; 00202 long size() const; 00203 bool contains(const Key & key) const; 00204 bool exist(const Key & key) const; 00205 bool empty() const; 00206 00207 bool push_back(const Key & key, const Value & value); 00208 bool push_front(const Key & key, const Value & value); 00209 bool insert(const Key & key, const Value & value); 00210 bool insert(long index, const Key & key, const Value & value); 00211 bool insert(const QxCollection<Key, Value> & other); 00212 bool insert(long index, const QxCollection<Key, Value> & other); 00213 bool replace(long index, const Key & key, const Value & value); 00214 bool swap(long index1, long index2); 00215 bool move(long indexFrom, long indexTo); 00216 00217 bool removeByKey(const Key & key); 00218 bool removeByIndex(long index); 00219 bool removeByIndex(long first, long last); 00220 bool removeFirst(); 00221 bool removeLast(); 00222 00223 const_reference_value getByKey(const Key & key) const; 00224 const_reference_value getByIndex(long index) const; 00225 const_reference_value getFirst() const; 00226 const_reference_value getLast() const; 00227 const_reference_key getKeyByIndex(long index) const; 00228 00229 void sortByKey(bool bAscending = true); 00230 void sortByValue(bool bAscending = true); 00231 00232 template <typename Compare> 00233 void sort(Compare comp) { std::sort(m_list.begin(), m_list.end(), comp); updateHashPosition(); } 00234 00235 protected: 00236 00237 void cloneCollection(QxCollection<Key, Value> * pClone, const QxCollection<Key, Value> & pRef); 00238 bool isSameCollection(const QxCollection<Key, Value> * p1, const QxCollection<Key, Value> & p2) const; 00239 void updateHashPosition(long from = 0, long to = -1); 00240 00241 template <bool bIsPointer /* = false */, int dummy> 00242 struct compareKeyValue 00243 { 00244 static bool compareByKeyAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.first < v2.first); } 00245 static bool compareByKeyDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.first > v2.first); } 00246 static bool compareByValueAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.second < v2.second); } 00247 static bool compareByValueDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.second > v2.second); } 00248 }; 00249 00250 template <int dummy> 00251 struct compareKeyValue<true, dummy> 00252 { 00253 static bool compareByKeyAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return ((v1.first && v2.first) ? ((* v1.first) < (* v2.first)) : false); } 00254 static bool compareByKeyDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return ((v1.first && v2.first) ? ((* v1.first) > (* v2.first)) : true); } 00255 static bool compareByValueAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return ((v1.second && v2.second) ? ((* v1.second) < (* v2.second)) : false); } 00256 static bool compareByValueDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return ((v1.second && v2.second) ? ((* v1.second) > (* v2.second)) : true); } 00257 }; 00258 00259 public: 00260 00261 virtual long _count() const { return this->count(); } 00262 virtual void _clear() { this->clear(); } 00263 virtual bool _remove(long index) { return this->removeByIndex(index); } 00264 virtual qx::any _at(long index) const { Value val = this->getByIndex(index); return qx::any(val); } 00265 00266 }; 00267 00268 } // namespace qx 00269 00270 #include "../../inl/QxCollection/QxCollection.inl" 00271 00272 QX_REGISTER_CLASS_NAME_TEMPLATE_2(qx::QxCollection) 00273 00274 #ifdef _MSC_VER 00275 #pragma warning(pop) 00276 #endif // _MSC_VER 00277 00278 #endif // _QX_COLLECTION_H_