/usr/include/vigra/numpy_array_converters.hxx is in libvigraimpex-dev 1.9.0+dfsg-10+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | /************************************************************************/
/* */
/* Copyright 2009 by Ullrich Koethe and Hans Meine */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_NUMPY_ARRAY_CONVERTERS_HXX
#define VIGRA_NUMPY_ARRAY_CONVERTERS_HXX
#include "numpy_array.hxx"
#include "metaprogramming.hxx"
#include <boost/python.hpp>
#include <boost/python/to_python_converter.hpp>
#include <set>
namespace vigra {
template <class Array>
PyObject * returnNumpyArray(Array const & a)
{
PyObject * pa = a.pyObject();
if(pa == 0)
PyErr_SetString(PyExc_ValueError, "returnNumpyArray(): Conversion to Python failed, array has no data.");
else
Py_INCREF(pa);
return pa;
}
VIGRA_EXPORT std::set<std::string> & exportedArrayKeys();
template <class ArrayType>
struct NumpyArrayConverter {};
template <unsigned int N, class T, class Stride>
struct NumpyArrayConverter<NumpyArray<N, T, Stride> >
{
typedef NumpyArray<N, T, Stride> ArrayType;
typedef typename ArrayType::ArrayTraits ArrayTraits;
NumpyArrayConverter();
static void* convertible(PyObject* obj);
// from Python
static void construct(PyObject* obj,
boost::python::converter::rvalue_from_python_stage1_data* data);
// to Python
static PyObject* convert(ArrayType const& a)
{
return returnNumpyArray(a);
}
};
template <unsigned int N, class T, class Stride>
NumpyArrayConverter<NumpyArray<N, T, Stride> >::NumpyArrayConverter()
{
using namespace boost::python;
converter::registration const * reg = converter::registry::query(type_id<ArrayType>());
// register the to_python_converter only once
// FIXME: I'm not sure if this is correct.
if(!reg || !reg->rvalue_chain)
{
to_python_converter<ArrayType, NumpyArrayConverter>();
}
converter::registry::insert(&convertible, &construct, type_id<ArrayType>());
}
template <unsigned int N, class T, class Stride>
void * NumpyArrayConverter<NumpyArray<N, T, Stride> >::convertible(PyObject* obj)
{
bool isCompatible = obj == Py_None || ArrayType::isStrictlyCompatible(obj);
// std::cerr << "compatible for " << typeid(NumpyArray<N, T, Stride>).name() << ": " << isCompatible << "\n";
return isCompatible
? obj
: 0;
}
// from Python
template <unsigned int N, class T, class Stride>
void NumpyArrayConverter<NumpyArray<N, T, Stride> >::construct(PyObject* obj,
boost::python::converter::rvalue_from_python_stage1_data* data)
{
void* const storage =
((boost::python::converter::rvalue_from_python_storage<ArrayType>* ) data)->storage.bytes;
ArrayType * array = new (storage) ArrayType();
if(obj != Py_None)
array->makeReferenceUnchecked(obj);
data->convertible = storage;
}
template <unsigned int N, class T, class Stride>
struct NumpyArrayConverter<MultiArrayView<N, T, Stride> >
: public NumpyArrayConverter<NumpyArray<N, T, Stride> >
{
typedef NumpyArrayConverter<NumpyArray<N, T, Stride> > BaseType;
typedef MultiArrayView<N, T, Stride> ArrayType;
NumpyArrayConverter()
{
using namespace boost::python;
converter::registry::insert(&BaseType::convertible, &BaseType::construct,
type_id<ArrayType>());
}
};
template <class Iter, class End>
struct RegisterNumpyArrayConverters
{
static void exec()
{
typedef typename UnqualifiedType<typename boost::mpl::deref<Iter>::type>::type Type;
NumpyArrayConverter<Type>();
RegisterNumpyArrayConverters<typename boost::mpl::next<Iter>::type, End>::exec();
}
};
template <class End>
struct RegisterNumpyArrayConverters<End, End>
{
static void exec()
{}
};
template <class Typelist>
void registerNumpyArrayConverters(Typelist)
{
RegisterNumpyArrayConverters<typename boost::mpl::begin<Typelist>::type,
typename boost::mpl::end<Typelist>::type >::exec();
}
template <class FN>
FN registerConverters(FN f)
{
registerNumpyArrayConverters(boost::python::detail::get_signature(f));
return f;
}
} // namespace vigra
namespace boost { namespace python {
#define VIGRA_PYTHON_MULTITYPE_FUNCTOR(functor_name, function) \
template <class T> \
struct functor_name##Impl \
{ \
typedef functor_name##Impl type; \
\
static void def(const char * pythonName) \
{ \
boost::python::def(pythonName, vigra::registerConverters(&function<T>)); \
} \
\
template <class A1> \
static void def(const char * pythonName, A1 const & a1) \
{ \
boost::python::def(pythonName, vigra::registerConverters(&function<T>), a1); \
} \
\
template <class A1, class A2> \
static void def(const char * pythonName, A1 const & a1, A2 const & a2) \
{ \
boost::python::def(pythonName, vigra::registerConverters(&function<T>), a1, a2); \
} \
\
template <class A1, class A2, class A3> \
static void def(const char * pythonName, A1 const & a1, A2 const & a2, A3 const & a3) \
{ \
boost::python::def(pythonName, vigra::registerConverters(&function<T>), a1, a2, a3); \
} \
}; \
\
template <> \
struct functor_name##Impl<void> \
{ \
typedef void type; \
}; \
\
template <class T1, \
class T2 = void, \
class T3 = void, \
class T4 = void, \
class T5 = void, \
class T6 = void, \
class T7 = void, \
class T8 = void, \
class T9 = void, \
class T10 = void> \
struct functor_name \
: public boost::python::TypeList<typename functor_name##Impl<T1>::type, \
boost::python::TypeList<typename functor_name##Impl<T2>::type, \
boost::python::TypeList<typename functor_name##Impl<T3>::type, \
boost::python::TypeList<typename functor_name##Impl<T4>::type, \
boost::python::TypeList<typename functor_name##Impl<T5>::type, \
boost::python::TypeList<typename functor_name##Impl<T6>::type, \
boost::python::TypeList<typename functor_name##Impl<T7>::type, \
boost::python::TypeList<typename functor_name##Impl<T8>::type, \
boost::python::TypeList<typename functor_name##Impl<T9>::type, \
boost::python::TypeList<typename functor_name##Impl<T10>::type, \
boost::python::TypeList<void, void> > > > > > > > > > > \
{};
template <class Head, class Tail>
struct TypeList
{
typedef Head head;
typedef Tail tail;
};
// in the sequel, the doc string is only registered with the last
// overload, so that it shows up only once
template <class Head, class Tail>
inline void multidef(char const* functor_name, TypeList<Head, Tail>)
{
Head::def(functor_name);
multidef(functor_name, Tail());
}
template <class Head, class Tail>
inline void multidef(char const* functor_name, TypeList<Head, Tail>, const char * help)
{
Head::def(functor_name);
multidef(functor_name, Tail(), help);
}
template <class Head, class Tail, class Args>
inline void multidef(char const* functor_name, TypeList<Head, Tail>, Args const& args)
{
Head::def(functor_name, args);
multidef(functor_name, Tail(), args);
}
template <class Head, class Tail, class Args>
inline void multidef(char const* functor_name, TypeList<Head, Tail>, Args const& args, char const * help)
{
Head::def(functor_name, args);
multidef(functor_name, Tail(), args, help);
}
template <class Head, class Tail>
inline void multidef(char const* functor_name, TypeList<Head, TypeList<void, Tail> >)
{
Head::def(functor_name);
}
template <class Head, class Tail, class Args>
inline void multidef(char const* functor_name, TypeList<Head, TypeList<void, Tail> >, Args const& args)
{
Head::def(functor_name, args);
}
template <class Head, class Tail>
inline void multidef(char const* functor_name, TypeList<Head, TypeList<void, Tail> >, const char * help)
{
Head::def(functor_name, help);
}
template <class Head, class Tail, class Args>
inline void multidef(char const* functor_name, TypeList<Head, TypeList<void, Tail> >, Args const& args, const char * help)
{
Head::def(functor_name, args, help);
}
}} // namespace boost::python
#endif // VIGRA_NUMPY_ARRAY_CONVERTERS_HXX
|