spot  1.2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
formater.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2012, 2013 Laboratoire de Recherche et Développement
3 // 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 #ifndef SPOT_MISC_FORMATER_HH
21 #define SPOT_MISC_FORMATER_HH
22 
23 #include "common.hh"
24 #include <iostream>
25 #include <string>
26 #include <vector>
27 
28 namespace spot
29 {
30  class printable
31  {
32  public:
33  virtual ~printable()
34  {
35  }
36 
37  virtual void
38  print(std::ostream&, const char*) const = 0;
39  };
40 
41 
42  template <class T>
43  class printable_value: public printable
44  {
45  protected:
46  T val_;
47  public:
48  const T& val() const
49  {
50  return val_;
51  }
52 
53  T& val()
54  {
55  return val_;
56  }
57 
58  operator const T&() const
59  {
60  return val();
61  }
62 
63  operator T&()
64  {
65  return val();
66  }
67 
69  operator=(const T& new_val)
70  {
71  val_ = new_val;
72  return *this;
73  }
74 
75  virtual void
76  print(std::ostream& os, const char*) const
77  {
78  os << val_;
79  }
80  };
81 
83  class printable_id: public printable
84  {
85  public:
86  virtual void
87  print(std::ostream& os, const char* x) const
88  {
89  os << '%' << *x;
90  }
91  };
92 
95  {
96  public:
97  virtual void
98  print(std::ostream& os, const char*) const
99  {
100  os << '%';
101  }
102  };
103 
104 
105  class SPOT_API formater
106  {
107  printable_id id;
108  printable_percent percent;
109  public:
110 
111  formater()
112  : has_(256), call_(256, &id)
113  {
114  call_['%'] = call_[0] = &percent;
115  }
116 
117  virtual ~formater()
118  {
119  }
120 
126  void
127  scan(const char* fmt, std::vector<bool>& has) const;
128 
129  void
130  scan(const std::string& fmt, std::vector<bool>& has) const
131  {
132  scan(fmt.c_str(), has);
133  }
135 
138  void
139  prime(const char* fmt);
140 
141  void
142  prime(const std::string& fmt)
143  {
144  prime(fmt.c_str());
145  }
147 
149  bool
150  has(char c) const
151  {
152  return has_[c];
153  }
154 
156  void
157  declare(char c, const printable* f)
158  {
159  call_[c] = f;
160  }
161 
163  void
164  set_output(std::ostream& output)
165  {
166  output_ = &output;
167  }
168 
170  std::ostream&
171  format(const char* fmt);
172 
174  std::ostream&
175  format(std::ostream& output, const char* fmt)
176  {
177  set_output(output);
178  return format(fmt);
179  }
180 
182  std::ostream&
183  format(const std::string& fmt)
184  {
185  return format(fmt.c_str());
186  }
187 
189  std::ostream&
190  format(std::ostream& output, const std::string& fmt)
191  {
192  return format(output, fmt.c_str());
193  }
194 
195  private:
196  std::vector<bool> has_;
197  std::vector<const printable*> call_;
198  protected:
199  std::ostream* output_;
200  };
201 
202 }
203 
204 #endif // SPOT_MISC_FORMATER_HH

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Thu May 15 2014 11:04:11 for spot by doxygen 1.8.4