spot  2.4.4
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 <spot/misc/timer.hh>
24 #include <iostream>
25 #include <string>
26 #include <vector>
27 
28 #define UNUSED(expr) do { (void)(expr); } while (0)
29 
30 namespace spot
31 {
32  class printable
33  {
34  public:
35  virtual ~printable()
36  {
37  }
38 
39  virtual void
40  print(std::ostream&, const char*) const = 0;
41  };
42 
43 
44  template <class T>
45  class printable_value: public printable
46  {
47  protected:
48  T val_;
49  public:
50  const T& val() const
51  {
52  return val_;
53  }
54 
55  T& val()
56  {
57  return val_;
58  }
59 
60  operator const T&() const
61  {
62  return val();
63  }
64 
65  operator T&()
66  {
67  return val();
68  }
69 
71  operator=(const T& new_val)
72  {
73  val_ = new_val;
74  return *this;
75  }
76 
78  operator=(T&& new_val)
79  {
80  val_ = std::move(new_val);
81  return *this;
82  }
83 
84  virtual void
85  print(std::ostream& os, const char*) const override
86  {
87  os << val_;
88  }
89  };
90 
91  // This function was defined to avoid compilation error when
92  // instantiating function spot::printable_value<spot::timer>::print
93  // because of: os << val_;
94  std::ostream& operator<<(std::ostream& os, const timer& dt)
95  {
96  UNUSED(dt);
97  return os;
98  }
99 
101  class printable_id: public printable
102  {
103  public:
104  virtual void
105  print(std::ostream& os, const char* x) const override
106  {
107  os << '%' << *x;
108  }
109  };
110 
113  {
114  public:
115  virtual void
116  print(std::ostream& os, const char*) const override
117  {
118  os << '%';
119  }
120  };
121 
122 
123  class SPOT_API formater
124  {
125  printable_id id;
126  printable_percent percent;
127  public:
128 
129  formater()
130  : has_(256), call_(256, &id)
131  {
132  call_['%'] = call_[0] = &percent;
133  }
134 
135  virtual ~formater()
136  {
137  }
138 
144  void
145  scan(const char* fmt, std::vector<bool>& has) const;
146 
147  void
148  scan(const std::string& fmt, std::vector<bool>& has) const
149  {
150  scan(fmt.c_str(), has);
151  }
153 
156  void
157  prime(const char* fmt);
158 
159  void
160  prime(const std::string& fmt)
161  {
162  prime(fmt.c_str());
163  }
165 
167  bool
168  has(char c) const
169  {
170  return has_[c];
171  }
172 
174  void
175  declare(char c, const printable* f)
176  {
177  call_[c] = f;
178  }
179 
181  void
182  set_output(std::ostream& output)
183  {
184  output_ = &output;
185  }
186 
188  std::ostream&
189  format(const char* fmt);
190 
192  std::ostream&
193  format(std::ostream& output, const char* fmt)
194  {
195  std::ostream* tmp = output_;
196  set_output(output);
197  format(fmt);
198  set_output(*tmp);
199  return output;
200  }
201 
203  std::ostream&
204  format(const std::string& fmt)
205  {
206  return format(fmt.c_str());
207  }
208 
210  std::ostream&
211  format(std::ostream& output, const std::string& fmt)
212  {
213  return format(output, fmt.c_str());
214  }
215 
216  private:
217  std::vector<bool> has_;
218  std::vector<const printable*> call_;
219  protected:
220  std::ostream* output_;
221  };
222 }
Definition: automata.hh:26
The default callback simply writes "%c".
Definition: formater.hh:101
void set_output(std::ostream &output)
Remember where to output any string.
Definition: formater.hh:182
Definition: formater.hh:45
std::ostream & format(std::ostream &output, const std::string &fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:211
bool has(char c) const
Whether c occurred in the primed formats.
Definition: formater.hh:168
void prime(const std::string &fmt)
Definition: formater.hh:160
std::ostream & format(const std::string &fmt)
Expand the %-sequences in fmt, write the result on output_.
Definition: formater.hh:204
void declare(char c, const printable *f)
Declare a callback function for c.
Definition: formater.hh:175
Definition: formater.hh:32
void scan(const std::string &fmt, std::vector< bool > &has) const
Scan the %-sequences occuring in fmt.
Definition: formater.hh:148
Called by default for "%%" and "%\0".
Definition: formater.hh:112
std::ostream & format(std::ostream &output, const char *fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:193
Definition: formater.hh:123
Definition: timer.hh:85

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Mon Dec 25 2017 14:51:14 for spot by doxygen 1.8.13