00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __IOFORMAT_H
00026 #define __IOFORMAT_H
00027
00028 #ifndef __COMMON_H
00029 #include "Common.h"
00030 #endif
00031
00032
00033 #include <sstream>
00034
00039 namespace Rulbus
00040 {
00041 template <typename T> class BoundIoFormat;
00042
00076 template <typename T>
00077 class IoFormat
00078 {
00079 public:
00084 typedef std::ios_base::fmtflags fmtflags;
00085
00088
00093 explicit IoFormat( int precision = 6 ):
00094 thePrecision(precision), theFlags(0), theWidth(0), theFillChar(' ')
00095 {
00096 ;
00097 }
00098
00102
00107 BoundIoFormat<T> operator()( const T& d ) const;
00108
00112
00117 int fill() const
00118 {
00119 return theFillChar;
00120 }
00121
00126 int flags() const
00127 {
00128 return theFlags;
00129 }
00130
00135 int width() const
00136 {
00137 return theWidth;
00138 }
00139
00144 int precision() const
00145 {
00146 return thePrecision;
00147 }
00148
00152
00157 IoFormat<T>& left()
00158 {
00159 setf( std::ios_base::left, std::ios_base::adjustfield );
00160 return *this;
00161 }
00162
00167 IoFormat<T>& right()
00168 {
00169 setf( std::ios_base::right, std::ios_base::adjustfield );
00170 return *this;
00171 }
00172
00177 IoFormat<T>& internal()
00178 {
00179 setf( std::ios_base::internal, std::ios_base::adjustfield );
00180 return *this;
00181 }
00182
00187 IoFormat<T>& boolalpha( bool alpha = true )
00188 {
00189 if ( alpha ) setf( std::ios_base::boolalpha );
00190 else unsetf( std::ios_base::boolalpha );
00191 return *this;
00192 }
00193
00198 IoFormat<T>& uppercase( bool upper = true )
00199 {
00200 if ( upper ) setf( std::ios_base::uppercase );
00201 else unsetf( std::ios_base::uppercase );
00202 return *this;
00203 }
00204
00209 IoFormat<T>& general()
00210 {
00211 setf( 0, std::ios_base::floatfield );
00212 return *this;
00213 }
00214
00219 IoFormat<T>& fixed()
00220 {
00221 setf( std::ios_base::fixed, std::ios_base::floatfield );
00222 return *this;
00223 }
00224
00229 IoFormat<T>& scientific()
00230 {
00231 setf( std::ios_base::scientific, std::ios_base::floatfield );
00232 return *this;
00233 }
00234
00239 IoFormat<T>& oct()
00240 {
00241 setf( std::ios_base::oct, std::ios_base::basefield );
00242 return *this;
00243 }
00244
00249 IoFormat<T>& dec()
00250 {
00251 setf( std::ios_base::dec, std::ios_base::basefield );
00252 return *this;
00253 }
00254
00259 IoFormat<T>& hex()
00260 {
00261 setf( std::ios_base::hex, std::ios_base::basefield );
00262 return *this;
00263 }
00264
00269 IoFormat<T>& showplus( bool show = true )
00270 {
00271 if ( show ) setf( std::ios_base::showpos );
00272 else unsetf( std::ios_base::showpos );
00273 return *this;
00274 }
00275
00280 IoFormat<T>& showbase( bool show = true )
00281 {
00282 if ( show ) setf( std::ios_base::showbase );
00283 else unsetf( std::ios_base::showbase );
00284 return *this;
00285 }
00286
00291 IoFormat<T>& showpoint( bool show = true )
00292 {
00293 if ( show ) setf( std::ios_base::showpoint );
00294 else unsetf( std::ios_base::showpoint );
00295 return *this;
00296 }
00297
00302 IoFormat<T>& fill( int c = ' ' )
00303 {
00304 theFillChar = c;
00305 return *this;
00306 }
00307
00312 IoFormat<T>& width( int w )
00313 {
00314 theWidth = w;
00315 return *this;
00316 }
00317
00322 IoFormat<T>& precision( int p )
00323 {
00324 thePrecision = p;
00325 return *this;
00326 }
00327
00329
00330 protected:
00335 void setf( fmtflags f )
00336 {
00337 theFlags = flags() | f;
00338 }
00339
00344 void setf( fmtflags f, fmtflags msk )
00345 {
00346 theFlags = ( flags() & ~msk ) | f & msk ;
00347 }
00348
00353 void unsetf( fmtflags msk = ~0 )
00354 {
00355 theFlags = flags() & ~msk;
00356 }
00357
00358 private:
00361
00362 int thePrecision;
00363 int theWidth;
00364 int theFillChar;
00365 fmtflags theFlags;
00366
00368 };
00369
00377 template <typename T>
00378 class BoundIoFormat
00379 {
00380 public:
00382
00383 BoundIoFormat( const IoFormat<T>& f, const T& v ): theFormat(f), theValue(v)
00384 {
00385 ;
00386 }
00387
00388 const IoFormat<T>& theFormat;
00389 T theValue;
00390 };
00391
00396 template <typename T>
00397 inline BoundIoFormat<T> IoFormat<T>::operator()( const T& v ) const
00398 {
00399 return BoundIoFormat<T>( *this, v );
00400 }
00401
00406 template <typename T> std::ostream& operator<< ( std::ostream& os, const Rulbus::BoundIoFormat<T>& bf )
00407 {
00408 std::ostringstream s;
00409
00410 s.fill ( bf.theFormat.fill() );
00411 s.width ( bf.theFormat.width() );
00412 s.precision( bf.theFormat.precision() );
00413 s.flags ( bf.theFormat.flags() );
00414
00415 s << bf.theValue;
00416
00417 return os << s.str();
00418 }
00419
00420 }
00421
00422 #endif // __IOFORMAT_H
00423
00424
00425
00426