number.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_FORMATTERS_NUMBER_HPP_
13 #define ECL_FORMATTERS_NUMBER_HPP_
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include "common.hpp"
20 #include <ecl/exceptions/standard_exception.hpp>
21 #include <ecl/converters/char_strings.hpp>
22 
23 /*****************************************************************************
24 ** Namespaces
25 *****************************************************************************/
26 
27 namespace ecl {
28 
29 /*****************************************************************************
30 ** Format Tags
31 *****************************************************************************/
38 {
39  Bin,
40  Hex,
41  Dec,
42 };
43 
44 namespace interfaces {
45 
46 /*****************************************************************************
47 ** FormatNumber Interface [Integral types]
48 *****************************************************************************/
49 
56 template < typename Number >
57 class FormatNumber {
58  public:
59  /******************************************
60  ** C&D's
61  *******************************************/
69 
70  virtual ~FormatNumber() {}
71 
72  /******************************************
73  ** Set
74  *******************************************/
80  FormatNumber<Number>& base(ecl::IntegralBase b);
85  FormatNumber<Number>& width(int w);
91  FormatNumber<Number>& align(ecl::Alignment a);
92 
93  /******************************************
94  * Set Format Combinations
95  ******************************************/
103  FormatNumber<Number>& operator ()(int w, ecl::Alignment a, ecl::IntegralBase b);
104 
105  /******************************************
106  ** Format a value
107  *******************************************/
114  FormatNumber<Number>& operator() (Number n);
115 
116  /******************************************
117  ** Common format usages
118  *******************************************/
127  FormatNumber<Number>& operator() (Number n, int w, ecl::IntegralBase b);
137  FormatNumber<Number>& operator() (Number n, int w, ecl::Alignment a, ecl::IntegralBase b);
138 
139  /******************************************
140  ** Insert the formatter into a stream
141  *******************************************/
152  template <typename OutputStream, typename N> friend OutputStream& operator << (OutputStream& ostream, FormatNumber<N>& formatter) ecl_assert_throw_decl(StandardException);
153 
154  protected:
155  /******************************************
156  ** Paramaters
157  *******************************************/
158  int prm_width, tmp_width;
161  int *width_;
163  bool ready_to_format;
164  Number value_;
165 
166  /******************************************
167  ** Padding
168  *******************************************/
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;
172 
173  /******************************************
174  ** Formatter Functions
175  *******************************************/
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;
179 
180 }; // FormatNumber
181 
182 /*****************************************************************************
183 * FormatNumber Implementation [configuration]
184 *****************************************************************************/
185 template <typename Number>
186 FormatNumber<Number>& FormatNumber<Number>::base(ecl::IntegralBase b)
187 {
188  *base_ = b;
189  return *this;
190 }
191 
192 template <typename Number>
193 FormatNumber<Number>& FormatNumber<Number>::width(int w)
194 {
195  *width_ = w;
196  if ( ( *width_ > 0 ) && ( *alignment_ == NoAlign ) ) {
198  }
199  return *this;
200 }
201 
202 template <typename Number>
203 FormatNumber<Number>& FormatNumber<Number>::align(ecl::Alignment a)
204 {
205  *alignment_ = a;
206  if ( *alignment_ == NoAlign )
207  {
208  *width_ = 0;
209  }
210  return *this;
211 }
212 template <typename Number>
214 {
215  base(b);
216  width(w);
217  align(a);
218  return *this;
219 }
220 
221 /*****************************************************************************
222 * FormatNumber Implementation [internal formatting]
223 *****************************************************************************/
224 template <typename Number>
225  template <typename OutputStream>
226 void FormatNumber<Number>::formatBin(OutputStream &ostream) const
227 {
228  int size = 8*sizeof(Number);
229  prePad(*width_ - (size + 2),ostream);
230  ostream << "0b";
231  for (int i = size - 1; i>=0; i--)
232  {
233  ostream << "01"[((value_ >> i) & 1)];
234  }
235  postPad(*width_ - (size + 2),ostream);
236 }
237 
238 template <typename Number>
239  template <typename OutputStream>
240 void FormatNumber<Number>::formatHex(OutputStream &ostream) const
241 {
242  static const char hex_string[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
243 
244  int size = 2*sizeof(Number);
245  prePad(*width_ - (size + 2),ostream);
246  ostream << "0x";
247  for (int i = size - 1; i>=0; i--)
248  {
249 // ostream << "0123456789abcdef"[((value_ >> i*4) & 0xF)];
250  ostream << hex_string[((value_ >> i*4) & 0xF)]; // Dont have to recreate this every time
251  }
252  postPad(*width_ - (size + 2),ostream);
253 }
254 
255 template <typename Number>
256  template <typename OutputStream>
257 void FormatNumber<Number>::formatDec(OutputStream &ostream) const
258 {
259  static ecl::Converter<char*> convert;
260  char *s = convert(value_);
261  int size = strlen(s);
262  prePad(*width_ - size,ostream);
263  ostream << s;
264  postPad(*width_ - size,ostream);
265 }
266 
267 template <typename Number>
268  template <typename OutputStream>
269 void FormatNumber<Number>::prePad(int n, OutputStream &ostream) const
270 {
271  if ( n <= 0 ) { return; }
272  switch ( *alignment_ )
273  {
274  case ( NoAlign ) : { break; }
275  case ( LeftAlign ) : { break; }
276  case ( RightAlign ) : { pad(n,ostream); break; }
277  case ( CentreAlign ) : { pad(n/2+ n%2,ostream); break; } // Add the remainder
278  default : break;
279  }
280 }
281 
282 template <typename Number>
283  template <typename OutputStream>
284 void FormatNumber<Number>::postPad(int n, OutputStream &ostream) const
285 {
286  if ( n <= 0 ) { return; }
287  switch ( *alignment_ )
288  {
289  case ( NoAlign ) : { break; }
290  case ( LeftAlign ) : { pad(n,ostream); break; }
291  case ( RightAlign ) : { break; }
292  case ( CentreAlign ) : { pad(n/2,ostream); break; } // Do not add the remainder
293  default : break;
294  }
295 }
296 
297 template <typename Number>
298  template <typename OutputStream>
299 void FormatNumber<Number>::pad(int n, OutputStream &ostream) const
300 {
301  for (int i = n; i > 0; --i )
302  {
303  ostream << ' ';
304  }
305 }
306 /*****************************************************************************
307 * FormatNumber Implementation [value]
308 *****************************************************************************/
312 template <typename Number>
313 FormatNumber<Number>& FormatNumber<Number>::operator()(Number n)
314 {
315  value_ = n;
316  ready_to_format = true;
317  return *this;
318 }
319 
320 /*****************************************************************************
321 * FormatNumber Implementation [temporary formatting]
322 *****************************************************************************/
323 
327 template <typename Number>
328 FormatNumber<Number>& FormatNumber<Number>::operator() (Number n, int w, ecl::IntegralBase b)
329 {
330  width_ = &tmp_width;
332  base_ = &tmp_base;
333  base(b);
334  width(w);
335  value_ = n;
336  ready_to_format = true;
337  return *this;
338 }
342 template <typename Number>
343 FormatNumber<Number>& FormatNumber<Number>::operator() (Number n, int w, ecl::Alignment a, ecl::IntegralBase b)
344 {
345  width_ = &tmp_width;
347  base_ = &tmp_base;
348  base(b);
349  width(w);
350  align(a);
351  value_ = n;
352  ready_to_format = true;
353  return *this;
354 }
355 
356 
357 /*****************************************************************************
358 * FormatNumber Implementation [streaming]
359 *****************************************************************************/
360 template <typename OutputStream, typename N>
361 OutputStream& operator << (OutputStream &ostream, FormatNumber<N>& formatter ) ecl_assert_throw_decl(StandardException)
362 {
363  bool ready = formatter.ready_to_format;
364 
365  ecl_assert_throw(ready, StandardException(LOC,UsageError,"The formatter cannot print any data - "
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.") );
370 
371  if ( ready )
372  {
373  switch(*(formatter.base_) )
374  {
375  case(Bin) : {
376  formatter.formatBin(ostream);
377  break;
378  }
379  case(Hex) : {
380  formatter.formatHex(ostream);
381  break;
382  }
383  case(Dec) : {
384  formatter.formatDec(ostream);
385  break;
386  }
387  }
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);
392  }
393  formatter.ready_to_format = false;
394  }
395  return ostream;
396 }
397 
398 } // namespace interfaces
399 
400 /*****************************************************************************
401 * Format Classes
402 *****************************************************************************/
407 template <>
408 class Format<short> : public interfaces::FormatNumber<short>
409 {
410  public:
417  Format(int w = -1, Alignment a = NoAlign, IntegralBase b = Dec) : interfaces::FormatNumber<short>(w, a, b) {}
418  virtual ~Format() {}
419 };
424 template <>
425 class Format<int> : public interfaces::FormatNumber<int>
426 {
427  public:
434  Format(int w = -1, Alignment a = NoAlign, IntegralBase b = Dec) : interfaces::FormatNumber<int>(w, a, b) {}
435  virtual ~Format() {}
436 };
441 template <>
442 class Format<long> : public interfaces::FormatNumber<long>
443 {
444  public:
451  Format(int w = -1, Alignment a = NoAlign, IntegralBase b = Dec) : interfaces::FormatNumber<long>(w, a, b) {}
452  virtual ~Format() {}
453 };
454 
459 template <>
460 class Format<char> : public interfaces::FormatNumber<char>
461 {
462  public:
469  Format(int w = -1, Alignment a = NoAlign, IntegralBase b = Dec) : interfaces::FormatNumber<char>(w, a, b) {}
470  virtual ~Format() {}
471 };
472 
477 template <>
478 class Format<signed char> : public interfaces::FormatNumber<signed char>
479 {
480  public:
488  virtual ~Format() {}
489 };
490 
495 template <>
496 class Format<unsigned short> : public interfaces::FormatNumber<unsigned short>
497 {
498  public:
506  virtual ~Format() {}
507 };
508 
513 template <>
514 class Format<unsigned int> : public interfaces::FormatNumber<unsigned int>
515 {
516  public:
524  virtual ~Format() {}
525 };
530 template <>
531 class Format<unsigned long> : public interfaces::FormatNumber<unsigned long>
532 {
533  public:
541  virtual ~Format() {}
542 };
547 template <>
548 class Format<unsigned char> : public interfaces::FormatNumber<unsigned char>
549 {
550  public:
552  virtual ~Format() {}
553 };
554 
555 }; // namespace ecl
556 
557 #endif /*ECL_FORMATTERS_NUMBER_HPP_*/
void formatHex(OutputStream &ostream) const
Definition: number.hpp:240
ecl::IntegralBase tmp_base
Definition: number.hpp:160
FormatNumber< Number > & align(ecl::Alignment a)
Definition: number.hpp:203
IntegralBase
Formatter for integral types.
Definition: number.hpp:57
#define LOC
Stringify the line of code you are at.
void prePad(int n, OutputStream &ostream) const
Definition: number.hpp:269
Primary template for all formatter classes.
Definition: common.hpp:99
#define ecl_assert_throw(expression, exception)
Debug mode throw with a logical condition check.
Standard exception type, provides code location and error string.
ecl::IntegralBase prm_base
Definition: number.hpp:160
FormatNumber< Number > & width(int w)
Definition: number.hpp:193
void pad(int n, OutputStream &ostream) const
Definition: number.hpp:299
UsageError
void formatBin(OutputStream &ostream) const
Definition: number.hpp:226
ecl::Alignment tmp_alignment
Definition: number.hpp:159
ecl_geometry_PUBLIC int size(const Trajectory2D &trajectory)
FormatNumber< Number > & operator()(int w, ecl::Alignment a, ecl::IntegralBase b)
Definition: number.hpp:213
void postPad(int n, OutputStream &ostream) const
Definition: number.hpp:284
RightAlign
#define ecl_assert_throw_decl(exception)
Assure throw exception declaration.
ecl::Alignment * alignment_
Definition: number.hpp:162
FormatNumber(int w=-1, ecl::Alignment a=NoAlign, ecl::IntegralBase b=Dec)
FormatNumber< Number > & base(ecl::IntegralBase b)
Definition: number.hpp:186
void formatDec(OutputStream &ostream) const
Definition: number.hpp:257
CentreAlign
ecl::Alignment prm_alignment
Definition: number.hpp:159


xbot_driver
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:27:37