|
| 1 | +// (C) Copyright Joel de Guzman 2003. |
| 2 | +// Permission to copy, use, modify, sell and distribute this software |
| 3 | +// is granted provided this copyright notice appears in all copies. This |
| 4 | +// software is provided "as is" without express or implied warranty, and |
| 5 | +// with no claim as to its suitability for any purpose. |
| 6 | + |
| 7 | +#ifndef MAP_INDEXING_SUITE_JDG20038_HPP |
| 8 | +# define MAP_INDEXING_SUITE_JDG20038_HPP |
| 9 | + |
| 10 | +# include <boost/python/suite/indexing/indexing_suite.hpp> |
| 11 | +# include <boost/python/iterator.hpp> |
| 12 | + |
| 13 | +namespace boost { namespace python { |
| 14 | + |
| 15 | + // Forward declaration |
| 16 | + template <class Container, bool NoProxy, class DerivedPolicies> |
| 17 | + class map_indexing_suite; |
| 18 | + |
| 19 | + namespace detail |
| 20 | + { |
| 21 | + template <class Container, bool NoProxy> |
| 22 | + class final_map_derived_policies |
| 23 | + : public map_indexing_suite<Container, |
| 24 | + NoProxy, final_map_derived_policies<Container, NoProxy> > {}; |
| 25 | + } |
| 26 | + |
| 27 | + // The map_indexing_suite class is a predefined indexing_suite derived |
| 28 | + // class for wrapping std::vector (and std::vector like) classes. It provides |
| 29 | + // all the policies required by the indexing_suite (see indexing_suite). |
| 30 | + // Example usage: |
| 31 | + // |
| 32 | + // class X {...}; |
| 33 | + // |
| 34 | + // ... |
| 35 | + // |
| 36 | + // class_<std::map<std::string, X> >("XMap") |
| 37 | + // .def(map_indexing_suite<std::map<std::string, X> >()) |
| 38 | + // ; |
| 39 | + // |
| 40 | + // By default indexed elements are returned by proxy. This can be |
| 41 | + // disabled by supplying *true* in the NoProxy template parameter. |
| 42 | + // |
| 43 | + template < |
| 44 | + class Container, |
| 45 | + bool NoProxy = false, |
| 46 | + class DerivedPolicies |
| 47 | + = detail::final_map_derived_policies<Container, NoProxy> > |
| 48 | + class map_indexing_suite |
| 49 | + : public indexing_suite< |
| 50 | + Container |
| 51 | + , DerivedPolicies |
| 52 | + , NoProxy |
| 53 | + , true |
| 54 | + , typename Container::mapped_type |
| 55 | + , typename Container::key_type |
| 56 | + , typename Container::key_type |
| 57 | + > |
| 58 | + { |
| 59 | + public: |
| 60 | + |
| 61 | + typedef typename Container::mapped_type data_type; |
| 62 | + typedef typename Container::key_type key_type; |
| 63 | + typedef typename Container::key_type index_type; |
| 64 | + typedef typename Container::size_type size_type; |
| 65 | + typedef typename Container::difference_type difference_type; |
| 66 | + |
| 67 | +// template <class Class> |
| 68 | +// static void |
| 69 | +// extension_def(Class& cl) |
| 70 | +// { |
| 71 | +// cl |
| 72 | +// .def("append", &base_append) |
| 73 | +// .def("extend", &base_extend) |
| 74 | +// ; |
| 75 | +// } |
| 76 | + |
| 77 | + static data_type& |
| 78 | + get_item(Container& container, index_type i_) |
| 79 | + { |
| 80 | + typename Container::iterator i = container.find(i_); |
| 81 | + key_check(container, i); |
| 82 | + return i->second; |
| 83 | + } |
| 84 | + |
| 85 | + static void |
| 86 | + set_item(Container& container, index_type i_, data_type const& v) |
| 87 | + { |
| 88 | + typename Container::iterator i = container.find(i_); |
| 89 | + key_check(container, i); |
| 90 | + i->second = v; |
| 91 | + } |
| 92 | + |
| 93 | + static void |
| 94 | + delete_item(Container& container, index_type i) |
| 95 | + { |
| 96 | + container.erase(i); |
| 97 | + } |
| 98 | + |
| 99 | + static size_t |
| 100 | + size(Container& container) |
| 101 | + { |
| 102 | + return container.size(); |
| 103 | + } |
| 104 | + |
| 105 | + static bool |
| 106 | + contains(Container& container, key_type const& key) |
| 107 | + { |
| 108 | + return container.find(key) != container.end(); |
| 109 | + } |
| 110 | + |
| 111 | + static bool |
| 112 | + compare_index(Container& container, index_type a, index_type b) |
| 113 | + { |
| 114 | + return container.key_comp()(a, b); |
| 115 | + } |
| 116 | + |
| 117 | + static index_type |
| 118 | + convert_index(Container& container, PyObject* i_) |
| 119 | + { |
| 120 | + extract<key_type const&> i(i_); |
| 121 | + if (i.check()) |
| 122 | + { |
| 123 | + return i(); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + extract<key_type> i(i_); |
| 128 | + if (i.check()) |
| 129 | + return i(); |
| 130 | + } |
| 131 | + |
| 132 | + PyErr_SetString(PyExc_TypeError, "Invalid index type"); |
| 133 | + throw_error_already_set(); |
| 134 | + return index_type(); |
| 135 | + } |
| 136 | + |
| 137 | + private: |
| 138 | + |
| 139 | + static void |
| 140 | + key_check(Container& container, typename Container::iterator i) |
| 141 | + { |
| 142 | + if (i == container.end()) |
| 143 | + { |
| 144 | + PyErr_SetString(PyExc_KeyError, "Invalid key"); |
| 145 | + throw_error_already_set(); |
| 146 | + } |
| 147 | + } |
| 148 | + }; |
| 149 | + |
| 150 | +}} // namespace boost::python |
| 151 | + |
| 152 | +#endif // MAP_INDEXING_SUITE_JDG20038_HPP |
0 commit comments