00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef GENLIB2_FORM_INCLUDED
00022 #define GENLIB2_FORM_INCLUDED
00023
00024 #include <string>
00025 #include <iostream>
00026 #include <sstream>
00027
00028 using namespace std;
00029
00030
00031 namespace GenLib2
00032 {
00033
00034 struct Bound_form_num;
00035 struct Bound_form_str;
00036
00042 class Form
00043 {
00044 friend ostream& operator<<(ostream&, const Bound_form_num&);
00045 friend ostream& operator<<(ostream&, const Bound_form_str&);
00046
00047 public:
00048 explicit Form(int p=6) : prc(p)
00049 {
00050 fmt = ios::fmtflags(0);
00051 wdt = 0;
00052 chr = ' ';
00053 }
00054
00055 Bound_form_num operator()(double d) const;
00056 Bound_form_num operator()(float f) const;
00057 Bound_form_num operator()(int i) const;
00058 Bound_form_num operator()(unsigned int i) const;
00059 Bound_form_num operator()(long unsigned int i) const;
00060
00061
00062 Bound_form_str operator()(const char* p) const;
00063 Bound_form_str operator()(const string& s) const;
00064
00066 Form& scientific() { fmt |= ios::scientific; return *this; }
00067
00069 Form& fixed() { fmt |= ios::fixed; return *this; }
00070
00071
00072 Form& general() { fmt |= ios::fmtflags(0); return *this; }
00073
00074 Form& uppercase() { fmt |= ios::uppercase; return *this; }
00075 Form& lowercase() { fmt &= ~ios::uppercase; return *this; }
00076
00078 Form& precision(int p) { prc = p; return *this; }
00079
00081 Form& width(int w) { wdt = w; return *this; }
00082
00084 Form& fill(char c) { chr = c; return *this; }
00085
00087 Form& showpoint() { fmt |= ios::showpoint; return *this; }
00088
00090 Form& noshowpoint() { fmt &= ~ios::showpoint; return *this; }
00091
00093 Form& showpos() { fmt |= ios::showpos; return *this; }
00094
00096 Form& noshowpos() { fmt &= ~ios::showpos; return *this; }
00097
00100 Form& left() { fmt = ios::left; return *this; }
00102 Form& right() { fmt = ios::right; return *this; }
00103
00105 Form& internal() { fmt = ios::internal; return *this; }
00106
00107 private:
00109 int prc;
00110
00112 int wdt;
00113
00115 ios::fmtflags fmt;
00116
00118 char chr;
00119 };
00120
00121 struct Bound_form_num
00122 {
00123 const Form& f;
00124 double val;
00125
00126 Bound_form_num(const Form& ff, double d) : f(ff), val(d) { }
00127 Bound_form_num(const Form& ff, float f) : f(ff), val(double(f)) { }
00128 Bound_form_num(const Form& ff, int i) : f(ff), val(double(i)) { }
00129 Bound_form_num(const Form& ff, unsigned int i) : f(ff), val(double(i)) { }
00130 Bound_form_num(const Form& ff, long unsigned int i) : f(ff), val(double(i)) { }
00131 };
00132
00133 struct Bound_form_str
00134 {
00135 const Form& f;
00136 const char* str;
00137
00138 Bound_form_str(const Form& ff, const char* p) : f(ff), str(p) { }
00139 Bound_form_str(const Form& ff, const string& s) : f(ff), str(s.c_str()) { }
00140 };
00141
00142
00143
00144
00145 inline Bound_form_num Form::operator()(double d) const
00146 {
00147 return Bound_form_num(*this, d);
00148 }
00149
00150
00151 inline Bound_form_num Form::operator()(float f) const
00152 {
00153 return Bound_form_num(*this, f);
00154 }
00155
00156
00157 inline Bound_form_num Form::operator()(int i) const
00158 {
00159 return Bound_form_num(*this, i);
00160 }
00161
00162
00163 inline Bound_form_num Form::operator()(unsigned int i) const
00164 {
00165 return Bound_form_num(*this, i);
00166 }
00167
00168
00169 inline Bound_form_num Form::operator()(long unsigned int i) const
00170 {
00171 return Bound_form_num(*this, i);
00172 }
00173
00174
00175 inline Bound_form_str Form::operator()(const char* p) const
00176 {
00177 return Bound_form_str(*this, p);
00178 }
00179
00180
00181 inline Bound_form_str Form::operator()(const string& s) const
00182 {
00183 return Bound_form_str(*this, s);
00184 }
00185
00186
00187 inline ostream& operator<<(ostream& os, const Bound_form_num& bf)
00188 {
00189 ostringstream s;
00190
00191 s.flags(bf.f.fmt);
00192 s.precision(bf.f.prc);
00193 s.width(bf.f.wdt);
00194 s << bf.val;
00195
00196 return os << s.str();
00197 }
00198
00199
00200 inline ostream& operator<<(ostream& os, const Bound_form_str& bf)
00201 {
00202 ostringstream s;
00203
00204 s.flags(bf.f.fmt);
00205 s.width(bf.f.wdt);
00206 s << bf.str;
00207
00208 return os << s.str();
00209 }
00210
00211
00212 }
00213
00214 #endif