Skip to content

Commit 36b91f4

Browse files
Merge pull request #13 from SylvainCorlay/no-macro
Replace function-like macros with functions
2 parents 818443d + ebd4a75 commit 36b91f4

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

include/xtensor-python/pyarray.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ namespace xt
493493
template <class T, int ExtraFlags>
494494
inline auto pyarray<T, ExtraFlags>::storage_begin() -> storage_iterator
495495
{
496-
return reinterpret_cast<storage_iterator>(PyArray_GET_(m_ptr, data));
496+
return reinterpret_cast<storage_iterator>(pybind11::backport::array_proxy(m_ptr)->data);
497497
}
498498

499499
template <class T, int ExtraFlags>
@@ -505,7 +505,7 @@ namespace xt
505505
template <class T, int ExtraFlags>
506506
inline auto pyarray<T, ExtraFlags>::storage_begin() const -> const_storage_iterator
507507
{
508-
return reinterpret_cast<const_storage_iterator>(PyArray_GET_(m_ptr, data));
508+
return reinterpret_cast<const_storage_iterator>(pybind11::backport::array_proxy(m_ptr)->data);
509509
}
510510

511511
template <class T, int ExtraFlags>

include/xtensor-python/pybind11_backport.hpp

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,25 @@ namespace pybind11
4646
PyObject* names;
4747
};
4848

49-
#ifndef PyArray_GET_
50-
#define PyArray_GET_(ptr, attr) \
51-
(reinterpret_cast<::pybind11::backport::PyArray_Proxy*>(ptr)->attr)
52-
#endif
53-
#ifndef PyArrayDescr_GET_
54-
#define PyArrayDescr_GET_(ptr, attr) \
55-
(reinterpret_cast<::pybind11::backport::PyArrayDescr_Proxy*>(ptr)->attr)
56-
#endif
49+
inline PyArray_Proxy* array_proxy(void* ptr) {
50+
return reinterpret_cast<PyArray_Proxy*>(ptr);
51+
}
52+
53+
inline const PyArray_Proxy* array_proxy(const void* ptr) {
54+
return reinterpret_cast<const PyArray_Proxy*>(ptr);
55+
}
56+
57+
inline PyArrayDescr_Proxy* array_descriptor_proxy(PyObject* ptr) {
58+
return reinterpret_cast<PyArrayDescr_Proxy*>(ptr);
59+
}
60+
61+
inline const PyArrayDescr_Proxy* array_descriptor_proxy(const PyObject* ptr) {
62+
return reinterpret_cast<const PyArrayDescr_Proxy*>(ptr);
63+
}
64+
65+
inline bool check_flags(const void* ptr, int flag) {
66+
return (flag == (array_proxy(ptr)->flags & flag));
67+
}
5768

5869
class array : public pybind11::array
5970
{
@@ -73,7 +84,10 @@ namespace pybind11
7384
format_descriptor<T>::value,
7485
shape.size(), shape, strides))
7586
{
76-
if (base) throw std::runtime_error("array base is not supported yet");
87+
if (base)
88+
{
89+
throw std::runtime_error("array base is not supported yet");
90+
}
7791
}
7892

7993
template<typename T>
@@ -96,32 +110,32 @@ namespace pybind11
96110

97111
size_type itemsize() const
98112
{
99-
return static_cast<size_type>(PyArrayDescr_GET_(PyArray_GET_(m_ptr, descr), elsize));
113+
return static_cast<size_type>(array_descriptor_proxy(array_proxy(m_ptr)->descr)->elsize);
100114
}
101115

102116
size_type ndim() const
103117
{
104-
return static_cast<size_type>(PyArray_GET_(m_ptr, nd));
118+
return static_cast<size_type>(array_proxy(m_ptr)->nd);
105119
}
106120

107121
const size_type* shape() const
108122
{
109-
return reinterpret_cast<const size_type*>(PyArray_GET_(m_ptr, dimensions));
123+
return reinterpret_cast<const size_type*>(array_proxy(m_ptr)->dimensions);
110124
}
111125

112126
const size_type* strides() const
113127
{
114-
return reinterpret_cast<const size_type*>(PyArray_GET_(m_ptr, strides));
128+
return reinterpret_cast<const size_type*>(array_proxy(m_ptr)->strides);
115129
}
116130

117131
void* data()
118132
{
119-
return static_cast<void*>(PyArray_GET_(m_ptr, data));
133+
return static_cast<void*>(array_proxy(m_ptr)->data);
120134
}
121135

122136
void* mutable_data()
123137
{
124-
return static_cast<void *>(PyArray_GET_(m_ptr, data));
138+
return static_cast<void *>(array_proxy(m_ptr)->data);
125139
}
126140

127141
protected:
@@ -146,9 +160,13 @@ namespace pybind11
146160
if (ndim)
147161
{
148162
std::fill(strides.begin(), strides.end(), itemsize);
149-
for (size_type i = 0; i < ndim - 1; i++)
150-
for (size_type j = 0; j < ndim - 1 - i; j++)
163+
for (size_type i = 0; i < ndim - 1; ++i)
164+
{
165+
for (size_type j = 0; j < ndim - 1 - i; ++j)
166+
{
151167
strides[j] *= shape[ndim - 1 - i];
168+
}
169+
}
152170
}
153171
return strides;
154172
}

0 commit comments

Comments
 (0)