spot  2.1.1
 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 
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 
132  void
133  scan(const char* fmt, std::vector<bool>& has) const;
134 
135  void
136  scan(const std::string& fmt, std::vector<bool>& has) const
137  {
138  scan(fmt.c_str(), has);
139  }
141 
144  void
145  prime(const char* fmt);
146 
147  void
148  prime(const std::string& fmt)
149  {
150  prime(fmt.c_str());
151  }
153 
155  bool
156  has(char c) const
157  {
158  return has_[c];
159  }
160 
162  void
163  declare(char c, const printable* f)
164  {
165  call_[c] = f;
166  }
167 
169  void
170  set_output(std::ostream& output)
171  {
172  output_ = &output;
173  }
174 
176  std::ostream&
177  format(const char* fmt);
178 
180  std::ostream&
181  format(std::ostream& output, const char* fmt)
182  {
183  std::ostream* tmp = output_;
184  set_output(output);
185  format(fmt);
186  set_output(*tmp);
187  return output;
188  }
189 
191  std::ostream&
192  format(const std::string& fmt)
193  {
194  return format(fmt.c_str());
195  }
196 
198  std::ostream&
199  format(std::ostream& output, const std::string& fmt)
200  {
201  return format(output, fmt.c_str());
202  }
203 
204  private:
205  std::vector<bool> has_;
206  std::vector<const printable*> call_;
207  protected:
208  std::ostream* output_;
209  };
210 }
Definition: graph.hh:32
The default callback simply writes "%c".
Definition: formater.hh:89
void set_output(std::ostream &output)
Remember where to output any string.
Definition: formater.hh:170
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:199
void scan(const std::string &fmt, std::vector< bool > &has) const
Scan the %-sequences occuring in fmt.
Definition: formater.hh:136
void prime(const std::string &fmt)
Definition: formater.hh:148
std::ostream & format(const std::string &fmt)
Expand the %-sequences in fmt, write the result on output_.
Definition: formater.hh:192
void declare(char c, const printable *f)
Declare a callback function for c.
Definition: formater.hh:163
Definition: formater.hh:29
Called by default for "%%" and "%\0".
Definition: formater.hh:100
bool has(char c) const
Whether c occurred in the primed formats.
Definition: formater.hh:156
std::ostream & format(std::ostream &output, const char *fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:181
Definition: formater.hh:111

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Tue Sep 20 2016 07:13:02 for spot by doxygen 1.8.8