[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);
enum class hb_bind_pos_t
{
_0,
_1,
};
template <typename Appl, hb_bind_pos_t P, typename V>
template <unsigned Pos, typename Appl, typename V>
struct hb_binder_t
{
hb_binder_t (Appl a, V v) : a (a), v (v) {}
template <typename ...Ts,
hb_bind_pos_t PP = P,
hb_enable_if (PP == hb_bind_pos_t::_0)> 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<Ts> (ds)...);
}
template <typename T0, typename ...Ts,
hb_bind_pos_t PP = P,
hb_enable_if (PP == hb_bind_pos_t::_1)> 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<Appl> a;
V v;
};
struct
{
template <typename Appl, typename V> auto
operator () (Appl&& a, V&& v) const HB_AUTO_RETURN
(( hb_binder_t<Appl, hb_bind_pos_t::_0, V> (a, v) ))
}
HB_FUNCOBJ (hb_bind0);
struct
{
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);
template <unsigned Pos=0, typename Appl, typename V>
auto hb_bind (Appl&& a, V&& v) HB_AUTO_RETURN
(( hb_binder_t<Pos, Appl, V> (a, v) ))
#define HB_PARTIALIZE(Pos) \
template <typename _T> \
auto operator () (_T&& _v) const HB_AUTO_RETURN (hb_bind<Pos> (this, hb_forward<_T> (_v))) \
static_assert (true, "")
struct
{
@ -834,7 +824,12 @@ struct hb_bitwise_sub
HB_FUNCOBJ (hb_bitwise_sub);
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);
struct
{ 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<const int*, int> (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;
}