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