LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Form.h
Go to the documentation of this file.
1 #ifndef MeasAlgoShapeletForm_H
2 #define MeasAlgoShapeletForm_H
3 
4 // Formatting for simplified stream output
5 // see Stroustrup(2000), p. 635
6 
7 #include <complex>
8 #include <sstream>
9 #include <iostream>
10 #include <string>
11 
12 namespace lsst {
13 namespace meas {
14 namespace algorithms {
15 namespace shapelet {
16 
17  template <typename T> class BoundForm;
18 
19  class Form
20  {
21  public:
22  Form() :
23  _prc(6), _wdt(0), _fmt(), _base(std::ios_base::dec),
24  _just(std::ios_base::left), _newFillCh(0),
25  _doUpper(0), _doPlus(0), _doTrail(0), _doBoolAlpha(0),
26  _nTrail(1), _trailCh(' ')
27  {}
28 
29  template <typename T> BoundForm<T> operator()(T val) const;
30 
31  Form& prec(int p) { _prc = p; return *this; }
32 
33  Form& sci() { _fmt = std::ios_base::scientific; return *this; }
34  Form& fix() { _fmt = std::ios_base::fixed; return *this; }
35  Form& gen() { _fmt = ~std::ios_base::floatfield; return *this; }
36 
37  Form& width(int w) { _wdt = w; return *this; }
38  Form& fill(char c) { _newFillCh = c; return *this; }
39 
40  Form& dec() { _base = std::ios_base::dec; return *this; }
41  Form& oct() { _base = std::ios_base::oct; return *this; }
42  Form& hex() { _base = std::ios_base::hex; return *this; }
43 
44  Form& left() { _just = std::ios_base::left; return *this; }
45  Form& right() { _just = std::ios_base::right; return *this; }
46  Form& internal() { _just = std::ios_base::internal; return *this; }
47 
48  Form& uppercase(bool b=true) { _doUpper = b?1:-1; return *this; }
49  Form& showpos(bool b=true) { _doPlus = b?1:-1; return *this; }
50  Form& showpoint(bool b=true) { _doTrail = b?1:-1; return *this; }
51  Form& boolalpha(bool b=true) { _doBoolAlpha = b?1:-1; return *this; }
52 
53  Form& trail(int n, char ch=' ')
54  { _nTrail = n; _trailCh = ch; return *this; }
55 
56  private:
57  template <typename T>
58  friend std::ostream& operator<<(std::ostream&, const BoundForm<T>&);
59 
60  friend void setupFloat(std::ostream&, const Form&);
61  friend void setupInt(std::ostream&, const Form&);
62 
63  int _prc; // precision
64  int _wdt; // width, 0 means as wide as necessary
65  std::ios_base::fmtflags _fmt; // general sci, or fixed
66  std::ios_base::fmtflags _base; // dec, hex, oct
67  std::ios_base::fmtflags _just; // left, right, internal fill
68  char _newFillCh; // fill character
69  int _doUpper; // +1 to have uppercase E,X, -1 turn off, (0 leave as is)
70  int _doPlus; // +1 to have explicit plus for positive values, -1 off, 0 same
71  int _doTrail; // +1 to write trailing zeros, -1 off, 0 same
72  int _doBoolAlpha; // +1 to write "true","false", -1 off, 0 same
73  int _nTrail; // number of spaces after output
74  char _trailCh; // character of trailing "spaces"
75 
76  };
77 
78  template <typename T>
79  class BoundForm
80  {
81  public:
82  const Form& f;
83  T val;
84  BoundForm(const Form& f_, T val_) : f(f_), val(val_) {}
85  };
86 
87  template <typename T>
89  { return BoundForm<T>(*this,val); }
90 
91  inline void setupFloat(std::ostream& s, const Form& f)
92  {
93  s.precision(f._prc);
94  s.setf(f._fmt,std::ios_base::floatfield);
95  s.setf(f._just,std::ios_base::adjustfield);
96  if (f._wdt) s.width(f._wdt);
97  if (f._newFillCh) s.fill(f._newFillCh);
98  if (f._doUpper && f._fmt == std::ios_base::scientific) {
99  if (f._doUpper>0) s.setf(std::ios_base::uppercase);
100  else s.unsetf(std::ios_base::uppercase);
101  }
102  if (f._doPlus) {
103  if (f._doPlus>0) s.setf(std::ios_base::showpos);
104  else s.unsetf(std::ios_base::showpos);
105  }
106  if (f._doTrail) {
107  if (f._doTrail>0) s.setf(std::ios_base::showpoint);
108  else s.unsetf(std::ios_base::showpoint);
109  }
110  }
111 
112  inline void setupInt(std::ostream& s, const Form& f)
113  {
114  s.setf(f._just,std::ios_base::adjustfield);
115  s.setf(f._base,std::ios_base::basefield);
116  if (f._wdt) s.width(f._wdt);
117  if (f._newFillCh) s.fill(f._newFillCh);
118  if (f._doUpper && f._base == std::ios_base::hex) {
119  if (f._doUpper>0) s.setf(std::ios_base::uppercase);
120  else s.unsetf(std::ios_base::uppercase);
121  }
122  if (f._doPlus) {
123  if (f._doPlus>0) s.setf(std::ios_base::showpos);
124  else s.unsetf(std::ios_base::showpos);
125  }
126  if (f._base != std::ios_base::dec) s.setf(std::ios_base::showbase);
127  }
128 
129  inline void setup(std::ostream& os, const BoundForm<double>& bf)
130  { setupFloat(os,bf.f); }
131 
132  inline void setup(std::ostream& os, const BoundForm<long double>& bf)
133  { setupFloat(os,bf.f); }
134 
135  inline void setup(std::ostream& os, const BoundForm<float>& bf)
136  { setupFloat(os,bf.f); }
137 
138  inline void setup(std::ostream& os, const BoundForm<std::complex<double> >& bf)
139  { setupFloat(os,bf.f); }
140 
141  inline void setup(
142  std::ostream& os, const BoundForm<std::complex<long double> >& bf)
143  { setupFloat(os,bf.f); }
144 
145  inline void setup(std::ostream& os, const BoundForm<std::complex<float> >& bf)
146  { setupFloat(os,bf.f); }
147 
148  inline void setup(std::ostream& os, const BoundForm<int>& bf)
149  { setupInt(os,bf.f); }
150 
151  inline void setup(std::ostream& os, const BoundForm<short>& bf)
152  { setupInt(os,bf.f); }
153 
154  inline void setup(std::ostream& os, const BoundForm<long>& bf)
155  { setupInt(os,bf.f); }
156 
157  inline void setup(std::ostream& os, const BoundForm<unsigned int>& bf)
158  { setupInt(os,bf.f); }
159 
160  inline void setup(std::ostream& os, const BoundForm<unsigned short>& bf)
161  { setupInt(os,bf.f); }
162 
163  inline void setup(std::ostream& os, const BoundForm<unsigned long>& bf)
164  { setupInt(os,bf.f); }
165 
166  template <typename T>
167  inline void setup(std::ostream& os, const BoundForm<T>& bf)
168  { setupFloat(os,bf.f); }
169 
170  template <typename T>
171  inline std::ostream& operator<<(std::ostream& os, const BoundForm<T>& bf)
172  {
173  std::ostringstream s;
174  setup(s,bf);
175  s << bf.val;
176  if (bf.f._nTrail>0) s << std::string(bf.f._nTrail,bf.f._trailCh);
177  os << s.str();
178  return os;
179  }
180 
181 }}}}
182 
183 #endif
void setupFloat(std::ostream &s, const Form &f)
Definition: Form.h:91
friend void setupInt(std::ostream &, const Form &)
Definition: Form.h:112
Form & showpoint(bool b=true)
Definition: Form.h:50
BoundForm(const Form &f_, T val_)
Definition: Form.h:84
bool val
std::ios_base::fmtflags _fmt
Definition: Form.h:65
void setup(std::ostream &os, const BoundForm< double > &bf)
Definition: Form.h:129
Form & showpos(bool b=true)
Definition: Form.h:49
double w
Definition: CoaddPsf.cc:57
BoundForm< T > operator()(T val) const
Definition: Form.h:88
std::ios_base::fmtflags _just
Definition: Form.h:67
Form & boolalpha(bool b=true)
Definition: Form.h:51
Form & trail(int n, char ch=' ')
Definition: Form.h:53
Form & uppercase(bool b=true)
Definition: Form.h:48
afw::table::Key< double > b
std::ios_base::fmtflags _base
Definition: Form.h:66
friend void setupFloat(std::ostream &, const Form &)
Definition: Form.h:91
void setupInt(std::ostream &s, const Form &f)
Definition: Form.h:112