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: aips2-request@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//# $Id: PycArrayNP.h,v 1.1 2006/11/06 00:14:44 gvandiep Exp $
27
28
29#ifndef PYRAP_PYCARRAYNP_H
30#define PYRAP_PYCARRAYNP_H
31
32// include first to avoid _POSIX_C_SOURCE redefined warnings
33#include <boost/python.hpp>
34#include <boost/python/object.hpp>
35#include <casacore/casa/Containers/ValueHolder.h>
36#include <casacore/casa/Arrays/Array.h>
37#include <numpy/arrayobject.h>
38
39namespace casacore { namespace python { namespace numpy {
40
41#define PYC_USE_PYARRAY "numpy"
42#include <casacore/python/Converters/PycArrayComH.h>
43#undef PYC_USE_PYARRAY
44
45
46 //# Define functions to deal with numpy array scalars.
47
48 // Check if it is an array scalar object.
49 bool PycArrayScalarCheck (PyObject* obj, int& type);
50
51 // Get the data type of the array scalar object.
52 // It returns TpBool, TpInt, TpFloat, or TpComplex.
53 // TpOther is returned if unrecognized.
54 DataType PycArrayScalarType (PyObject* obj_ptr);
55
56 // Make a scalar object.
57 ValueHolder makeScalar (PyObject* obj, int type);
58
59 // Register all array scalar converters.
61
62 // Templated helper function to get a value from a ValueHolder.
63 // Specialize for each type supported.
64 // <group>
65 template<typename T> T getScalar (const ValueHolder&);
66 template<> inline Bool getScalar (const ValueHolder& vh)
67 { return vh.asBool(); }
68 template<> inline Char getScalar (const ValueHolder& vh)
69 { return vh.asShort(); }
70 template<> inline uChar getScalar (const ValueHolder& vh)
71 { return vh.asuChar(); }
72 template<> inline Short getScalar (const ValueHolder& vh)
73 { return vh.asShort(); }
74 template<> inline uShort getScalar (const ValueHolder& vh)
75 { return vh.asuShort(); }
76 template<> inline Int getScalar (const ValueHolder& vh)
77 { return vh.asInt(); }
78 template<> inline uInt getScalar (const ValueHolder& vh)
79 { return vh.asuInt(); }
80 template<> inline Long getScalar (const ValueHolder& vh)
81 { return vh.asInt(); }
82 template<> inline uLong getScalar (const ValueHolder& vh)
83 { return vh.asuInt(); }
84 template<> inline Int64 getScalar (const ValueHolder& vh)
85 { return vh.asInt(); }
86 template<> inline uInt64 getScalar (const ValueHolder& vh)
87 { return vh.asuInt(); }
88 template<> inline Float getScalar (const ValueHolder& vh)
89 { return vh.asFloat(); }
90 template<> inline Double getScalar (const ValueHolder& vh)
91 { return vh.asDouble(); }
92 template<> inline Complex getScalar (const ValueHolder& vh)
93 { return vh.asComplex(); }
94 template<> inline DComplex getScalar (const ValueHolder& vh)
95 { return vh.asDComplex(); }
96 // </group>
97
98 // Struct with static functions to convert a numpy array scalar to
99 // the templated type (e.g. Int).
100 template <typename T>
102 {
104 {
105 boost::python::converter::registry::push_back(
107 &construct,
108 boost::python::type_id<T>());
109 }
110
111 // Check if it is a type we can convert.
112 static void* convertible(PyObject* obj_ptr)
113 {
114 int type;
115 if (PycArrayScalarCheck(obj_ptr, type)) {
116 return obj_ptr;
117 }
118 return 0;
119 }
120
121 // Constructs a T from a Python array scalar object.
122 static void construct(
123 PyObject* obj_ptr,
124 boost::python::converter::rvalue_from_python_stage1_data* data)
125 {
126 using namespace boost::python;
127 void* storage = ((converter::rvalue_from_python_storage<T>*)
128 data)->storage.bytes;
129 new (storage) T();
130 data->convertible = storage;
131 int type;
132 PycArrayScalarCheck (obj_ptr, type);
133 *static_cast<T*>(storage) = getScalar<T> (makeScalar(obj_ptr, type));
134 }
135 };
136
137
138
139}}}
140
141#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:47
unsigned long uLong
Definition aipstype.h:53
long Long
Definition aipstype.h:52
short Short
Definition aipstype.h:48
unsigned int uInt
Definition aipstype.h:51
unsigned short uShort
Definition aipstype.h:49
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:38
float Float
Definition aipstype.h:54
int Int
Definition aipstype.h:50
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:42
double Double
Definition aipstype.h:55
char Char
Definition aipstype.h:46
unsigned long long uInt64
Definition aipsxtype.h:39
Struct with static functions to convert a numpy array scalar to the templated type (e....
Definition PycArrayNP.h:102
static void * convertible(PyObject *obj_ptr)
Check if it is a type we can convert.
Definition PycArrayNP.h:112
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:122