casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
37 #include <numpy/arrayobject.h>
38 
39 namespace casacore { namespace python { namespace numpy {
40 
41 #define PYC_USE_PYARRAY "numpy"
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(
106  &convertible,
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
uChar asuChar() const
Definition: ValueHolder.h:233
Bool asBool() const
Get the value.
Definition: ValueHolder.h:231
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
DataType PycArrayScalarType(PyObject *obj_ptr)
Get the data type of the array scalar object.
Double asDouble() const
Definition: ValueHolder.h:247
static void * convertible(PyObject *obj_ptr)
Check if it is a type we can convert.
Definition: PycArrayNP.h:112
DComplex asDComplex() const
Definition: ValueHolder.h:251
unsigned long long uInt64
Definition: aipsxtype.h:39
unsigned char uChar
Definition: aipstype.h:47
char Char
Definition: aipstype.h:46
virtual casacore::String type() const
Implements RegionShape::type.
Definition: RegionShapes.h:548
uShort asuShort() const
Definition: ValueHolder.h:237
short Short
Definition: aipstype.h:48
ABSTRACT CLASSES Deliberately vague to be general enough to allow for many different types of data
Definition: PlotData.h:48
Float asFloat() const
Definition: ValueHolder.h:245
long Long
Definition: aipstype.h:52
ValueHolder makeScalar(PyObject *obj, int type)
Make a scalar object.
uInt asuInt() const
Definition: ValueHolder.h:241
Complex asComplex() const
Definition: ValueHolder.h:249
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.
Short asShort() const
Definition: ValueHolder.h:235
Struct with static functions to convert a numpy array scalar to the templated type (e...
Definition: PycArrayNP.h:101
Int asInt() const
Definition: ValueHolder.h:239
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
float Float
Definition: aipstype.h:54
T getScalar(const ValueHolder &)
Templated helper function to get a value from a ValueHolder.
Definition: PycArrayNP.h:66
unsigned long uLong
Definition: aipstype.h:53
bool PycArrayScalarCheck(PyObject *obj, int &type)
Check if it is an array scalar object.
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
unsigned int uInt
Definition: aipstype.h:51
unsigned short uShort
Definition: aipstype.h:49
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42