Claw  1.7.0
multi_type_map.tpp
Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2011 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien.jorge@gamned.org
00024 */
00031 #include <claw/assert.hpp>
00032 #include <claw/meta/same_type.hpp>
00033 
00034 namespace claw
00035 {
00036   /*
00037    * Here is the implementation of multi_type_map_wrapper for the case where the
00038    * ValueType is the first type in the type list of the map.
00039    */
00040   template<typename Key, typename Head, typename Tail>
00041   class multi_type_map_wrapper
00042   < Head, multi_type_map<Key, meta::type_list<Head, Tail> > >
00043   {
00044     typedef Key key_type;
00045     typedef Head value_type;
00046     typedef multi_type_map<Key, meta::type_list<Head, Tail> > map_type;
00047     typedef typename map_type::iterator_type iterator;
00048     typedef typename map_type::const_iterator_type const_iterator;
00049 
00050   public:
00051     static void erase( map_type& self, iterator it )
00052     {
00053       self.m_data.erase(it);
00054     }
00055 
00056     static std::size_t erase( map_type& self, const key_type& k )
00057     {
00058       return self.m_data.erase(k);
00059     }
00060 
00061     static const value_type& get( const map_type& self, const key_type& k )
00062     {
00063       CLAW_PRECOND( exists(self, k) );
00064       return self.m_data.find(k)->second;
00065     }
00066 
00067     static value_type& get( map_type& self, const key_type& k )
00068     {
00069       CLAW_PRECOND( exists(self, k) );
00070       return self.m_data.find(k)->second;
00071     }
00072 
00073     static void set( map_type& self, const key_type& k, const value_type& v )
00074     {
00075       self.m_data[k] = v;
00076     }
00077 
00078     static bool exists( const map_type& self, const key_type& k )
00079     {
00080       return self.m_data.find(k) != self.m_data.end();
00081     }
00082 
00083     static iterator begin( map_type& self )
00084     {
00085       return self.m_data.begin();
00086     }
00087 
00088     static iterator end( map_type& self )
00089     {
00090       return self.m_data.end();
00091     }
00092 
00093     static const_iterator begin( const map_type& self )
00094     {
00095       return self.m_data.begin();
00096     }
00097 
00098     static const_iterator end( const map_type& self )
00099     {
00100       return self.m_data.end();
00101     }
00102 
00103   }; // class multi_type_map_wrapper
00104 
00105   /*
00106    * Here is the implementation of multi_type_map_wrapper for the case where the
00107    * ValueType is not the first type in the type list of the map.
00108    */
00109   template<typename ValueType, typename Key, typename Head, typename Tail>
00110   class multi_type_map_wrapper
00111   < ValueType, multi_type_map< Key, meta::type_list<Head, Tail> > >:
00112     public multi_type_map_wrapper< ValueType, multi_type_map<Key, Tail> >
00113   {
00114 
00115   }; // class multi_type_map_wrapper
00116 
00117   /*
00118    * Here is the implementation of multi_type_map_helper for the case where the
00119    * ValueType is the first type in the type list of the map.
00120    */
00121   template<typename Key, typename Head, typename Tail>
00122   class multi_type_map_helper
00123   < multi_type_map<Key, meta::type_list<Head, Tail> > >
00124   {
00125     typedef Key key_type;
00126     typedef Head value_type;
00127     typedef multi_type_map<Key, meta::type_list<Head, Tail> > map_type;
00128     typedef typename map_type::iterator_type iterator;
00129     typedef typename map_type::const_iterator_type const_iterator;
00130 
00131   public:
00132     static void set( map_type& self, const map_type& that )
00133     {
00134       for ( const_iterator it=that.template begin<value_type>();
00135       it!=that.template end<value_type>(); ++it )
00136   self.template set<Head>( it->first, it->second );
00137 
00138       multi_type_map_helper< multi_type_map<Key, Tail> >::set( self, that );
00139     } // size()
00140 
00141     static std::size_t size( const map_type& self )
00142     {
00143       return self.m_data.size()
00144   + multi_type_map_helper< multi_type_map<Key, Tail> >::size( self );
00145     } // size()
00146 
00147   }; // class multi_type_map_helper
00148 
00149   /*
00150    * Here is the implementation of multi_type_map_helper that stops the
00151    * recursivity.
00152    */
00153   template<typename Key>
00154   class multi_type_map_helper
00155   < multi_type_map< Key, claw::meta::no_type > >
00156   {
00157   private:
00158     typedef multi_type_map<Key, claw::meta::no_type> map_type;
00159 
00160   public:
00161     static void set( map_type& self, const map_type& that )
00162     {
00163       // nothing to do
00164     } // set()
00165 
00166     static std::size_t size( const map_type& self )
00167     {
00168       return 0;
00169     } // size()
00170 
00171   }; // class multi_type_map_helper
00172 } // namespace claw
00173 
00174 
00175 
00176 
00177 /*----------------------------------------------------------------------------*/
00183 template<typename Key, typename Head, typename Tail>
00184 template<typename ValueType>
00185 void
00186 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::erase
00187 ( typename iterator<ValueType>::type it )
00188 {
00189   multi_type_map_wrapper<ValueType, self_type>::erase(*this, it);
00190 } // multi_type_map::erase()
00191 
00192 /*----------------------------------------------------------------------------*/
00198 template<typename Key, typename Head, typename Tail>
00199 template<typename ValueType>
00200 std::size_t
00201 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::erase
00202 ( const key_type& k )
00203 {
00204   return multi_type_map_wrapper<ValueType, self_type>::erase(*this, k);
00205 } // multi_type_map::erase()
00206 
00207 /*----------------------------------------------------------------------------*/
00212 template<typename Key, typename Head, typename Tail>
00213 template<typename ValueType>
00214 const ValueType&
00215 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::get
00216 ( const key_type& k ) const
00217 {
00218   return multi_type_map_wrapper<ValueType, self_type>::get(*this, k);
00219 } // multi_type_map::get()
00220 
00221 /*----------------------------------------------------------------------------*/
00226 template<typename Key, typename Head, typename Tail>
00227 template<typename ValueType>
00228 ValueType& claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::get
00229 ( const key_type& k )
00230 {
00231   return multi_type_map_wrapper<ValueType, self_type>::get(*this, k);
00232 } // multi_type_map::get()
00233 
00234 /*----------------------------------------------------------------------------*/
00240 template<typename Key, typename Head, typename Tail>
00241 template<typename ValueType>
00242 void claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::set
00243 ( const key_type& k, const ValueType& v )
00244 {
00245   return multi_type_map_wrapper<ValueType, self_type>::set(*this, k, v);
00246 } // multi_type_map::set()
00247 
00248 /*----------------------------------------------------------------------------*/
00254 template<typename Key, typename Head, typename Tail>
00255 void claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::set
00256 ( const self_type& m )
00257 {
00258   multi_type_map_helper<self_type>::set(*this, m);
00259 } // multi_type_map::set()
00260 
00261 /*----------------------------------------------------------------------------*/
00266 template<typename Key, typename Head, typename Tail>
00267 template<typename ValueType>
00268 bool claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::exists
00269 ( const key_type& k ) const
00270 {
00271   return multi_type_map_wrapper<ValueType, self_type>::exists(*this, k);
00272 } // multi_type_map::exists()
00273 
00274 /*----------------------------------------------------------------------------*/
00278 template<typename Key, typename Head, typename Tail>
00279 std::size_t
00280 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::size() const
00281 {
00282   return multi_type_map_helper<self_type>::size(*this);
00283 } // multi_type_map::size()
00284 
00285 /*----------------------------------------------------------------------------*/
00289 template<typename Key, typename Head, typename Tail>
00290 template<typename ValueType>
00291 typename claw::multi_type_map
00292 < Key, claw::meta::type_list<Head, Tail> >::template iterator<ValueType>::type
00293 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::begin()
00294 {
00295   return multi_type_map_wrapper<ValueType, self_type>::begin(*this);
00296 } // multi_type_map::begin()
00297 
00298 /*----------------------------------------------------------------------------*/
00302 template<typename Key, typename Head, typename Tail>
00303 template<typename ValueType>
00304 typename claw::multi_type_map
00305 < Key, claw::meta::type_list<Head, Tail> >::template iterator<ValueType>::type
00306 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::end()
00307 {
00308   return multi_type_map_wrapper<ValueType, self_type>::end(*this);
00309 } // multi_type_map::end()
00310 
00311 /*----------------------------------------------------------------------------*/
00315 template<typename Key, typename Head, typename Tail>
00316 template<typename ValueType>
00317 typename
00318 claw::multi_type_map
00319 < Key, claw::meta::type_list<Head, Tail> >
00320 ::template iterator<ValueType>::const_type
00321 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::begin() const
00322 {
00323   return multi_type_map_wrapper<ValueType, self_type>::begin(*this);
00324 } // multi_type_map::begin()
00325 
00326 /*----------------------------------------------------------------------------*/
00330 template<typename Key, typename Head, typename Tail>
00331 template<typename ValueType>
00332 typename
00333 claw::multi_type_map
00334 < Key, claw::meta::type_list<Head, Tail> >
00335 ::template iterator<ValueType>::const_type
00336 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::end() const
00337 {
00338   return multi_type_map_wrapper<ValueType, self_type>::end(*this);
00339 } // multi_type_map::end()