LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Vector.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #ifndef NDARRAY_SWIG_Vector_h_INCLUDED
25 #define NDARRAY_SWIG_Vector_h_INCLUDED
26 
33 
34 namespace ndarray {
35 
40 template <typename T, int N>
41 struct PyConverter< Vector<T,N> > : public detail::PyConverterBase< Vector<T,N> > {
42 
49  static PyObject * toPython(
50  Vector<T,N> const & input
51  ) {
52  PyObject * r = PyTuple_New(N);
53  for (int i=0; i<N; ++i) {
54  PyTuple_SET_ITEM(r,i,PyConverter<T>::toPython(input[i]));
55  }
56  return r;
57  }
58 
63  static PyTypeObject const * getPyType() { return &PyTuple_Type; }
64 
73  static bool fromPythonStage1(
74  PyPtr & p
78  ) {
79  if (!PySequence_Check(p.get())) {
80  PyErr_Format(PyExc_TypeError,"Expected a Python sequence of length %i.",N);
81  return false;
82  }
83  if (PySequence_Size(p.get()) != N) {
84  PyErr_Format(PyExc_ValueError,"Incorrect sequence length for Vector<T,%i>", N);
85  return false;
86  }
87  return true;
88  }
89 
97  static bool fromPythonStage2(
98  PyPtr const & p,
99  Vector<T,N> & output
100  ) {
101  NDARRAY_ASSERT(p);
102  NDARRAY_ASSERT(PySequence_Check(p.get()));
103  Vector<T,N> tmp;
104  for (int n=0; n<N; ++n) {
105  PyPtr item(PySequence_ITEM(p.get(),n),false);
106  if (!item) return false;
107  if (!PyConverter<T>::fromPythonStage1(item)) return false;
108  if (!PyConverter<T>::fromPythonStage2(item,tmp[n])) return false;
109  }
110  output = tmp;
111  return true;
112  }
113 
114 };
115 
116 } // namespace ndarray
117 
118 #endif // !NDARRAY_SWIG_Vector_h_INCLUDED
boost::intrusive_ptr< PyObject > PyPtr
A reference-counting smart pointer for PyObject.
Definition: PyConverter.h:48
static PyTypeObject const * getPyType()
Return the Python TypeObject that corresponds to the object the toPython() function returns...
Definition: Vector.h:63
#define NDARRAY_ASSERT(ARG)
Definition: ndarray_fwd.h:51
A fixed-size 1D array class.
Definition: Vector.h:93
A class providing Python conversion functions for T.
Definition: PyConverter.h:50
static PyObject * toPython(Vector< T, N > const &input)
Convert a Vector to a new Python object.
Definition: Vector.h:49
static bool fromPythonStage1(PyPtr &p)
Check if a Python object is convertible to T and optionally begin the conversion by replacing the inp...
Definition: Vector.h:73
Python C-API conversions for standard numeric types.
static bool fromPythonStage2(PyPtr const &p, Vector< T, N > &output)
Complete a Python to C++ conversion begun with fromPythonStage1().
Definition: Vector.h:97