Belle II Software development
|
Python object converter utilities namespace. More...
Classes | |
class | convertToPythonObjectVisitor |
Helper function object to unpack a value from a variant to a python object. More... | |
struct | SizeT |
Helper construct for TMP that provides an index at compile time to recurse through type lists. More... | |
struct | Type |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< bool > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< boost::variant< Types... > > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< double > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< float > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< int > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< std::map< A, B > > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< std::optional< T > > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< std::set< T > > |
Convert a templeate arugment into a string for corresponding Python type. More... | |
struct | Type< std::shared_ptr< Path > > |
Some modules take a path as argument so let's add this as well. More... | |
struct | Type< std::string > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< std::tuple< Types... > > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< std::vector< T > > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< unsigned int > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | Type< unsigned long int > |
Converts a template argument into a string for corresponding Python type. More... | |
struct | VariadicType |
Recursively convert multiple types to type names (used for tuples). More... | |
struct | VariadicType< T > |
Recursively convert multiple types to type names (used for tuples). More... | |
Functions | |
bool | checkPythonObject (const boost::python::object &pyObject, bool) |
check if the python object can be converted to the given type. | |
bool | checkPythonObject (const boost::python::object &pyObject, float) |
check if the python object can be converted to the given type. | |
bool | checkPythonObject (const boost::python::object &pyObject, double) |
check if the python object can be converted to the given type. | |
bool | checkPythonObject (const boost::python::object &pyObject, int) |
check if the python object can be converted to the given an integral number | |
bool | checkPythonObject (const boost::python::object &pyObject, unsigned int) |
check if the python object can be converted to the given an integral number | |
bool | checkPythonObject (const boost::python::object &pyObject, unsigned long int) |
check if the python object can be converted to the given an integral number | |
bool | checkPythonObject (const boost::python::object &pyObject, const std::string &) |
check if the python object can be converted to the given a string | |
template<typename Key , typename Value > | |
bool | checkPythonObject (const boost::python::object &pyObject, const std::map< Key, Value > &) |
Check if the python object is a mapping of the correct key and value types. | |
template<typename Value > | |
bool | checkPythonObject (const boost::python::object &pyObject, const std::vector< Value > &) |
check if the python object can be converted to the given std::vector type | |
template<typename Value > | |
bool | checkPythonObject (const boost::python::object &pyObject, const std::set< Value > &) |
check if the python object can be converted to the given std::set type | |
template<typename... Types> | |
bool | checkPythonObject (const boost::python::object &pyObject, const std::tuple< Types... > &) |
check if the python object can be converted to the given std::tuple type | |
template<typename... Types> | |
bool | checkPythonObject (const boost::python::object &pyObject, const boost::variant< Types... > &) |
check if the python object can be converted to the given boost::variant type | |
template<typename Type > | |
bool | checkPythonObject (const boost::python::object &pyObject, const std::optional< Type > &) |
check if the python object can be converted to the given std::optional<T> type | |
template<typename Scalar > | |
Scalar | convertPythonObject (const boost::python::object &pyObject, Scalar) |
Convert from Python to given type. | |
template<typename Key , typename Value > | |
std::map< Key, Value > | convertPythonObject (const boost::python::object &pyObject, const std::map< Key, Value > &) |
Convert from Python to given type. | |
template<typename Value > | |
std::vector< Value > | convertPythonObject (const boost::python::object &pyObject, const std::vector< Value > &) |
Convert from Python to given type. | |
template<typename Value > | |
std::set< Value > | convertPythonObject (const boost::python::object &pyObject, const std::set< Value > &) |
Convert from Python to given type. | |
template<typename... Types> | |
std::tuple< Types... > | convertPythonObject (const boost::python::object &pyObject, const std::tuple< Types... > &) |
Convert from Python to given type. | |
template<typename... Types> | |
boost::variant< Types... > | convertPythonObject (const boost::python::object &pyObject, const boost::variant< Types... > &) |
Convert from Python to given type. | |
template<typename Type > | |
std::optional< Type > | convertPythonObject (const boost::python::object &pyObject, const std::optional< Type > &) |
Convert from Python to the given std::optional type. | |
template<typename Scalar > | |
boost::python::object | convertToPythonObject (const Scalar &value) |
------------— From C++ TO Python Converter ---------------------— | |
template<typename Value > | |
boost::python::list | convertToPythonObject (const std::vector< Value > &vector) |
Writes content of a std::vector to a python list. | |
template<typename Value > | |
boost::python::object | convertToPythonObject (const std::set< Value > &set) |
Convert std::set to a python object. | |
template<typename Key , typename Value > | |
boost::python::dict | convertToPythonObject (const std::map< Key, Value > &map) |
Writes content of a std::map to a python dict. | |
template<typename... Types> | |
boost::python::tuple | convertToPythonObject (const std::tuple< Types... > &tuple) |
Writes content of a std::tuple to a python tuple. | |
template<typename... Types> | |
boost::python::object | convertToPythonObject (const boost::variant< Types... > &variant) |
Writes content of a boost::variant to a python object. | |
template<typename Type > | |
boost::python::object | convertToPythonObject (const std::optional< Type > &optional) |
Convert from the given std::optional type to Python. | |
template<class Functor > | |
bool | iteratePythonObject (const boost::python::object &pyObject, Functor function) |
Helper function to loop over a python object that implements the iterator concept and call a functor with each element as argument. | |
template<typename... Types, std::size_t ... Is> | |
bool | CheckTuple (const std::tuple< Types... > &tuple, const boost::python::tuple &pyTuple, std::index_sequence< Is... >) |
Check if all tuple elements match. | |
template<typename VariantType > | |
bool | CheckVariant (const VariantType &, const boost::python::object &, SizeT< 0 >) |
Recursion sentinal for the case that all former type checks failed for the variant. | |
template<typename... Types, size_t N> | |
bool | CheckVariant (const boost::variant< Types... > &variant, const boost::python::object &pyObject, SizeT< N >) |
Recursion through the variant types checking the python object for the type at position N - 1. | |
template<typename... Types> | |
bool | checkPythonObject (const boost::python::object &pyObject, const boost::variant< Types... > variant) |
Check if the python object is a tuple of objects of the correct types. | |
template<typename TupleType > | |
void | GetTuple (const TupleType &tuple, boost::python::list &pyList) |
TMP (Template Meta Programming ) The given python list is filled, and later converted into a python tuple (in convertToPythonObject). | |
template<typename TupleType > | |
void | GetTuple (const TupleType &, boost::python::list &, SizeT< 0 >) |
Break recursion when all members are already appended to the list. | |
template<typename TupleType , size_t N> | |
void | GetTuple (const TupleType &tuple, boost::python::list &pyList, SizeT< N >) |
Append elements of the tuple to pyList by recursively calling this function with the element index as argument type. | |
template<typename TupleType > | |
void | SetTuple (TupleType &tuple, const boost::python::tuple &pyTuple) |
TMP (Template Meta Programming ) The given python tuple is written into the given c++ tuple. | |
template<typename TupleType > | |
void | SetTuple (TupleType &, const boost::python::tuple &, SizeT< 0 >) |
Break recursion once all elements have been set. | |
template<typename TupleType , size_t N> | |
void | SetTuple (TupleType &tuple, const boost::python::tuple &pyTuple, SizeT< N >) |
Set the elements of the tuple recursively by calling with the function with the next tuple index and converting one element. | |
template<typename... Types> | |
void | SetVariant (boost::variant< Types... > &, const boost::python::object &pyObject, SizeT< 0 >) |
TMP (Template Meta Programming ) The given python object is written into the given c++ boost variant. | |
template<typename... Types, size_t N> | |
void | SetVariant (boost::variant< Types... > &variant, const boost::python::object &pyObject, SizeT< N >) |
Recursively go through all possible types of the variant and check which one is a possible conversion. | |
Python object converter utilities namespace.
Provides templates for converting python objects to c++ objects and the other way round.
This is done by using the type of the value provided by the module as a template parameter. Hence the compiler can infer which converter should be used. Note: We are only interested in the type of the value, the actual value isn't used at all!
|
inline |
check if the python object can be converted to the given type.
------------— Check python objects for exact type match ---------------------—
Check if the python object is a boolean
Definition at line 192 of file PyObjConvUtils.h.
bool checkPythonObject | ( | const boost::python::object & | pyObject, |
const boost::variant< Types... > | variant | ||
) |
Check if the python object is a tuple of objects of the correct types.
Definition at line 327 of file PyObjConvUtils.h.
bool checkPythonObject | ( | const boost::python::object & | pyObject, |
const std::map< Key, Value > & | |||
) |
Check if the python object is a mapping of the correct key and value types.
Definition at line 234 of file PyObjConvUtils.h.
bool checkPythonObject | ( | const boost::python::object & | pyObject, |
const std::optional< Type > & | |||
) |
check if the python object can be converted to the given std::optional<T> type
Check if a python object can be converted to std::optional.
Definition at line 336 of file PyObjConvUtils.h.
bool checkPythonObject | ( | const boost::python::object & | pyObject, |
const std::set< Value > & | |||
) |
check if the python object can be converted to the given std::set type
Definition at line 278 of file PyObjConvUtils.h.
|
inline |
check if the python object can be converted to the given a string
Check if the python object is a string.
Definition at line 227 of file PyObjConvUtils.h.
bool checkPythonObject | ( | const boost::python::object & | pyObject, |
const std::tuple< Types... > & | tuple | ||
) |
check if the python object can be converted to the given std::tuple type
Check if the python object is a tuple of objects of the correct types.
Definition at line 296 of file PyObjConvUtils.h.
bool checkPythonObject | ( | const boost::python::object & | pyObject, |
const std::vector< Value > & | |||
) |
check if the python object can be converted to the given std::vector type
Check if the python object is a list of objects of the correct value type.
Definition at line 269 of file PyObjConvUtils.h.
|
inline |
check if the python object can be converted to the given type.
Check if the python object is a float number.
Definition at line 221 of file PyObjConvUtils.h.
|
inline |
check if the python object can be converted to the given type.
Check if the python object is a float number.
Definition at line 215 of file PyObjConvUtils.h.
|
inline |
check if the python object can be converted to the given an integral number
Check if the python object is an integral number.
Definition at line 198 of file PyObjConvUtils.h.
|
inline |
check if the python object can be converted to the given an integral number
Check if the python object is an integral number.
Definition at line 203 of file PyObjConvUtils.h.
|
inline |
check if the python object can be converted to the given an integral number
Check if the python object is an integral number.
Definition at line 209 of file PyObjConvUtils.h.
bool CheckTuple | ( | const std::tuple< Types... > & | tuple, |
const boost::python::tuple & | pyTuple, | ||
std::index_sequence< Is... > | |||
) |
Check if all tuple elements match.
Definition at line 288 of file PyObjConvUtils.h.
bool CheckVariant | ( | const boost::variant< Types... > & | variant, |
const boost::python::object & | pyObject, | ||
SizeT< N > | |||
) |
Recursion through the variant types checking the python object for the type at position N - 1.
Definition at line 314 of file PyObjConvUtils.h.
bool CheckVariant | ( | const VariantType & | , |
const boost::python::object & | , | ||
SizeT< 0 > | |||
) |
Recursion sentinal for the case that all former type checks failed for the variant.
Definition at line 305 of file PyObjConvUtils.h.
boost::variant< Types... > convertPythonObject | ( | const boost::python::object & | pyObject, |
const boost::variant< Types... > & | |||
) |
Convert from Python to given type.
Reads boost::variant from a python object.
pyObject | Python object which stores the value. |
Definition at line 667 of file PyObjConvUtils.h.
std::map< Key, Value > convertPythonObject | ( | const boost::python::object & | pyObject, |
const std::map< Key, Value > & | |||
) |
Convert from Python to given type.
Reads std::map from a python object.
pyObject | Python object which stores the map. |
Definition at line 562 of file PyObjConvUtils.h.
std::optional< Type > convertPythonObject | ( | const boost::python::object & | pyObject, |
const std::optional< Type > & | |||
) |
Convert from Python to the given std::optional type.
Convert a python object to std::optional.
Definition at line 681 of file PyObjConvUtils.h.
std::set< Value > convertPythonObject | ( | const boost::python::object & | pyObject, |
const std::set< Value > & | |||
) |
Convert from Python to given type.
Convert a python set to a std::set.
Definition at line 540 of file PyObjConvUtils.h.
std::tuple< Types... > convertPythonObject | ( | const boost::python::object & | pyObject, |
const std::tuple< Types... > & | |||
) |
Convert from Python to given type.
Reads std::tuple from a python object.
pyObject | Python object which stores the map. |
Definition at line 618 of file PyObjConvUtils.h.
std::vector< Value > convertPythonObject | ( | const boost::python::object & | pyObject, |
const std::vector< Value > & | |||
) |
Convert from Python to given type.
Reads std::vector from a python object.
If the python object isn't a list, a std::vector with the given object as single entry is returned.
pyObject | Python object which stores the vector. |
Definition at line 523 of file PyObjConvUtils.h.
Scalar convertPythonObject | ( | const boost::python::object & | pyObject, |
const | Scalar | ||
) |
Convert from Python to given type.
------------— From Python TO C++ Converter ---------------------—
Reads a scalar type (int, double, string, bool) from a python object.
pyObject | Python object which stores the scalar type. |
Scalar | A scalar. |
Definition at line 500 of file PyObjConvUtils.h.
boost::python::object convertToPythonObject | ( | const boost::variant< Types... > & | variant | ) |
Writes content of a boost::variant to a python object.
variant | The variant whose content should be stored in a python object. |
Definition at line 466 of file PyObjConvUtils.h.
boost::python::object convertToPythonObject | ( | const Scalar & | value | ) |
------------— From C++ TO Python Converter ---------------------—
Writes a scalar type (int, double, string, bool) to a python object.
value | The scalar who should be converted to a python object. |
Definition at line 352 of file PyObjConvUtils.h.
boost::python::dict convertToPythonObject | ( | const std::map< Key, Value > & | map | ) |
Writes content of a std::map to a python dict.
map | The map whose items should be stored to a python dict. |
Definition at line 394 of file PyObjConvUtils.h.
boost::python::object convertToPythonObject | ( | const std::optional< Type > & | optional | ) |
Convert from the given std::optional type to Python.
Convert a std::optional to python.
optional | the optional object to convert |
Definition at line 479 of file PyObjConvUtils.h.
boost::python::object convertToPythonObject | ( | const std::set< Value > & | set | ) |
Convert std::set to a python object.
Write the content of a std::set to a python set.
set | the set that should be converted |
Definition at line 378 of file PyObjConvUtils.h.
boost::python::tuple convertToPythonObject | ( | const std::tuple< Types... > & | tuple | ) |
Writes content of a std::tuple to a python tuple.
tuple | The tuple whose items should be stored to a python tuple. |
Definition at line 438 of file PyObjConvUtils.h.
boost::python::list convertToPythonObject | ( | const std::vector< Value > & | vector | ) |
Writes content of a std::vector to a python list.
vector | The vector whose items should be stored to a python list. |
Definition at line 364 of file PyObjConvUtils.h.
|
inline |
Break recursion when all members are already appended to the list.
Definition at line 419 of file PyObjConvUtils.h.
|
inline |
TMP (Template Meta Programming ) The given python list is filled, and later converted into a python tuple (in convertToPythonObject).
To fill the python list from the C++ std::tuple we need again TMP methods. The variadic template std::tuple is copied by the recursive defined template function GetTuple, the overloaded argument (type SizeT<>) of the function serves as a counter for the recursion depth.
Definition at line 412 of file PyObjConvUtils.h.
|
inline |
Append elements of the tuple to pyList by recursively calling this function with the element index as argument type.
Definition at line 424 of file PyObjConvUtils.h.
bool iteratePythonObject | ( | const boost::python::object & | pyObject, |
Functor | function | ||
) |
Helper function to loop over a python object that implements the iterator concept and call a functor with each element as argument.
pyObject | the python object to iterate over |
function | any functor object which accepts a boost::python::object as its only argument and returns true if the iteration should be continued |
Definition at line 255 of file PyObjConvUtils.h.
|
inline |
Break recursion once all elements have been set.
Definition at line 599 of file PyObjConvUtils.h.
|
inline |
TMP (Template Meta Programming ) The given python tuple is written into the given c++ tuple.
To fill the C++ std::tuple we need again TMP methods. The variadic template std::tuple is filled by the recursive defined template function SetTuple, the overloaded argument (type SizeT<>) of the function serves as a counter for the recursion depth.
Definition at line 586 of file PyObjConvUtils.h.
|
inline |
Set the elements of the tuple recursively by calling with the function with the next tuple index and converting one element.
Definition at line 604 of file PyObjConvUtils.h.
|
inline |
TMP (Template Meta Programming ) The given python object is written into the given c++ boost variant.
To fill the C++ boost::variant we need again TMP methods. The variadic template boost::variant is filled by the recursive defined template function SetVariant, the overloaded argument (type SizeT<>) of the function serves as a counter for the recursion depth.
Definition at line 635 of file PyObjConvUtils.h.
|
inline |
Recursively go through all possible types of the variant and check which one is a possible conversion.
Definition at line 644 of file PyObjConvUtils.h.