28 #ifndef PYRAP_PYCBASICDATA_H
29 #define PYRAP_PYCBASICDATA_H
32 #include <boost/python.hpp>
33 #include <boost/python/object.hpp>
34 #include <casacore/python/Converters/PycArray.h>
35 #include <casacore/casa/BasicSL/String.h>
36 #include <casacore/casa/Arrays/IPosition.h>
37 #include <casacore/casa/Arrays/Vector.h>
38 #include <casacore/casa/Utilities/Assert.h>
39 #include <casacore/casa/Exceptions/Error.h>
43 #if PY_MAJOR_VERSION >= 3
50 namespace casacore {
namespace python {
56 static bool get (
const std::string& name);
57 static void set (
const std::string& name);
72 {
return boost::python::object((
const std::string&)s); }
74 {
return boost::python::incref(
makeobject(s).ptr()); }
82 boost::python::converter::registry::push_back(
85 boost::python::type_id<String>());
91 if (!PyUnicode_Check(obj_ptr))
return 0;
93 if (!PyString_Check(obj_ptr) && !PyUnicode_Check(obj_ptr))
return 0;
100 boost::python::converter::rvalue_from_python_stage1_data* data)
103 boost::python::object temp_bytes_obj;
104 if (PyUnicode_Check(obj_ptr)) {
105 PyObject * temp_bytes = PyUnicode_AsEncodedString(obj_ptr,
"UTF-8",
"strict");
107 if (temp_bytes != NULL) {
109 temp_bytes_obj = boost::python::object(boost::python::handle<>(temp_bytes));
110 value = PyBytes_AS_STRING(temp_bytes);
112 boost::python::throw_error_already_set();
115 }
else if (PyString_Check(obj_ptr)) {
116 value = PyString_AsString(obj_ptr);
119 boost::python::throw_error_already_set();
121 if (
value == 0) boost::python::throw_error_already_set();
123 (boost::python::converter::rvalue_from_python_storage<String>*)
124 data)->storage.bytes;
126 data->convertible = storage;
146 template <
typename ContainerType>
147 static bool check_size(boost::type<ContainerType>, std::size_t)
152 template <
typename ContainerType>
156 template <
typename ContainerType>
157 static void reserve(ContainerType&, std::size_t)
174 template <
typename ContainerType>
175 static void reserve(ContainerType& a, std::size_t sz)
180 template <
typename ContainerType,
typename ValueType>
181 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
190 template <
typename ContainerType>
191 static void reserve(ContainerType& a, std::size_t sz)
196 template <
typename ContainerType,
typename ValueType>
197 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
199 assert(a.size() > i);
206 template <
typename ContainerType>
207 static void reserve(ContainerType& a, std::size_t sz)
212 template <
typename ContainerType,
typename ValueType>
213 static void set_value(ContainerType& a, std::size_t i, ValueType
const& v)
215 assert(a.size() > i);
216 a[a.size() - i - 1] = v;
233 template <
typename ContainerType >
243 boost::python::list result;
244 typename ContainerType::const_iterator i =
c.begin();
245 typename ContainerType::const_iterator iEnd =
c.end();
246 for( ; i != iEnd; ++i) {
264 boost::python::list result;
265 for (
int i=
c.size()-1; i>=0; --i) {
287 boost::python::list result;
288 ContainerType::const_iterator i =
c.begin();
289 ContainerType::const_iterator iEnd =
c.end();
290 for( ; i != iEnd; ++i) {
307 boost::python::list result;
308 ContainerType::const_iterator i =
c.begin();
309 ContainerType::const_iterator iEnd =
c.end();
310 for( ; i != iEnd; ++i) {
311 result.append((std::string
const&)(*i));
326 boost::python::list result;
329 for( ; i != iEnd; ++i) {
330 result.append((std::string
const&)(*i));
345 boost::python::list result;
348 for( ; i != iEnd; ++i) {
349 result.append((std::string
const&)(*i));
367 template <
typename T >
372 boost::python::to_python_converter < std::vector < T >,
376 template <
typename T >
381 boost::python::to_python_converter < casacore::Array < T >,
385 template <
typename T >
390 boost::python::to_python_converter < casacore::Vector < T >,
413 template <
typename ContainerType,
typename ConversionPolicy>
420 boost::python::converter::registry::push_back(
423 boost::python::type_id<ContainerType>());
430 using namespace boost::python;
431 handle<> py_hdl(obj_ptr);
432 if (PyErr_Occurred()) {
436 object py_obj(py_hdl);
439 if (PyBool_Check(obj_ptr)
440 || PyLong_Check(obj_ptr)
441 || PyFloat_Check(obj_ptr)
442 || PyComplex_Check(obj_ptr)
444 || PyInt_Check(obj_ptr)
445 || PyString_Check(obj_ptr)
447 || PyUnicode_Check(obj_ptr)) {
448 extract<container_element_type> elem_proxy(py_obj);
449 if (!elem_proxy.check())
return 0;
464 handle<> obj_iter(allow_null(PyObject_GetIter(py_obj.ptr())));
465 if (!obj_iter.get()) {
478 boost::python::converter::rvalue_from_python_stage1_data* data)
480 using namespace boost::python;
481 using boost::python::converter::rvalue_from_python_storage;
483 (rvalue_from_python_storage<ContainerType>*)
484 data)->storage.bytes;
485 new (storage) ContainerType();
486 data->convertible = storage;
487 ContainerType& result = *((ContainerType*)storage);
488 if (PyBool_Check(obj_ptr)
489 || PyLong_Check(obj_ptr)
490 || PyFloat_Check(obj_ptr)
491 || PyComplex_Check(obj_ptr)
492 || PyUnicode_Check(obj_ptr)
494 || PyString_Check(obj_ptr)
495 || PyInt_Check(obj_ptr)
498 extract<container_element_type> elem_proxy(obj_ptr);
499 ConversionPolicy::reserve(result, 1);
500 ConversionPolicy::set_value(result, 0, elem_proxy());
503 handle<> py_hdl(obj_ptr);
504 object py_obj = object(py_hdl);
516 ContainerType result;
524 using namespace boost::python;
525 int obj_size = PyObject_Length(obj_ptr);
526 handle<> obj_iter(PyObject_GetIter(obj_ptr));
527 ConversionPolicy::reserve(result, obj_size);
530 handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
531 if (PyErr_Occurred()) throw_error_already_set();
532 if (!py_elem_hdl.get())
break;
533 object py_elem_obj(py_elem_hdl);
534 extract<container_element_type> elem_proxy(py_elem_obj);
535 ConversionPolicy::set_value(result, i, elem_proxy());
537 ConversionPolicy::assert_size(boost::type<ContainerType>(), i);
542 using namespace boost::python;
543 handle<> obj_iter(allow_null(PyObject_GetIter(obj_ptr)));
544 if (!obj_iter.get()) {
548 int obj_size = PyObject_Length(obj_ptr);
553 if (ConversionPolicy::check_convertibility_per_element()) {
554 if (!ConversionPolicy::check_size(
555 boost::type<ContainerType>(), obj_size))
return false;
558 bool is_same = PyRange_Check(obj_ptr) ||
559 (PySequence_Check(obj_ptr)
560 && !PyTuple_Check(obj_ptr) && !PyList_Check(obj_ptr));
563 handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
564 if (PyErr_Occurred()) {
568 if (!py_elem_hdl.get())
break;
569 object py_elem_obj(py_elem_hdl);
570 extract<container_element_type> elem_proxy(py_elem_obj);
571 if (!elem_proxy.check())
return false;
574 if (!is_same) assert(i == obj_size );
597 template <
typename T >
602 std::string tname(
typeid(std::vector<T>).name());
611 template <
typename T >
616 template <
typename T >
631 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)
const Double c
Fundamental physical constants (SI units):
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.
casa_string_from_python_str()
static void * convertible(PyObject *obj_ptr)
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Convert a String object to python.
static boost::python::object makeobject(String const &s)
static PyObject * convert(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 * convertible(PyObject *obj_ptr)
Appears to return obj_ptr if it is type of Python sequence that can be convertible to C++ container.
static void fill_container(ContainerType &result, PyObject *obj_ptr)
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.