QxOrm  1.2.3
C++ Object Relational Mapping library
QxCollection.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _QX_COLLECTION_H_
00027 #define _QX_COLLECTION_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #ifdef _MSC_VER
00041 #pragma warning(push)
00042 #pragma warning(disable:4996)
00043 #pragma warning(disable:4503)
00044 #endif // _MSC_VER
00045 
00046 #include <boost/multi_index_container.hpp>
00047 #include <boost/multi_index/member.hpp>
00048 #include <boost/multi_index/random_access_index.hpp>
00049 #include <boost/multi_index/hashed_index.hpp>
00050 #include <boost/type_traits/is_pointer.hpp>
00051 
00052 #include <QtCore/qpair.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 boost::multi_index::member<type_pair_key_value, Key, & type_pair_key_value::first> type_member;
00157    typedef boost::multi_index::random_access<> type_first_index;
00158    typedef boost::multi_index::hashed_unique<type_member> type_second_index;
00159    typedef boost::multi_index::indexed_by<type_first_index, type_second_index> type_indexed_by;
00160    typedef boost::multi_index::multi_index_container<type_pair_key_value, type_indexed_by> type_container;
00161 
00162    typedef typename type_container::template nth_index<0>::type type_index_rand;
00163    typedef typename type_container::template nth_index<1>::type type_index_hash;
00164 
00165 public:
00166 
00167    typedef typename type_index_rand::iterator iterator;
00168    typedef typename type_index_rand::const_iterator const_iterator;
00169    typedef typename type_index_rand::reverse_iterator reverse_iterator;
00170    typedef typename type_index_rand::const_reverse_iterator const_reverse_iterator;
00171 
00172    typedef const Key & const_reference_key;
00173    typedef const Value & const_reference_value;
00174 
00175 protected:
00176 
00177    type_container m_qxCollection;   
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    inline iterator begin();                  
00190    inline iterator end();                    
00191    inline const_iterator begin() const;      
00192    inline const_iterator end() const;        
00193 
00194    inline reverse_iterator rbegin();               
00195    inline reverse_iterator rend();                 
00196    inline const_reverse_iterator rbegin() const;   
00197    inline const_reverse_iterator rend() const;     
00198 
00199    inline long capacity() const;                   
00200    inline void reserve(long size);                 
00201    inline void reverse();                          
00202    inline void clear();                            
00203    inline long count() const;                      
00204    inline long size() const;                       
00205    inline bool contains(const Key & key) const;    
00206    inline bool exist(const Key & key) const;       
00207    inline bool empty() const;                      
00208 
00209    inline bool push_back(const Key & key, const Value & value);               
00210    inline bool push_front(const Key & key, const Value & value);              
00211    inline bool insert(const Key & key, const Value & value);                  
00212    inline bool insert(long index, const Key & key, const Value & value);      
00213    inline bool insert(const QxCollection<Key, Value> & other);                
00214    inline bool insert(long index, const QxCollection<Key, Value> & other);    
00215    inline bool replace(long index, const Key & key, const Value & value);     
00216    inline bool swap(long index1, long index2);                                
00217    inline bool move(long indexFrom, long indexTo);                            
00218 
00219    inline bool removeByKey(const Key & key);             
00220    inline bool removeByIndex(long index);                
00221    inline bool removeByIndex(long first, long last);     
00222    inline bool removeFirst();                            
00223    inline bool removeLast();                             
00224 
00225    inline const_reference_value getByKey(const Key & key) const;     
00226    inline const_reference_value getByIndex(long index) const;        
00227    inline const_reference_value getFirst() const;                    
00228    inline const_reference_value getLast() const;                     
00229    inline const_reference_key getKeyByIndex(long index) const;       
00230 
00231    inline void sortByKey(bool bAscending = true);        
00232    inline void sortByValue(bool bAscending = true);      
00233 
00234    template <typename Compare>
00235    inline void sort(Compare comp) { m_qxCollection.template get<0>().sort(comp); }
00236 
00237 private:
00238 
00239    void cloneCollection(QxCollection<Key, Value> * pClone, const QxCollection<Key, Value> & pRef);
00240    bool isSameCollection(const QxCollection<Key, Value> * p1, const QxCollection<Key, Value> & p2) const;
00241 
00242    template <bool bIsPointer /* = false */, int dummy>
00243    struct compareKeyValue
00244    {
00245       static inline bool compareByKeyAscending(const type_pair_key_value & v1, const type_pair_key_value & v2)    { return (v1.first < v2.first); }
00246       static inline bool compareByKeyDescending(const type_pair_key_value & v1, const type_pair_key_value & v2)   { return (v1.first > v2.first); }
00247       static inline bool compareByValueAscending(const type_pair_key_value & v1, const type_pair_key_value & v2)  { return (v1.second < v2.second); }
00248       static inline bool compareByValueDescending(const type_pair_key_value & v1, const type_pair_key_value & v2) { return (v1.second > v2.second); }
00249    };
00250 
00251    template <int dummy>
00252    struct compareKeyValue<true, dummy>
00253    {
00254       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); }
00255       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); }
00256       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); }
00257       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); }
00258    };
00259 
00260 public:
00261 
00262    virtual long _count() const               { return this->count(); }
00263    virtual void _clear()                     { this->clear(); }
00264    virtual bool _remove(long index)          { return this->removeByIndex(index); }
00265    virtual boost::any _at(long index) const  { Value val = this->getByIndex(index); return boost::any(val); }
00266 
00267 };
00268 
00269 } // namespace qx
00270 
00271 #include "../../inl/QxCollection/QxCollection.inl"
00272 
00273 QX_REGISTER_CLASS_NAME_TEMPLATE_2(qx::QxCollection)
00274 
00275 #ifdef _MSC_VER
00276 #pragma warning(pop)
00277 #endif // _MSC_VER
00278 
00279 #endif // _QX_COLLECTION_H_