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