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