blocxx

List.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00038 #ifndef BLOCXX_LIST_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_LIST_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/COWReference.hpp"
00042 #include <list>
00043 
00044 namespace BLOCXX_NAMESPACE
00045 {
00046 
00047 // forward declarations are necessary for template friends.
00048 template<class T> class List;
00049 
00050 template <class T>
00051 inline bool operator==(const List<T>& x, const List<T>& y);
00052 
00053 template <class T>
00054 inline bool operator<(const List<T>& x, const List<T>& y);
00055 
00056    
00060 template<class T> class List
00061 {
00062 private:
00063    typedef std::list<T> L;
00064    COWReference<L> m_impl;
00065 public:
00066    typedef typename L::value_type value_type;
00067    typedef typename L::pointer pointer;
00068    typedef typename L::const_pointer const_pointer;
00069    typedef typename L::reference reference;
00070    typedef typename L::const_reference const_reference;
00071    typedef typename L::size_type size_type;
00072    typedef typename L::difference_type difference_type;
00073    typedef typename L::iterator iterator;
00074    typedef typename L::const_iterator const_iterator;
00075    typedef typename L::reverse_iterator reverse_iterator;
00076    typedef typename L::const_reverse_iterator const_reverse_iterator;
00077 
00081    List() : m_impl(new L) {}
00086    explicit List(L* toWrap) : m_impl(toWrap)
00087    {
00088    }
00094    template<class InputIterator>
00095    List(InputIterator first, InputIterator last)
00096       : m_impl(new L(first, last))
00097    {
00098    }
00106    List(size_type n, const T& value) : m_impl(new L(n, value))
00107    {
00108    }
00116    List(int n, const T& value) : m_impl(new L(n, value))
00117    {
00118    }
00126    List(long n, const T& value) : m_impl(new L(n, value))
00127    {
00128    }
00134    explicit List(size_type n) : m_impl(new L(n))
00135    {
00136    }
00141    L* getImpl()
00142    {
00143       return &*m_impl;
00144    }
00150    iterator begin()
00151    {
00152       return m_impl->begin();
00153    }
00159    const_iterator begin() const
00160    {
00161       return m_impl->begin();
00162    }
00168    iterator end()
00169    {
00170       return m_impl->end();
00171    }
00177    const_iterator end() const
00178    {
00179       return m_impl->end();
00180    }
00186    reverse_iterator rbegin()
00187    {
00188       return m_impl->rbegin();
00189    }
00195    const_reverse_iterator rbegin() const
00196    {
00197       return m_impl->rbegin();
00198    }
00204    reverse_iterator rend()
00205    {
00206       return m_impl->rend();
00207    }
00213    const_reverse_iterator rend() const
00214    {
00215       return m_impl->rend();
00216    }
00220    bool empty() const
00221    {
00222       return m_impl->empty();
00223    }
00227    size_type size() const
00228    {
00229       return m_impl->size();
00230    }
00234    size_type max_size() const
00235    {
00236       return m_impl->max_size();
00237    }
00241    reference front()
00242    {
00243       return m_impl->front();
00244    }
00248    const_reference front() const
00249    {
00250       return m_impl->front();
00251    }
00255    reference back()
00256    {
00257       return m_impl->back();
00258    }
00262    const_reference back() const
00263    {
00264       return m_impl->back();
00265    }
00270    void swap(List<T>& x)
00271    {
00272       m_impl.swap(x.m_impl);
00273    }
00282    iterator insert(iterator position, const T& x)
00283    {
00284       return m_impl->insert(position, x);
00285    }
00293    iterator insert(iterator position)
00294    {
00295       return m_impl->insert(position);
00296    }
00304    template<class InputIterator>
00305    void insert(iterator position, InputIterator first, InputIterator last)
00306    {
00307       m_impl->insert(position, first, last);
00308    }
00318    void insert(iterator pos, size_type n, const T& x)
00319    {
00320       m_impl->insert(pos, n, x);
00321    }
00331    void insert(iterator pos, int n, const T& x)
00332    {
00333       m_impl->insert(pos, n, x);
00334    }
00344    void insert(iterator pos, long n, const T& x)
00345    {
00346       m_impl->insert(pos, n, x);
00347    }
00353    void push_front(const T& x)
00354    {
00355       m_impl->push_front(x);
00356    }
00362    void push_back(const T& x)
00363    {
00364       m_impl->push_back(x);
00365    }
00373    iterator erase(iterator position)
00374    {
00375       return m_impl->erase(position);
00376    }
00386    iterator erase(iterator first, iterator last)
00387    {
00388       return m_impl->erase(first, last);
00389    }
00396    void resize(size_type new_size, const T& x)
00397    {
00398       m_impl->resize(new_size, x);
00399    }
00406    void resize(size_type new_size)
00407    {
00408       m_impl->resize(new_size);
00409    }
00414    void clear()
00415    {
00416       m_impl->clear();
00417    }
00427    const_iterator find(const T &x, const_iterator first,
00428                                    const_iterator last) const
00429    {
00430       for( ; first != end(); ++first)
00431       {
00432          if( x == *first)
00433             return first;
00434          if( first == last)
00435             break;
00436       }
00437       return end();
00438    }
00445    const_iterator find(const T &x) const
00446    {
00447       return find(x, begin(), end());
00448    }
00458    iterator       find(const T &x, iterator first, iterator last)
00459    {
00460       for( ; first != end(); ++first)
00461       {
00462          if( x == *first)
00463             return first;
00464          if( first == last)
00465             break;
00466       }
00467       return end();
00468    }
00475    iterator       find(const T &x)
00476    {
00477       return find(x, begin(), end());
00478    }
00488    bool contains(const T& x, const_iterator first,
00489                              const_iterator last) const
00490    {
00491       return find(x, first, last) != end();
00492    }
00498    bool contains(const T& x) const
00499    {
00500       return find(x, begin(), end()) != end();
00501    }
00505    void pop_front()
00506    {
00507       m_impl->pop_front();
00508    }
00512    void pop_back()
00513    {
00514       m_impl->pop_back();
00515    }
00522    void splice(iterator position, List& x)
00523    {
00524       m_impl->splice(position, *x.m_impl);
00525    }
00534    void splice(iterator position, List& x, iterator i)
00535    {
00536       m_impl->splice(position, *x.m_impl, i);
00537    }
00547    void splice(iterator position, List& x, iterator first, iterator last)
00548    {
00549       m_impl->splice(position, *x.m_impl, first, last);
00550    }
00555    void remove(const T& value)
00556    {
00557       m_impl->remove(value);
00558    }
00562    void unique()
00563    {
00564       m_impl->unique();
00565    }
00571    void merge(List& x)
00572    {
00573       m_impl->merge(*x.m_impl);
00574    }
00578    void reverse()
00579    {
00580       m_impl->reverse();
00581    }
00585    void sort()
00586    {
00587       m_impl->sort();
00588    }
00594    template<class Predicate> void remove_if (Predicate p)
00595    {
00596       m_impl->remove_if (p);
00597    }
00603    template<class BinaryPredicate> void unique(BinaryPredicate bp)
00604    {
00605       m_impl->unique(bp);
00606    }
00612    template<class StrictWeakOrdering> void merge(List& x, StrictWeakOrdering swo)
00613    {
00614       m_impl->merge(*x.m_impl, swo);
00615    }
00620    template<class StrictWeakOrdering> void sort(StrictWeakOrdering swo)
00621    {
00622       m_impl->sort(swo);
00623    }
00632    friend bool operator== <>(const List<T>& x, const List<T>& y);
00640    friend bool operator< <>(const List<T>& x, const List<T>& y);
00641 };
00642 template <class T>
00643 inline bool operator==(const List<T>& x, const List<T>& y)
00644 {
00645    return *x.m_impl == *y.m_impl;
00646 }
00647 template <class T>
00648 inline bool operator<(const List<T>& x, const List<T>& y)
00649 {
00650    return *x.m_impl < *y.m_impl;
00651 }
00652 template <class T>
00653 inline void swap(List<T>& x, List<T>& y)
00654 {
00655    x.swap(y);
00656 }
00657 template <class T>
00658 std::list<T>* COWReferenceClone(std::list<T>* obj)
00659 {
00660    return new std::list<T>(*obj);
00661 }
00662 
00663 } // end namespace BLOCXX_NAMESPACE
00664 
00665 #endif