QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxCollection.h
Go to the documentation of this file.
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
32#ifndef _QX_COLLECTION_H_
33#define _QX_COLLECTION_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#ifdef _MSC_VER
47#pragma warning(push)
48#pragma warning(disable:4996)
49#pragma warning(disable:4503)
50#endif // _MSC_VER
51
52#include <QtCore/qmutex.h>
53
56
58
61
62namespace qx {
63
146template <typename Key, typename Value>
148{
149
150public:
151
152 typedef QPair<Key, Value> type_pair_key_value;
153
154protected:
155
156 typedef QList<type_pair_key_value> type_list_pair_key_value;
157 typedef QHash<Key, long> type_hash_position;
158
159public:
160
161 typedef typename type_list_pair_key_value::iterator iterator;
162 typedef typename type_list_pair_key_value::const_iterator const_iterator;
163
164#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
165 typedef typename type_list_pair_key_value::reverse_iterator reverse_iterator;
166 typedef typename type_list_pair_key_value::const_reverse_iterator const_reverse_iterator;
167#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
168
169 typedef const Key & const_reference_key;
170 typedef const Value & const_reference_value;
171
172protected:
173
174 mutable QMutex m_mutex;
177 bool m_batch;
178
179public:
180
183 virtual ~QxCollection();
184
186 bool operator== (const QxCollection<Key, Value> & other) const;
187 bool operator!= (const QxCollection<Key, Value> & other) const;
188
193
194#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
199#endif // (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
200
201 void reserve(long size);
202 void reverse();
203 void clear();
204 long count() const;
205 long size() const;
206 bool contains(const Key & key) const;
207 bool exist(const Key & key) const;
208 bool empty() const;
209
210 bool push_back(const Key & key, const Value & value);
211 bool push_front(const Key & key, const Value & value);
212 bool insert(const Key & key, const Value & value);
213 bool insert(long index, const Key & key, const Value & value);
214 bool insert(const QxCollection<Key, Value> & other);
215 bool insert(long index, const QxCollection<Key, Value> & other);
216 bool replace(long index, const Key & key, const Value & value);
217 bool swap(long index1, long index2);
218 bool move(long indexFrom, long indexTo);
219
220 bool removeByKey(const Key & key);
221 bool removeByIndex(long index);
222 bool removeByIndex(long first, long last);
223 bool removeFirst();
224 bool removeLast();
225
226 const_reference_value getByKey(const Key & key) const;
231
232 void sortByKey(bool bAscending = true);
233 void sortByValue(bool bAscending = true);
234
235 template <typename Compare>
236 void sort(Compare comp) { { QMutexLocker locker(& m_mutex); std::sort(m_list.begin(), m_list.end(), comp); } updateHashPosition(); }
237
238protected:
239
242 void updateHashPosition(long from = 0, long to = -1, bool check = false);
243
244 template <bool bIsPointer /* = false */, int dummy>
246 {
247 static bool compareByKeyAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.first < v2.first); }
248 static bool compareByKeyDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.first > v2.first); }
249 static bool compareByValueAscending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.second < v2.second); }
250 static bool compareByValueDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.second > v2.second); }
251 };
252
253 template <int dummy>
254 struct compareKeyValue<true, dummy>
255 {
256 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); }
257 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); }
258 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); }
259 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); }
260 };
261
262public:
263
264 virtual long _count() const { return this->count(); }
265 virtual void _clear() { this->clear(); }
266 virtual bool _remove(long index) { return this->removeByIndex(index); }
267 virtual qx::any _at(long index) const { Value val = this->getByIndex(index); return qx::any(val); }
268
269};
270
271} // namespace qx
272
273#include "../../inl/QxCollection/QxCollection.inl"
274
276
277#ifdef _MSC_VER
278#pragma warning(pop)
279#endif // _MSC_VER
280
281#endif // _QX_COLLECTION_H_
Common interface for all QxOrm containers qx::QxCollection<Key, Value>
foreach-style (based on BOOST_FOREACH macro) to iterate over all stl, boost and Qt containers + qx::Q...
Specialize hash_value function for some Qt and boost types (used for example by qx::QxCollection<Key,...
qx::IxCollection : common interface for all QxOrm containers qx::QxCollection<Key,...
qx::QxCollection<Key, Value> : QxOrm thread-safe container (keep insertion order + quick access by in...
const_iterator begin() const
Return a const STL-style iterator pointing to the first item in the list.
reverse_iterator rbegin()
Return a reverse STL-style iterator pointing to the first item in the list.
bool contains(const Key &key) const
Return 'true' if the list contains an occurrence of 'key', otherwise return 'false' (same as 'exist()...
void sortByValue(bool bAscending=true)
Sort all items in the list.
type_list_pair_key_value::const_iterator const_iterator
bool insert(const Key &key, const Value &value)
Add element 'value' at the end of the list indexed by 'key'.
QxCollection< Key, Value > & operator=(const QxCollection< Key, Value > &other)
Assign 'other' to this list and return a reference to this list.
void sort(Compare comp)
virtual void _clear()
const_reverse_iterator rbegin() const
Return a const reverse STL-style iterator pointing to the first item in the list.
reverse_iterator rend()
Return a reverse STL-style iterator pointing to the imaginary item after the last item in the list.
const_reference_key getKeyByIndex(long index) const
Return the key associated with the element at index position 'index'.
bool removeLast()
Remove the last item in the list.
bool move(long indexFrom, long indexTo)
Move the item at index position 'indexFrom' to index position 'indexTo'.
long size() const
Return the number of items in the list (same as 'count()')
QxCollection()
Construct an empty list.
const_reference_value getByKey(const Key &key) const
Return the item associated with the 'key'.
void updateHashPosition(long from=0, long to=-1, bool check=false)
void reserve(long size)
Request that the capacity of the allocated storage space for the items of the container be at least e...
void sortByKey(bool bAscending=true)
Sort all items in the list using associated keys to compare.
type_hash_position m_hash
Container for fast search by key.
virtual ~QxCollection()
Destroy the list.
const_reference_value getFirst() const
Return the first element in the list.
virtual qx::any _at(long index) const
void cloneCollection(QxCollection< Key, Value > *pClone, const QxCollection< Key, Value > &pRef)
bool insert(long index, const Key &key, const Value &value)
Insert element 'value' at position 'index' in the list indexed by 'key'.
bool replace(long index, const Key &key, const Value &value)
Replace the item at index position 'index' with element 'value' indexed by 'key'.
QPair< Key, Value > type_pair_key_value
const_reference_value getLast() const
Return the last element in the list.
bool removeByKey(const Key &key)
Remove the item indexed by 'key' in the list.
bool push_back(const Key &key, const Value &value)
Add element 'value' at the end of the list indexed by 'key'.
bool insert(long index, const QxCollection< Key, Value > &other)
Insert all items of 'other' at the end of the list.
type_list_pair_key_value::iterator iterator
const_iterator end() const
Return a const STL-style iterator pointing to the imaginary item after the last item in the list.
bool insert(const QxCollection< Key, Value > &other)
Add all items of 'other' at the end of the list.
long count() const
Return the number of items in the list (same as 'size()')
virtual bool _remove(long index)
iterator begin()
Return an STL-style iterator pointing to the first item in the list.
const_reverse_iterator rend() const
Return a const reverse STL-style iterator pointing to the imaginary item after the last item in the l...
virtual long _count() const
bool removeFirst()
Remove the first item in the list.
bool swap(long index1, long index2)
Exchange the item at index position 'index1' with the item at index position 'index2'.
void reverse()
Reverse all items in the list.
QxCollection(const QxCollection< Key, Value > &other)
Construct a copy of 'other'.
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 ord...
QMutex m_mutex
Mutex => qx::QxCollection is thread-safe.
bool removeByIndex(long first, long last)
Remove all items from index position 'first' to index position 'last'.
void clear()
Remove all items from the list.
type_list_pair_key_value::const_reverse_iterator const_reverse_iterator
bool push_front(const Key &key, const Value &value)
Insert 'value' at the beginning of the list indexed by 'key'.
QHash< Key, long > type_hash_position
const Value & const_reference_value
QList< type_pair_key_value > type_list_pair_key_value
bool removeByIndex(long index)
Remove the item at index position 'index'.
bool isSameCollection(const QxCollection< Key, Value > *p1, const QxCollection< Key, Value > &p2) const
type_list_pair_key_value::reverse_iterator reverse_iterator
bool empty() const
Return 'true' if the list contains no items; otherwise return 'false'.
bool m_batch
Batch mode to sync internal containers.
const Key & const_reference_key
iterator end()
Return an STL-style iterator pointing to the imaginary item after the last item in the list.
type_list_pair_key_value m_list
Container to keep insertion order.
const_reference_value getByIndex(long index) const
Return the item at index position 'index'.
bool operator!=(const QxCollection< Key, Value > &other) const
Return 'true' if 'other' is not equal to this list, otherwise return 'false'.
bool exist(const Key &key) const
Return 'true' if the list contains an occurrence of 'key', otherwise return 'false' (same as 'contain...
qx::trait::get_class_name<T>::get() : return class name of type T under const char * format,...
#define QX_REGISTER_CLASS_NAME_TEMPLATE_2(className)
qx::trait::is_smart_ptr<T>::value : return true if T is a smart-pointer of boost, Qt or QxOrm librari...
Root namespace for all QxOrm library features.
static bool compareByKeyDescending(const type_pair_key_value &v1, const type_pair_key_value &v2)
static bool compareByValueDescending(const type_pair_key_value &v1, const type_pair_key_value &v2)
static bool compareByKeyAscending(const type_pair_key_value &v1, const type_pair_key_value &v2)
static bool compareByValueAscending(const type_pair_key_value &v1, const type_pair_key_value &v2)
static bool compareByValueAscending(const type_pair_key_value &v1, const type_pair_key_value &v2)
static bool compareByKeyAscending(const type_pair_key_value &v1, const type_pair_key_value &v2)
static bool compareByValueDescending(const type_pair_key_value &v1, const type_pair_key_value &v2)
static bool compareByKeyDescending(const type_pair_key_value &v1, const type_pair_key_value &v2)