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
|
/*************************************************************************
* odil - Copyright (C) Universite de Strasbourg
* Distributed under the terms of the CeCILL-B license, as published by
* the CEA-CNRS-INRIA. Refer to the LICENSE file or to
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
* for details.
************************************************************************/
#ifndef _0dcf136c_4136_40c3_a036_2a74ca0c54d1
#define _0dcf136c_4136_40c3_a036_2a74ca0c54d1
#include <algorithm>
#include "pybind11/pybind11.h"
#include "odil/DataSet.h"
#include "odil/Exception.h"
#include "odil/Value.h"
// The following must be in every translation unit.
PYBIND11_MAKE_OPAQUE(odil::Value::Integers);
PYBIND11_MAKE_OPAQUE(odil::Value::Reals);
PYBIND11_MAKE_OPAQUE(odil::Value::Strings);
PYBIND11_MAKE_OPAQUE(odil::Value::DataSets);
PYBIND11_MAKE_OPAQUE(odil::Value::Binary);
PYBIND11_MAKE_OPAQUE(odil::Value::Binary::value_type);
template<typename T, typename ... Args>
T convert_sequence(pybind11::sequence & source, Args && ... args)
{
if(pybind11::len(source) == 0)
{
throw odil::Exception("Empty sequence has no type");
}
#define try_convert(Items) \
try \
{ \
Items items(len(source)); \
std::transform( \
source.begin(), source.end(), items.begin(), \
[](pybind11::handle const h) { \
return h.cast<typename Items::value_type>(); \
} \
); \
return T{items, std::forward<Args>(args)...}; \
} \
catch(pybind11::cast_error const &) \
{ \
/* Ignore */ \
}
// WARNING: Strings and DataSets are ambiguous when passing bytearray
// Skip conversion to strings if we get an sequence of bytearrays
auto const is_byte_array = std::all_of(
source.begin(), source.end(), [](pybind11::object && x) {
return PyByteArray_CheckExact(x.ptr()); });
try_convert(odil::Value::Integers);
try_convert(odil::Value::Reals);
if(!is_byte_array)
{
try_convert(odil::Value::Strings);
}
try_convert(odil::Value::DataSets);
try_convert(odil::Value::Binary);
#undef try_convert
try
{
odil::Value::Binary items(len(source));
std::transform(
source.begin(), source.end(), items.begin(),
[](pybind11::handle const h)
{
auto pyobject = h.ptr();
if(PyByteArray_Check(pyobject))
{
auto const begin = PyByteArray_AsString(pyobject);
auto const length = PyObject_Length(pyobject);
return odil::Value::Binary::value_type{begin, begin+length};
}
else
{
throw pybind11::cast_error();
}
}
);
return T{items, std::forward<Args>(args)...};
}
catch(pybind11::cast_error const &)
{
/* ignore */
}
throw odil::Exception("Unknown value type");
}
#endif // _0dcf136c_4136_40c3_a036_2a74ca0c54d1
|