Vcsn  2.2
Be Rational
make-context.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sstream>
4 
6 #include <vcsn/ctx/fwd.hh>
7 #include <vcsn/dyn/automaton.hh>
8 #include <vcsn/dyn/context.hh>
9 #include <vcsn/dyn/expansion.hh>
10 #include <vcsn/misc/raise.hh>
11 
12 namespace vcsn
13 {
14 
15  /*---------------.
16  | make_context. |
17  `---------------*/
18 
19  template <typename Ctx>
20  Ctx
21  make_context(const std::string& name)
22  {
23  std::istringstream is{name};
24  auto res = Ctx::make(is);
25  // Something extremely weird is going wrong with str_escape when
26  // called here from Python. I have not been able to understand
27  // what the problem was, and maybe it's actually a problem bw the
28  // compiler (clang 3.4), the c++ lib (libstc++), and Python, and
29  // possibly Boost after all.
30  //
31  // The good news is that this seems to work properly.
32  if (is.peek() != -1)
33  raise(__func__, ": invalid context name: ", str_escape(name),
34  ", unexpected ", str_escape(is.peek()));
35  return res;
36  }
37 
38  namespace dyn
39  {
40  namespace detail
41  {
43  template <typename Ctx>
44  context
45  make_context(const std::string& name)
46  {
47  return dyn::make_context(vcsn::make_context<Ctx>(name));
48  }
49  }
50  }
51 
52 
53  /*-------------.
54  | context_of. |
55  `-------------*/
56 
57  namespace dyn
58  {
59  namespace detail
60  {
62  template <Automaton Aut>
63  context
64  context_of(const automaton& aut)
65  {
66  const auto& a = aut->as<Aut>();
67  return dyn::make_context(a->context());
68  }
69 
71  template <typename ExpSet>
72  context
74  {
75  const auto& e = exp->as<ExpSet>().expressionset();
76  return dyn::make_context(e.context());
77  }
78 
80  template <typename ExpansionSet>
81  context
83  {
84  const auto& e = exp->as<ExpansionSet>().expansionset();
85  return dyn::make_context(e.context());
86  }
87  }
88  }
89 
90 
91  /*--------.
92  | join. |
93  `--------*/
94 
95  namespace dyn
96  {
97  namespace detail
98  {
100  template <typename Ctx1, typename Ctx2>
101  context
102  join(const context& c1, const context& c2)
103  {
104  return dyn::make_context(join(c1->as<Ctx1>(), c2->as<Ctx2>()));
105  }
106  }
107  }
108 
109 
110 
111  /*--------------------.
112  | make_word_context. |
113  `--------------------*/
114 
115  namespace dyn
116  {
117  namespace detail
118  {
120  template <typename Ctx>
121  context
123  {
124  const auto& c = ctx->as<Ctx>();
126  }
127  }
128  }
129 
130 
131  /*-------------.
132  | num_tapes. |
133  `-------------*/
134 
135  template <typename Ctx>
136  constexpr auto
137  num_tapes(const Ctx&)
138  -> std::enable_if_t<Ctx::is_lat, size_t>
139  {
140  return Ctx::labelset_t::size();
141  }
142 
143  template <typename Ctx>
144  constexpr auto
145  num_tapes(const Ctx&)
146  -> std::enable_if_t<!Ctx::is_lat, size_t>
147  {
148  return 0;
149  }
150 
151  namespace dyn
152  {
153  namespace detail
154  {
156  template <typename Ctx>
157  size_t
159  {
160  return vcsn::num_tapes(ctx->as<Ctx>());
161  }
162  }
163  }
164 
165 
166 } // vcsn::
weightset_mixin< rat::expressionset_impl< Context >> expressionset
Definition: fwd.hh:200
context context_of(const automaton &aut)
Bridge.
Definition: make-context.hh:64
Definition: a-star.hh:8
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:54
std::shared_ptr< const detail::expansion_base > expansion
Definition: expansion.hh:73
size_t num_tapes(const context &ctx)
Bridge.
context context_of_expression(const expression &exp)
Bridge (context_of).
Definition: make-context.hh:73
context context_of_expansion(const expansion &exp)
Bridge (context_of).
Definition: make-context.hh:82
constexpr auto num_tapes(const Ctx &) -> std::enable_if_t< Ctx::is_lat, size_t >
context make_context(const std::string &name)
Bridge.
Definition: make-context.hh:45
word_context_t< context< LabelSet, WeightSet > > make_word_context(const context< LabelSet, WeightSet > &ctx)
The wordset context of a context.
Definition: labelset.hh:285
rat::expansionset< ExpSet > expansionset
Definition: fwd.hh:203
std::shared_ptr< const detail::context_base > context
A dyn::context.
Definition: fwd.hh:43
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:380
context make_context(const std::string &name)
Build a context from its name.
Definition: others.cc:99
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:82
std::shared_ptr< detail::expression_base > expression
Definition: expression.hh:92
context join(const context &c1, const context &c2)
Bridge.
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
context make_word_context(const context &ctx)
Bridge.
Ctx make_context(const std::string &name)
Definition: make-context.hh:21