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