|
1 | | -/*! |
| 1 | +/*! |
2 | 2 | * \file bindings/python/include/TFEL/Python/MapConverter.hxx |
3 | 3 | * \brief |
4 | 4 | * \author Helfer Thomas |
5 | 5 | * \brief 11 mars 2013 |
6 | | - * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights |
7 | | - * reserved. |
8 | | - * This project is publicly released under either the GNU GPL Licence |
9 | | - * or the CECILL-A licence. A copy of thoses licences are delivered |
10 | | - * with the sources of TFEL. CEA or EDF may also distribute this |
11 | | - * project under specific licensing conditions. |
| 6 | + * \copyright Copyright (C) 2006-2018 CEA/DEN, EDF R&D. All rights |
| 7 | + * reserved. |
| 8 | + * This project is publicly released under either the GNU GPL Licence |
| 9 | + * or the CECILL-A licence. A copy of thoses licences are delivered |
| 10 | + * with the sources of TFEL. CEA or EDF may also distribute this |
| 11 | + * project under specific licensing conditions. |
12 | 12 | */ |
13 | 13 |
|
14 | 14 | #ifndef LIB_TFEL_PYTHON_MAPCONVERTER_H_ |
15 | | -#define LIB_TFEL_PYTHON_MAPCONVERTER_H_ |
| 15 | +#define LIB_TFEL_PYTHON_MAPCONVERTER_H_ |
16 | 16 |
|
17 | | -#include<map> |
| 17 | +#include <map> |
18 | 18 |
|
19 | | -#include<boost/python.hpp> |
20 | | -#include<boost/python/stl_iterator.hpp> |
| 19 | +#include <boost/python.hpp> |
| 20 | +#include <boost/python/stl_iterator.hpp> |
21 | 21 |
|
22 | | -namespace tfel |
23 | | -{ |
| 22 | +namespace tfel { |
24 | 23 |
|
25 | | - namespace python |
26 | | - { |
| 24 | + namespace python { |
27 | 25 |
|
28 | 26 | /*! |
29 | 27 | * convert map converter to python dict |
30 | 28 | */ |
31 | | - template<typename K, |
32 | | - typename V> |
33 | | - struct map_to_python_dict |
34 | | - { |
35 | | - static PyObject* convert(const std::map<K,V>& v) |
36 | | - { |
37 | | - using namespace std; |
38 | | - using namespace boost::python; |
39 | | - using boost::python::iterator; |
40 | | - typename map<K,V>::const_iterator p; |
41 | | - dict d; |
42 | | - for(p=v.begin();p!=v.end();++p){ |
43 | | - d[p->first] = p->second; |
44 | | - } |
45 | | - return incref(d.ptr()); |
| 29 | + template <typename K, typename V> |
| 30 | + struct map_to_python_dict { |
| 31 | + static PyObject* convert(const std::map<K, V>& v) { |
| 32 | + using namespace std; |
| 33 | + using namespace boost::python; |
| 34 | + using boost::python::iterator; |
| 35 | + typename map<K, V>::const_iterator p; |
| 36 | + dict d; |
| 37 | + for (p = v.begin(); p != v.end(); ++p) { |
| 38 | + d[p->first] = p->second; |
| 39 | + } |
| 40 | + return incref(d.ptr()); |
46 | 41 | } |
47 | 42 | }; |
48 | 43 |
|
49 | | - template<typename K, |
50 | | - typename V> |
51 | | - struct map_from_python_dict |
52 | | - { |
53 | | - |
54 | | - map_from_python_dict() |
55 | | - { |
56 | | - using boost::python::type_id; |
57 | | - using namespace boost::python::converter; |
58 | | - registry::push_back(&convertible,&construct, |
59 | | - type_id<std::map<K,V> > ()); |
| 44 | + template <typename K, typename V> |
| 45 | + struct map_from_python_dict { |
| 46 | + map_from_python_dict() { |
| 47 | + using boost::python::type_id; |
| 48 | + using namespace boost::python::converter; |
| 49 | + registry::push_back(&convertible, &construct, |
| 50 | + type_id<std::map<K, V>>()); |
60 | 51 | } |
61 | 52 |
|
62 | | - static void* convertible(PyObject * ptr) |
63 | | - { |
64 | | - using namespace boost::python; |
65 | | - if (!PyDict_Check(ptr)){ |
66 | | - return nullptr; |
67 | | - } |
68 | | - handle<> h(borrowed(ptr)); |
69 | | - dict d(h); |
70 | | - list keys = d.keys(); |
71 | | - list values = d.values(); |
72 | | - stl_input_iterator<object> pk(keys); |
73 | | - stl_input_iterator<object> pv(values); |
74 | | - stl_input_iterator<object> pe; |
75 | | - while(pk!=pe){ |
76 | | - extract<K> k(*pk); |
77 | | - extract<V> v(*pv); |
78 | | - if(!k.check()){ |
79 | | - return nullptr; |
80 | | - } |
81 | | - if(!v.check()){ |
82 | | - return nullptr; |
83 | | - } |
84 | | - ++pk; |
85 | | - ++pv; |
86 | | - } |
87 | | - return ptr; |
| 53 | + static void* convertible(PyObject* ptr) { |
| 54 | + using namespace boost::python; |
| 55 | + if (!PyDict_Check(ptr)) { |
| 56 | + return nullptr; |
| 57 | + } |
| 58 | + handle<> h(borrowed(ptr)); |
| 59 | + dict d(h); |
| 60 | + list keys = d.keys(); |
| 61 | + list values = d.values(); |
| 62 | + stl_input_iterator<object> pk(keys); |
| 63 | + stl_input_iterator<object> pv(values); |
| 64 | + stl_input_iterator<object> pe; |
| 65 | + while (pk != pe) { |
| 66 | + extract<K> k(*pk); |
| 67 | + extract<V> v(*pv); |
| 68 | + if (!k.check()) { |
| 69 | + return nullptr; |
| 70 | + } |
| 71 | + if (!v.check()) { |
| 72 | + return nullptr; |
| 73 | + } |
| 74 | + ++pk; |
| 75 | + ++pv; |
| 76 | + } |
| 77 | + return ptr; |
88 | 78 | } |
89 | 79 |
|
90 | | - static void construct(PyObject* ptr, |
91 | | - boost::python::converter::rvalue_from_python_stage1_data * data) |
92 | | - { |
93 | | - using namespace boost::python; |
94 | | - using namespace boost::python::converter; |
95 | | - typedef rvalue_from_python_storage<std::map<K,V> > py_storage; |
96 | | - using std::map; |
97 | | - assert(PyDict_Check(ptr)); |
98 | | - handle<> h(borrowed(ptr)); |
99 | | - dict d(h); |
100 | | - list keys = d.keys(); |
101 | | - list values = d.values(); |
102 | | - stl_input_iterator<object> pk(keys); |
103 | | - stl_input_iterator<object> pv(values); |
104 | | - stl_input_iterator<object> pe; |
105 | | - // Grab pointer to memory into which to construct the new map<T> |
106 | | - void* storage = reinterpret_cast<py_storage *>(data)->storage.bytes; |
107 | | - // in-place construct the new map<T> using the character data |
108 | | - // extraced from the python object |
109 | | - new (storage) map<K,V>(); |
110 | | - map<K,V>& m = *(static_cast<map<K,V> *>(storage)); |
111 | | - while(pk!=pe){ |
112 | | - extract<K> k(*pk); |
113 | | - extract<V> v(*pv); |
114 | | - assert(k.check()); |
115 | | - assert(v.check()); |
116 | | - m[k()] = v(); |
117 | | - ++pk; |
118 | | - ++pv; |
119 | | - } |
120 | | - // Stash the memory chunk pointer for later use by boost.python |
121 | | - data->convertible = storage; |
| 80 | + static void construct( |
| 81 | + PyObject* ptr, |
| 82 | + boost::python::converter::rvalue_from_python_stage1_data* data) { |
| 83 | + using namespace boost::python; |
| 84 | + using namespace boost::python::converter; |
| 85 | + typedef rvalue_from_python_storage<std::map<K, V>> py_storage; |
| 86 | + using std::map; |
| 87 | + assert(PyDict_Check(ptr)); |
| 88 | + handle<> h(borrowed(ptr)); |
| 89 | + dict d(h); |
| 90 | + list keys = d.keys(); |
| 91 | + list values = d.values(); |
| 92 | + stl_input_iterator<object> pk(keys); |
| 93 | + stl_input_iterator<object> pv(values); |
| 94 | + stl_input_iterator<object> pe; |
| 95 | + // Grab pointer to memory into which to construct the new map<T> |
| 96 | + void* storage = reinterpret_cast<py_storage*>(data)->storage.bytes; |
| 97 | + // in-place construct the new map<T> using the character data |
| 98 | + // extraced from the python object |
| 99 | + new (storage) map<K, V>(); |
| 100 | + map<K, V>& m = *(static_cast<map<K, V>*>(storage)); |
| 101 | + while (pk != pe) { |
| 102 | + extract<K> k(*pk); |
| 103 | + extract<V> v(*pv); |
| 104 | + assert(k.check()); |
| 105 | + assert(v.check()); |
| 106 | + m[k()] = v(); |
| 107 | + ++pk; |
| 108 | + ++pv; |
| 109 | + } |
| 110 | + // Stash the memory chunk pointer for later use by boost.python |
| 111 | + data->convertible = storage; |
122 | 112 | } |
123 | | - |
124 | 113 | }; |
125 | 114 |
|
126 | | - template<typename K, |
127 | | - typename V> |
128 | | - static void initializeMapConverter() |
129 | | - { |
| 115 | + template <typename K, typename V> |
| 116 | + static void initializeMapConverter() { |
130 | 117 | using namespace boost::python; |
131 | 118 | using std::map; |
132 | 119 | // register the to-python converter |
133 | | - to_python_converter<map<K,V>,map_to_python_dict<K,V> >(); |
| 120 | + to_python_converter<map<K, V>, map_to_python_dict<K, V>>(); |
134 | 121 | // register the from-python converter |
135 | | - map_from_python_dict<K,V>(); |
| 122 | + map_from_python_dict<K, V>(); |
136 | 123 | } |
137 | 124 |
|
138 | | - } // end of namespace python |
| 125 | + } // end of namespace python |
139 | 126 |
|
140 | | -} // end of namespace tfel |
| 127 | +} // end of namespace tfel |
141 | 128 |
|
142 | 129 | #endif /* LIB_TFEL_PYTHON_MAPCONVERTER_H_ */ |
0 commit comments