Vcsn  2.2a
Be Rational
expression.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 
6 #include <vcsn/dyn/fwd.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  {
16 
19  {
20  public:
22  virtual symbol vname() const = 0;
23 
25  template <typename ExpSet>
26  auto& as()
27  {
28  return dyn_cast<expression_wrapper<ExpSet>&>(*this);
29  }
30 
32  template <typename ExpSet>
33  auto& as() const
34  {
35  return dyn_cast<const expression_wrapper<ExpSet>&>(*this);
36  }
37  };
38 
39 
41  template <typename ExpSet>
42  class expression_wrapper final: public expression_base
43  {
44  public:
45  using expressionset_t = ExpSet;
47  using expression_t = typename expressionset_t::value_t;
49  const expression_t& r)
50  : expressionset_(rs)
51  , expression_(r)
52  {}
53 
54  virtual symbol vname() const override
55  {
56  return expressionset().sname();
57  }
58 
60  {
61  return expressionset_;
62  }
63 
64  const expression_t expression() const
65  {
66  return expression_;
67  }
68 
69  private:
74  };
75 
78  template <typename ExpSetLhs, typename ExpSetRhs>
79  auto
80  join(const expression& lhs, const expression& rhs)
81  {
82  const auto& l = lhs->as<ExpSetLhs>();
83  const auto& r = rhs->as<ExpSetRhs>();
84  auto rs = join(l.expressionset(), r.expressionset());
85  auto lr = rs.conv(l.expressionset(), l.expression());
86  auto rr = rs.conv(r.expressionset(), r.expression());
87  return std::make_tuple(rs, lr, rr);
88  }
89 
90  } // namespace detail
91 
92  using expression = std::shared_ptr<detail::expression_base>;
93 
94  template <typename ExpSet>
95  inline
97  make_expression(const ExpSet& rs,
98  const typename ExpSet::value_t& r)
99  {
100  using wrapper_t = detail::expression_wrapper<ExpSet>;
101  return std::make_shared<wrapper_t>(rs, r);
102  }
103 
104  } // namespace dyn
105 } // namespace vcsn
#define LIBVCSN_API
Definition: export.hh:8
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
Aggregate an expression and its expressionset.
Definition: expression.hh:42
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
const expressionset_t expressionset_
The expression set.
Definition: expression.hh:71
To dyn_cast(From &&from)
A dynamic_cast in debug mode, static_cast with NDEBUG.
Definition: cast.hh:12
expression make_expression(const ExpSet &rs, const typename ExpSet::value_t &r)
Definition: expression.hh:97
context join(const context &c1, const context &c2)
Bridge.
const expression_t expression() const
Definition: expression.hh:64
expression_wrapper(const expressionset_t &rs, const expression_t &r)
Definition: expression.hh:48
auto & as() const
Extract wrapped typed expression.
Definition: expression.hh:33
const expressionset_t & expressionset() const
Definition: expression.hh:59
An abstract expression.
Definition: expression.hh:18
auto rs
Definition: lift.hh:151
const expression_t expression_
The expression.
Definition: expression.hh:73
auto & as()
Extract wrapped typed expression.
Definition: expression.hh:26
virtual symbol vname() const override
A description of the expression type.
Definition: expression.hh:54
typename expressionset_t::value_t expression_t
Definition: expression.hh:47
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
symbol vname(T &t)
Definition: name.hh:101
std::shared_ptr< detail::expression_base > expression
Definition: expression.hh:92
Definition: a-star.hh:8