26#ifndef PYRAP_PYCBASICDATA_H
27#define PYRAP_PYCBASICDATA_H
30#include <boost/python.hpp>
31#include <boost/python/object.hpp>
32#include <casacore/python/Converters/PycArray.h>
33#include <casacore/casa/BasicSL/String.h>
34#include <casacore/casa/Arrays/IPosition.h>
35#include <casacore/casa/Arrays/Vector.h>
36#include <casacore/casa/Utilities/Assert.h>
37#include <casacore/casa/Exceptions/Error.h>
41#if PY_MAJOR_VERSION >= 3
48namespace casacore {
namespace python {
54 static bool get (
const std::string& name);
55 static void set (
const std::string& name);
70 {
return boost::python::object((
const std::string&)s); }
72 {
return boost::python::incref(
makeobject(s).ptr()); }
80 boost::python::converter::registry::push_back(
83 boost::python::type_id<String>());
89 if (!PyUnicode_Check(obj_ptr))
return 0;
91 if (!PyString_Check(obj_ptr) && !PyUnicode_Check(obj_ptr))
return 0;
98 boost::python::converter::rvalue_from_python_stage1_data* data)
101 boost::python::object temp_bytes_obj;
102 if (PyUnicode_Check(obj_ptr)) {
103 PyObject * temp_bytes = PyUnicode_AsEncodedString(obj_ptr,
"UTF-8",
"strict");
105 if (temp_bytes != NULL) {
107 temp_bytes_obj = boost::python::object(boost::python::handle<>(temp_bytes));
108 value = PyBytes_AS_STRING(temp_bytes);
110 boost::python::throw_error_already_set();
113 }
else if (PyString_Check(obj_ptr)) {
114 value = PyString_AsString(obj_ptr);
117 boost::python::throw_error_already_set();
119 if (
value == 0) boost::python::throw_error_already_set();
121 (boost::python::converter::rvalue_from_python_storage<String>*)
122 data)->storage.bytes;
124 data->convertible = storage;
144 template <
typename ContainerType>
145 static bool check_size(boost::type<ContainerType>, std::size_t)
150 template <
typename ContainerType>
154 template <
typename ContainerType>
155 static void reserve(ContainerType&, std::size_t)
172 template <
typename ContainerType>
173 static void reserve(ContainerType& a, std::size_t sz)
178 template <
typename ContainerType,
typename ValueType>
179 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
188 template <
typename ContainerType>
189 static void reserve(ContainerType& a, std::size_t sz)
194 template <
typename ContainerType,
typename ValueType>
195 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
197 assert(a.size() > i);
204 template <
typename ContainerType>
205 static void reserve(ContainerType& a, std::size_t sz)
210 template <
typename ContainerType,
typename ValueType>
211 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
213 assert(a.size() > i);
214 a[a.size() - i - 1] = v;
231 template <
typename ContainerType >
239 static boost::python::object
makeobject (ContainerType
const& c)
241 boost::python::list result;
242 typename ContainerType::const_iterator i = c.begin();
243 typename ContainerType::const_iterator iEnd = c.end();
244 for( ; i != iEnd; ++i) {
249 static PyObject*
convert (ContainerType
const& c)
251 return boost::python::incref(
makeobject(c).ptr());
262 boost::python::list result;
263 for (
int i=c.size()-1; i>=0; --i) {
270 return boost::python::incref(
makeobject(c).ptr());
285 boost::python::list result;
286 ContainerType::const_iterator i = c.begin();
287 ContainerType::const_iterator iEnd = c.end();
288 for( ; i != iEnd; ++i) {
296 return boost::python::incref(
makeobject(c).ptr());
305 boost::python::list result;
306 ContainerType::const_iterator i = c.begin();
307 ContainerType::const_iterator iEnd = c.end();
308 for( ; i != iEnd; ++i) {
309 result.append((std::string
const&)(*i));
315 return boost::python::incref(
makeobject(c).ptr());
324 boost::python::list result;
327 for( ; i != iEnd; ++i) {
328 result.append((std::string
const&)(*i));
334 return boost::python::incref(
makeobject(c).ptr());
343 boost::python::list result;
346 for( ; i != iEnd; ++i) {
347 result.append((std::string
const&)(*i));
353 return boost::python::incref(
makeobject(c).ptr());
365 template <
typename T >
370 boost::python::to_python_converter < std::vector < T >,
371 to_list < std::vector < T > > > ();
374 template <
typename T >
379 boost::python::to_python_converter < casacore::Array < T >,
380 to_list < casacore::Array < T > > > ();
383 template <
typename T >
388 boost::python::to_python_converter < casacore::Vector < T >,
389 to_list < casacore::Vector < T > > > ();
397 to_list < casacore::IPosition > > ();
411 template <
typename ContainerType,
typename ConversionPolicy>
418 boost::python::converter::registry::push_back(
421 boost::python::type_id<ContainerType>());
428 using namespace boost::python;
429 handle<> py_hdl(obj_ptr);
430 if (PyErr_Occurred()) {
434 object py_obj(py_hdl);
437 if (PyBool_Check(obj_ptr)
438 || PyLong_Check(obj_ptr)
439 || PyFloat_Check(obj_ptr)
440 || PyComplex_Check(obj_ptr)
442 || PyInt_Check(obj_ptr)
443 || PyString_Check(obj_ptr)
445 || PyUnicode_Check(obj_ptr)) {
446 extract<container_element_type> elem_proxy(py_obj);
447 if (!elem_proxy.check())
return 0;
462 handle<> obj_iter(allow_null(PyObject_GetIter(py_obj.ptr())));
463 if (!obj_iter.get()) {
476 boost::python::converter::rvalue_from_python_stage1_data* data)
478 using namespace boost::python;
479 using boost::python::converter::rvalue_from_python_storage;
481 (rvalue_from_python_storage<ContainerType>*)
482 data)->storage.bytes;
483 new (storage) ContainerType();
484 data->convertible = storage;
485 ContainerType& result = *((ContainerType*)storage);
486 if (PyBool_Check(obj_ptr)
487 || PyLong_Check(obj_ptr)
488 || PyFloat_Check(obj_ptr)
489 || PyComplex_Check(obj_ptr)
490 || PyUnicode_Check(obj_ptr)
492 || PyString_Check(obj_ptr)
493 || PyInt_Check(obj_ptr)
496 extract<container_element_type> elem_proxy(obj_ptr);
497 ConversionPolicy::reserve(result, 1);
498 ConversionPolicy::set_value(result, 0, elem_proxy());
501 handle<> py_hdl(obj_ptr);
502 object py_obj = object(py_hdl);
514 ContainerType result;
522 using namespace boost::python;
523 int obj_size = PyObject_Length(obj_ptr);
524 handle<> obj_iter(PyObject_GetIter(obj_ptr));
525 ConversionPolicy::reserve(result, obj_size);
528 handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
529 if (PyErr_Occurred()) throw_error_already_set();
530 if (!py_elem_hdl.get())
break;
531 object py_elem_obj(py_elem_hdl);
532 extract<container_element_type> elem_proxy(py_elem_obj);
533 ConversionPolicy::set_value(result, i, elem_proxy());
535 ConversionPolicy::assert_size(boost::type<ContainerType>(), i);
540 using namespace boost::python;
541 handle<> obj_iter(allow_null(PyObject_GetIter(obj_ptr)));
542 if (!obj_iter.get()) {
546 int obj_size = PyObject_Length(obj_ptr);
551 if (ConversionPolicy::check_convertibility_per_element()) {
552 if (!ConversionPolicy::check_size(
553 boost::type<ContainerType>(), obj_size))
return false;
556 bool is_same = PyRange_Check(obj_ptr) ||
557 (PySequence_Check(obj_ptr)
558 && !PyTuple_Check(obj_ptr) && !PyList_Check(obj_ptr));
561 handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
562 if (PyErr_Occurred()) {
566 if (!py_elem_hdl.get())
break;
567 object py_elem_obj(py_elem_hdl);
568 extract<container_element_type> elem_proxy(py_elem_obj);
569 if (!elem_proxy.check())
return false;
572 if (!is_same) assert(i == obj_size );
595 template <
typename T >
600 std::string tname(
typeid(std::vector<T>).name());
603 std_vector_to_list < T > ();
604 from_python_sequence < std::vector < T >,
609 template <
typename T >
614 template <
typename T >
622 casa_array_to_list < T > ();
623 casa_vector_to_list < T > ();
624 from_python_sequence < casacore::Vector < T >,
629 template <
typename T >
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
ConstIteratorSTL const_iterator
String: the storage and methods of handling collections of characters.
Prevent a converter from being registered multiple times.
static bool get(const std::string &name)
static std::map< std::string, bool > _registry
static void set(const std::string &name)
void register_convert_casa_string()
void register_convert_basicdata()
Register all standard basic conversions.
Bool PycArrayScalarCheck(PyObject *obj_ptr)
Check if the PyObject is an array scalar object.
void register_convert_casa_iposition()
void register_convert_casa_vector()
bool getSeqObject(boost::python::object &py_obj)
Check if the given object is a sequence object.
void register_convert_std_vector()
this file contains all the compiler specific defines
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
static void reserve(ContainerType &a, std::size_t sz)
Convert a String object from python.
static void * convertible(PyObject *obj_ptr)
casa_string_from_python_str()
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Convert a String object to python.
static PyObject * convert(String const &s)
static boost::python::object makeobject(String const &s)
static void reserve(ContainerType &a, std::size_t sz)
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
Register the IPosition conversion.
Register the String conversion.
Register the casacore::Vector conversions.
Register the std::vector conversions.
Default operations on all containers for conversion from Python container to C++ one.
static void assert_size(boost::type< ContainerType >, std::size_t)
static bool check_size(boost::type< ContainerType >, std::size_t)
static bool check_convertibility_per_element()
static void reserve(ContainerType &, std::size_t)
Conversion of Python sequence to C++ container.
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Constructs a C++ container from a Python sequence.
static ContainerType make_container(PyObject *obj_ptr)
Constructs a C++ container from a Python sequence.
static void fill_container(ContainerType &result, PyObject *obj_ptr)
static void * convertible(PyObject *obj_ptr)
Appears to return obj_ptr if it is type of Python sequence that can be convertible to C++ container.
static bool check_convertibility(PyObject *obj_ptr)
ContainerType::value_type container_element_type
Converts an STL vector or casa Array of T objects to Python list.
Operations on containers that have variable capacity for conversion from Python container to C++ one.
static void reserve(ContainerType &a, std::size_t sz)
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
static boost::python::object makeobject(ContainerType const &c)
static PyObject * convert(ContainerType const &c)
casacore::Array< casacore::String > ContainerType
static PyObject * convert(ContainerType const &c)
static boost::python::list makeobject(ContainerType const &c)
static PyObject * convert(ContainerType const &c)
casacore::Vector< casacore::String > ContainerType
static boost::python::object makeobject(ContainerType const &c)
std::vector< bool > ContainerType
static PyObject * convert(ContainerType const &c)
static boost::python::list makeobject(ContainerType const &c)
static boost::python::list makeobject(ContainerType const &c)
static PyObject * convert(ContainerType const &c)
std::vector< casacore::String > ContainerType
A wrapper of a conversion function to convert a STL vector to a Python list.
static PyObject * convert(ContainerType const &c)
static boost::python::object makeobject(ContainerType const &c)
Creates and returns a Python list from the elements copied from a STL container.