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