-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiomanip.h
541 lines (432 loc) · 14.9 KB
/
iomanip.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#ifndef COOL_IOMANIP_H_
#define COOL_IOMANIP_H_
#include <cassert>
#include <ios>
#include <istream>
#include <optional>
#include <ostream>
#include <string>
///////////////////////////////////////////////////////////////////////////////
//
// These io manipulators save off the old state when streamed and restore that
// state when destructed.
//
// Those which take a parameter set the new state after saving off the old
// state.
//
// These correspond to the manipulators in <iomanip>:
//
// resetiosflags(std::ios_base::fmtflags)
//
// setiosflags(std::ios_base::fmtflags)
//
// setbase()
// setbase(int)
//
// template<typename charT, typename traits> setfill()
// template<typename charT, typename traits> setfill(charT)
//
// setprecision()
// setprecision(std::streamsize)
//
//
// These are new io manipulators:
//
// setflags saves and restores all flags:
//
// setflags()
// setflags(std::ios_base::fmtflags)
//
// setiomanip saves and restores precision, flags and fill
// If you pass in true, then the following are set after save
// (values from <http://eel.is/c++draft/basic.ios.cons>):
//
// precision = 6
// flags = skipws | dec
// fill = widen(' ')
//
// template<typename charT, typename traits> setiomanip()
// template<typename charT, typename traits> setiomanip(bool)
//
//
// While they are intended to be used as one-liners, you can declare them for
// multiline streaming operations. They all take an optional stream parameter
// if you wish to set them up when you declare them.
//
// There are deduction guides to make the syntax more regular for the templated
// classes, but deduction guides are a fairly new C++17 feature and compilers
// (gcc7 and clang5) do not yet agree on their usage.
//
//
// Example:
//
// std::cout << cool::setiomanip(true) << std::boolalpha << false << ' ' << true << '\n';
// std::cout << false << ' ' << true << '\n';
//
// Outputs:
// false true
// 0 1
//
///////////////////////////////////////////////////////////////////////////////
namespace cool
{
class resetiosflags
{
public:
using fmtflags = std::ios_base::fmtflags;
explicit resetiosflags(fmtflags mask)
: m_mask{mask}
{}
explicit resetiosflags(fmtflags mask, std::ios_base& ios)
: m_mask{mask}
{ save(ios); }
explicit resetiosflags(std::ios_base& ios, fmtflags mask)
: m_mask{mask}
{ save(ios); }
resetiosflags(resetiosflags const&) = delete;
resetiosflags& operator=(resetiosflags const&) = delete;
resetiosflags& operator=(resetiosflags&&) = delete;
resetiosflags(resetiosflags&&) = delete;
~resetiosflags()
{
if (m_ios)
m_ios->setf(m_flags, m_mask);
}
template<typename charT, typename traits>
friend auto& operator>>(std::basic_istream<charT, traits>& is, resetiosflags const& that)
{
that.save(is);
return is;
}
template<typename charT, typename traits>
friend auto& operator<<(std::basic_ostream<charT, traits>& os, resetiosflags const& that)
{
that.save(os);
return os;
}
private:
void save(std::ios_base& ios) const
{
assert(!m_ios);
m_flags = ios.setf(fmtflags{}, m_mask);
m_ios = &ios;
}
mutable std::ios_base* m_ios = nullptr;
fmtflags m_mask;
mutable fmtflags m_flags;
};
class setiosflags
{
public:
using fmtflags = std::ios_base::fmtflags;
explicit setiosflags(fmtflags mask)
: m_mask{mask}
{}
explicit setiosflags(fmtflags mask, std::ios_base& ios)
: m_mask{mask}
{ save(ios); }
explicit setiosflags(std::ios_base& ios, fmtflags mask)
: m_mask{mask}
{ save(ios); }
setiosflags(setiosflags const&) = delete;
setiosflags& operator=(setiosflags const&) = delete;
setiosflags& operator=(setiosflags&&) = delete;
setiosflags(setiosflags&&) = delete;
~setiosflags()
{
if (m_ios)
m_ios->setf(m_flags, m_mask);
}
template<typename charT, typename traits>
friend auto& operator>>(std::basic_istream<charT, traits>& is, setiosflags const& that)
{
that.save(is);
return is;
}
template<typename charT, typename traits>
friend auto& operator<<(std::basic_ostream<charT, traits>& os, setiosflags const& that)
{
that.save(os);
return os;
}
private:
void save(std::ios_base& ios) const
{
assert(!m_ios);
m_flags = ios.setf(m_mask);
m_ios = &ios;
}
mutable std::ios_base* m_ios = nullptr;
fmtflags m_mask;
mutable fmtflags m_flags;
};
class setbase
{
using fmtflags = std::ios_base::fmtflags;
public:
using base_type = int;
setbase() = default;
explicit setbase(int base)
: m_base{base}
{}
explicit setbase(int base, std::ios_base& ios)
: m_base{base}
{ save(ios); }
explicit setbase(std::ios_base& ios, int base)
: m_base{base}
{ save(ios); }
setbase(setbase const&) = delete;
setbase& operator=(setbase const&) = delete;
setbase& operator=(setbase&&) = delete;
setbase(setbase&&) = delete;
~setbase()
{
if (m_ios)
m_ios->setf(m_flags, std::ios_base::basefield);
}
template<typename charT, typename traits>
friend auto& operator>>(std::basic_istream<charT, traits>& is, setbase const& that)
{
that.save(is);
return is;
}
template<typename charT, typename traits>
friend auto& operator<<(std::basic_ostream<charT, traits>& os, setbase const& that)
{
that.save(os);
return os;
}
private:
void save(std::ios_base& ios) const
{
assert(!m_ios);
m_flags = m_base ? ios.setf(m_base == 8 ? std::ios_base::oct :
m_base == 10 ? std::ios_base::dec :
m_base == 16 ? std::ios_base::hex :
std::ios_base::fmtflags{} , std::ios_base::basefield) :
ios.flags();
m_ios = &ios;
}
mutable std::ios_base* m_ios = nullptr;
std::optional<int> m_base;
mutable fmtflags m_flags;
};
template<typename charT, typename traits = std::char_traits<charT>>
class setfill
{
public:
using char_type = charT;
using traits_type = traits;
setfill() = default;
explicit setfill(charT f)
: m_newFill{f}
{}
explicit setfill(charT f, std::basic_ios<charT, traits>& ios)
: m_newFill{f}
{ save(ios); }
explicit setfill(std::basic_ios<charT, traits>& ios)
{ save(ios); }
explicit setfill(std::basic_ios<charT, traits>& ios, charT f)
: m_newFill{f}
{ save(ios); }
setfill(setfill const&) = delete;
setfill& operator=(setfill const&) = delete;
setfill& operator=(setfill&&) = delete;
setfill(setfill&&) = delete;
~setfill()
{
if (m_ios)
m_ios->fill(m_oldFill);
}
friend auto& operator>>(std::basic_istream<charT, traits>& is, setfill const& that)
{
that.save(is);
return is;
}
friend auto& operator<<(std::basic_ostream<charT, traits>& os, setfill const& that)
{
that.save(os);
return os;
}
private:
void save(std::basic_ios<charT, traits>& ios) const
{
assert(!m_ios);
m_oldFill = m_newFill ? ios.fill(*m_newFill) : ios.fill();
m_ios = &ios;
}
mutable std::basic_ios<charT, traits>* m_ios = nullptr;
std::optional<charT> m_newFill;
mutable charT m_oldFill;
};
setfill() -> setfill<char>;
template<typename charT>
explicit setfill(charT) -> setfill<charT>;
template<typename charT, typename traits>
explicit setfill(charT, std::basic_ios<charT, traits>&) -> setfill<charT, traits>;
template<typename charT, typename traits>
explicit setfill(std::basic_ios<charT, traits>&) -> setfill<charT, traits>;
template<typename charT, typename traits>
explicit setfill(std::basic_ios<charT, traits>&, charT) -> setfill<charT, traits>;
class setprecision
{
public:
using streamsize = std::streamsize;
setprecision() = default;
explicit setprecision(streamsize p)
: m_newPrecision{p}
{}
explicit setprecision(streamsize p, std::ios_base& ios)
: m_newPrecision{p}
{ save(ios); }
explicit setprecision(std::ios_base& ios)
{ save(ios); }
explicit setprecision(std::ios_base& ios, std::streamsize p)
: m_newPrecision{p}
{ save(ios); }
setprecision(setprecision const&) = delete;
setprecision& operator=(setprecision const&) = delete;
setprecision& operator=(setprecision&&) = delete;
setprecision(setprecision&&) = delete;
~setprecision()
{
if (m_ios)
m_ios->precision(m_oldPrecision);
}
template<typename charT, typename traits>
friend auto& operator>>(std::basic_istream<charT, traits>& is, setprecision const& that)
{
that.save(is);
return is;
}
template<typename charT, typename traits>
friend auto& operator<<(std::basic_ostream<charT, traits>& os, setprecision const& that)
{
that.save(os);
return os;
}
private:
void save(std::ios_base& ios) const
{
assert(!m_ios);
m_oldPrecision = m_newPrecision ? ios.precision(*m_newPrecision) : ios.precision();
m_ios = &ios;
}
mutable std::ios_base* m_ios = nullptr;
std::optional<streamsize> m_newPrecision;
mutable streamsize m_oldPrecision;
};
class setflags
{
public:
using fmtflags = std::ios::fmtflags;
setflags() = default;
explicit setflags(fmtflags f)
: m_newFlags{f}
{}
explicit setflags(fmtflags f, std::ios_base& ios)
: m_newFlags{f}
{ save(ios); }
explicit setflags(std::ios_base& ios)
{ save(ios); }
explicit setflags(std::ios_base& ios, fmtflags f)
: m_newFlags{f}
{ save(ios); }
setflags(setflags const&) = delete;
setflags& operator=(setflags const&) = delete;
setflags& operator=(setflags&&) = delete;
setflags(setflags&&) = delete;
~setflags()
{
if (m_ios)
m_ios->flags(m_oldFlags);
}
template<typename charT, typename traits>
friend auto& operator>>(std::basic_istream<charT, traits>& is, setflags const& that)
{
that.save(is);
return is;
}
template<typename charT, typename traits>
friend auto& operator<<(std::basic_ostream<charT, traits>& os, setflags const& that)
{
that.save(os);
return os;
}
private:
void save(std::ios_base& ios) const
{
assert(!m_ios);
m_oldFlags = m_newFlags ? ios.flags(*m_newFlags) : ios.flags();
m_ios = &ios;
}
mutable std::ios_base* m_ios = nullptr;
std::optional<fmtflags> m_newFlags;
mutable fmtflags m_oldFlags;
};
template<typename charT, typename traits = std::char_traits<charT>>
class setiomanip
{
public:
using char_type = charT;
using traits_type = traits;
setiomanip() = default;
explicit setiomanip(bool init)
: m_init{init}
{}
explicit setiomanip(bool init, std::basic_ios<charT, traits>& ios)
: m_init{init}
{ save(ios); }
explicit setiomanip(std::basic_ios<charT, traits>& ios)
{ save(ios); }
explicit setiomanip(std::basic_ios<charT, traits>& ios, bool init)
: m_init{init}
{ save(ios); }
setiomanip(setiomanip const&) = delete;
setiomanip& operator=(setiomanip const&) = delete;
setiomanip& operator=(setiomanip&&) = delete;
setiomanip(setiomanip&&) = delete;
~setiomanip()
{
if (m_ios)
{
m_ios->fill(m_fill);
m_ios->flags(m_flags);
m_ios->precision(m_precision);
}
}
friend auto& operator>>(std::basic_istream<charT, traits>& is, setiomanip const& that)
{
that.save(is);
return is;
}
friend auto& operator<<(std::basic_ostream<charT, traits>& os, setiomanip const& that)
{
that.save(os);
return os;
}
private:
void save(std::basic_ios<charT, traits>& ios) const
{
assert(!m_ios);
m_precision = m_init ? ios.precision(6) : ios.precision();
m_flags = m_init ? ios.flags(std::ios_base::skipws | std::ios_base::dec) : ios.flags();
m_fill = m_init ? ios.fill(ios.widen(' ')) : ios.fill();
m_ios = &ios;
}
mutable std::basic_ios<charT, traits>* m_ios = nullptr;
mutable std::streamsize m_precision;
mutable std::ios_base::fmtflags m_flags;
mutable charT m_fill;
bool m_init = false;
};
setiomanip() -> setiomanip<char>;
explicit setiomanip(bool) -> setiomanip<char>;
template<typename charT, typename traits>
explicit setiomanip(bool, std::basic_ios<charT, traits>&) -> setiomanip<charT, traits>;
template<typename charT, typename traits>
explicit setiomanip(std::basic_ios<charT, traits>&) -> setiomanip<charT, traits>;
template<typename charT, typename traits>
explicit setiomanip(std::basic_ios<charT, traits>&, bool) -> setiomanip<charT, traits>;
} // cool namespace
#endif /* COOL_IOMANIP_H_ */