12 #ifndef ECL_FORMATTERS_NUMBER_HPP_
13 #define ECL_FORMATTERS_NUMBER_HPP_
44 namespace interfaces {
56 template <
typename Number >
85 FormatNumber<Number>&
width(
int w);
152 template <
typename OutputStream,
typename N>
friend OutputStream&
operator << (OutputStream& ostream, FormatNumber<N>& formatter);
169 template <
typename OutputStream>
void pad(
int n, OutputStream &ostream)
const;
170 template <
typename OutputStream>
void prePad(
int n, OutputStream &ostream)
const;
171 template <
typename OutputStream>
void postPad(
int n, OutputStream &ostream)
const;
176 template <
typename OutputStream>
void formatBin(OutputStream &ostream)
const;
177 template <
typename OutputStream>
void formatHex(OutputStream &ostream)
const;
178 template <
typename OutputStream>
void formatDec(OutputStream &ostream)
const;
185 template <
typename Number>
192 template <
typename Number>
196 if ( ( *width_ > 0 ) && ( *alignment_ ==
NoAlign ) ) {
202 template <
typename Number>
212 template <
typename Number>
224 template <
typename Number>
225 template <
typename OutputStream>
228 int size = 8*
sizeof(Number);
229 prePad(*width_ - (size + 2),ostream);
231 for (
int i = size - 1; i>=0; i--)
233 ostream <<
"01"[((value_ >> i) & 1)];
235 postPad(*width_ - (size + 2),ostream);
238 template <
typename Number>
239 template <
typename OutputStream>
242 static const char hex_string[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
244 int size = 2*
sizeof(Number);
245 prePad(*width_ - (size + 2),ostream);
247 for (
int i = size - 1; i>=0; i--)
250 ostream << hex_string[((value_ >> i*4) & 0xF)];
252 postPad(*width_ - (size + 2),ostream);
255 template <
typename Number>
256 template <
typename OutputStream>
260 char *s = convert(value_);
261 int size = strlen(s);
262 prePad(*width_ - size,ostream);
264 postPad(*width_ - size,ostream);
267 template <
typename Number>
268 template <
typename OutputStream>
271 if ( n <= 0 ) {
return; }
272 switch ( *alignment_ )
276 case (
RightAlign ) : { pad(n,ostream);
break; }
277 case (
CentreAlign ) : { pad(n/2+ n%2,ostream);
break; }
282 template <
typename Number>
283 template <
typename OutputStream>
286 if ( n <= 0 ) {
return; }
287 switch ( *alignment_ )
292 case (
CentreAlign ) : { pad(n/2,ostream);
break; }
297 template <
typename Number>
298 template <
typename OutputStream>
301 for (
int i = n; i > 0; --i )
312 template <
typename Number>
316 ready_to_format =
true;
327 template <
typename Number>
331 alignment_ = &tmp_alignment;
336 ready_to_format =
true;
342 template <
typename Number>
346 alignment_ = &tmp_alignment;
352 ready_to_format =
true;
360 template <
typename OutputStream,
typename N>
363 bool ready = formatter.ready_to_format;
366 "either there is no data available, or you have tried to use the "
367 "formatter more than once in a single streaming operation. "
368 "C++ produces unspecified results when functors are used multiply "
369 "in the same stream sequence, so this is not permitted here.") );
373 switch(*(formatter.base_) )
376 formatter.formatBin(ostream);
380 formatter.formatHex(ostream);
384 formatter.formatDec(ostream);
388 if ( formatter.width_ != &(formatter.prm_width) ) {
389 formatter.width_ = &(formatter.prm_width);
390 formatter.alignment_ = &(formatter.prm_alignment);
391 formatter.base_ = &(formatter.prm_base);
393 formatter.ready_to_format =
false;
408 class Format<short> :
public interfaces::FormatNumber<short>
478 class Format<signed char> :
public interfaces::FormatNumber<signed char>
496 class Format<unsigned short> :
public interfaces::FormatNumber<unsigned short>
514 class Format<unsigned int> :
public interfaces::FormatNumber<unsigned int>