spot 2.11.4.dev
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
28namespace spot
29{
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>
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
76 operator=(T&& new_val)
77 {
78 val_ = std::move(new_val);
79 return *this;
80 }
81
82 virtual void
83 print(std::ostream& os, const char*) const override
84 {
85 os << val_;
86 }
87 };
88
90 class printable_id: public printable
91 {
92 public:
93 virtual void
94 print(std::ostream& os, const char* x) const override
95 {
96 os << '%' << *x;
97 }
98 };
99
102 {
103 public:
104 virtual void
105 print(std::ostream& os, const char*) const override
106 {
107 os << '%';
108 }
109 };
110
111
112 class SPOT_API formater
113 {
114 printable_id id;
115 printable_percent percent;
116 public:
117
118 formater()
119 : has_(256), call_(256, &id)
120 {
121 call_['%'] = call_[0] = &percent;
122 }
123
124 virtual ~formater()
125 {
126 }
127
133 void
134 scan(const char* fmt, std::vector<bool>& has) const;
135
136 void
137 scan(const std::string& fmt, std::vector<bool>& has) const
138 {
139 scan(fmt.c_str(), has);
140 }
142
145 void
146 prime(const char* fmt);
147
148 void
149 prime(const std::string& fmt)
150 {
151 prime(fmt.c_str());
152 }
154
156 bool
157 has(char c) const
158 {
159 return has_[c];
160 }
161
163 void
164 declare(char c, const printable* f)
165 {
166 call_[c] = f;
167 }
168
170 void
171 set_output(std::ostream& output)
172 {
173 output_ = &output;
174 }
175
177 std::ostream&
178 format(const char* fmt);
179
181 std::ostream&
182 format(std::ostream& output, const char* fmt)
183 {
184 std::ostream* tmp = output_;
185 set_output(output);
186 format(fmt);
187 set_output(*tmp);
188 return output;
189 }
190
192 std::ostream&
193 format(const std::string& fmt)
194 {
195 return format(fmt.c_str());
196 }
197
199 std::ostream&
200 format(std::ostream& output, const std::string& fmt)
201 {
202 return format(output, fmt.c_str());
203 }
204
205 private:
206 std::vector<bool> has_;
207 std::vector<const printable*> call_;
208 protected:
209 std::ostream* output_;
210 };
211}
Definition: formater.hh:113
void declare(char c, const printable *f)
Declare a callback function for c.
Definition: formater.hh:164
std::ostream & format(std::ostream &output, const char *fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:182
void set_output(std::ostream &output)
Remember where to output any string.
Definition: formater.hh:171
bool has(char c) const
Whether c occurred in the primed formats.
Definition: formater.hh:157
std::ostream & format(std::ostream &output, const std::string &fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:200
std::ostream & format(const std::string &fmt)
Expand the %-sequences in fmt, write the result on output_.
Definition: formater.hh:193
std::ostream & format(const char *fmt)
Expand the %-sequences in fmt, write the result on output_.
void scan(const char *fmt, std::vector< bool > &has) const
Scan the %-sequences occuring in fmt.
void prime(const std::string &fmt)
Definition: formater.hh:149
void scan(const std::string &fmt, std::vector< bool > &has) const
Scan the %-sequences occuring in fmt.
Definition: formater.hh:137
void prime(const char *fmt)
The default callback simply writes "%c".
Definition: formater.hh:91
Called by default for "%%" and "%\0".
Definition: formater.hh:102
Definition: formater.hh:44
Definition: formater.hh:31
Definition: automata.hh:27

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.4