Vcsn  2.8
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/ctx/traits.hh> // is_multitape.
7 #include <vcsn/dyn/cast.hh>
8 #include <vcsn/misc/export.hh>
9 #include <vcsn/misc/static-if.hh>
10 #include <vcsn/misc/symbol.hh>
11 
12 namespace vcsn
13 {
14  namespace dyn
15  {
16  namespace detail
17  {
19  struct expansion_tag {};
20 
22  struct expression_tag {};
23 
25  struct label_tag {};
26 
28  struct polynomial_tag {};
29 
31  struct weight_tag {};
32  }
33 
35  template <typename Tag>
36  class LIBVCSN_API value_impl
37  {
38  public:
41 
42  template <typename ValueSet>
43  value_impl(const ValueSet& vs, const typename ValueSet::value_t& v)
44  : self_{std::make_shared<model<ValueSet>>(vs, v)}
45  {}
46 
48  symbol vname() const
49  {
50  return self_->vname();
51  }
52 
54  template <typename ValueSet>
55  auto& as()
56  {
57  return detail::dyn_cast<model<ValueSet>&>(*self_);
58  }
59 
61  template <typename ValueSet>
62  const auto& as() const
63  {
64  return detail::dyn_cast<const model<ValueSet>&>(*self_);
65  }
66 
67  value_impl* operator->()
68  {
69  return this;
70  }
71 
72  const value_impl* operator->() const
73  {
74  return this;
75  }
76 
77  bool operator!()
78  {
79  return !self_;
80  }
81 
82  auto& operator=(std::nullptr_t)
83  {
84  self_ = nullptr;
85  return *this;
86  }
87 
88  auto& operator=(const value_impl& l)
89  {
90  self_ = std::move(l.self_);
91  return *this;
92  }
93 
94  private:
96  struct base
97  {
98  virtual ~base() = default;
99  virtual symbol vname() const = 0;
100  };
101 
103  template <typename ValueSet>
104  struct model final : base
105  {
106  using valueset_t = ValueSet;
107  using value_t = typename valueset_t::value_t;
109  : valueset_(std::move(vs))
110  , value_(std::move(v))
111  {}
112 
113  symbol vname() const override
114  {
115  return valueset().sname();
116  }
117 
118  const valueset_t& valueset() const
119  {
120  return valueset_;
121  }
122 
123  const value_t value() const
124  {
125  return value_;
126  }
127 
128  private:
133  };
134 
136  std::shared_ptr<base> self_ = nullptr;
137  };
138 
141  template <typename ValueSetLhs, typename ValueSetRhs, typename Tag>
142  auto
143  join(const value_impl<Tag>& lhs, const value_impl<Tag>& rhs)
144  {
145  const auto& l = lhs->template as<ValueSetLhs>();
146  const auto& r = rhs->template as<ValueSetRhs>();
147  auto rs = join(l.valueset(), r.valueset());
148  auto lr = rs.conv(l.valueset(), l.value());
149  auto rr = rs.conv(r.valueset(), r.value());
150  return std::make_tuple(rs, lr , rr);
151  }
152 
153  // A class representing an expansion/expansionset.
155  // A class representing an expression/expressionset.
157  // A class representing a label/labelset.
159  // A class representing an polynomial/polynomialset.
161  // A class representing a weight/weightset.
163  } // namespace dyn
164 } // namespace vcsn
Abstract wrapped typed Value/ValueSet.
Definition: value.hh:96
symbol vname(T &t)
Definition: name.hh:99
Tag for polynomial/polynomialset.
Definition: value.hh:28
std::shared_ptr< base > self_
The wrapped value/valueset.
Definition: value.hh:136
const value_t value_
The value.
Definition: value.hh:132
value_impl()
Default construction: empty.
Definition: value.hh:40
auto & operator=(std::nullptr_t)
Definition: value.hh:82
#define LIBVCSN_API
Definition: export.hh:8
const valueset_t & valueset() const
Definition: value.hh:118
auto & as()
Extract wrapped typed value.
Definition: value.hh:55
const valueset_t valueset_
The value set.
Definition: value.hh:130
context join(const context &c1, const context &c2)
Bridge.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:21
const auto & as() const
Extract wrapped typed value.
Definition: value.hh:62
symbol vname() const override
Definition: value.hh:113
model(valueset_t vs, value_t v)
Definition: value.hh:108
A wrapped typed Value/ValueSet.
Definition: value.hh:104
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
To dyn_cast(From &&from)
A dynamic_cast in debug mode, static_cast with NDEBUG.
Definition: cast.hh:14
A dyn Value/ValueSet.
Definition: fwd.hh:29
Tag for expansion/expansionset.
Definition: value.hh:19
symbol vname() const
A description of the value type.
Definition: value.hh:48
Definition: a-star.hh:8
Tag for label/labelset.
Definition: value.hh:25
typename valueset_t::value_t value_t
Definition: value.hh:107
value_impl * operator->()
Definition: value.hh:67
return v
Definition: multiply.hh:362
auto & operator=(const value_impl &l)
Definition: value.hh:88
value_impl(const ValueSet &vs, const typename ValueSet::value_t &v)
Definition: value.hh:43
Tag for weight/weightset.
Definition: value.hh:31
Tag for expression/expressionset.
Definition: value.hh:22
STL namespace.
const value_t value() const
Definition: value.hh:123
const value_impl * operator->() const
Definition: value.hh:72