OW_Map.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #ifndef OW_MAP_HPP_INCLUDE_GUARD_
00037 #define OW_MAP_HPP_INCLUDE_GUARD_
00038 #include "OW_config.h"
00039 #include "OW_COWReference.hpp"
00040 #include <map>
00041 #include <functional>
00042 
00043 // This class is a wrapper around std::map<> and adds COW capabilities.
00044 namespace OW_NAMESPACE
00045 {
00046 
00047 
00048 // forward declarations are necessary for template friends.
00049 template<class Key, class T, class Compare > class Map;
00050 
00051 template<class Key, class T, class Compare>
00052 inline bool operator==(const Map<Key, T, Compare>& x,
00053    const Map<Key, T, Compare>& y);
00054 
00055 template<class Key, class T, class Compare>
00056 inline bool operator<(const Map<Key, T, Compare>& x,
00057    const Map<Key, T, Compare>& y);
00058 
00059 
00060 template<class Key, class T, class Compare = std::less<Key> > class Map
00061 {
00062    typedef std::map<Key, T, Compare > M;
00063    COWReference<M> m_impl;
00064 public:
00065    typedef typename M::key_type key_type;
00066    typedef typename M::mapped_type mapped_type;
00067    typedef typename M::value_type value_type;
00068    typedef typename M::key_compare key_compare;
00069    typedef typename M::value_compare value_compare;
00070    typedef typename M::pointer pointer;
00071    typedef typename M::const_pointer const_pointer;
00072    typedef typename M::reference reference;
00073    typedef typename M::const_reference const_reference;
00074    typedef typename M::iterator iterator;
00075    typedef typename M::const_iterator const_iterator;
00076    typedef typename M::reverse_iterator reverse_iterator;
00077    typedef typename M::const_reverse_iterator const_reverse_iterator;
00078    typedef typename M::size_type size_type;
00079    typedef typename M::difference_type difference_type;
00080    Map() : m_impl(new M) {  }
00081    explicit Map(M* toWrap) : m_impl(toWrap)
00082       { }
00083    explicit Map(const Compare& comp)
00084       : m_impl(new M(comp)) {  }
00085    template <class InputIterator>
00086    Map(InputIterator first, InputIterator last) :
00087       m_impl(new M(first, last))
00088    {
00089    }
00090    template <class InputIterator>
00091    Map(InputIterator first, InputIterator last, const Compare& comp) :
00092       m_impl(new M(first, last, comp))
00093    {
00094    }
00095    M* getImpl()
00096    {
00097       return &*m_impl;
00098    }
00099    key_compare key_comp() const
00100    {
00101       return m_impl->key_comp();
00102    }
00103    value_compare value_comp() const
00104    {
00105       return m_impl->value_comp();
00106    }
00107    iterator begin()
00108    {
00109       return m_impl->begin();
00110    }
00111    const_iterator begin() const
00112    {
00113       return m_impl->begin();
00114    }
00115    iterator end()
00116    {
00117       return m_impl->end();
00118    }
00119    const_iterator end() const
00120    {
00121       return m_impl->end();
00122    }
00123    reverse_iterator rbegin()
00124    {
00125       return m_impl->rbegin();
00126    }
00127    const_reverse_iterator rbegin() const
00128    {
00129       return m_impl->rbegin();
00130    }
00131    reverse_iterator rend()
00132    {
00133       return m_impl->rend();
00134    }
00135    const_reverse_iterator rend() const
00136    {
00137       return m_impl->rend();
00138    }
00139    bool empty() const
00140    {
00141       return m_impl->empty();
00142    }
00143    size_type size() const
00144    {
00145       return m_impl->size();
00146    }
00147    size_type max_size() const
00148    {
00149       return m_impl->max_size();
00150    }
00151    T& operator[](const key_type& k)
00152    {
00153       return m_impl->operator[](k);
00154    }
00155    void swap(Map<Key, T, Compare>& x)
00156    {
00157       m_impl.swap(x.m_impl);
00158    }
00159    std::pair<iterator, bool> insert(const value_type& x)
00160    {
00161       return m_impl->insert(x);
00162    }
00163    iterator insert(iterator position, const value_type& x)
00164    {
00165       return m_impl->insert(position, x);
00166    }
00167    template <class InputIterator>
00168    void insert(InputIterator first, InputIterator last)
00169    {
00170       m_impl->insert(first, last);
00171    }
00172    void erase(iterator position)
00173    {
00174       m_impl->erase(position);
00175    }
00176    size_type erase(const key_type& x)
00177    {
00178       return m_impl->erase(x);
00179    }
00180    void erase(iterator first, iterator last)
00181    {
00182       m_impl->erase(first, last);
00183    }
00184    void clear()
00185    {
00186       m_impl->clear();
00187    }
00188    iterator find(const key_type& x)
00189    {
00190       return m_impl->find(x);
00191    }
00192    const_iterator find(const key_type& x) const
00193    {
00194       return m_impl->find(x);
00195    }
00196    size_type count(const key_type& x) const
00197    {
00198       return m_impl->count(x);
00199    }
00200    iterator lower_bound(const key_type& x)
00201    {
00202       return m_impl->lower_bound(x);
00203    }
00204    const_iterator lower_bound(const key_type& x) const
00205    {
00206       return m_impl->lower_bound(x);
00207    }
00208    iterator upper_bound(const key_type& x)
00209    {
00210       return m_impl->upper_bound(x);
00211    }
00212    const_iterator upper_bound(const key_type& x) const
00213    {
00214       return m_impl->upper_bound(x);
00215    }
00216    std::pair<iterator, iterator> equal_range(const key_type& x)
00217    {
00218       return m_impl->equal_range(x);
00219    }
00220    std::pair<const_iterator, const_iterator>
00221       equal_range(const key_type& x) const
00222    {
00223       return m_impl->equal_range(x);
00224    }
00225    friend bool operator== <>(const Map<Key, T, Compare>& x,
00226       const Map<Key, T, Compare>& y);
00227    friend bool operator< <>(const Map<Key, T, Compare>& x,
00228       const Map<Key, T, Compare>& y);
00229 };
00230 template <class Key, class T, class Compare>
00231 std::map<Key, T, Compare>* COWReferenceClone(std::map<Key, T, Compare>* obj)
00232 {
00233    return new std::map<Key, T, Compare>(*obj);
00234 }
00235 template<class Key, class T, class Compare>
00236 inline bool operator==(const Map<Key, T, Compare>& x,
00237    const Map<Key, T, Compare>& y)
00238 {
00239    return *x.m_impl == *y.m_impl;
00240 }
00241 template<class Key, class T, class Compare>
00242 inline bool operator<(const Map<Key, T, Compare>& x,
00243    const Map<Key, T, Compare>& y)
00244 {
00245    return *x.m_impl < *y.m_impl;
00246 }
00247 template <class Key, class T, class Compare>
00248 inline void swap(Map<Key, T, Compare>& x,
00249    Map<Key, T, Compare>& y)
00250 {
00251    x.swap(y);
00252 }
00253 
00254 } // end namespace OW_NAMESPACE
00255 
00256 #endif

Generated on Thu Feb 9 08:48:05 2006 for openwbem by  doxygen 1.4.6