spot  1.0
formater.hh
Go to the documentation of this file.
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2012 Laboratoire de Recherche et Développement de
3 // 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 <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
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
87  {
88  os << '%' << *x;
89  }
90  };
91 
94  {
95  public:
96  virtual void
97  print(std::ostream& os, const char*) const
98  {
99  os << '%';
100  }
101  };
102 
103 
104  class formater
105  {
108  public:
109 
111  : has_(256), call_(256, &id)
112  {
113  call_['%'] = call_[0] = &percent;
114  }
115 
116  virtual ~formater()
117  {
118  }
119 
121  void
122  prime(const char* fmt);
123 
125  void
126  prime(const std::string& fmt)
127  {
128  prime(fmt.c_str());
129  }
130 
132  bool
133  has(char c) const
134  {
135  return has_[c];
136  }
137 
139  void
140  declare(char c, const printable* f)
141  {
142  call_[c] = f;
143  }
144 
146  void
147  set_output(std::ostream& output)
148  {
149  output_ = &output;
150  }
151 
153  std::ostream&
154  format(const char* fmt);
155 
157  std::ostream&
158  format(std::ostream& output, const char* fmt)
159  {
160  set_output(output);
161  return format(fmt);
162  }
163 
165  std::ostream&
166  format(const std::string& fmt)
167  {
168  return format(fmt.c_str());
169  }
170 
172  std::ostream&
173  format(std::ostream& output, const std::string& fmt)
174  {
175  return format(output, fmt.c_str());
176  }
177 
178  private:
179  std::vector<bool> has_;
180  std::vector<const printable*> call_;
181  protected:
182  std::ostream* output_;
183  const char* pos_;
184  };
185 
186 }
187 
188 #endif // SPOT_MISC_FORMATER_HH

Please comment this page and report errors about it on the RefDocComments page.
Generated on Sat Oct 27 2012 09:34:32 for spot by doxygen 1.8.1.2