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