Vcsn  2.4
Be Rational
value.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 
6 #include <vcsn/dyn/cast.hh>
7 #include <vcsn/misc/export.hh>
8 #include <vcsn/misc/symbol.hh>
9 
10 namespace vcsn
11 {
12  namespace dyn
13  {
14  namespace detail
15  {
17  struct expansion_tag {};
18 
20  struct expression_tag {};
21 
23  struct label_tag {};
24 
26  struct polynomial_tag {};
27 
29  struct weight_tag {};
30  }
31 
33  template <typename Tag>
35  {
36  public:
39 
40  template <typename ValueSet>
41  value_impl(const ValueSet& vs, const typename ValueSet::value_t& v)
42  : self_{std::make_shared<model<ValueSet>>(vs, v)}
43  {}
44 
46  symbol vname() const
47  {
48  return self_->vname();
49  }
50 
52  template <typename ValueSet>
53  auto& as()
54  {
55  return detail::dyn_cast<model<ValueSet>&>(*self_);
56  }
57 
59  template <typename ValueSet>
60  const auto& as() const
61  {
62  return detail::dyn_cast<const model<ValueSet>&>(*self_);
63  }
64 
66  {
67  return this;
68  }
69 
70  const value_impl* operator->() const
71  {
72  return this;
73  }
74 
75  bool operator!()
76  {
77  return !self_;
78  }
79 
80  auto& operator=(std::nullptr_t)
81  {
82  self_ = nullptr;
83  return *this;
84  }
85 
86  auto& operator=(const value_impl& l)
87  {
88  self_ = std::move(l.self_);
89  return *this;
90  }
91 
92  private:
94  struct base
95  {
96  virtual ~base() = default;
97  virtual symbol vname() const = 0;
98  };
99 
101  template <typename ValueSet>
102  struct model final : base
103  {
104  using valueset_t = ValueSet;
105  using value_t = typename valueset_t::value_t;
107  : valueset_(std::move(vs))
108  , value_(std::move(v))
109  {}
110 
111  symbol vname() const override
112  {
113  return valueset().sname();
114  }
115 
116  const valueset_t& valueset() const
117  {
118  return valueset_;
119  }
120 
121  const value_t value() const
122  {
123  return value_;
124  }
125 
126  private:
131  };
132 
134  std::shared_ptr<base> self_ = nullptr;
135  };
136 
139  template <typename ValueSetLhs, typename ValueSetRhs, typename Tag>
140  auto
141  join(const value_impl<Tag>& lhs, const value_impl<Tag>& rhs)
142  {
143  const auto& l = lhs->template as<ValueSetLhs>();
144  const auto& r = rhs->template as<ValueSetRhs>();
145  auto rs = join(l.valueset(), r.valueset());
146  auto lr = rs.conv(l.valueset(), l.value());
147  auto rr = rs.conv(r.valueset(), r.value());
148  return std::make_tuple(rs, lr, rr);
149  }
150 
151  // A class representing an expansion/expansionset.
152  using expansion = value_impl<detail::expansion_tag>;
153  // A class representing an expression/expressionset.
154  using expression = value_impl<detail::expression_tag>;
155  // A class representing a label/labelset.
156  using label = value_impl<detail::label_tag>;
157  // A class representing an polynomial/polynomialset.
158  using polynomial = value_impl<detail::polynomial_tag>;
159  // A class representing a weight/weightset.
160  using weight = value_impl<detail::weight_tag>;
161  } // namespace dyn
162 } // namespace vcsn
value_impl< detail::expansion_tag > expansion
Definition: fwd.hh:24
STL namespace.
auto & operator=(std::nullptr_t)
Definition: value.hh:80
const valueset_t & valueset() const
Definition: value.hh:116
Tag for polynomial/polynomialset.
Definition: value.hh:26
To dyn_cast(From &&from)
A dynamic_cast in debug mode, static_cast with NDEBUG.
Definition: cast.hh:14
auto rs
Definition: lift.hh:152
value_impl< detail::weight_tag > weight
Definition: fwd.hh:28
context join(const context &lhs, const context &rhs)
The join between two contexts, i.e., their lowest common supertype.
const valueset_t valueset_
The value set.
Definition: value.hh:128
const value_impl * operator->() const
Definition: value.hh:70
std::shared_ptr< base > self_
The wrapped value/valueset.
Definition: value.hh:134
Tag for weight/weightset.
Definition: value.hh:29
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
const value_t value_
The value.
Definition: value.hh:130
#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
const auto & as() const
Extract wrapped typed value.
Definition: value.hh:60
A wrapped typed Value/ValueSet.
Definition: value.hh:102
Definition: a-star.hh:8
Tag for expression/expressionset.
Definition: value.hh:20
value_impl()
Default construction: empty.
Definition: value.hh:38
value_impl(const ValueSet &vs, const typename ValueSet::value_t &v)
Definition: value.hh:41
auto & operator=(const value_impl &l)
Definition: value.hh:86
Tag for label/labelset.
Definition: value.hh:23
value_impl * operator->()
Definition: value.hh:65
return v
Definition: multiply.hh:361
symbol vname() const
A description of the value type.
Definition: value.hh:46
auto & as()
Extract wrapped typed value.
Definition: value.hh:53
Tag for expansion/expansionset.
Definition: value.hh:17
model(valueset_t vs, value_t v)
Definition: value.hh:106
value_impl< detail::label_tag > label
Definition: fwd.hh:26
A dyn Value/ValueSet.
Definition: fwd.hh:23
value_impl< detail::expression_tag > expression
Definition: fwd.hh:25
symbol vname() const override
Definition: value.hh:111
Abstract wrapped typed Value/ValueSet.
Definition: value.hh:94
const value_t value() const
Definition: value.hh:121
value_impl< detail::polynomial_tag > polynomial
Definition: fwd.hh:27
typename valueset_t::value_t value_t
Definition: value.hh:105
symbol vname(T &t)
Definition: name.hh:99