spot 2.12.2
formater.hh
1// -*- coding: utf-8 -*-
2// Copyright (C) by the Spot authors, see the AUTHORS file for details.
3//
4// This file is part of Spot, a model checking library.
5//
6// Spot is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 3 of the License, or
9// (at your option) any later version.
10//
11// Spot is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14// License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19#pragma once
20
21#include <spot/misc/common.hh>
22#include <spot/misc/timer.hh>
23#include <iostream>
24#include <string>
25#include <vector>
26
27namespace spot
28{
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>
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: formater.hh:112
void declare(char c, const printable *f)
Declare a callback function for c.
Definition: formater.hh:163
std::ostream & format(std::ostream &output, const char *fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:181
void set_output(std::ostream &output)
Remember where to output any string.
Definition: formater.hh:170
bool has(char c) const
Whether c occurred in the primed formats.
Definition: formater.hh:156
std::ostream & format(std::ostream &output, const std::string &fmt)
Expand the %-sequences in fmt, write the result on output.
Definition: formater.hh:199
std::ostream & format(const std::string &fmt)
Expand the %-sequences in fmt, write the result on output_.
Definition: formater.hh:192
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 occurring in fmt.
void prime(const std::string &fmt)
Definition: formater.hh:148
void scan(const std::string &fmt, std::vector< bool > &has) const
Scan the %-sequences occurring in fmt.
Definition: formater.hh:136
void prime(const char *fmt)
The default callback simply writes "%c".
Definition: formater.hh:90
Called by default for "%%" and "%\0".
Definition: formater.hh:101
Definition: formater.hh:43
Definition: formater.hh:30
Definition: automata.hh:26

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