Vcsn  2.3
Be Rational
print.cc
Go to the documentation of this file.
2 #include <vcsn/dyn/algos.hh>
3 #include <vcsn/dyn/automaton.hh>
4 #include <vcsn/dyn/context.hh>
5 #include <vcsn/dyn/registries.hh>
6 #include <vcsn/dyn/value.hh>
7 #include <vcsn/misc/escape.hh>
8 #include <vcsn/misc/raise.hh>
9 #include <vcsn/misc/xalloc.hh>
10 
11 namespace vcsn
12 {
13  namespace dyn
14  {
15 
16  /*---------------------------.
17  | print(expansion, stream). |
18  `---------------------------*/
19 
20  REGISTRY_DEFINE(print_expansion);
21 
22  std::ostream&
23  print(const dyn::expansion& w, std::ostream& out,
24  const std::string& format)
25  {
26  if (format == "null")
27  {}
28  else if (format == "latex" || format == "utf8")
29  detail::print_expansion_registry().call(w, out, format);
30  else if (format == "text" || format == "default" || format == "")
31  {
32  // FIXME: problem with rvalue if we pass
33  // 'std::string("text")'.
34  // FIXME: We _need_ the const, see name.hh.
35  const std::string format = "text";
36  detail::print_expansion_registry().call(w, out, format);
37  }
38  else
39  raise("invalid output format for expansion: ", str_escape(format));
40  return out;
41  }
42 
43  /*-----------------------------.
44  | print(expression, stream). |
45  `-----------------------------*/
46 
47  REGISTRY_DEFINE(print_expression);
48 
49  std::ostream&
50  print(const expression& exp, std::ostream& out, const std::string& format)
51  {
52  if (format == "info")
53  info(exp, out);
54  else if (format == "null")
55  {}
56  else if (format == "dot"
57  || format == "dot,logical" || format == "dot,physical"
58  || format == "latex" || format == "utf8")
59  detail::print_expression_registry().call(exp, out, format);
60  else if (format == "text" || format == "default" || format == "")
61  {
62  // FIXME: problem with rvalue if we pass
63  // 'std::string("text")'.
64  // FIXME: We _need_ the const, see name.hh.
65  const std::string format = "text";
66  detail::print_expression_registry().call(exp, out, format);
67  }
68  else
69  raise("invalid output format for expression: ", str_escape(format));
70  return out;
71  }
72 
73 
74  /*-----------------------.
75  | print(label, stream). |
76  `-----------------------*/
77 
78  REGISTRY_DEFINE(print_label);
79 
80  std::ostream&
81  print(const dyn::label& w, std::ostream& out, const std::string& format)
82  {
83  if (format == "null")
84  {}
85  else if (format == "latex" || format == "utf8")
86  detail::print_label_registry().call(w, out, format);
87  else if (format == "text" || format == "default" || format == "")
88  {
89  // FIXME: problem with rvalue if we pass
90  // 'std::string("text")'.
91  // FIXME: We _need_ the const, see name.hh.
92  const std::string format = "text";
93  detail::print_label_registry().call(w, out, format);
94  }
95  else
96  raise("invalid output format for label: ", str_escape(format));
97  return out;
98  }
99 
100  /*----------------------------.
101  | print(polynomial, stream). |
102  `----------------------------*/
103 
104  REGISTRY_DEFINE(list_polynomial);
105  REGISTRY_DEFINE(print_polynomial);
106 
107  std::ostream&
108  print(const polynomial& p, std::ostream& out, const std::string& format)
109  {
110  if (format == "list")
111  detail::list_polynomial_registry().call(p, out);
112  else if (format == "null")
113  {}
114  else if (format == "latex" || format == "utf8")
115  detail::print_polynomial_registry().call(p, out, format);
116  else if (format == "text" || format == "default" || format == "")
117  {
118  // FIXME: problem with rvalue if we pass
119  // 'std::string("text")'.
120  // FIXME: We _need_ the const, see name.hh.
121  const std::string format = "text";
122  detail::print_polynomial_registry().call(p, out, format);
123  }
124  else
125  raise("invalid output format for polynomial: ", str_escape(format));
126  return out;
127  }
128 
129 
130  /*------------------------.
131  | print(weight, stream). |
132  `------------------------*/
133 
134  REGISTRY_DEFINE(print_weight);
135 
136  std::ostream&
137  print(const dyn::weight& w, std::ostream& out, const std::string& format)
138  {
139  if (format == "null")
140  {}
141  else if (format == "latex" || format == "utf8")
142  detail::print_weight_registry().call(w, out, format);
143  else if (format == "text" || format == "default" || format == "")
144  {
145  // FIXME: problem with rvalue if we pass
146  // 'std::string("text")'.
147  // FIXME: We _need_ the const, see name.hh.
148  const std::string format = "text";
149  detail::print_weight_registry().call(w, out, format);
150  }
151  else
152  raise("invalid output format for weight: ", str_escape(format));
153  return out;
154  }
155 
156 
157  /*-----------------.
158  | ostream format. |
159  `-----------------*/
160 
162 
163  void
164  set_format(std::ostream& o, const std::string& format)
165  {
166  static auto formats = std::set<std::string>{};
167  format_flag(o) = &*formats.insert(format).first;
168  }
169 
170  std::string
171  get_format(std::ostream& o)
172  {
173  return format_flag(o) ? *format_flag(o) : std::string{"default"};
174  }
175 
176 
177  std::ostream&
178  operator<<(std::ostream& o, const automaton& a)
179  {
180  return print(a, o, get_format(o));
181  }
182 
183  std::ostream&
184  operator<<(std::ostream& o, const context& c)
185  {
186  return print(c, o, get_format(o));
187  }
188 
189  std::ostream&
190  operator<<(std::ostream& o, const expansion& e)
191  {
192  return print(e, o, get_format(o));
193  }
194 
195  std::ostream&
196  operator<<(std::ostream& o, const expression& r)
197  {
198  return print(r, o, get_format(o));
199  }
200 
201  std::ostream&
202  operator<<(std::ostream& o, const label& l)
203  {
204  return print(l, o, get_format(o));
205  }
206 
207  std::ostream&
208  operator<<(std::ostream& o, const polynomial& p)
209  {
210  return print(p, o, get_format(o));
211  }
212 
213  std::ostream&
214  operator<<(std::ostream& o, const weight& w)
215  {
216  return print(w, o, get_format(o));
217  }
218  }
219 }
value_impl< detail::weight_tag > weight
Definition: fwd.hh:28
std::ostream & operator<<(std::ostream &o, const automaton &a)
Output automaton a on o.
Definition: print.cc:178
std::ostream & print_label(const label &label, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:218
std::ostream & print(const automaton &aut, std::ostream &out=std::cout, const std::string &format="default")
Print automaton a on out using format format.
Definition: print.hh:108
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:51
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:64
A dyn Value/ValueSet.
Definition: fwd.hh:23
std::ostream & print_expression(const expression &exp, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:199
std::string get_format(std::ostream &out)
Get the output format for out.
Definition: print.cc:171
value_impl< detail::label_tag > label
Definition: fwd.hh:26
Template-less root for contexts.
Definition: context.hh:16
std::ostream & print_expansion(const expansion &expansion, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:157
Definition: a-star.hh:8
std::ostream & list_polynomial(const polynomial &polynomial, std::ostream &o)
Bridge (list).
Definition: print.hh:253
A dyn automaton.
Definition: automaton.hh:17
An input/output format for valuesets.
Definition: format.hh:13
value_impl< detail::polynomial_tag > polynomial
Definition: fwd.hh:27
std::ostream & print_weight(const weight &weight, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:292
Declaration of vcsn::xalloc.
xalloc< const std::string * > format_flag
Definition: print.cc:161
Allocate slots in std::ostreams.
Definition: xalloc.hh:38
std::ostream & info(const automaton &aut, std::ostream &out=std::cout, bool detailed=false)
Output various facts about an automaton.
Definition: info.hh:131
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
value_impl< detail::expression_tag > expression
Definition: fwd.hh:25
std::ostream & print_polynomial(const polynomial &polynomial, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:272
void set_format(std::ostream &out, const std::string &format)
Specify the output format for out.
Definition: print.cc:164