@@ -15,45 +15,48 @@ namespace cpp2py {
15
15
// c2py implementation
16
16
template <typename T, auto ... Is> static PyObject *c2py_impl (T &&t, std::index_sequence<Is...>) {
17
17
auto objs = std::array<pyref, sizeof ...(Is)>{convert_to_python (std::get<Is>(std::forward<T>(t)))...};
18
- bool one_is_null = std::accumulate (std::begin (objs), std::end (objs), false , [](bool x, PyObject *a) { return x or ( a == NULL ) ; });
18
+ bool one_is_null = std::any_of (std::begin (objs), std::end (objs), [](PyObject *a) { return a == NULL ; });
19
19
if (one_is_null) return NULL ;
20
20
return PyTuple_Pack (sizeof ...(Types), (PyObject *)(objs[Is])...);
21
21
}
22
22
23
- // is_convertible implementation
24
- template <auto ... Is> static bool is_convertible_impl (PyObject *seq, bool raise_exception, std::index_sequence<Is...>) {
25
- return (py_converter<std::decay_t <Types>>::is_convertible (PySequence_Fast_GET_ITEM (seq, Is), raise_exception) and ...);
26
- }
27
-
28
- template <auto ... Is> static auto py2c_impl (PyObject *seq, std::index_sequence<Is...>) {
29
- return std::make_tuple (py_converter<std::decay_t <Types>>::py2c (PySequence_Fast_GET_ITEM (seq, Is))...);
30
- }
31
-
32
23
public:
33
- // -----------------------------------------
34
-
35
24
template <typename T> static PyObject *c2py (T &&t) {
36
25
static_assert (is_instantiation_of_v<std::tuple, std::decay_t <T>>, " Logic Error" );
37
26
return c2py_impl (std::forward<T>(t), std::make_index_sequence<sizeof ...(Types)>());
38
27
}
39
28
40
29
// -----------------------------------------
41
30
31
+ private:
32
+ // is_convertible implementation
33
+ template <auto ... Is> static bool is_convertible_impl (PyObject *seq, bool raise_exception, std::index_sequence<Is...>) {
34
+ return (py_converter<std::decay_t <Types>>::is_convertible (PySequence_Fast_GET_ITEM (seq, Is), raise_exception) and ...);
35
+ }
36
+
37
+ public:
42
38
static bool is_convertible (PyObject *ob, bool raise_exception) {
43
- if (PySequence_Check (ob)) {
44
- pyref seq = PySequence_Fast (ob, " expected a sequence" );
45
- // Sizes must match! Could we relax this condition to '<'?
46
- if (PySequence_Fast_GET_SIZE ((PyObject *)seq) != std::tuple_size<tuple_t >::value) goto _false;
47
- if (!is_convertible_impl ((PyObject *)seq, raise_exception, std::make_index_sequence<sizeof ...(Types)>())) goto _false;
48
- return true ;
39
+ if (not PySequence_Check (ob)) {
40
+ if (raise_exception) { PyErr_SetString (PyExc_TypeError, " Cannot convert non-sequence to std::tuple" ); }
41
+ return false ;
42
+ }
43
+ pyref seq = PySequence_Fast (ob, " expected a sequence" );
44
+ // Sizes must match! Could we relax this condition to '<'?
45
+ if (PySequence_Fast_GET_SIZE ((PyObject *)seq) != std::tuple_size<tuple_t >::value) {
46
+ if (raise_exception) { PyErr_SetString (PyExc_TypeError, " Cannot convert to std::tuple due to improper length" ); }
47
+ return false ;
49
48
}
50
- _false:
51
- if (raise_exception) { PyErr_SetString (PyExc_TypeError, " Cannot convert to std::tuple" ); }
52
- return false ;
49
+ return is_convertible_impl ((PyObject *)seq, raise_exception, std::make_index_sequence<sizeof ...(Types)>());
53
50
}
54
51
55
52
// -----------------------------------------
56
53
54
+ private:
55
+ template <auto ... Is> static auto py2c_impl (PyObject *seq, std::index_sequence<Is...>) {
56
+ return std::make_tuple (py_converter<std::decay_t <Types>>::py2c (PySequence_Fast_GET_ITEM (seq, Is))...);
57
+ }
58
+
59
+ public:
57
60
static tuple_t py2c (PyObject *ob) {
58
61
pyref seq = PySequence_Fast (ob, " expected a sequence" );
59
62
return py2c_impl ((PyObject *)seq, std::make_index_sequence<sizeof ...(Types)>());
0 commit comments