Crazy Eddie's GUI System  ${CEGUI_VERSION}
map.hpp
1 // This file has been generated by Py++.
2 
3 // Copyright (c) 2003 Raoul M. Gough
4 //
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
7 // at http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // Header file map.hpp
10 //
11 // Indexing algorithms support for std::map instances
12 //
13 // History
14 // =======
15 // 2003/10/28 rmg File creation from algo_selector.hpp
16 // 2008/12/08 Roman Change indexing suite layout
17 // 2010/04/29 Roman Adding "__len__" method
18 //
19 // $Id: map.hpp,v 1.1.2.6 2004/02/08 18:57:42 raoulgough Exp $
20 //
21 
22 #ifndef BOOST_PYTHON_INDEXING_MAP_HPP
23 #define BOOST_PYTHON_INDEXING_MAP_HPP
24 
25 #include <indexing_suite/container_traits.hpp>
26 #include <indexing_suite/container_suite.hpp>
27 #include <indexing_suite/algorithms.hpp>
28 #include <boost/detail/workaround.hpp>
29 #include <map>
30 #include <indexing_suite/pair.hpp>
31 
32 namespace boost { namespace python { namespace indexing {
34  // ContainerTraits implementation for std::map instances
36 
37  template<typename Container>
38  class map_traits : public base_container_traits<Container>
39  {
41 
42  public:
43 # if BOOST_WORKAROUND (BOOST_MSVC, <= 1200)
44  // MSVC6 has a nonstandard name for mapped_type in std::map
45  typedef typename Container::referent_type value_type;
46 # else
47  typedef typename Container::mapped_type value_type;
48 # endif
49  typedef value_type & reference;
50  typedef typename Container::key_type index_type; // operator[]
51  typedef typename Container::key_type key_type; // find, count, ...
52 
53  typedef typename BOOST_PYTHON_INDEXING_CALL_TRAITS <value_type>::param_type
54  value_param;
55  typedef typename BOOST_PYTHON_INDEXING_CALL_TRAITS <key_type>::param_type
56  key_param;
57  typedef typename BOOST_PYTHON_INDEXING_CALL_TRAITS <index_type>::param_type
58  index_param;
59 
60  BOOST_STATIC_CONSTANT(
61  method_set_type,
62  supported_methods = (
63  method_iter
64 
65  | method_getitem
66  | method_contains
67  | method_count
68  | method_has_key
69  | method_len
70 
72  base_class::is_mutable,
73  method_setitem
74  | method_delitem
75  | method_insert
76  >::value
77  ));
78  };
79 
81  // Algorithms implementation for std::map instances
83 
84  template<typename ContainerTraits, typename Ovr = detail::no_override>
86  : public assoc_algorithms
87  <ContainerTraits,
88  typename detail::maybe_override
89  <map_algorithms<ContainerTraits, Ovr>, Ovr>
90  ::type>
91  {
93  typedef typename detail::maybe_override<self_type, Ovr>::type most_derived;
95 
96  public:
97  typedef typename Parent::container container;
98  typedef typename Parent::reference reference;
99  typedef typename Parent::index_param index_param;
100  typedef typename Parent::value_param value_param;
101 
102  static reference get (container &, index_param);
103  // Version to return only the mapped type
104 
105  static boost::python::list keys( container & );
106 
107  static void assign (container &, index_param, value_param);
108  static void insert (container &, index_param, value_param);
109 
110  template<typename PythonClass, typename Policy>
111  static void visit_container_class( PythonClass &pyClass, Policy const &policy)
112  {
113  ContainerTraits::visit_container_class (pyClass, policy);
114  pyClass.def( "keys", &self_type::keys );
115 
116  typedef BOOST_DEDUCED_TYPENAME most_derived::container::value_type value_type;
117  mapping::register_value_type< PythonClass, value_type, Policy >( pyClass );
118  //now we can expose iterators functionality
119  pyClass.def( "__iter__", python::iterator< BOOST_DEDUCED_TYPENAME most_derived::container >() );
120  }
121 
122  };
123 
124 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
125  namespace detail {
127  // algorithms support for std::map instances
129 
130  template <class Key, class T, class Compare, class Allocator>
131  class algorithms_selector<std::map<Key, T, Compare, Allocator> >
132  {
133  typedef std::map<Key, T, Compare, Allocator> Container;
134 
137 
138  public:
141  };
142 
144  // algorithms support for std::multimap instances
146 
147  template <class Key, class T, class Compare, class Allocator>
148  class algorithms_selector<std::multimap<Key, T, Compare, Allocator> >
149  {
150  typedef std::multimap<Key, T, Compare, Allocator> Container;
151 
154 
155  public:
158  };
159  }
160 #endif
161 
162  template<
163  class Container,
164  method_set_type MethodMask = all_methods,
165  class Traits = map_traits<Container>
166  >
167  struct map_suite
168  : container_suite<Container, MethodMask, map_algorithms<Traits> >
169  {
170  };
171 
173  // Index into a container (map version)
175 
176  template<typename ContainerTraits, typename Ovr>
177  BOOST_DEDUCED_TYPENAME map_algorithms<ContainerTraits, Ovr>::reference
178  map_algorithms<ContainerTraits, Ovr>::get (container &c, index_param ix)
179  {
180  return most_derived::find_or_throw (c, ix)->second;
181  }
182 
183 
184  template<typename ContainerTraits, typename Ovr>
185  boost::python::list
187  {
188  boost::python::list _keys;
189  //For some reason code with set could not be compiled
190  //std::set< key_param > unique_keys;
191  typedef BOOST_DEDUCED_TYPENAME container::iterator iter_type;
192  for( iter_type index = most_derived::begin(c); index != most_derived::end(c); ++index ){
193  //if( unique_keys.end() == unique_keys.find( index->first ) ){
194  // unique_keys.insert( index->first );
195  if( !_keys.count( index->first ) ){
196  _keys.append( index->first );
197  }
198  //}
199  }
200 
201  return _keys;
202  }
203 
205  // Assign a value at a particular index (map version)
207 
208  template<typename ContainerTraits, typename Ovr>
209  void
211  container &c, index_param ix, value_param val)
212  {
213  c[ix] = val; // Handles overwrite and insert
214  }
215 
216 
218  // Insert a new key, value pair into a map
220 
221  template<typename ContainerTraits, typename Ovr>
222  void
224  container &c, index_param ix, value_param val)
225  {
226  typedef std::pair
227  <BOOST_DEDUCED_TYPENAME self_type::container_traits::index_type,
228  BOOST_DEDUCED_TYPENAME self_type::container_traits::value_type>
229  pair_type;
230 
231  // Can't use std::make_pair, because param types may be references
232 
233  if (!c.insert (pair_type (ix, val)).second)
234  {
235  PyErr_SetString(
236  PyExc_ValueError, "Map already holds value for insertion");
237 
238  boost::python::throw_error_already_set ();
239  }
240  }
241 } } }
242 
243 #endif // BOOST_PYTHON_INDEXING_MAP_HPP
244 
245 
246 
Definition: algorithms.hpp:128
Definition: python_CEGUI.h:11
Definition: container_traits.hpp:59
Definition: map.hpp:167
Definition: proxy_iterator.hpp:155
Definition: container_suite.hpp:42