Skip to content

Commit

Permalink
Try non-array approach
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed Nov 30, 2018
1 parent 414f65c commit 2d2c142
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ template <typename T> struct same_size {
};

// Lookup a type according to its size, and return a value corresponding to the NumPy typenum.
template <typename Concrete, typename... Check>
constexpr int platform_lookup(const std::array<int, sizeof...(Check)> codes) {
using code_index = std::integral_constant<int, constexpr_first<same_size<Concrete>::template as, Check...>()>;
static_assert(code_index::value != sizeof...(Check), "Unable to match type on this platform");
return codes[code_index::value];
template <typename Concrete, typename... Check, typename... Int>
constexpr int platform_lookup(Int... codes) {
constexpr int code_index = constexpr_first<same_size<Concrete>::template as, Check...>();
static_assert(code_index != sizeof...(Check), "Unable to match type on this platform");
return std::get<code_index>(std::make_tuple(codes...));
}

struct npy_api {
Expand Down Expand Up @@ -148,14 +148,14 @@ struct npy_api {
// `npy_common.h` defines the integer aliases. In order, it checks:
// NPY_BITSOF_LONG, NPY_BITSOF_LONGLONG, NPY_BITSOF_INT, NPY_BITSOF_SHORT, NPY_BITSOF_CHAR
// and assigns the alias to the first matching size, so we should check in this order.
NPY_INT32_ = platform_lookup<std::int32_t, long, int, short>({{
NPY_LONG_, NPY_INT_, NPY_SHORT_}}),
NPY_UINT32_ = platform_lookup<std::uint32_t, unsigned long, unsigned int, unsigned short>({{
NPY_ULONG_, NPY_UINT_, NPY_USHORT_}}),
NPY_INT64_ = platform_lookup<std::int64_t, long, long long, int>({{
NPY_LONG_, NPY_LONGLONG_, NPY_INT_}}),
NPY_UINT64_ = platform_lookup<std::uint64_t, unsigned long, unsigned long long, unsigned int>({{
NPY_ULONG_, NPY_ULONGLONG_, NPY_UINT_}}),
NPY_INT32_ = platform_lookup<std::int32_t, long, int, short>(
NPY_LONG_, NPY_INT_, NPY_SHORT_),
NPY_UINT32_ = platform_lookup<std::uint32_t, unsigned long, unsigned int, unsigned short>(
NPY_ULONG_, NPY_UINT_, NPY_USHORT_),
NPY_INT64_ = platform_lookup<std::int64_t, long, long long, int>(
NPY_LONG_, NPY_LONGLONG_, NPY_INT_),
NPY_UINT64_ = platform_lookup<std::uint64_t, unsigned long, unsigned long long, unsigned int>(
NPY_ULONG_, NPY_ULONGLONG_, NPY_UINT_),
};

typedef struct {
Expand Down

0 comments on commit 2d2c142

Please sign in to comment.