Jpp  16.0.1
the software that should make you happy
 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 #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 WIDTH
448 {
449  /**
450  * Constructor.
451  *
452  * \param width width
453  * \param precision precision
454  */
455  FIXED(const int width,
456  const int precision) :
457  WIDTH(width)
458  {
459  this->precision = precision;
460  }
461 
462 
463  /**
464  * Format specifier.
465  *
466  * \param out output stream
467  * \param format format
468  * \return output stream
469  */
470  friend inline std::ostream& operator<<(std::ostream& out, const FIXED& format)
471  {
472  using namespace std;
473 
474  return out << fixed << right << setw(format.width) << setprecision(format.precision);
475  }
476 
478 };
479 
480 
481 /**
482  * Auxiliary data structure for floating point format specification.
483  */
484 struct SCIENTIFIC :
485  public WIDTH
486 {
487  /**
488  * Constructor.
489  *
490  * \param width width
491  * \param precision precision
492  */
493  SCIENTIFIC(const int width,
494  const int precision) :
495  WIDTH(width)
496  {
497  this->precision = precision;
498  }
499 
500 
501  /**
502  * Format specifier.
503  *
504  * \param out output stream
505  * \param format format
506  * \return output stream
507  */
508  friend inline std::ostream& operator<<(std::ostream& out, const SCIENTIFIC& format)
509  {
510  using namespace std;
511 
512  return out << scientific << right << setw(format.width) << setprecision(format.precision);
513  }
514 
516 };
517 
518 
519 /**
520  * Data structure for format specifications.
521  */
522 struct JFormat_t {
523 
524  typedef std::ios_base::fmtflags fmtflags;
525 
526  /**
527  * Default constructor.
528  */
530  width (0),
531  precision(0),
532  flags (),
533  fill (' ')
534  {}
535 
536 
537  /**
538  * Constructor.
539  *
540  * \param width width
541  * \param precision precision
542  * \param flags flags
543  * \param fill fill character
544  */
545  JFormat_t(const int width,
546  const int precision = 0,
547  const fmtflags flags = fmtflags(),
548  const char fill = ' ') :
549  width (width),
550  precision(precision),
551  flags (flags),
552  fill (fill)
553  {}
554 
555 
556  /**
557  * Constructor.
558  *
559  * \param out output stream
560  */
561  JFormat_t(std::ostream& out)
562  {
563  get(out);
564  }
565 
566 
567  /**
568  * Check validity of this manipulator.
569  *
570  * \return true if valid; else false
571  */
572  inline bool is_valid() const
573  {
574  return (width > 0);
575  }
576 
577 
578  /**
579  * Get format specificaton from given output stream.
580  *
581  * \param out output stream
582  */
583  void get(std::ostream& out)
584  {
585  this->width = out.width();
586  this->precision = out.precision();
587  this->flags = out.flags();
588  this->fill = out.fill();
589  }
590 
591 
592  /**
593  * Put format specificaton to given output stream.
594  *
595  * \param out output stream
596  */
597  void put(std::ostream& out) const
598  {
599  out.width (this->width);
600  out.precision(this->precision);
601  out.flags (this->flags);
602  out.fill (this->fill);
603  }
604 
605 
606  /**
607  * Format specifier.
608  *
609  * \param out output stream
610  * \param format format
611  * \return output stream
612  */
613  friend inline std::ostream& operator<<(std::ostream& out, const JFormat_t& format)
614  {
615  format.put(out);
616 
617  return out;
618  }
619 
620  int width;
623  char fill;
624 };
625 
626 
627 /**
628  * Auxiliary class to temporarily define format specifications.
629  *
630  * The format specification of the output stream in use will be restored when this object is destroyed.
631  */
632 struct JFormat :
633  public JFormat_t
634 {
635  /**
636  * Constructor.
637  *
638  * \param out output stream
639  */
640  JFormat(std::ostream& out) :
641  JFormat_t(),
642  out (out),
643  format (out)
644  {}
645 
646 
647  /**
648  * Constructor.
649  *
650  * \param out output stream
651  * \param format format
652  */
653  JFormat(std::ostream& out, const JFormat_t& format) :
654  JFormat_t(format),
655  out (out),
656  format (out)
657  {}
658 
659 
660  /**
661  * Destructor.
662  */
664  {
665  format.put(out);
666  }
667 
668 private:
669  std::ostream& out;
671 };
672 
673 
674 /**
675  * Get format for given type.
676  *
677  * \return format
678  */
679 template<class T>
681 {
682  static JFormat_t manip;
683 
684  return manip;
685 }
686 
687 
688 /**
689  * Get format for given type.
690  *
691  * \param format default format
692  * \return actual format
693  */
694 template<class T>
695 inline JFormat_t getFormat(const JFormat_t& format)
696 {
697  const JFormat_t& buffer = getFormat<T>();
698 
699  if (buffer.is_valid())
700  return buffer;
701  else
702  return format;
703 }
704 
705 
706 /**
707  * Set format for given type.
708  *
709  * \param format format
710  */
711 template<class T>
712 inline void setFormat(const JFormat_t& format)
713 {
714  getFormat<T>() = format;
715 }
716 
717 
718 /**
719  * Auxiliary data structure to convert (lambda) function to printable object.
720  *
721  * The (lambda) function should conform with the type definition LAMBDA::function_type.\n
722  * This data structure acts as a simple "wrapper" and should be used if the lambda has capture functionality.
723  */
724 struct LAMBDA {
725  /**
726  * Type definition of print function.
727  */
728  typedef std::function<void(std::ostream&)> function_type;
729 
730 
731  /**
732  * Constructor.
733  *
734  * \param f1 (lambda) function
735  */
736  LAMBDA(const function_type& f1) :
737  f1(f1)
738  {}
739 
740 
741  /**
742  * Write printable object to output stream.
743  *
744  * \param out output stream
745  * \param object printable object
746  * \return output stream
747  */
748  friend inline std::ostream& operator<<(std::ostream& out, const LAMBDA& object)
749  {
750  object.f1(out);
751 
752  return out;
753  }
754 
755 private:
757 };
758 
759 #endif
JFormat_t()
Default constructor.
Definition: JManip.hh:529
RIGHT(const int width)
Constructor.
Definition: JManip.hh:304
Auxiliary data structure for alignment of data.
Definition: JManip.hh:231
friend std::ostream & operator<<(std::ostream &out, const JFormat_t &format)
Format specifier.
Definition: JManip.hh:613
void put(std::ostream &out) const
Put format specificaton to given output stream.
Definition: JManip.hh:597
data_type w[N+1][M+1]
Definition: JPolint.hh:757
LAMBDA(const function_type &f1)
Constructor.
Definition: JManip.hh:736
const function_type & f1
Definition: JManip.hh:756
friend std::ostream & operator<<(std::ostream &out, const WIDTH &format)
Format specifier.
Definition: JManip.hh:250
medium print
Definition: JManip.hh:39
JFormat(std::ostream &out)
Constructor.
Definition: JManip.hh:640
std::ostream & out
Definition: JManip.hh:669
bool getMediumprint(std::ostream &out)
Get medium print option.
Definition: JManip.hh:98
JFormat_t(std::ostream &out)
Constructor.
Definition: JManip.hh:561
int width
Definition: JManip.hh:620
int width
Definition: JManip.hh:257
friend std::ostream & operator<<(std::ostream &out, const SCIENTIFIC &format)
Format specifier.
Definition: JManip.hh:508
JFormat_t & getFormat()
Get format for given type.
Definition: JManip.hh:680
friend std::ostream & operator<<(std::ostream &out, const RIGHT &format)
Format specifier.
Definition: JManip.hh:316
int precision
Definition: JManip.hh:515
WIDTH(const int width)
Constructor.
Definition: JManip.hh:237
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
bool is_valid() const
Check validity of this manipulator.
Definition: JManip.hh:572
Auxiliary class to temporarily define format specifications.
Definition: JManip.hh:632
friend std::ostream & operator<<(std::ostream &out, const FIXED &format)
Format specifier.
Definition: JManip.hh:470
Auxiliary data structure for alignment of data.
Definition: JManip.hh:366
void setShortprint(std::ostream &out)
Set short print option.
Definition: JManip.hh:86
std::ostream & rewind(std::ostream &out)
Rewind character.
Definition: JManip.hh:222
char fill
Definition: JManip.hh:359
Auxiliary class for format center.
Definition: JManip.hh:373
int precision
Definition: JManip.hh:477
Auxiliary data structure to convert (lambda) function to printable object.
Definition: JManip.hh:724
char fill
Definition: JManip.hh:623
void setLongprint(std::ostream &out)
Set long print option.
Definition: JManip.hh:132
std::ostream & newline(std::ostream &out)
Print newline character.
Definition: JManip.hh:186
LEFT(const int width)
Constructor.
Definition: JManip.hh:272
long print
Definition: JManip.hh:40
do set_variable OUTPUT_DIRECTORY $WORKDIR T
bool getLongprint(std::ostream &out)
Get long print option.
Definition: JManip.hh:121
~JFormat()
Destructor.
Definition: JManip.hh:663
int precision
Definition: JManip.hh:621
int getIndex()
Get index for user I/O manipulation.
Definition: JManip.hh:26
std::ostream & shortprint(std::ostream &out)
Set short printing.
Definition: JManip.hh:144
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
std::ostream & longprint(std::ostream &out)
Set long printing.
Definition: JManip.hh:172
friend std::ostream & operator<<(std::ostream &out, const LEFT &format)
Format specifier.
Definition: JManip.hh:284
short print
Definition: JManip.hh:38
const JFormat_t format
Definition: JManip.hh:670
int getPrintOption(std::ostream &out)
Get print option.
Definition: JManip.hh:51
SCIENTIFIC(const int width, const int precision)
Constructor.
Definition: JManip.hh:493
JCenter(std::ostream &out, const WIDTH &format)
Constructor.
Definition: JManip.hh:382
void setPrintOption(std::ostream &out, const int option)
Set print option.
Definition: JManip.hh:63
JFormat_t(const int width, const int precision=0, const fmtflags flags=fmtflags(), const char fill= ' ')
Constructor.
Definition: JManip.hh:545
friend std::ostream & operator<<(std::ostream &out, const FILL &format)
Format specifier.
Definition: JManip.hh:352
std::ostream & whitespace(std::ostream &out)
Print white space character.
Definition: JManip.hh:198
FILL(const int width=0, const char fill= ' ')
Constructor.
Definition: JManip.hh:337
bool getShortprint(std::ostream &out)
Get short print option.
Definition: JManip.hh:75
std::ostream & out
Definition: JManip.hh:415
JFormat(std::ostream &out, const JFormat_t &format)
Constructor.
Definition: JManip.hh:653
CENTER(const int width)
Constructor.
Definition: JManip.hh:424
$WORKDIR ev_configure_domsimulator txt echo process $DOM_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DOM_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
FIXED(const int width, const int precision)
Constructor.
Definition: JManip.hh:455
JPrintOption_t
Print options.
Definition: JManip.hh:37
std::ostream & tab(std::ostream &out)
Print tab character.
Definition: JManip.hh:210
std::function< void(std::ostream &)> function_type
Type definition of print function.
Definition: JManip.hh:728
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
std::ostream & operator<<(const T &value)
Write value to output stream.
Definition: JManip.hh:395
Data structure for format specifications.
Definition: JManip.hh:522
void setFormat(const JFormat_t &format)
Set format for given type.
Definition: JManip.hh:712
friend std::ostream & operator<<(std::ostream &out, const LAMBDA &object)
Write printable object to output stream.
Definition: JManip.hh:748
std::ios_base::fmtflags fmtflags
Definition: JManip.hh:524
std::ostream & mediumprint(std::ostream &out)
Set medium printing.
Definition: JManip.hh:158
void setMediumprint(std::ostream &out)
Set medium print option.
Definition: JManip.hh:109
friend JCenter operator<<(std::ostream &out, const CENTER &format)
Format specifier.
Definition: JManip.hh:436
fmtflags flags
Definition: JManip.hh:622