8#ifndef FMTPYTHONCORE_H_INCLUDED
9#define FMTPYTHONCORE_H_INCLUDED
11#if defined FMTWITHPYTHON
12#ifndef BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS
13 #define BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS
16#include <boost/python.hpp>
17#include <boost/algorithm/string.hpp>
18#include <boost/python/stl_iterator.hpp>
19#include <boost/regex.hpp>
28#include <boost/dynamic_bitset.hpp>
29#include <boost/unordered_map.hpp>
39static size_t pyhash(
const T& value)
41 return boost::hash<T>()(value);
47 template <
class K,
class V>
50 static PyObject* convert(
const std::map<K, V>& lmap)
52 boost::python::dict* dictionary =
new boost::python::dict();
53 for (
typename std::map<K, V>::const_iterator it = lmap.begin(); it != lmap.end(); ++it)
55 dictionary->operator[](it->first) = it->second;
57 return dictionary->ptr();
62 template <
class K,
class V>
65 static PyObject* convert(
const boost::unordered_map<K, V>& lmap)
67 boost::python::dict* dictionary =
new boost::python::dict();
68 for (
typename boost::unordered_map<K, V>::const_iterator it = lmap.begin(); it != lmap.end(); ++it)
70 dictionary->operator[](it->first) = it->second;
72 return dictionary->ptr();
82 static PyObject* convert(
const std::vector<T>& vec)
84 boost::python::list* l =
new boost::python::list();
85 for (
size_t i = 0; i < vec.size(); i++) {
99 boost::python::converter::registry::push_back(&VecFrList<T>::convertible,
100 &VecFrList<T>::construct,
101 boost::python::type_id<std::vector<T> >());
105 static void* convertible(PyObject* obj_ptr)
107 if (!PyList_Check(obj_ptr)) {
108 PyObject* nullobj =
nullptr;
115 static void construct(
117 boost::python::converter::rvalue_from_python_stage1_data* data)
121 boost::python::list l(boost::python::handle<>(boost::python::borrowed(obj_ptr)));
128 (boost::python::converter::rvalue_from_python_storage<std::vector<T> >*)
129 data)->storage.bytes;
133 std::vector<T>& v = *(
new (storage) std::vector<T>());
136 int le =
static_cast<int>(boost::python::len(l));
138 for (
int i = 0; i != le; ++i) {
139 v[i] = boost::python::extract<T>(l[i]);
143 data->convertible = storage;
149 struct iterable_converter
153 template <
typename Container>
157 boost::python::converter::registry::push_back(
158 &iterable_converter::convertible,
159 &iterable_converter::construct<Container>,
160 boost::python::type_id<Container>());
166 static void* convertible(PyObject*
object)
168 return PyObject_GetIter(
object) ? object : NULL;
171 template <
typename Container>
172 static void construct(
174 boost::python::converter::rvalue_from_python_stage1_data* data)
179 boost::python::handle<> handle(boost::python::borrowed(
object));
183 typedef boost::python::converter::rvalue_from_python_storage<Container>
185 void* storage =
reinterpret_cast<storage_type*
>(data)->storage.bytes;
187 typedef boost::python::stl_input_iterator<typename Container::value_type>
194 new (storage) Container(
195 iterator(boost::python::object(handle)),
197 data->convertible = storage;
201 template<
class k,
class e>
209 boost::python::converter::registry::push_back(&MapFrDict<k, e>::convertible,
210 &MapFrDict<k, e>::construct,
211 boost::python::type_id<std::map<k, e> >());
216 static void* convertible(PyObject* obj_ptr)
218 if (PyMapping_Check(obj_ptr)) {
222 PyObject* nullobj =
nullptr;
228 static void construct(
230 boost::python::converter::rvalue_from_python_stage1_data* data)
232 boost::python::dict mapping(boost::python::handle<>(boost::python::borrowed(obj_ptr)));
239 (boost::python::converter::rvalue_from_python_storage<std::map<k, e>>*)
240 data)->storage.bytes;
244 std::map<k, e>& v = *(
new (storage) std::map<k, e>());
247 boost::python::list keys = mapping.keys();
248 for (
int i = 0; i < boost::python::len(keys); ++i)
250 boost::python::extract<k> extracted_key(keys[i]);
251 k newkey = extracted_key;
252 boost::python::extract<e> extracted_val(mapping[newkey]);
253 e value = extracted_val;
258 data->convertible = storage;
265 PyErr_SetString(PyExc_UserWarning, e.
what());
268 PyObject* FMTexceptiontype = NULL;
274 assert(FMTexceptiontype != NULL);
275 boost::python::object pythonExceptionInstance(error);
276 PyErr_SetObject(FMTexceptiontype, pythonExceptionInstance.ptr());
279 PyErr_SetString(PyExc_RuntimeError, error.
what());
283 template<
typename T1,
typename T2>
284 struct PairToPythonConverter {
285 static PyObject* convert(
const std::pair<T1, T2>& pair)
287 return boost::python::incref(boost::python::make_tuple(pair.first, pair.second).ptr());
291 template<
typename T1,
typename T2>
292 struct PythonToPairConverter {
293 PythonToPairConverter()
295 boost::python::converter::registry::push_back(&convertible, &construct, boost::python::type_id<std::pair<T1, T2> >());
297 static void* convertible(PyObject* obj)
299 if (!PyTuple_CheckExact(obj))
return 0;
300 if (PyTuple_Size(obj) != 2)
return 0;
303 static void construct(PyObject* obj, boost::python::converter::rvalue_from_python_stage1_data* data)
305 boost::python::tuple tuple(boost::python::borrowed(obj));
306 void* storage = ((boost::python::converter::rvalue_from_python_storage<std::pair<T1, T2> >*) data)->storage.bytes;
307 new (storage) std::pair<T1, T2>(boost::python::extract<T1>(tuple[0]), boost::python::extract<T2>(tuple[1]));
308 data->convertible = storage;
312 template<
typename T1,
typename T2>
314 boost::python::to_python_converter<std::pair<T1, T2>, PairToPythonConverter<T1, T2> > toPy;
315 PythonToPairConverter<T1, T2> fromPy;
Definition: FMTerror.hpp:32
const char * what() const override
Definition: FMTwarning.hpp:32
Definition: PYdefinitions.hpp:14
Definition: FMTaction.hpp:364