casacore
Loading...
Searching...
No Matches
PycArrayNP.h
Go to the documentation of this file.
1//# PycArrayNP.h: Class to convert an Array to/from a Python numpy array
2//# Copyright (C) 2006
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26
27#ifndef PYRAP_PYCARRAYNP_H
28#define PYRAP_PYCARRAYNP_H
29
30// include first to avoid _POSIX_C_SOURCE redefined warnings
31#include <boost/python.hpp>
32#include <boost/python/object.hpp>
33#include <casacore/casa/Containers/ValueHolder.h>
34#include <casacore/casa/Arrays/Array.h>
35#include <numpy/arrayobject.h>
36
37namespace casacore { namespace python { namespace numpy {
38
39#define PYC_USE_PYARRAY "numpy"
40#include <casacore/python/Converters/PycArrayComH.h>
41#undef PYC_USE_PYARRAY
42
43
44 //# Define functions to deal with numpy array scalars.
45
46 // Check if it is an array scalar object.
47 bool PycArrayScalarCheck (PyObject* obj, int& type);
48
49 // Get the data type of the array scalar object.
50 // It returns TpBool, TpInt, TpFloat, or TpComplex.
51 // TpOther is returned if unrecognized.
52 DataType PycArrayScalarType (PyObject* obj_ptr);
53
54 // Make a scalar object.
55 ValueHolder makeScalar (PyObject* obj, int type);
56
57 // Register all array scalar converters.
59
60 // Templated helper function to get a value from a ValueHolder.
61 // Specialize for each type supported.
62 // <group>
63 template<typename T> T getScalar (const ValueHolder&);
64 template<> inline Bool getScalar (const ValueHolder& vh)
65 { return vh.asBool(); }
66 template<> inline Char getScalar (const ValueHolder& vh)
67 { return vh.asShort(); }
68 template<> inline uChar getScalar (const ValueHolder& vh)
69 { return vh.asuChar(); }
70 template<> inline Short getScalar (const ValueHolder& vh)
71 { return vh.asShort(); }
72 template<> inline uShort getScalar (const ValueHolder& vh)
73 { return vh.asuShort(); }
74 template<> inline Int getScalar (const ValueHolder& vh)
75 { return vh.asInt(); }
76 template<> inline uInt getScalar (const ValueHolder& vh)
77 { return vh.asuInt(); }
78 template<> inline Long getScalar (const ValueHolder& vh)
79 { return vh.asInt(); }
80 template<> inline uLong getScalar (const ValueHolder& vh)
81 { return vh.asuInt(); }
82 template<> inline Int64 getScalar (const ValueHolder& vh)
83 { return vh.asInt(); }
84 template<> inline uInt64 getScalar (const ValueHolder& vh)
85 { return vh.asuInt(); }
86 template<> inline Float getScalar (const ValueHolder& vh)
87 { return vh.asFloat(); }
88 template<> inline Double getScalar (const ValueHolder& vh)
89 { return vh.asDouble(); }
90 template<> inline Complex getScalar (const ValueHolder& vh)
91 { return vh.asComplex(); }
92 template<> inline DComplex getScalar (const ValueHolder& vh)
93 { return vh.asDComplex(); }
94 // </group>
95
96 // Struct with static functions to convert a numpy array scalar to
97 // the templated type (e.g. Int).
98 template <typename T>
100 {
102 {
103 boost::python::converter::registry::push_back(
105 &construct,
106 boost::python::type_id<T>());
107 }
108
109 // Check if it is a type we can convert.
110 static void* convertible(PyObject* obj_ptr)
111 {
112 int type;
113 if (PycArrayScalarCheck(obj_ptr, type)) {
114 return obj_ptr;
115 }
116 return 0;
117 }
118
119 // Constructs a T from a Python array scalar object.
120 static void construct(
121 PyObject* obj_ptr,
122 boost::python::converter::rvalue_from_python_stage1_data* data)
123 {
124 using namespace boost::python;
125 void* storage = ((converter::rvalue_from_python_storage<T>*)
126 data)->storage.bytes;
127 new (storage) T();
128 data->convertible = storage;
129 int type;
130 PycArrayScalarCheck (obj_ptr, type);
131 *static_cast<T*>(storage) = getScalar<T> (makeScalar(obj_ptr, type));
132 }
133 };
134
135
136
137}}}
138
139#endif
Complex asComplex() const
Double asDouble() const
Float asFloat() const
Short asShort() const
uChar asuChar() const
uShort asuShort() const
Bool asBool() const
Get the value.
DComplex asDComplex() const
ValueHolder makeScalar(PyObject *obj, int type)
Make a scalar object.
T getScalar(const ValueHolder &)
Templated helper function to get a value from a ValueHolder.
void register_convert_arrayscalars()
Register all array scalar converters.
bool PycArrayScalarCheck(PyObject *obj, int &type)
Check if it is an array scalar object.
DataType PycArrayScalarType(PyObject *obj_ptr)
Get the data type of the array scalar object.
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned char uChar
Definition aipstype.h:45
unsigned long uLong
Definition aipstype.h:51
long Long
Definition aipstype.h:50
short Short
Definition aipstype.h:46
unsigned int uInt
Definition aipstype.h:49
unsigned short uShort
Definition aipstype.h:47
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53
char Char
Definition aipstype.h:44
unsigned long long uInt64
Definition aipsxtype.h:37
Struct with static functions to convert a numpy array scalar to the templated type (e....
Definition PycArrayNP.h:100
static void * convertible(PyObject *obj_ptr)
Check if it is a type we can convert.
Definition PycArrayNP.h:110
static void construct(PyObject *obj_ptr, boost::python::converter::rvalue_from_python_stage1_data *data)
Constructs a T from a Python array scalar object.
Definition PycArrayNP.h:120