spot  2.0.3
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
formater.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2012, 2013, 2016 Laboratoire de Recherche et
3 // Développement de l'Epita (LRDE).
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 #pragma once
21 
22 #include <spot/misc/common.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 
74  virtual void
75  print(std::ostream& os, const char*) const override
76  {
77  os << val_;
78  }
79  };
80 
82  class printable_id: public printable
83  {
84  public:
85  virtual void
86  print(std::ostream& os, const char* x) const override
87  {
88  os << '%' << *x;
89  }
90  };
91 
94  {
95  public:
96  virtual void
97  print(std::ostream& os, const char*) const override
98  {
99  os << '%';
100  }
101  };
102 
103 
104  class SPOT_API formater
105  {
106  printable_id id;
107  printable_percent percent;
108  public:
109 
110  formater()
111  : has_(256), call_(256, &id)
112  {
113  call_['%'] = call_[0] = &percent;
114  }
115 
116  virtual ~formater()
117  {
118  }
119 
125  void
126  scan(const char* fmt, std::vector<bool>& has) const;
127 
128  void
129  scan(const std::string& fmt, std::vector<bool>& has) const
130  {
131  scan(fmt.c_str(), has);
132  }
134 
137  void
138  prime(const char* fmt);
139 
140  void
141  prime(const std::string& fmt)
142  {
143  prime(fmt.c_str());
144  }
146 
148  bool
149  has(char c) const
150  {
151  return has_[c];
152  }
153 
155  void
156  declare(char c, const printable* f)
157  {
158  call_[c] = f;
159  }
160 
162  void
163  set_output(std::ostream& output)
164  {
165  output_ = &output;
166  }
167 
169  std::ostream&
170  format(const char* fmt);
171 
173  std::ostream&
174  format(std::ostream& output, const char* fmt)
175  {
176  set_output(output);
177  return format(fmt);
178  }
179 
181  std::ostream&
182  format(const std::string& fmt)
183  {
184  return format(fmt.c_str());
185  }
186 
188  std::ostream&
189  format(std::ostream& output, const std::string& fmt)
190  {
191  return format(output, fmt.c_str());
192  }
193 
194  private:
195  std::vector<bool> has_;
196  std::vector<const printable*> call_;
197  protected:
198  std::ostream* output_;
199  };
200 }
Definition: graph.hh:32
The default callback simply writes "%c".
Definition: formater.hh:82
void set_output(std::ostream &output)
Remember where to output any string.
Definition: formater.hh:163
Definition: formater.hh:42
std::ostream & format(std::ostream &output, const std::string &fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:189
void scan(const std::string &fmt, std::vector< bool > &has) const
Scan the %-sequences occuring in fmt.
Definition: formater.hh:129
void prime(const std::string &fmt)
Definition: formater.hh:141
std::ostream & format(const std::string &fmt)
Expand the %-sequences in fmt, write the result on output_.
Definition: formater.hh:182
void declare(char c, const printable *f)
Declare a callback function for c.
Definition: formater.hh:156
Definition: formater.hh:29
Called by default for "%%" and "%\0".
Definition: formater.hh:93
bool has(char c) const
Whether c occurred in the primed formats.
Definition: formater.hh:149
std::ostream & format(std::ostream &output, const char *fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:174
Definition: formater.hh:104

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Mon Jul 11 2016 09:54:34 for spot by doxygen 1.8.8