8#ifndef FMTPYTHONCORE_Hm_included
9#define FMTPYTHONCORE_Hm_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.h>
29#include <boost/dynamic_bitset.hpp>
30#include <boost/unordered_map.hpp>
47static size_t pyHash(
const T& value)
49 return boost::hash<T>()(value);
61 template <
class K,
class V>
70 static PyObject* convert(
const std::map<K, V>& lmap)
72 boost::python::dict* dictionary =
new boost::python::dict();
73 for (
typename std::map<K, V>::const_iterator it = lmap.begin(); it != lmap.end(); ++it)
75 dictionary->operator[](it->first) = it->second;
77 return dictionary->ptr();
88 template <
class K,
class V>
97 static PyObject* convert(
const boost::unordered_map<K, V>& lmap)
99 boost::python::dict* dictionary =
new boost::python::dict();
100 for (
typename boost::unordered_map<K, V>::const_iterator it = lmap.begin(); it != lmap.end(); ++it)
102 dictionary->operator[](it->first) = it->second;
104 return dictionary->ptr();
125 static PyObject* convert(
const std::vector<T>& vec)
127 boost::python::list* l =
new boost::python::list();
128 for (
size_t i = 0; i < vec.size(); i++) {
150 static PyObject* convert(
const std::set<T>& p_set)
152 boost::python::list* l =
new boost::python::list();
153 for (
const auto& i : p_set)
176 boost::python::converter::registry::push_back(&VecFrList<T>::convertible,
177 &VecFrList<T>::construct,
178 boost::python::type_id<std::vector<T> >());
188 static void* convertible(PyObject* obj_ptr)
190 if (!PyList_Check(obj_ptr)) {
191 PyObject* nullobj =
nullptr;
204 static void construct(
206 boost::python::converter::rvalue_from_python_stage1_data* data)
210 boost::python::list l(boost::python::handle<>(boost::python::borrowed(obj_ptr)));
217 (boost::python::converter::rvalue_from_python_storage<std::vector<T> >*)
218 data)->storage.bytes;
222 std::vector<T>& v = *(
new (storage) std::vector<T>());
225 int le =
static_cast<int>(boost::python::len(l));
227 for (
int i = 0; i != le; ++i) {
228 v[i] = boost::python::extract<T>(l[i]);
232 data->convertible = storage;
242 struct iterable_converter
252 template <
typename Container>
256 boost::python::converter::registry::push_back(
257 &iterable_converter::convertible,
258 &iterable_converter::construct<Container>,
259 boost::python::type_id<Container>());
271 static void* convertible(PyObject*
object)
273 return PyObject_GetIter(
object) ? object : NULL;
283 template <
typename Container>
284 static void construct(
286 boost::python::converter::rvalue_from_python_stage1_data* data)
291 boost::python::handle<> handle(boost::python::borrowed(
object));
295 typedef boost::python::converter::rvalue_from_python_storage<Container>
297 void* storage =
reinterpret_cast<storage_type*
>(data)->storage.bytes;
299 typedef boost::python::stl_input_iterator<typename Container::value_type>
306 new (storage) Container(
307 iterator(boost::python::object(handle)),
309 data->convertible = storage;
319 template<
class k,
class e>
331 boost::python::converter::registry::push_back(&MapFrDict<k, e>::convertible,
332 &MapFrDict<k, e>::construct,
333 boost::python::type_id<std::map<k, e> >());
344 static void* convertible(PyObject* obj_ptr)
346 if (PyMapping_Check(obj_ptr)) {
350 PyObject* nullobj =
nullptr;
362 static void construct(
364 boost::python::converter::rvalue_from_python_stage1_data* data)
366 boost::python::dict mapping(boost::python::handle<>(boost::python::borrowed(obj_ptr)));
373 (boost::python::converter::rvalue_from_python_storage<std::map<k, e>>*)
374 data)->storage.bytes;
378 std::map<k, e>& v = *(
new (storage) std::map<k, e>());
381 boost::python::list keys = mapping.keys();
382 for (
int i = 0; i < boost::python::len(keys); ++i)
384 boost::python::extract<k> extractedKey(keys[i]);
385 k newkey = extractedKey;
386 boost::python::extract<e> extractedVal(mapping[newkey]);
387 e value = extractedVal;
392 data->convertible = storage;
404 PyErr_SetString(PyExc_UserWarning, e.
what());
407 PyObject* FMTexceptiontype = NULL;
418 assert(FMTexceptiontype != NULL);
419 boost::python::object pythonExceptionInstance(error);
420 PyErr_SetObject(FMTexceptiontype, pythonExceptionInstance.ptr());
423 PyErr_SetString(PyExc_RuntimeError, error.
what());
433 template<
typename T1,
typename T2>
434 struct PairToPythonConverter {
441 static PyObject* convert(
const std::pair<T1, T2>& pair)
443 return boost::python::incref(boost::python::make_tuple(pair.first, pair.second).ptr());
453 template<
typename T1,
typename T2>
454 struct PythonToPairConverter {
459 PythonToPairConverter()
461 boost::python::converter::registry::push_back(&convertible, &construct, boost::python::type_id<std::pair<T1, T2> >());
469 static void* convertible(PyObject* obj)
471 if (!PyTuple_CheckExact(obj))
return 0;
472 if (PyTuple_Size(obj) != 2)
return 0;
481 static void construct(PyObject* obj, boost::python::converter::rvalue_from_python_stage1_data* data)
483 boost::python::tuple tuple(boost::python::borrowed(obj));
484 void* storage = ((boost::python::converter::rvalue_from_python_storage<std::pair<T1, T2> >*) data)->storage.bytes;
485 new (storage) std::pair<T1, T2>(boost::python::extract<T1>(tuple[0]), boost::python::extract<T2>(tuple[1]));
486 data->convertible = storage;
496 template<
typename T1,
typename T2>
498 boost::python::to_python_converter<std::pair<T1, T2>, PairToPythonConverter<T1, T2> > toPy;
499 PythonToPairConverter<T1, T2> fromPy;
Exception class derived from FMTException for errors thrown in FMT.
Definition: FMTError.h:32
bool hold() const
Return the value of the holdup member.
const char * what() const noexcept override
Override the what function of the base exception class returning the message string.
Exception class derived from FMTException for warnings in FMT.
Definition: FMTWarning.h:31
Definition: PYdefinitions.h:14
Definition: FMTAction.h:463