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