[algs] Rewrite bind API

And add a partialization API use example to hb_add()
This commit is contained in:
Behdad Esfahbod 2019-05-15 21:09:56 -07:00
parent 16a3540ea4
commit edc69ec935
2 changed files with 26 additions and 28 deletions

View File

@ -119,19 +119,14 @@ struct
} }
HB_FUNCOBJ (hb_invoke); HB_FUNCOBJ (hb_invoke);
enum class hb_bind_pos_t template <unsigned Pos, typename Appl, typename V>
{
_0,
_1,
};
template <typename Appl, hb_bind_pos_t P, typename V>
struct hb_binder_t struct hb_binder_t
{ {
hb_binder_t (Appl a, V v) : a (a), v (v) {} hb_binder_t (Appl a, V v) : a (a), v (v) {}
template <typename ...Ts, template <typename ...Ts,
hb_bind_pos_t PP = P, unsigned P = Pos,
hb_enable_if (PP == hb_bind_pos_t::_0)> auto hb_enable_if (P == 0)> auto
operator () (Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl), operator () (Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl),
hb_declval (V), hb_declval (V),
hb_declval (Ts)...)) hb_declval (Ts)...))
@ -141,8 +136,8 @@ struct hb_binder_t
hb_forward<Ts> (ds)...); hb_forward<Ts> (ds)...);
} }
template <typename T0, typename ...Ts, template <typename T0, typename ...Ts,
hb_bind_pos_t PP = P, unsigned P = Pos,
hb_enable_if (PP == hb_bind_pos_t::_1)> auto hb_enable_if (P == 1)> auto
operator () (T0&& d0, Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl), operator () (T0&& d0, Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl),
hb_declval (T0), hb_declval (T0),
hb_declval (V), hb_declval (V),
@ -158,20 +153,15 @@ struct hb_binder_t
hb_reference_wrapper<Appl> a; hb_reference_wrapper<Appl> a;
V v; V v;
}; };
struct template <unsigned Pos=0, typename Appl, typename V>
{ auto hb_bind (Appl&& a, V&& v) HB_AUTO_RETURN
template <typename Appl, typename V> auto (( hb_binder_t<Pos, Appl, V> (a, v) ))
operator () (Appl&& a, V&& v) const HB_AUTO_RETURN
(( hb_binder_t<Appl, hb_bind_pos_t::_0, V> (a, v) )) #define HB_PARTIALIZE(Pos) \
} template <typename _T> \
HB_FUNCOBJ (hb_bind0); auto operator () (_T&& _v) const HB_AUTO_RETURN (hb_bind<Pos> (this, hb_forward<_T> (_v))) \
struct static_assert (true, "")
{
template <typename Appl, typename V> auto
operator () (Appl&& a, V&& v) const HB_AUTO_RETURN
(( hb_binder_t<Appl, hb_bind_pos_t::_1, V> (a, v) ))
}
HB_FUNCOBJ (hb_bind1);
struct struct
{ {
@ -834,7 +824,12 @@ struct hb_bitwise_sub
HB_FUNCOBJ (hb_bitwise_sub); HB_FUNCOBJ (hb_bitwise_sub);
struct struct
{ template <typename T, typename T2> auto operator () (const T &a, const T2 &b) const HB_AUTO_RETURN (a + b) } {
HB_PARTIALIZE(0);
template <typename T, typename T2>
auto operator () (const T &a, const T2 &b) const HB_AUTO_RETURN (a + b)
}
HB_FUNCOBJ (hb_add); HB_FUNCOBJ (hb_add);
struct struct
{ template <typename T, typename T2> auto operator () (const T &a, const T2 &b) const HB_AUTO_RETURN (a - b) } { template <typename T, typename T2> auto operator () (const T &a, const T2 &b) const HB_AUTO_RETURN (a - b) }

View File

@ -77,12 +77,15 @@ main (int argc, char **argv)
xp = hb_pair_t<int *, double> (nullptr, 1); xp = hb_pair_t<int *, double> (nullptr, 1);
xp = hb_pair_t<const int*, int> (nullptr, 1); xp = hb_pair_t<const int*, int> (nullptr, 1);
assert (3 == hb_bind0 (hb_min, 3) (4)); assert (3 == hb_bind (hb_min, 3) (4));
assert (3 == hb_bind0 (hb_min, 4) (3)); 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) == 0);
assert (M0 (+2) == 2); assert (M0 (+2) == 2);
assert (hb_add (2) (5) == 7);
assert (hb_add (5) (2) == 7);
return 0; return 0;
} }