Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JManip.hh
Go to the documentation of this file.
1 #ifndef __JLANG_JMANIP__
2 #define __JLANG_JMANIP__
3 
4 #include <string>
5 #include <ostream>
6 #include <sstream>
7 #include <iomanip>
8 
9 
10 /**
11  * \file
12  * I/O manipulators.
13  * \author mdejong
14  */
15 namespace JLANG {}
16 namespace JPP { using namespace JLANG; }
17 
18 namespace JLANG {
19 
20  /**
21  * Get index for user I/O manipulation.
22  *
23  * \return index
24  */
25  inline int getIndex()
26  {
27  static const int index = std::ios_base::xalloc();
28 
29  return index;
30  }
31 
32 
33  /**
34  * Print options.
35  */
37  SHORT_PRINT = 1, //!< short print
38  MEDIUM_PRINT = 2, //!< medium print
39  LONG_PRINT = 3 //!< long print
40  };
41 }
42 
43 
44 /**
45  * Get print option.
46  *
47  * \param out output stream
48  * \return print option
49  */
50 inline int getPrintOption(std::ostream& out)
51 {
52  return out.iword(JLANG::getIndex());
53 }
54 
55 
56 /**
57  * Set print option.
58  *
59  * \param out output stream
60  * \param option print option
61  */
62 inline void setPrintOption(std::ostream& out, const int option)
63 {
64  out.iword(JLANG::getIndex()) = option;
65 }
66 
67 
68 /**
69  * Get short print option.
70  *
71  * \param out output stream
72  * \return true if short print option is on; else false
73  */
74 inline bool getShortprint(std::ostream& out)
75 {
76  return getPrintOption(out) == JLANG::SHORT_PRINT;
77 }
78 
79 
80 /**
81  * Set short print option.
82  *
83  * \param out output stream
84  */
85 inline void setShortprint(std::ostream& out)
86 {
88 }
89 
90 
91 /**
92  * Get medium print option.
93  *
94  * \param out output stream
95  * \return true if medium print option is on; else false
96  */
97 inline bool getMediumprint(std::ostream& out)
98 {
99  return getPrintOption(out) == JLANG::MEDIUM_PRINT;
100 }
101 
102 
103 /**
104  * Set medium print option.
105  *
106  * \param out output stream
107  */
108 inline void setMediumprint(std::ostream& out)
109 {
110  return setPrintOption(out, JLANG::MEDIUM_PRINT);
111 }
112 
113 
114 /**
115  * Get long print option.
116  *
117  * \param out output stream
118  * \return true if long print option is on; else false
119  */
120 inline bool getLongprint(std::ostream& out)
121 {
122  return getPrintOption(out) == JLANG::LONG_PRINT;
123 }
124 
125 
126 /**
127  * Set long print option.
128  *
129  * \param out output stream
130  */
131 inline void setLongprint(std::ostream& out)
132 {
133  return setPrintOption(out, JLANG::LONG_PRINT);
134 }
135 
136 
137 /**
138  * Set short printing.
139  *
140  * \param out output stream
141  * \return output stream
142  */
143 inline std::ostream& shortprint(std::ostream& out)
144 {
145  setShortprint(out);
146 
147  return out;
148 }
149 
150 
151 /**
152  * Set medium printing.
153  *
154  * \param out output stream
155  * \return output stream
156  */
157 inline std::ostream& mediumprint(std::ostream& out)
158 {
159  setMediumprint(out);
160 
161  return out;
162 }
163 
164 
165 /**
166  * Set long printing.
167  *
168  * \param out output stream
169  * \return output stream
170  */
171 inline std::ostream& longprint(std::ostream& out)
172 {
173  setLongprint(out);
174 
175  return out;
176 }
177 
178 
179 /**
180  * Print newline character.
181  *
182  * \param out output stream
183  * \return output stream
184  */
185 inline std::ostream& newline(std::ostream& out)
186 {
187  return out << '\n';
188 }
189 
190 
191 /**
192  * Print white space character.
193  *
194  * \param out output stream
195  * \return output stream
196  */
197 inline std::ostream& whitespace(std::ostream& out)
198 {
199  return out << ' ';
200 }
201 
202 
203 /**
204  * Print tab character.
205  *
206  * \param out output stream
207  * \return output stream
208  */
209 inline std::ostream& tab(std::ostream& out)
210 {
211  return out << '\t';
212 }
213 
214 
215 /**
216  * Rewind character.
217  *
218  * \param out output stream
219  * \return output stream
220  */
221 inline std::ostream& rewind(std::ostream& out)
222 {
223  return (out << '\r').flush();
224 }
225 
226 
227 /**
228  * Auxiliary data structure for alignment of data.
229  */
230 struct WIDTH {
231  /**
232  * Constructor.
233  *
234  * \param width width
235  */
236  WIDTH(const int width)
237  {
238  this->width = width;
239  }
240 
241 
242  /**
243  * Format specifier.
244  *
245  * \param out output stream
246  * \param format format
247  * \return output stream
248  */
249  friend inline std::ostream& operator<<(std::ostream& out, const WIDTH& format)
250  {
251  using namespace std;
252 
253  return out << setw(format.width);
254  }
255 
256  int width;
257 };
258 
259 
260 /**
261  * Auxiliary data structure for alignment of data.
262  */
263 struct LEFT :
264  public WIDTH
265 {
266  /**
267  * Constructor.
268  *
269  * \param width width
270  */
271  LEFT(const int width) :
272  WIDTH(width)
273  {}
274 
275 
276  /**
277  * Format specifier.
278  *
279  * \param out output stream
280  * \param format format
281  * \return output stream
282  */
283  friend inline std::ostream& operator<<(std::ostream& out, const LEFT& format)
284  {
285  using namespace std;
286 
287  return out << setw(format.width) << left;
288  }
289 };
290 
291 
292 /**
293  * Auxiliary data structure for alignment of data.
294  */
295 struct RIGHT :
296  public WIDTH
297 {
298  /**
299  * Constructor.
300  *
301  * \param width width
302  */
303  RIGHT(const int width) :
304  WIDTH(width)
305  {}
306 
307 
308  /**
309  * Format specifier.
310  *
311  * \param out output stream
312  * \param format format
313  * \return output stream
314  */
315  friend inline std::ostream& operator<<(std::ostream& out, const RIGHT& format)
316  {
317  using namespace std;
318 
319  return out << setw(format.width) << right;
320  }
321 };
322 
323 
324 /**
325  * Auxiliary data structure for sequence of same character.
326  */
327 struct FILL :
328  public WIDTH
329 {
330  /**
331  * Constructor.
332  *
333  * \param width width
334  * \param fill fill character
335  */
336  FILL(const int width = 0,
337  const char fill = ' ') :
338  WIDTH(width)
339  {
340  this->fill = fill;
341  }
342 
343 
344  /**
345  * Format specifier.
346  *
347  * \param out output stream
348  * \param format format
349  * \return output stream
350  */
351  friend inline std::ostream& operator<<(std::ostream& out, const FILL& format)
352  {
353  using namespace std;
354 
355  return out << setfill(format.fill) << setw(format.width);
356  }
357 
358  char fill;
359 };
360 
361 
362 /**
363  * Auxiliary data structure for alignment of data.
364  */
365 struct CENTER :
366  public WIDTH
367 {
368 protected:
369  /**
370  * Auxiliary class for format center.
371  */
372  struct JCenter :
373  public WIDTH
374  {
375  /**
376  * Constructor.
377  *
378  * \param out output stream
379  * \param format format center
380  */
381  JCenter(std::ostream& out, const WIDTH& format) :
382  WIDTH(format),
383  out (out)
384  {}
385 
386 
387  /**
388  * Write value to output stream.
389  *
390  * \param value value
391  * \return this JCenter
392  */
393  template<class T>
394  std::ostream& operator<<(const T& value)
395  {
396  using namespace std;
397 
398  ostringstream os;
399 
400  os.copyfmt(out);
401 
402  os << value;
403 
404  const int w = this->width - os.str().size();
405  const char c = this->out.fill();
406 
407  if (w > 0)
408  return this->out << FILL(w/2) << ' ' << os.str() << FILL((w+1)/2) << ' ' << setfill(c);
409  else
410  return this->out << os.str();
411  }
412 
413  private:
414  std::ostream& out;
415  };
416 
417 public:
418  /**
419  * Constructor.
420  *
421  * \param width width
422  */
423  CENTER(const int width) :
424  WIDTH(width)
425  {}
426 
427 
428  /**
429  * Format specifier.
430  *
431  * \param out output stream
432  * \param format format
433  * \return output stream
434  */
435  friend inline JCenter operator<<(std::ostream& out, const CENTER& format)
436  {
437  return JCenter(out, format);
438  }
439 };
440 
441 
442 /**
443  * Auxiliary data structure for floating point format specification.
444  */
445 struct FIXED :
446  public WIDTH
447 {
448  /**
449  * Constructor.
450  *
451  * \param width width
452  * \param precision precision
453  */
454  FIXED(const int width,
455  const int precision) :
456  WIDTH(width)
457  {
458  this->precision = precision;
459  }
460 
461 
462  /**
463  * Format specifier.
464  *
465  * \param out output stream
466  * \param format format
467  * \return output stream
468  */
469  friend inline std::ostream& operator<<(std::ostream& out, const FIXED& format)
470  {
471  using namespace std;
472 
473  return out << fixed << right << setw(format.width) << setprecision(format.precision);
474  }
475 
477 };
478 
479 
480 /**
481  * Auxiliary data structure for floating point format specification.
482  */
483 struct SCIENTIFIC :
484  public WIDTH
485 {
486  /**
487  * Constructor.
488  *
489  * \param width width
490  * \param precision precision
491  */
492  SCIENTIFIC(const int width,
493  const int precision) :
494  WIDTH(width)
495  {
496  this->precision = precision;
497  }
498 
499 
500  /**
501  * Format specifier.
502  *
503  * \param out output stream
504  * \param format format
505  * \return output stream
506  */
507  friend inline std::ostream& operator<<(std::ostream& out, const SCIENTIFIC& format)
508  {
509  using namespace std;
510 
511  return out << scientific << right << setw(format.width) << setprecision(format.precision);
512  }
513 
515 };
516 
517 
518 /**
519  * Data structure for format specifications.
520  */
521 struct JFormat_t {
522 
523  typedef std::ios_base::fmtflags fmtflags;
524 
525  /**
526  * Default constructor.
527  */
529  width (0),
530  precision(0),
531  flags (),
532  fill (' ')
533  {}
534 
535 
536  /**
537  * Constructor.
538  *
539  * \param width width
540  * \param precision precision
541  * \param flags flags
542  * \param fill fill character
543  */
544  JFormat_t(const int width,
545  const int precision = 0,
546  const fmtflags flags = fmtflags(),
547  const char fill = ' ') :
548  width (width),
549  precision(precision),
550  flags (flags),
551  fill (fill)
552  {}
553 
554 
555  /**
556  * Constructor.
557  *
558  * \param out output stream
559  */
560  JFormat_t(std::ostream& out)
561  {
562  get(out);
563  }
564 
565 
566  /**
567  * Check validity of this manipulator.
568  *
569  * \return true if valid; else false
570  */
571  inline bool is_valid() const
572  {
573  return (width > 0);
574  }
575 
576 
577  /**
578  * Get format specificaton from given output stream.
579  *
580  * \param out output stream
581  */
582  void get(std::ostream& out)
583  {
584  this->width = out.width();
585  this->precision = out.precision();
586  this->flags = out.flags();
587  this->fill = out.fill();
588  }
589 
590 
591  /**
592  * Put format specificaton to given output stream.
593  *
594  * \param out output stream
595  */
596  void put(std::ostream& out) const
597  {
598  out.width (this->width);
599  out.precision(this->precision);
600  out.flags (this->flags);
601  out.fill (this->fill);
602  }
603 
604 
605  /**
606  * Format specifier.
607  *
608  * \param out output stream
609  * \param format format
610  * \return output stream
611  */
612  friend inline std::ostream& operator<<(std::ostream& out, const JFormat_t& format)
613  {
614  format.put(out);
615 
616  return out;
617  }
618 
619  int width;
622  char fill;
623 };
624 
625 
626 /**
627  * Auxiliary class to temporarily define format specifications.
628  *
629  * The format specification of the output stream in use will be restored when this object is destroyed.
630  */
631 struct JFormat :
632  public JFormat_t
633 {
634  /**
635  * Constructor.
636  *
637  * \param out output stream
638  */
639  JFormat(std::ostream& out) :
640  JFormat_t(),
641  out (out),
642  format (out)
643  {}
644 
645 
646  /**
647  * Constructor.
648  *
649  * \param out output stream
650  * \param format format
651  */
652  JFormat(std::ostream& out, const JFormat_t& format) :
653  JFormat_t(format),
654  out (out),
655  format (out)
656  {}
657 
658 
659  /**
660  * Destructor.
661  */
663  {
664  format.put(out);
665  }
666 
667 private:
668  std::ostream& out;
670 };
671 
672 
673 /**
674  * Get format for given type.
675  *
676  * \return format
677  */
678 template<class T>
680 {
681  static JFormat_t manip;
682 
683  return manip;
684 }
685 
686 
687 /**
688  * Get format for given type.
689  *
690  * \param format default format
691  * \return actual format
692  */
693 template<class T>
694 inline JFormat_t getFormat(const JFormat_t& format)
695 {
696  const JFormat_t& buffer = getFormat<T>();
697 
698  if (buffer.is_valid())
699  return buffer;
700  else
701  return format;
702 }
703 
704 
705 /**
706  * Set format for given type.
707  *
708  * \param format format
709  */
710 template<class T>
711 inline void setFormat(const JFormat_t& format)
712 {
713  getFormat<T>() = format;
714 }
715 
716 #endif
JFormat_t()
Default constructor.
Definition: JManip.hh:528
RIGHT(const int width)
Constructor.
Definition: JManip.hh:303
Auxiliary data structure for alignment of data.
Definition: JManip.hh:230
friend std::ostream & operator<<(std::ostream &out, const JFormat_t &format)
Format specifier.
Definition: JManip.hh:612
void put(std::ostream &out) const
Put format specificaton to given output stream.
Definition: JManip.hh:596
data_type w[N+1][M+1]
Definition: JPolint.hh:741
friend std::ostream & operator<<(std::ostream &out, const WIDTH &format)
Format specifier.
Definition: JManip.hh:249
medium print
Definition: JManip.hh:38
JFormat(std::ostream &out)
Constructor.
Definition: JManip.hh:639
std::ostream & out
Definition: JManip.hh:668
bool getMediumprint(std::ostream &out)
Get medium print option.
Definition: JManip.hh:97
JFormat_t(std::ostream &out)
Constructor.
Definition: JManip.hh:560
int width
Definition: JManip.hh:619
int width
Definition: JManip.hh:256
friend std::ostream & operator<<(std::ostream &out, const SCIENTIFIC &format)
Format specifier.
Definition: JManip.hh:507
JFormat_t & getFormat()
Get format for given type.
Definition: JManip.hh:679
friend std::ostream & operator<<(std::ostream &out, const RIGHT &format)
Format specifier.
Definition: JManip.hh:315
int precision
Definition: JManip.hh:514
WIDTH(const int width)
Constructor.
Definition: JManip.hh:236
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:445
bool is_valid() const
Check validity of this manipulator.
Definition: JManip.hh:571
Auxiliary class to temporarily define format specifications.
Definition: JManip.hh:631
friend std::ostream & operator<<(std::ostream &out, const FIXED &format)
Format specifier.
Definition: JManip.hh:469
Auxiliary data structure for alignment of data.
Definition: JManip.hh:365
void setShortprint(std::ostream &out)
Set short print option.
Definition: JManip.hh:85
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition: JManip.hh:221
char fill
Definition: JManip.hh:358
Auxiliary class for format center.
Definition: JManip.hh:372
int precision
Definition: JManip.hh:476
char fill
Definition: JManip.hh:622
void setLongprint(std::ostream &out)
Set long print option.
Definition: JManip.hh:131
std::ostream & newline(std::ostream &out)
Print newline character.
Definition: JManip.hh:185
LEFT(const int width)
Constructor.
Definition: JManip.hh:271
long print
Definition: JManip.hh:39
do set_variable OUTPUT_DIRECTORY $WORKDIR T
bool getLongprint(std::ostream &out)
Get long print option.
Definition: JManip.hh:120
~JFormat()
Destructor.
Definition: JManip.hh:662
int precision
Definition: JManip.hh:620
int getIndex()
Get index for user I/O manipulation.
Definition: JManip.hh:25
std::ostream & shortprint(std::ostream &out)
Set short printing.
Definition: JManip.hh:143
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:327
std::ostream & longprint(std::ostream &out)
Set long printing.
Definition: JManip.hh:171
friend std::ostream & operator<<(std::ostream &out, const LEFT &format)
Format specifier.
Definition: JManip.hh:283
short print
Definition: JManip.hh:37
const JFormat_t format
Definition: JManip.hh:669
int getPrintOption(std::ostream &out)
Get print option.
Definition: JManip.hh:50
SCIENTIFIC(const int width, const int precision)
Constructor.
Definition: JManip.hh:492
JCenter(std::ostream &out, const WIDTH &format)
Constructor.
Definition: JManip.hh:381
void setPrintOption(std::ostream &out, const int option)
Set print option.
Definition: JManip.hh:62
JFormat_t(const int width, const int precision=0, const fmtflags flags=fmtflags(), const char fill= ' ')
Constructor.
Definition: JManip.hh:544
friend std::ostream & operator<<(std::ostream &out, const FILL &format)
Format specifier.
Definition: JManip.hh:351
std::ostream & whitespace(std::ostream &out)
Print white space character.
Definition: JManip.hh:197
FILL(const int width=0, const char fill= ' ')
Constructor.
Definition: JManip.hh:336
bool getShortprint(std::ostream &out)
Get short print option.
Definition: JManip.hh:74
std::ostream & out
Definition: JManip.hh:414
JFormat(std::ostream &out, const JFormat_t &format)
Constructor.
Definition: JManip.hh:652
CENTER(const int width)
Constructor.
Definition: JManip.hh:423
FIXED(const int width, const int precision)
Constructor.
Definition: JManip.hh:454
JPrintOption_t
Print options.
Definition: JManip.hh:36
std::ostream & tab(std::ostream &out)
Print tab character.
Definition: JManip.hh:209
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:483
std::ostream & operator<<(const T &value)
Write value to output stream.
Definition: JManip.hh:394
Data structure for format specifications.
Definition: JManip.hh:521
void setFormat(const JFormat_t &format)
Set format for given type.
Definition: JManip.hh:711
std::ios_base::fmtflags fmtflags
Definition: JManip.hh:523
std::ostream & mediumprint(std::ostream &out)
Set medium printing.
Definition: JManip.hh:157
void setMediumprint(std::ostream &out)
Set medium print option.
Definition: JManip.hh:108
friend JCenter operator<<(std::ostream &out, const CENTER &format)
Format specifier.
Definition: JManip.hh:435
fmtflags flags
Definition: JManip.hh:621