28 #ifndef PYRAP_PYCBASICDATA_H
29 #define PYRAP_PYCBASICDATA_H
32 #include <boost/python.hpp>
33 #include <boost/python/object.hpp>
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);
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)
104 if (PyUnicode_Check(obj_ptr)) {
105 PyObject * temp_bytes = PyUnicode_AsEncodedString(obj_ptr,
"UTF-8",
"strict");
107 if (temp_bytes != NULL) {
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;
125 new (storage)
String(value);
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;
246 for( ; i != iEnd; ++i) {
253 return boost::python::incref(
makeobject(c).ptr());
264 boost::python::list result;
265 for (
int i=c.
size()-1; i>=0; --i) {
272 return boost::python::incref(
makeobject(c).ptr());
287 boost::python::list result;
290 for( ; i != iEnd; ++i) {
298 return boost::python::incref(
makeobject(c).ptr());
302 struct to_list <std::vector <casacore::String> >
307 boost::python::list result;
310 for( ; i != iEnd; ++i) {
311 result.append((std::string
const&)(*i));
317 return boost::python::incref(
makeobject(c).ptr());
326 boost::python::list result;
329 for( ; i != iEnd; ++i) {
330 result.append((std::string
const&)(*i));
336 return boost::python::incref(
makeobject(c).ptr());
345 boost::python::list result;
348 for( ; i != iEnd; ++i) {
349 result.append((std::string
const&)(*i));
355 return boost::python::incref(
makeobject(c).ptr());
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 >
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
static PyObject * convert(ContainerType const &c)
A Vector of integers, for indexing into Array<T> objects.
Register the casacore::Vector conversions.
A 1-D Specialization of the Array class.
void register_convert_casa_string()
static bool check_size(boost::type< ContainerType >, std::size_t)
Convert a String object to python.
Elements::const_iterator const_iterator
static boost::python::list makeobject(ContainerType const &c)
static boost::python::object makeobject(String const &s)
void register_convert_basicdata()
Register all standard basic conversions.
static boost::python::list makeobject(ContainerType const &c)
casa_string_from_python_str()
iterator begin()
Get the begin iterator object for any array.
static boost::python::object makeobject(ContainerType const &c)
ABSTRACT CLASSES Abstract class for colors Any implementation of color should be able to provide a hexadecimal form of the if a human readable name(i.e."black").In many places throughout the plotter
static void assert_size(boost::type< ContainerType >, std::size_t)
std::vector< bool > ContainerType
ABSTRACT CLASSES Deliberately vague to be general enough to allow for many different types of data
void register_convert_std_vector()
Register the String conversion.
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Constructs a C++ container from a Python sequence.
Register the std::vector conversions.
std::string object(const std::string &name)
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...
bool getSeqObject(boost::python::object &py_obj)
Check if the given object is a sequence object.
static void reserve(ContainerType &a, std::size_t sz)
static boost::python::object makeobject(ContainerType const &c)
Creates and returns a Python list from the elements copied from a STL container.
static void fill_container(ContainerType &result, PyObject *obj_ptr)
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
static void reserve(ContainerType &a, std::size_t sz)
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
Bool PycArrayScalarCheck(PyObject *obj_ptr)
Check if the PyObject is an array scalar object.
Converts an STL vector or casa Array of T objects to Python list.
void register_convert_casa_iposition()
static bool check_convertibility(PyObject *obj_ptr)
void register_convert_casa_vector()
static PyObject * convert(ContainerType const &c)
template <class T, class U> class vector;
static boost::python::object makeobject(ContainerType const &c)
static std::map< std::string, bool > _registry
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Register the IPosition conversion.
Operations on containers that have variable capacity for conversion from Python container to C++ one...
Base class for all Casacore library errors.
static void * convertible(PyObject *obj_ptr)
static ContainerType make_container(PyObject *obj_ptr)
Constructs a C++ container from a Python sequence.
const Double c
Fundamental physical constants (SI units):
Prevent a converter from being registered multiple times.
A wrapper of a conversion function to convert a STL vector to a Python list.
String: the storage and methods of handling collections of characters.
Default operations on all containers for conversion from Python container to C++ one.
static void reserve(ContainerType &, std::size_t)
static PyObject * convert(ContainerType const &c)
static PyObject * convert(ContainerType const &c)
static void set(const std::string &name)
casacore::Vector< casacore::String > ContainerType
static PyObject * convert(String const &s)
static bool get(const std::string &name)
static PyObject * convert(ContainerType const &c)
ContainerType::value_type container_element_type
static boost::python::list makeobject(ContainerType const &c)
static bool check_convertibility_per_element()
Conversion of Python sequence to C++ container.
static void reserve(ContainerType &a, std::size_t sz)
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
casacore::Array< casacore::String > ContainerType
Convert a String object from python.
static void set_value(ContainerType &a, std::size_t i, ValueType const &v)
std::vector< casacore::String > ContainerType
static PyObject * convert(ContainerType const &c)
ConstIteratorSTL const_iterator
#define casacore
<X11/Intrinsic.h> #defines true, false, casacore::Bool, and String.