From efb719b8224254f7432679d3b74fae2c1286acc1 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 24 Aug 2017 14:39:06 +1000 Subject: [PATCH] coord/base: add indices method --- coord/base.hpp | 15 +++++++++++++++ test/coord.cpp | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/coord/base.hpp b/coord/base.hpp index 8a6dbc03..8a32ade2 100644 --- a/coord/base.hpp +++ b/coord/base.hpp @@ -160,6 +160,21 @@ namespace util::coord { std::fill (next, std::end (out), fill); return out; } + + + /////////////////////////////////////////////////////////////////////// + /// returns an instance with elements specified by the Indices + /// parameter. eg, point2f p{}.indices<0,2> would return {p.x, p.z}. + /// + /// it's ugly as sin, but simplifies some situations where we don't + /// want a temporary. + template + KLASS + indices (void) const + { + static_assert (all (make_vector ((Indices < S)...))); + return KLASS { this->data[Indices]... }; + } }; } diff --git a/test/coord.cpp b/test/coord.cpp index 593cc0ec..8de918aa 100644 --- a/test/coord.cpp +++ b/test/coord.cpp @@ -91,5 +91,13 @@ main (void) tap.expect_eq (limit (val, 0.f, 2.f), util::vector3f { 0, 0, 2 }, "limit with vec/num/num"); } + // ensure that klass::indices appears to link correctly + { + const util::vector3i seq { 0, 1, 2 }; + const util::vector4i res { 2, 0, 0, 1 }; + + tap.expect_eq (seq.indices<2,0,0,1> (), res, "coord::indices expansion"); + }; + return tap.status (); }