QxCollection.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _QX_COLLECTION_H_
  32. #define _QX_COLLECTION_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxCollection.h
  38. * \author Lionel Marty
  39. * \ingroup QxCollection
  40. * \brief QxOrm thread-safe container (keep insertion order + quick access by index + quick access by key)
  41. */
  42. #ifdef _MSC_VER
  43. #pragma warning(push)
  44. #pragma warning(disable:4996)
  45. #pragma warning(disable:4503)
  46. #endif // _MSC_VER
  47. #include <QtCore/qmutex.h>
  48. #include <QxCollection/IxCollection.h>
  49. #include <QxCollection/QxForeach.h>
  50. #include <QxCommon/QxHashValue.h>
  51. #include <QxTraits/get_class_name.h>
  52. #include <QxTraits/is_smart_ptr.h>
  53. namespace qx {
  54. /*!
  55. * \ingroup QxCollection
  56. * \brief qx::QxCollection<Key, Value> : QxOrm thread-safe container (keep insertion order + quick access by index + quick access by key)
  57. *
  58. * Based on boost::multi_index_container, this collection has advantages of std::vector<T> (keep insertion order + quick access by index)
  59. * and boost::unordered_map<Key, Value> or QHash<Key, Value> (quick access by key : hash-map).
  60. *
  61. * <i>Note :</i> qx::QxCollection<Key, Value> is compatible with the foreach macro provided by Qt library and the BOOST_FOREACH macro provided by boost library.
  62. * However, each element returned by these 2 macros corresponds to an object of type std::pair<Key, Value>.
  63. * To obtain a more natural and more readable result, it is advised to use the _foreach macro : this macro uses BOOST_FOREACH for all the containers except for qx::QxCollection<Key, Value>.
  64. * In this case, the returned element corresponds to the Value type (cf. following sample).
  65. * The macro _foreach is compatible with all containers (stl, Qt, boost...) since it uses the macro BOOST_FOREACH.
  66. *
  67. * <i>Additional note :</i> qx::QxCollection<Key, Value> is particularly suited to receive data resulting from a database.
  68. * Indeed, these data can be sorted (by using ORDER BY in a SQL request for example), it is thus important to preserve the insertion order of the elements in the list.
  69. * Furthermore, each data resulting from a database has a unique id. It is thus important to be able to access quickly to an element based on this single identifier (hash-map).
  70. *
  71. * Quick sample using qx::QxCollection<Key, Value> container :
  72. * \code
  73. // definition of drug class with 3 properties : 'code', 'name' and 'description'
  74. class drug { public: QString code; QString name; QString desc; };
  75. // typedef a smart-pointer of drug class
  76. typedef std::shared_ptr<drug> drug_ptr;
  77. // collection of drugs indexed by 'code' property (QString type)
  78. qx::QxCollection<QString, drug_ptr> lstDrugs;
  79. // create 3 new drugs
  80. drug_ptr d1; d1.reset(new drug()); d1->code = "code1"; d1->name = "name1"; d1->desc = "desc1";
  81. drug_ptr d2; d2.reset(new drug()); d2->code = "code2"; d2->name = "name2"; d2->desc = "desc2";
  82. drug_ptr d3; d3.reset(new drug()); d3->code = "code3"; d3->name = "name3"; d3->desc = "desc3";
  83. // insert 3 drugs into the collection
  84. lstDrugs.insert(d1->code, d1);
  85. lstDrugs.insert(d2->code, d2);
  86. lstDrugs.insert(d3->code, d3);
  87. // iterate over drugs container using QxOrm '_foreach' keyword
  88. _foreach(drug_ptr p, lstDrugs)
  89. { qDebug() << qPrintable(p->name) << " " << qPrintable(p->desc); }
  90. // iterate over drugs container using classic C++ 'for' keyword
  91. for (long l = 0; l < lstDrugs.count(); ++l)
  92. {
  93. drug_ptr p = lstDrugs.getByIndex(l);
  94. QString code = lstDrugs.getKeyByIndex(l);
  95. qDebug() << qPrintable(p->name) << " " << qPrintable(p->desc);
  96. }
  97. // iterate over drugs container using 'qx::QxCollectionIterator' Java-style iterator
  98. qx::QxCollectionIterator<QString, drug_ptr> itr(lstDrugs);
  99. while (itr.next())
  100. {
  101. QString code = itr.key();
  102. qDebug() << qPrintable(itr.value()->name) << " " << qPrintable(itr.value()->desc);
  103. }
  104. // sort drugs container ascending by key and sort descending by value
  105. lstDrugs.sortByKey(true);
  106. lstDrugs.sortByValue(false);
  107. // access to a drug into the collection by its 'code' property
  108. drug_ptr p = lstDrugs.getByKey("code2");
  109. // access to a drug into the collection by index (position in the list)
  110. drug_ptr p = lstDrugs.getByIndex(2);
  111. // test if a drug exists into the collection and if the collection is empty
  112. bool bExist = lstDrugs.exist("code3");
  113. bool bEmpty = lstDrugs.empty();
  114. // remove the second drug from the collection
  115. lstDrugs.removeByIndex(2);
  116. // remove a drug from the collection using its 'code' property
  117. lstDrugs.removeByKey("code3");
  118. // clear the collection : remove all items from the list
  119. lstDrugs.clear();
  120. * \endcode
  121. */
  122. template <typename Key, typename Value>
  123. class QxCollection : public IxCollection
  124. {
  125. public:
  126. typedef QPair<Key, Value> type_pair_key_value;
  127. protected:
  128. typedef QList<type_pair_key_value> type_list_pair_key_value;
  129. typedef QHash<Key, long> type_hash_position;
  130. public:
  131. typedef typename type_list_pair_key_value::iterator iterator;
  132. typedef typename type_list_pair_key_value::const_iterator const_iterator;
  133. #if (QT_VERSION >= 0x050600)
  134. typedef typename type_list_pair_key_value::reverse_iterator reverse_iterator;
  135. typedef typename type_list_pair_key_value::const_reverse_iterator const_reverse_iterator;
  136. #endif // (QT_VERSION >= 0x050600)
  137. typedef const Key & const_reference_key;
  138. typedef const Value & const_reference_value;
  139. protected:
  140. mutable QMutex m_mutex; //!< Mutex => qx::QxCollection is thread-safe
  141. type_list_pair_key_value m_list; //!< Container to keep insertion order
  142. type_hash_position m_hash; //!< Container for fast search by key
  143. bool m_batch; //!< Batch mode to sync internal containers
  144. public:
  145. QxCollection(); //!< Construct an empty list
  146. QxCollection(const QxCollection<Key, Value> & other); //!< Construct a copy of 'other'
  147. virtual ~QxCollection(); //!< Destroy the list
  148. QxCollection<Key, Value> & operator= (const QxCollection<Key, Value> & other); //!< Assign 'other' to this list and return a reference to this list
  149. bool operator== (const QxCollection<Key, Value> & other) const; //!< Return 'true' if 'other' is equal to this list, otherwise return 'false' (same values in the same order)
  150. bool operator!= (const QxCollection<Key, Value> & other) const; //!< Return 'true' if 'other' is not equal to this list, otherwise return 'false'
  151. iterator begin(); //!< Return an STL-style iterator pointing to the first item in the list
  152. iterator end(); //!< Return an STL-style iterator pointing to the imaginary item after the last item in the list
  153. const_iterator begin() const; //!< Return a const STL-style iterator pointing to the first item in the list
  154. const_iterator end() const; //!< Return a const STL-style iterator pointing to the imaginary item after the last item in the list
  155. #if (QT_VERSION >= 0x050600)
  156. reverse_iterator rbegin(); //!< Return a reverse STL-style iterator pointing to the first item in the list
  157. reverse_iterator rend(); //!< Return a reverse STL-style iterator pointing to the imaginary item after the last item in the list
  158. const_reverse_iterator rbegin() const; //!< Return a const reverse STL-style iterator pointing to the first item in the list
  159. const_reverse_iterator rend() const; //!< Return a const reverse STL-style iterator pointing to the imaginary item after the last item in the list
  160. #endif // (QT_VERSION >= 0x050600)
  161. void reserve(long size); //!< Request that the capacity of the allocated storage space for the items of the container be at least enough to hold 'size' elements
  162. void reverse(); //!< Reverse all items in the list
  163. void clear(); //!< Remove all items from the list
  164. long count() const; //!< Return the number of items in the list (same as 'size()')
  165. long size() const; //!< Return the number of items in the list (same as 'count()')
  166. bool contains(const Key & key) const; //!< Return 'true' if the list contains an occurrence of 'key', otherwise return 'false' (same as 'exist()')
  167. bool exist(const Key & key) const; //!< Return 'true' if the list contains an occurrence of 'key', otherwise return 'false' (same as 'contains()')
  168. bool empty() const; //!< Return 'true' if the list contains no items; otherwise return 'false'
  169. bool push_back(const Key & key, const Value & value); //!< Add element 'value' at the end of the list indexed by 'key'
  170. bool push_front(const Key & key, const Value & value); //!< Insert 'value' at the beginning of the list indexed by 'key'
  171. bool insert(const Key & key, const Value & value); //!< Add element 'value' at the end of the list indexed by 'key'
  172. bool insert(long index, const Key & key, const Value & value); //!< Insert element 'value' at position 'index' in the list indexed by 'key'
  173. bool insert(const QxCollection<Key, Value> & other); //!< Add all items of 'other' at the end of the list
  174. bool insert(long index, const QxCollection<Key, Value> & other); //!< Insert all items of 'other' at the end of the list
  175. bool replace(long index, const Key & key, const Value & value); //!< Replace the item at index position 'index' with element 'value' indexed by 'key'
  176. bool swap(long index1, long index2); //!< Exchange the item at index position 'index1' with the item at index position 'index2'
  177. bool move(long indexFrom, long indexTo); //!< Move the item at index position 'indexFrom' to index position 'indexTo'
  178. bool removeByKey(const Key & key); //!< Remove the item indexed by 'key' in the list
  179. bool removeByIndex(long index); //!< Remove the item at index position 'index'
  180. bool removeByIndex(long first, long last); //!< Remove all items from index position 'first' to index position 'last'
  181. bool removeFirst(); //!< Remove the first item in the list
  182. bool removeLast(); //!< Remove the last item in the list
  183. const_reference_value getByKey(const Key & key) const; //!< Return the item associated with the 'key'
  184. const_reference_value getByIndex(long index) const; //!< Return the item at index position 'index'
  185. const_reference_value getFirst() const; //!< Return the first element in the list
  186. const_reference_value getLast() const; //!< Return the last element in the list
  187. const_reference_key getKeyByIndex(long index) const; //!< Return the key associated with the element at index position 'index'
  188. void sortByKey(bool bAscending = true); //!< Sort all items in the list using associated keys to compare
  189. void sortByValue(bool bAscending = true); //!< Sort all items in the list
  190. template <typename Compare>
  191. void sort(Compare comp) { { QMutexLocker locker(& m_mutex); std::sort(m_list.begin(), m_list.end(), comp); } updateHashPosition(); }
  192. protected:
  193. void cloneCollection(QxCollection<Key, Value> * pClone, const QxCollection<Key, Value> & pRef);
  194. bool isSameCollection(const QxCollection<Key, Value> * p1, const QxCollection<Key, Value> & p2) const;
  195. void updateHashPosition(long from = 0, long to = -1, bool check = false);
  196. template <bool bIsPointer /* = false */, int dummy>
  197. struct compareKeyValue
  198. {
  199. static bool compareByKeyAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.first < v2.first); }
  200. static bool compareByKeyDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.first > v2.first); }
  201. static bool compareByValueAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.second < v2.second); }
  202. static bool compareByValueDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.second > v2.second); }
  203. };
  204. template <int dummy>
  205. struct compareKeyValue<true, dummy>
  206. {
  207. 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); }
  208. 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); }
  209. 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); }
  210. 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); }
  211. };
  212. public:
  213. virtual long _count() const { return this->count(); }
  214. virtual void _clear() { this->clear(); }
  215. virtual bool _remove(long index) { return this->removeByIndex(index); }
  216. virtual qx::any _at(long index) const { Value val = this->getByIndex(index); return qx::any(val); }
  217. };
  218. } // namespace qx
  219. #include "../../inl/QxCollection/QxCollection.inl"
  220. QX_REGISTER_CLASS_NAME_TEMPLATE_2(qx::QxCollection)
  221. #ifdef _MSC_VER
  222. #pragma warning(pop)
  223. #endif // _MSC_VER
  224. #endif // _QX_COLLECTION_H_