Vcsn  2.1
Be Rational
weightset.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <memory> // std::make_shared
5 
6 #include <vcsn/misc/type_traits.hh> // detect
7 
8 // It is much simpler and saner in C++ to put types and functions on
9 // these types in the same namespace. Since "using q =
10 // detail::weightset_mixin<q_impl>" would just create an alias of
11 // q, its original namespace, detail::, would still be the namespace
12 // used in ADL.
13 //
14 // This is really troublesome to implement free-functions such as join.
15 //
16 // Therefore, although this wrapper should be hidden as a detail::, it
17 // will remain in vcsn::, where join and the like will find it.
18 
19 namespace vcsn
20 {
22  template <typename WeightSet>
23  struct weightset_mixin : WeightSet
24  {
25  using super_t = WeightSet;
26  using typename super_t::value_t;
27 
29  using super_t::super_t;
30 
32  using super_t::mul;
33 
35  template <typename... Ts>
36  value_t mul(const Ts&... ts) const
37  {
38  value_t res = this->one();
39  // FIXME: Remove once GCC is fixed.
40  using swallow = int[];
41  (void) swallow
42  {
43  ((res = super_t::mul(res, ts)), 0)...
44  };
45  return res;
46  }
47 
48  private:
50  template <typename T>
51  using power_t = decltype(std::declval<T>()
52  .power(std::declval<typename T::value_t>(), 0));
53 
55  template <typename T>
57 
60  template <typename WS = super_t>
61  auto power_(value_t e, unsigned n) const
63  {
64  return super_t::power(e, n);
65  }
66 
69  template <typename WS = super_t>
70  auto power_(value_t e, unsigned n) const
72  {
74  if (!super_t::is_one(e))
75  while (n--)
76  res = mul(res, e);
77  return res;
78  }
79 
80  public:
81 
83  value_t power(value_t e, unsigned n) const
84  {
85  return power_<WeightSet>(e, n);
86  }
87  };
88 }
value_t mul(const Ts &...ts) const
A variadic multiplication.
Definition: weightset.hh:36
auto power_(value_t e, unsigned n) const -> vcsn::enable_if_t< has_power_mem_fn< WS >
Case where the weightset T features a power(value_t, unsigned) member function.
Definition: weightset.hh:61
auto power_(value_t e, unsigned n) const -> vcsn::enable_if_t<!has_power_mem_fn< WS >
Case where the weightset T does not feature a power(value_t, unsigned) member function.
Definition: weightset.hh:70
typename std::enable_if< Cond, T >::type enable_if_t
Definition: type_traits.hh:16
value_t power(value_t e, unsigned n) const
Repeated multiplication.
Definition: weightset.hh:83
decltype(std::declval< T >().power(std::declval< typename T::value_t >(), 0)) power_t
The signature of power.
Definition: weightset.hh:52
constant< type_t::one, Context > one
Definition: fwd.hh:111