From edc69ec935511d1993240fb68b54b2cfd6afa888 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 15 May 2019 21:09:56 -0700 Subject: [PATCH] [algs] Rewrite bind API And add a partialization API use example to hb_add() --- src/hb-algs.hh | 45 ++++++++++++++++++++------------------------- src/test-algs.cc | 9 ++++++--- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 17296e17e..2420311ef 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -119,19 +119,14 @@ struct } HB_FUNCOBJ (hb_invoke); -enum class hb_bind_pos_t -{ - _0, - _1, -}; -template +template struct hb_binder_t { hb_binder_t (Appl a, V v) : a (a), v (v) {} template auto + unsigned P = Pos, + hb_enable_if (P == 0)> auto operator () (Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl), hb_declval (V), hb_declval (Ts)...)) @@ -141,8 +136,8 @@ struct hb_binder_t hb_forward (ds)...); } template auto + unsigned P = Pos, + hb_enable_if (P == 1)> auto operator () (T0&& d0, Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl), hb_declval (T0), hb_declval (V), @@ -158,20 +153,15 @@ struct hb_binder_t hb_reference_wrapper a; V v; }; -struct -{ - template auto - operator () (Appl&& a, V&& v) const HB_AUTO_RETURN - (( hb_binder_t (a, v) )) -} -HB_FUNCOBJ (hb_bind0); -struct -{ - template auto - operator () (Appl&& a, V&& v) const HB_AUTO_RETURN - (( hb_binder_t (a, v) )) -} -HB_FUNCOBJ (hb_bind1); +template +auto hb_bind (Appl&& a, V&& v) HB_AUTO_RETURN +(( hb_binder_t (a, v) )) + +#define HB_PARTIALIZE(Pos) \ + template \ + auto operator () (_T&& _v) const HB_AUTO_RETURN (hb_bind (this, hb_forward<_T> (_v))) \ + static_assert (true, "") + struct { @@ -834,7 +824,12 @@ struct hb_bitwise_sub HB_FUNCOBJ (hb_bitwise_sub); struct -{ template auto operator () (const T &a, const T2 &b) const HB_AUTO_RETURN (a + b) } +{ + HB_PARTIALIZE(0); + + template + auto operator () (const T &a, const T2 &b) const HB_AUTO_RETURN (a + b) +} HB_FUNCOBJ (hb_add); struct { template auto operator () (const T &a, const T2 &b) const HB_AUTO_RETURN (a - b) } diff --git a/src/test-algs.cc b/src/test-algs.cc index de96bde14..6906d9361 100644 --- a/src/test-algs.cc +++ b/src/test-algs.cc @@ -77,12 +77,15 @@ main (int argc, char **argv) xp = hb_pair_t (nullptr, 1); xp = hb_pair_t (nullptr, 1); - assert (3 == hb_bind0 (hb_min, 3) (4)); - assert (3 == hb_bind0 (hb_min, 4) (3)); + assert (3 == hb_bind (hb_min, 3) (4)); + assert (3 == hb_bind (hb_min, 4) (3)); - auto M0 = hb_bind1 (hb_max, 0); + auto M0 = hb_bind<1> (hb_max, 0); assert (M0 (-2) == 0); assert (M0 (+2) == 2); + assert (hb_add (2) (5) == 7); + assert (hb_add (5) (2) == 7); + return 0; }