Vcsn  2.5
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>
34  class LIBVCSN_API value_impl
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 
65  value_impl* operator->()
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.
153  // A class representing an expression/expressionset.
155  // A class representing a label/labelset.
157  // A class representing an polynomial/polynomialset.
159  // A class representing a weight/weightset.
161  } // namespace dyn
162 } // namespace vcsn
auto & operator=(std::nullptr_t)
Definition: value.hh:80
return v
Definition: multiply.hh:361
Definition: a-star.hh:8
value_impl * operator->()
Definition: value.hh:65
symbol vname() const override
Definition: value.hh:111
const valueset_t & valueset() const
Definition: value.hh:116
value_impl(const ValueSet &vs, const typename ValueSet::value_t &v)
Definition: value.hh:41
typename valueset_t::value_t value_t
Definition: value.hh:105
auto & operator=(const value_impl &l)
Definition: value.hh:86
const value_t value_
The value.
Definition: value.hh:130
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:21
To dyn_cast(From &&from)
A dynamic_cast in debug mode, static_cast with NDEBUG.
Definition: cast.hh:14
STL namespace.
context join(const context &c1, const context &c2)
Bridge.
const value_impl * operator->() const
Definition: value.hh:70
symbol vname() const
A description of the value type.
Definition: value.hh:46
Tag for expansion/expansionset.
Definition: value.hh:17
auto & as()
Extract wrapped typed value.
Definition: value.hh:53
Abstract wrapped typed Value/ValueSet.
Definition: value.hh:94
model(valueset_t vs, value_t v)
Definition: value.hh:106
const auto & as() const
Extract wrapped typed value.
Definition: value.hh:60
symbol vname(T &t)
Definition: name.hh:99
std::shared_ptr< base > self_
The wrapped value/valueset.
Definition: value.hh:134
const valueset_t valueset_
The value set.
Definition: value.hh:128
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
value_impl()
Default construction: empty.
Definition: value.hh:38
Tag for label/labelset.
Definition: value.hh:23
#define LIBVCSN_API
Definition: export.hh:8
Tag for weight/weightset.
Definition: value.hh:29
Tag for expression/expressionset.
Definition: value.hh:20
A dyn Value/ValueSet.
Definition: fwd.hh:29
A wrapped typed Value/ValueSet.
Definition: value.hh:102
Tag for polynomial/polynomialset.
Definition: value.hh:26
const value_t value() const
Definition: value.hh:121