LOOS  v2.3.2
Fmt.hpp
1 /*
2  Fmt.hpp
3  Tod D. Romo
4 
5  Formatter based heavily on example from Stroustrup's C++ book...
6 */
7 
8 
9 
10 
11 #if !defined(LOOS_FMT_HPP)
12 #define LOOS_FMT_HPP
13 
14 #include <ios>
15 #include <sstream>
16 #include <iostream>
17 
18 #include <loos_defs.hpp>
19 
20 namespace loos {
21 
22  struct BoundFmt;
23 
25  class Fmt {
26  friend std::ostream& operator<<(std::ostream&, const BoundFmt&);
27 
28  public:
30  enum FmtAlignment { LEFT, RIGHT, INTERNAL };
31 
34  explicit Fmt(uint p = 6) : prc(p), wdth(0), fil(' '), trl(false), pos(false), ali(LEFT) {
35  fmt = ~(std::ios_base::fixed|std::ios_base::scientific);
36  }
37 
39  BoundFmt operator()(double d) const;
40 
42  Fmt& scientific(void);
44  Fmt& fixed(void);
46  Fmt& general(void);
47 
49  Fmt& precision(const uint);
51  Fmt& width(const uint);
53  Fmt& fill(const char);
54 
56  Fmt& trailingZeros(bool b = true);
58  Fmt& plus(bool b = true);
59 
61  Fmt& left(void);
63  Fmt& right(void);
65  Fmt& internal(void);
66 
67  private:
68 
69  private:
70  std::ios_base::fmtflags fmt;
71  uint prc, wdth;
72  char fil;
73  bool trl, pos;
74  FmtAlignment ali;
75 
76  };
77 
78 
80  struct BoundFmt {
81  BoundFmt(const Fmt& ff, double v) : f(ff), val(v) { }
82  const Fmt& f;
83  double val;
84  };
85 
86  std::ostream& operator<<(std::ostream&, const BoundFmt&);
87 
88 
89 }
90 
91 #endif
Fmt & fixed(void)
Output in fixed-point.
Definition: Fmt.cpp:14
Fmt & plus(bool b=true)
Prepend plus sign?
Definition: Fmt.cpp:22
Fmt & fill(const char)
Set the fill character.
Definition: Fmt.cpp:19
Fmt(uint p=6)
Definition: Fmt.hpp:34
Internal helper class to bind formatting state.
Definition: Fmt.hpp:80
friend std::ostream & operator<<(std::ostream &, const BoundFmt &)
Create the output with the specified formatter.
Definition: Fmt.cpp:33
Fmt & trailingZeros(bool b=true)
Determines whether or not trailing zeros are shown.
Definition: Fmt.cpp:21
Fmt & precision(const uint)
Set the precision.
Definition: Fmt.cpp:17
BoundFmt operator()(double d) const
Returns the bound formatter.
Definition: Fmt.cpp:30
FmtAlignment
Alignment of the text.
Definition: Fmt.hpp:30
Fmt & width(const uint)
Set the output field width.
Definition: Fmt.cpp:18
Fmt & scientific(void)
Output in scientific format.
Definition: Fmt.cpp:13
Output formatter class, adapted from Stroustrup's book.
Definition: Fmt.hpp:25
Namespace for most things not already encapsulated within a class.
Fmt & right(void)
Align right.
Definition: Fmt.cpp:25
Fmt & left(void)
Align left.
Definition: Fmt.cpp:24
Fmt & general(void)
Output normally (i.e. general)
Definition: Fmt.cpp:15