FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
iomanip.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef IOMANIP_H
22#define IOMANIP_H
23
24#include <ctype.h>
25#include <stddef.h>
26#include "ios.h"
27
28namespace streams
29{
31 class setw_
32 {
33 public:
34 template<typename FSTREAM> void operator()(FSTREAM& stream) const
35 {
36 stream.width(width_);
37 }
38
39 private:
40 explicit constexpr setw_(uint8_t width) : width_{width} {}
41 const uint8_t width_;
42 friend constexpr const setw_ setw(uint8_t width);
43 };
44 template<typename FSTREAM> FSTREAM& operator<<(FSTREAM& stream, const setw_ f)
45 {
46 f(stream);
47 return stream;
48 }
49 template<typename FSTREAM> FSTREAM& operator>>(FSTREAM& stream, const setw_ f)
50 {
51 f(stream);
52 return stream;
53 }
54
55 class setprecision_
56 {
57 public:
58 template<typename FSTREAM> void operator()(FSTREAM& stream) const
59 {
60 stream.precision(precision_);
61 }
62
63 private:
64 explicit constexpr setprecision_(uint8_t precision) : precision_{precision} {}
65 const uint8_t precision_;
66 friend constexpr const setprecision_ setprecision(uint8_t precision);
67 };
68 template<typename FSTREAM> FSTREAM& operator<<(FSTREAM& stream, const setprecision_ f)
69 {
70 f(stream);
71 return stream;
72 }
73 template<typename FSTREAM> FSTREAM& operator>>(FSTREAM& stream, const setprecision_ f)
74 {
75 f(stream);
76 return stream;
77 }
78
79 class setfill_
80 {
81 public:
82 template<typename FSTREAM> void operator()(FSTREAM& stream) const
83 {
84 stream.fill(fill_);
85 }
86
87 private:
88 explicit constexpr setfill_(char fill) : fill_{fill} {}
89 const char fill_;
90 friend constexpr const setfill_ setfill(char fill);
91 };
92 template<typename FSTREAM> FSTREAM& operator<<(FSTREAM& stream, const setfill_ f)
93 {
94 f(stream);
95 return stream;
96 }
97 template<typename FSTREAM> FSTREAM& operator>>(FSTREAM& stream, const setfill_ f)
98 {
99 f(stream);
100 return stream;
101 }
102
103 class setbase_
104 {
105 public:
106 template<typename FSTREAM> void operator()(FSTREAM& stream) const
107 {
108 stream.setf(base_, ios::basefield);
109 }
110
111 private:
112 static constexpr ios::fmtflags convert_base(int base)
113 {
114 if (base == 2) return ios::bin;
115 if (base == 8) return ios::oct;
116 if (base == 16) return ios::hex;
117 return ios::dec;
118 }
119 explicit constexpr setbase_(int base) : base_{convert_base(base)} {}
120 const ios::fmtflags base_;
121 friend constexpr const setbase_ setbase(int base);
122 };
123 template<typename FSTREAM> FSTREAM& operator<<(FSTREAM& stream, const setbase_ f)
124 {
125 f(stream);
126 return stream;
127 }
128 template<typename FSTREAM> FSTREAM& operator>>(FSTREAM& stream, const setbase_ f)
129 {
130 f(stream);
131 return stream;
132 }
133
134 class setiosflags_
135 {
136 public:
137 template<typename FSTREAM> void operator()(FSTREAM& stream) const
138 {
139 stream.setf(mask_);
140 }
141
142 private:
143 explicit constexpr setiosflags_(ios::fmtflags mask) : mask_{mask} {}
144 const ios::fmtflags mask_;
145 friend constexpr const setiosflags_ setiosflags(ios::fmtflags mask);
146 };
147 template<typename FSTREAM> FSTREAM& operator<<(FSTREAM& stream, const setiosflags_ f)
148 {
149 f(stream);
150 return stream;
151 }
152 template<typename FSTREAM> FSTREAM& operator>>(FSTREAM& stream, const setiosflags_ f)
153 {
154 f(stream);
155 return stream;
156 }
157
158 class resetiosflags_
159 {
160 public:
161 template<typename FSTREAM> void operator()(FSTREAM& stream) const
162 {
163 stream.unsetf(mask_);
164 }
165
166 private:
167 explicit constexpr resetiosflags_(ios::fmtflags mask) : mask_{mask} {}
168 const ios::fmtflags mask_;
169 friend constexpr const resetiosflags_ resetiosflags(ios::fmtflags mask);
170 };
171 template<typename FSTREAM> FSTREAM& operator<<(FSTREAM& stream, const resetiosflags_ f)
172 {
173 f(stream);
174 return stream;
175 }
176 template<typename FSTREAM> FSTREAM& operator>>(FSTREAM& stream, const resetiosflags_ f)
177 {
178 f(stream);
179 return stream;
180 }
182
195 constexpr const setw_ setw(uint8_t width)
196 {
197 return setw_{width};
198 }
199
214 constexpr const setprecision_ setprecision(uint8_t precision)
215 {
216 return setprecision_{precision};
217 }
218
238 constexpr const setbase_ setbase(int base)
239 {
240 return setbase_{base};
241 }
242
253 constexpr const setfill_ setfill(char fill)
254 {
255 return setfill_{fill};
256 }
257
270 constexpr const setiosflags_ setiosflags(ios::fmtflags mask)
271 {
272 return setiosflags_{mask};
273 }
274
287 constexpr const resetiosflags_ resetiosflags(ios::fmtflags mask)
288 {
289 return resetiosflags_{mask};
290 }
291}
292
293#endif /* IOMANIP_H */
uint16_t fmtflags
Bitmask type to represent stream format flags.
Definition: ios.h:209
C++-like std::iostream facilities.
Defines C++-like streams API, based on circular buffers for input or output.
Definition: empty_streams.h:34
constexpr const setbase_ setbase(int base)
Set the ios::basefield to one of its possible values (ios::dec, ios::bin, ios::oct or ios::hex) accor...
Definition: iomanip.h:238
constexpr const setprecision_ setprecision(uint8_t precision)
Set the decimal precision to be used to format floating-point values on output operations.
Definition: iomanip.h:214
constexpr const setw_ setw(uint8_t width)
Set the field width to be used on output (and some input) operations.
Definition: iomanip.h:195
constexpr const resetiosflags_ resetiosflags(ios::fmtflags mask)
Unset the format flags specified by mask.
Definition: iomanip.h:287
constexpr const setfill_ setfill(char fill)
Set a new fill character.
Definition: iomanip.h:253
constexpr const setiosflags_ setiosflags(ios::fmtflags mask)
Set the format flags specified by mask.
Definition: iomanip.h:270