Vcsn  2.2a
Be Rational
weight.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 
6 #include <vcsn/misc/export.hh>
7 
8 namespace vcsn
9 {
10  namespace dyn
11  {
12  namespace detail
13  {
14 
17  {
18  public:
20  virtual symbol vname() const = 0;
21 
23  template <typename WeightSet>
24  auto& as()
25  {
26  return dyn_cast<weight_wrapper<WeightSet>&>(*this);
27  }
28 
30  template <typename WeightSet>
31  auto& as() const
32  {
33  return dyn_cast<const weight_wrapper<WeightSet>&>(*this);
34  }
35  };
36 
38  template <typename WeightSet>
39  class weight_wrapper final: public weight_base
40  {
41  public:
44  using weight_t = typename weightset_t::value_t;
45  weight_wrapper(const weightset_t& ws, const weight_t& w)
46  : weightset_(ws)
47  , weight_(w)
48  {}
49 
50  virtual symbol vname() const override
51  {
52  return weightset().sname();
53  }
54 
55  const weightset_t& weightset() const
56  {
57  return weightset_;
58  }
59 
60  const weight_t weight() const
61  {
62  return weight_;
63  }
64 
65  private:
70  };
71 
72  } // namespace detail
73 
74  using weight = std::shared_ptr<const detail::weight_base>;
75 
76  template <typename WeightSet>
77  inline
78  weight
80  const typename WeightSet::value_t& w)
81  {
82  return std::make_shared<detail::weight_wrapper<WeightSet>>(ws, w);
83  }
84  } // namespace dyn
85 } // namespace vcsn
#define LIBVCSN_API
Definition: export.hh:8
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
To dyn_cast(From &&from)
A dynamic_cast in debug mode, static_cast with NDEBUG.
Definition: cast.hh:12
const weightset_t & weightset() const
Definition: weight.hh:55
auto & as()
Extract wrapped typed weight.
Definition: weight.hh:24
const weight_t weight_
The weight.
Definition: weight.hh:69
An abstract weight.
Definition: weight.hh:16
virtual symbol vname() const override
A description of the weight type.
Definition: weight.hh:50
weight_wrapper(const weightset_t &ws, const weight_t &w)
Definition: weight.hh:45
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:71
const weightset_t weightset_
The weight set.
Definition: weight.hh:67
weight make_weight(const WeightSet &ws, const typename WeightSet::value_t &w)
Definition: weight.hh:79
symbol vname(T &t)
Definition: name.hh:101
auto & as() const
Extract wrapped typed weight.
Definition: weight.hh:31
const weight_t weight() const
Definition: weight.hh:60
typename weightset_t::value_t weight_t
Definition: weight.hh:44
Aggregate a weight and its weightset.
Definition: fwd.hh:69
Definition: a-star.hh:8