From 061088467fc1cfc578f19eed05c7fc49bbb5a368 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 15 Jun 2017 16:24:44 +1000 Subject: [PATCH] coord: add structured bindings support --- coord/ops.hpp | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ test/coord.cpp | 7 ++++ 2 files changed, 114 insertions(+) diff --git a/coord/ops.hpp b/coord/ops.hpp index dcf0afa6..e66a945c 100644 --- a/coord/ops.hpp +++ b/coord/ops.hpp @@ -864,6 +864,113 @@ namespace util { floor_func); return out; } + + /// returns the data at a templated index in a coordinate. + /// + /// specifically required for structured bindings support. + /// + /// \tparam I index of the requested data + /// \tparam S dimensionality of the coordinate + /// \tparam T underlying data type of the coordinate + /// \tparam K coordinate data type to operate on + template < + size_t I, + size_t S, + typename T, + template< + size_t, + typename + > class K + > + const std::enable_if_t< + is_coord_v>, + T + >& + get (const K &k) + { + static_assert (I < S); + return k[I]; + }; + + + /// returns the data at a templated index in a coordinate. + /// + /// specifically required for structured bindings support. + /// + /// \tparam I index of the requested data + /// \tparam S dimensionality of the coordinate + /// \tparam T underlying data type of the coordinate + /// \tparam K coordinate data type to operate on + template < + size_t I, + size_t S, + typename T, + template< + size_t, + typename + > class K + > + std::enable_if_t< + is_coord_v>, + T + >& + get (K &k) + { + static_assert (I < S); + return k[I]; + }; } + +#include + + +namespace std { + /// returns the dimensions of a coordinate type. + /// + /// specifically required for structured bindings support. + /// + /// \tparam S dimensions + /// \tparam T data type + /// \tparam K coordinate class + template < + size_t S, + typename T, + template< + size_t, + typename + > class K + > + class tuple_size> : public std::enable_if_t< + ::util::is_coord_v>, + std::integral_constant + > { }; + + + /// indicates the type at a given index of a coordinate type + /// + /// specifically required for structured bindings support. + /// + /// \tparam I data index + /// \tparam S dimensionality of the coordinate + /// \tparam T data type for the coordinate + /// \tparam K the underlying coordinate class + template < + size_t I, + size_t S, + typename T, + template< + size_t, + typename + > class K + > + class tuple_element< + I, K + > : public enable_if< + ::util::is_coord_v>, + T + > { }; +} + + #endif diff --git a/test/coord.cpp b/test/coord.cpp index cdc860b2..d9c4d9b0 100644 --- a/test/coord.cpp +++ b/test/coord.cpp @@ -36,5 +36,12 @@ main (void) tap.expect_eq (sum (util::vector4f::ones ()), 4.f, "elementwise summation"); + // check that structured bindings work + { + const auto &[x,y] = p; + tap.expect (x == p.x && y == p.y, "structured bindings extract correct data"); + } + + return tap.status (); }