Claw  1.7.3
multi_type_map_visitor.tpp
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
25 /**
26  * \file multi_type_map_visitor.tpp
27  * \brief Implementation of the claw::multi_type_map_visitor class.
28  * \author Julien Jorge
29  */
30 
31 namespace claw
32 {
33  /**
34  * \brief This class goes through all entries of a given type in a
35  * multi_type_map and apply a function to them.
36  * \author Julien Jorge
37  */
38  template<typename Type>
39  class multi_type_map_visitor_process
40  {
41  public:
42  template<typename Key, typename TailType, typename Function>
43  void execute
44  ( multi_type_map< Key, claw::meta::type_list<Type, TailType> >& m,
45  Function f )
46  {
47  typedef claw::meta::type_list<Type, TailType> type_list_type;
48  typedef multi_type_map<Key, type_list_type> map_type;
49  typedef typename map_type::template iterator<Type>::type iterator_type;
50 
51  iterator_type it( m.template begin<Type>() );
52  const iterator_type eit( m.template end<Type>() );
53 
54  while ( it!=eit )
55  {
56  iterator_type current(it);
57  ++it;
58  f(current->first, current->second);
59  }
60  } // execute()
61 
62  }; // class multi_type_map_visitor_process
63 
64  /**
65  * \brief This class goes through all entries in a multi_type_map and apply a
66  * function to them.
67  * \author Julien Jorge
68  */
69  template<typename Key, typename TypeList>
70  class multi_type_map_visitor_rec;
71 
72  /**
73  * \brief Specialization of multi_type_map_visitor_rec for an empty type list.
74  * \author Julien Jorge
75  */
76  template<typename Key>
77  class multi_type_map_visitor_rec<Key, claw::meta::no_type>
78  {
79  public:
80  template<typename Function>
81  void execute( multi_type_map<Key, claw::meta::no_type>& vars, Function f )
82  {
83  // nothing to do.
84  } // execute()
85 
86  }; // class multi_type_map_visitor
87 
88  /**
89  * \brief Specialization of multi_type_map_visitor_rec for a non empty type
90  * list.
91  * \author Julien Jorge
92  */
93  template<typename KeyType, typename HeadType, typename TailType>
94  class multi_type_map_visitor_rec
95  < KeyType, claw::meta::type_list<HeadType, TailType> >
96  {
97  public:
98  template<typename Function>
99  void execute
100  ( multi_type_map< KeyType, claw::meta::type_list<HeadType, TailType> >& m,
101  Function f )
102  {
103  multi_type_map_visitor_process<HeadType> process;
104  multi_type_map_visitor_rec<KeyType, TailType> rec_call;
105 
106  process.execute( m, f );
107  rec_call.execute( m, f );
108  } // execute()
109 
110  }; // class multi_type_map_visitor_rec
111 
112 } // namespce claw
113 
114 /*----------------------------------------------------------------------------*/
115 /**
116  * \brief Execute the visitor.
117  * \param m The map to visit.
118  * \param f The function to apply to the entries.
119  */
120 template<typename Key, typename TypeList, typename Function>
121 void claw::multi_type_map_visitor::run
122 ( multi_type_map<Key, TypeList>& m, Function f ) const
123 {
124  multi_type_map_visitor_rec<Key, TypeList> rec_call;
125  rec_call.execute( m, f );
126 } // multi_type_map_visitor::run()