spot  2.16
formater.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) by the Spot authors, see the AUTHORS file for details.
3 //
4 // This file is part of Spot, a model checking library.
5 //
6 // Spot is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Spot is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #pragma once
20 
21 #include <spot/misc/common.hh>
22 #include <spot/misc/timer.hh>
23 #include <iostream>
24 #include <string>
25 #include <vector>
26 
27 namespace spot
28 {
31 
34  class printable
35  {
36  public:
37  virtual ~printable()
38  {
39  }
40 
42  virtual void
43  print(std::ostream&, const char*) const = 0;
44  };
45 
46 
48  template <class T>
49  class printable_value: public printable
50  {
51  protected:
52  T val_;
53  public:
55  const T& val() const
56  {
57  return val_;
58  }
59 
61  T& val()
62  {
63  return val_;
64  }
65 
67  operator const T&() const
68  {
69  return val();
70  }
71 
73  operator T&()
74  {
75  return val();
76  }
77 
80  operator=(const T& new_val)
81  {
82  val_ = new_val;
83  return *this;
84  }
85 
88  operator=(T&& new_val)
89  {
90  val_ = std::move(new_val);
91  return *this;
92  }
93 
95  virtual void
96  print(std::ostream& os, const char*) const override
97  {
98  os << val_;
99  }
100  };
101 
103  class printable_id: public printable
104  {
105  public:
107  virtual void
108  print(std::ostream& os, const char* x) const override
109  {
110  os << '%' << *x;
111  }
112  };
113 
116  {
117  public:
119  virtual void
120  print(std::ostream& os, const char*) const override
121  {
122  os << '%';
123  }
124  };
125 
126 
129  class SPOT_API formater
130  {
131  printable_id id;
132  printable_percent percent;
133  public:
134 
135  formater()
136  : has_(256), call_(256, &id)
137  {
138  call_['%'] = call_[0] = &percent;
139  }
140 
141  virtual ~formater()
142  {
143  }
144 
150  void
151  scan(const char* fmt, std::vector<bool>& has) const;
152 
153  void
154  scan(const std::string& fmt, std::vector<bool>& has) const
155  {
156  scan(fmt.c_str(), has);
157  }
159 
162  void
163  prime(const char* fmt);
164 
165  void
166  prime(const std::string& fmt)
167  {
168  prime(fmt.c_str());
169  }
171 
173  bool
174  has(char c) const
175  {
176  return has_[c];
177  }
178 
180  void
181  declare(char c, const printable* f)
182  {
183  call_[c] = f;
184  }
185 
187  void
188  set_output(std::ostream& output)
189  {
190  output_ = &output;
191  }
192 
194  std::ostream&
195  format(const char* fmt);
196 
198  std::ostream&
199  format(std::ostream& output, const char* fmt)
200  {
201  std::ostream* tmp = output_;
202  set_output(output);
203  format(fmt);
204  set_output(*tmp);
205  return output;
206  }
207 
209  std::ostream&
210  format(const std::string& fmt)
211  {
212  return format(fmt.c_str());
213  }
214 
216  std::ostream&
217  format(std::ostream& output, const std::string& fmt)
218  {
219  return format(output, fmt.c_str());
220  }
221 
222  private:
223  std::vector<bool> has_;
224  std::vector<const printable*> call_;
225  protected:
226  std::ostream* output_;
227  };
228 
230 }
A string formatter that dispatches %-escape sequences to printable objects.
Definition: formater.hh:130
void declare(char c, const printable *f)
Declare a callback function for c.
Definition: formater.hh:181
std::ostream & format(std::ostream &output, const char *fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:199
void set_output(std::ostream &output)
Remember where to output any string.
Definition: formater.hh:188
bool has(char c) const
Whether c occurred in the primed formats.
Definition: formater.hh:174
std::ostream & format(const std::string &fmt)
Expand the %-sequences in fmt, write the result on output_.
Definition: formater.hh:210
std::ostream & format(const char *fmt)
Expand the %-sequences in fmt, write the result on output_.
void scan(const char *fmt, std::vector< bool > &has) const
Scan the %-sequences occurring in fmt.
std::ostream * output_
Current output stream.
Definition: formater.hh:226
void prime(const std::string &fmt)
Definition: formater.hh:166
void scan(const std::string &fmt, std::vector< bool > &has) const
Scan the %-sequences occurring in fmt.
Definition: formater.hh:154
std::ostream & format(std::ostream &output, const std::string &fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:217
void prime(const char *fmt)
The default callback simply writes "%c".
Definition: formater.hh:104
virtual void print(std::ostream &os, const char *x) const override
Print the requested escape code.
Definition: formater.hh:108
Called by default for "%%" and "%\0".
Definition: formater.hh:116
virtual void print(std::ostream &os, const char *) const override
Print a literal percent sign.
Definition: formater.hh:120
A printable wrapper around a value of type T.
Definition: formater.hh:50
virtual void print(std::ostream &os, const char *) const override
Print the stored value.
Definition: formater.hh:96
T val_
Stored value.
Definition: formater.hh:52
T & val()
Access the stored value.
Definition: formater.hh:61
const T & val() const
Access the stored value.
Definition: formater.hh:55
printable_value & operator=(T &&new_val)
Assign a new value.
Definition: formater.hh:88
printable_value & operator=(const T &new_val)
Assign a new value.
Definition: formater.hh:80
Abstract base class for objects that can be printed to a stream via a format string.
Definition: formater.hh:35
virtual void print(std::ostream &, const char *) const =0
Print this object using fmt.
Definition: automata.hh:26

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.1