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-2010 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@yahoo.fr
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 const value_type& get( const map_type& self, const key_type& k )
00052     {
00053       CLAW_PRECOND( exists(self, k) );
00054       return self.m_data.find(k)->second;
00055     }
00056 
00057     static void set( map_type& self, const key_type& k, const value_type& v )
00058     {
00059       self.m_data[k] = v;
00060     }
00061 
00062     static bool exists( const map_type& self, const key_type& k )
00063     {
00064       return self.m_data.find(k) != self.m_data.end();
00065     }
00066 
00067     static iterator begin( map_type& self )
00068     {
00069       return self.m_data.begin();
00070     }
00071 
00072     static iterator end( map_type& self )
00073     {
00074       return self.m_data.end();
00075     }
00076 
00077     static const_iterator begin( const map_type& self )
00078     {
00079       return self.m_data.begin();
00080     }
00081 
00082     static const_iterator end( const map_type& self )
00083     {
00084       return self.m_data.end();
00085     }
00086 
00087   }; // class multi_type_map_wrapper
00088 
00089   /*
00090    * Here is the implementation of multi_type_map_wrapper for the case where the
00091    * ValueType is not the first type in the type list of the map.
00092    */
00093   template<typename ValueType, typename Key, typename Head, typename Tail>
00094   class multi_type_map_wrapper
00095   < ValueType, multi_type_map< Key, meta::type_list<Head, Tail> > >:
00096     public multi_type_map_wrapper< ValueType, multi_type_map<Key, Tail> >
00097   {
00098 
00099   }; // class multi_type_map_wrapper
00100 
00101 } // namespace claw
00102 
00103 
00104 
00105 
00106 /*----------------------------------------------------------------------------*/
00111 template<typename Key, typename Head, typename Tail>
00112 template<typename ValueType>
00113 const ValueType&
00114 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::get
00115 ( const key_type& k ) const
00116 {
00117   return multi_type_map_wrapper<ValueType, self_type>::get(*this, k);
00118 } // multi_type_map::get()
00119 
00120 /*----------------------------------------------------------------------------*/
00126 template<typename Key, typename Head, typename Tail>
00127 template<typename ValueType>
00128 void claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::set
00129 ( const key_type& k, const ValueType& v )
00130 {
00131   return multi_type_map_wrapper<ValueType, self_type>::set(*this, k, v);
00132 } // multi_type_map::set()
00133 
00134 /*----------------------------------------------------------------------------*/
00139 template<typename Key, typename Head, typename Tail>
00140 template<typename ValueType>
00141 bool claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::exists
00142 ( const key_type& k ) const
00143 {
00144   return multi_type_map_wrapper<ValueType, self_type>::exists(*this, k);
00145 } // multi_type_map::exists()
00146 
00147 /*----------------------------------------------------------------------------*/
00151 template<typename Key, typename Head, typename Tail>
00152 template<typename ValueType>
00153 typename claw::multi_type_map
00154 < Key, claw::meta::type_list<Head, Tail> >::template iterator<ValueType>::type
00155 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::begin()
00156 {
00157   return multi_type_map_wrapper<ValueType, self_type>::begin(*this);
00158 } // multi_type_map::begin()
00159 
00160 /*----------------------------------------------------------------------------*/
00164 template<typename Key, typename Head, typename Tail>
00165 template<typename ValueType>
00166 typename claw::multi_type_map
00167 < Key, claw::meta::type_list<Head, Tail> >::template iterator<ValueType>::type
00168 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::end()
00169 {
00170   return multi_type_map_wrapper<ValueType, self_type>::end(*this);
00171 } // multi_type_map::end()
00172 
00173 /*----------------------------------------------------------------------------*/
00177 template<typename Key, typename Head, typename Tail>
00178 template<typename ValueType>
00179 typename
00180 claw::multi_type_map
00181 < Key, claw::meta::type_list<Head, Tail> >
00182 ::template iterator<ValueType>::const_type
00183 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::begin() const
00184 {
00185   return multi_type_map_wrapper<ValueType, self_type>::begin(*this);
00186 } // multi_type_map::begin()
00187 
00188 /*----------------------------------------------------------------------------*/
00192 template<typename Key, typename Head, typename Tail>
00193 template<typename ValueType>
00194 typename
00195 claw::multi_type_map
00196 < Key, claw::meta::type_list<Head, Tail> >
00197 ::template iterator<ValueType>::const_type
00198 claw::multi_type_map< Key, claw::meta::type_list<Head, Tail> >::end() const
00199 {
00200   return multi_type_map_wrapper<ValueType, self_type>::end(*this);
00201 } // multi_type_map::end()