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