spot  2.15.1
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 {
29  class printable
30  {
31  public:
32  virtual ~printable()
33  {
34  }
35 
36  virtual void
37  print(std::ostream&, const char*) const = 0;
38  };
39 
40 
41  template <class T>
42  class printable_value: public printable
43  {
44  protected:
45  T val_;
46  public:
47  const T& val() const
48  {
49  return val_;
50  }
51 
52  T& val()
53  {
54  return val_;
55  }
56 
57  operator const T&() const
58  {
59  return val();
60  }
61 
62  operator T&()
63  {
64  return val();
65  }
66 
68  operator=(const T& new_val)
69  {
70  val_ = new_val;
71  return *this;
72  }
73 
75  operator=(T&& new_val)
76  {
77  val_ = std::move(new_val);
78  return *this;
79  }
80 
81  virtual void
82  print(std::ostream& os, const char*) const override
83  {
84  os << val_;
85  }
86  };
87 
89  class printable_id: public printable
90  {
91  public:
92  virtual void
93  print(std::ostream& os, const char* x) const override
94  {
95  os << '%' << *x;
96  }
97  };
98 
101  {
102  public:
103  virtual void
104  print(std::ostream& os, const char*) const override
105  {
106  os << '%';
107  }
108  };
109 
110 
111  class SPOT_API formater
112  {
113  printable_id id;
114  printable_percent percent;
115  public:
116 
117  formater()
118  : has_(256), call_(256, &id)
119  {
120  call_['%'] = call_[0] = &percent;
121  }
122 
123  virtual ~formater()
124  {
125  }
126 
133  void
134  scan(const char* fmt, std::vector<bool>& has) const;
135 
136  void
137  scan(const std::string& fmt, std::vector<bool>& has) const
138  {
139  scan(fmt.c_str(), has);
140  }
142 
145  void
146  prime(const char* fmt);
147 
148  void
149  prime(const std::string& fmt)
150  {
151  prime(fmt.c_str());
152  }
154 
156  bool
157  has(char c) const
158  {
159  return has_[c];
160  }
161 
163  void
164  declare(char c, const printable* f)
165  {
166  call_[c] = f;
167  }
168 
170  void
171  set_output(std::ostream& output)
172  {
173  output_ = &output;
174  }
175 
177  std::ostream&
178  format(const char* fmt);
179 
181  std::ostream&
182  format(std::ostream& output, const char* fmt)
183  {
184  std::ostream* tmp = output_;
185  set_output(output);
186  format(fmt);
187  set_output(*tmp);
188  return output;
189  }
190 
192  std::ostream&
193  format(const std::string& fmt)
194  {
195  return format(fmt.c_str());
196  }
197 
199  std::ostream&
200  format(std::ostream& output, const std::string& fmt)
201  {
202  return format(output, fmt.c_str());
203  }
204 
205  private:
206  std::vector<bool> has_;
207  std::vector<const printable*> call_;
208  protected:
209  std::ostream* output_;
210  };
211 }
Definition: formater.hh:112
void declare(char c, const printable *f)
Declare a callback function for c.
Definition: formater.hh:164
std::ostream & format(std::ostream &output, const char *fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:182
void set_output(std::ostream &output)
Remember where to output any string.
Definition: formater.hh:171
bool has(char c) const
Whether c occurred in the primed formats.
Definition: formater.hh:157
std::ostream & format(const std::string &fmt)
Expand the %-sequences in fmt, write the result on output_.
Definition: formater.hh:193
std::ostream & format(const char *fmt)
Expand the %-sequences in fmt, write the result on output_.
void prime(const std::string &fmt)
Definition: formater.hh:149
std::ostream & format(std::ostream &output, const std::string &fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:200
void prime(const char *fmt)
The default callback simply writes "%c".
Definition: formater.hh:90
Called by default for "%%" and "%\0".
Definition: formater.hh:101
Definition: formater.hh:43
Definition: formater.hh:30
void scan(const char *fmt, std::vector< bool > &has) const
Scan the %-sequences occurring in fmt.
void scan(const std::string &fmt, std::vector< bool > &has) const
Scan the %-sequences occurring in fmt.
Definition: formater.hh:137
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