home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / cxxinclude / iomanip.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  16.6 KB  |  420 lines

  1. /* Copyright (c) 1993             by SAS Institute Inc., Cary NC     */
  2.  
  3. #ifndef __IOMANIP_H
  4. #define __IOMANIP_H 
  5.  
  6. /*
  7.  
  8.  
  9. A manipulator is a value which can be used to effect some change
  10. to a stream by inserting them into or extracting them from the stream.
  11. For example the 'flush' funtion is a manipulator of ostreams:
  12.  
  13.     cout << flush;     // will cause cout to be flushed.
  14.  
  15. In fact any function of one the following types is a manipulator:
  16.  
  17.    ostream& (ostream&)     - is a manipulator for ostreams
  18.    istream& (istream&)     - is a manipulator for istreams
  19.    ios&     (ios&)         - is a manipulator of ios's for
  20.                                      istreams or ostreams
  21.  
  22.  
  23. Manipulators can also be created with arguments.  This
  24. file contains some single argument manipulator creation functions
  25. as well as support classes and functions for creating other
  26. single argument manipulator creators.
  27.  
  28. SMANIP(T) - (where T is a typedef name)
  29.    is the name of a class whos instances are single argument
  30.    manipulators of ios's.  The type of the argument is T.
  31.  
  32. Similarly, the following are class names for other kinds of
  33. single argument manipulators.
  34.  
  35.     IMANIP(T)   for istreams
  36.     OMANIP(T)   for ostreams
  37.     IOMANIP(T)  for iostreams
  38.  
  39.  
  40. ---------------------------------------------------------
  41. This header file contains the following:
  42.  
  43.  
  44. First this file declares some single argument manipulator
  45. creators:
  46.  
  47. ---------------------------------------------------------
  48. SMANIP(int) setw( int w )
  49.  
  50.     setw returns a manipulator (a SMANIP(int)) that will set
  51.     the 'ios::width()' value of the ios it is sent to.
  52.  
  53.     Example usage:
  54.         cout << setw( 10 )
  55.  
  56.     is similar to:
  57.          cout.width( 10 )
  58.  
  59.     except that the first version returns an (ios&).
  60.  
  61. ---------------------------------------------------------
  62. SMANIP(int) setfill( int f )
  63.  
  64.     setfill returns a minipulator that will set the 'ios::fill()'
  65.     value of the ios it is sent to.
  66.  
  67.     Example usage:
  68.         cout << setfill( '\t' )
  69.  
  70.  
  71. ---------------------------------------------------------
  72. SMANIP(int) setprecision( int p )
  73.  
  74.     returns a manipulator that will set the ios::precision
  75.     value of the ios it is sent to.
  76.  
  77.     Example usage:
  78.         cout << setprecision( 10 )
  79.  
  80. ---------------------------------------------------------
  81. SMANIP(long) setiosflags( long flags )
  82.  
  83.     returns a manipulator that will set the ios::flags
  84.     value of the ios it is sent to.
  85.  
  86.     Example usage:
  87.         cout << setiosflags( ios::skipws )
  88.  
  89. ---------------------------------------------------------
  90. SMANIP(long) resetiosflags( long flags )
  91.  
  92.     returns a manipulator that will reset the 'flag' bits
  93.     of ios::flags value of the ios it is sent to.
  94.  
  95.     Example usage:
  96.         cout << resetiosflags( ios::skipws )
  97.  
  98. ---------------------------------------------------------
  99.  
  100. This header file also declares the following macro:
  101.  
  102. IOMANIPdeclare( T )
  103.  
  104.    which when envoked with a typedef name for 'T' will
  105.    declare the following classes:
  106.  
  107.        SMANIP(T), SAPP(T), IMANIP(T), IAPP(T), OMANIP(T),
  108.        OAPP(T), IOMANIP(T), IOAPP(T)
  109.  
  110.    the usage of these classes is discribed below.
  111.  
  112. ---------------------------------------------------------
  113.  
  114. This header file also declares:
  115.  
  116. IOMANIPdeclare(int);
  117. IOMANIPdeclare(long);
  118.  
  119.     which will expand as expected.
  120.  
  121. ---------------------------------------------------------
  122.  
  123. This section discribes the classes created by IOMANIPdeclare(t).
  124.  
  125. class SMANIP(T) { T must always be a typedef name.
  126.     public:
  127.         SMANIP(T)( ios& (*f)(ios&, T ), T d );
  128.  
  129.             Construct an SMANIP(T).
  130.  
  131.             Returns a single argument manipulator by collecting
  132.             the function 'f' and argument 'd' into a single
  133.             manipulator value.
  134.  
  135.             It is assumed that 'f' will be a function that
  136.             changes ios' in some way using the value of 'd'.
  137.  
  138.         friend istream& operator>> ( istream& i, const SMANIP(T)& m );
  139.         friend ostream& operator<< ( ostream& o, const SMANIP(T)& m );
  140.  
  141.             These are the functions that allow SMANIP(T)'s to be
  142.             'inserted-into' istreams and 'extracted-from' ostreams
  143.             respectivly.
  144.  
  145.             They each take the values of 'f' and 'd' from 'm'.
  146.             They then call
  147.  
  148.                   f( ios, d)
  149.  
  150.             where 'ios' is the ios part of 'i' or 'o' respectivly.
  151.  
  152.             It is assumed that 'f' will be a function that
  153.             changes ios' in some way using the value of 'd'.
  154.      };
  155.  
  156.  
  157.  
  158. SAPP(T)'s make it easier to use SMANIP(T)'s.
  159.  
  160. Example usage:
  161.  
  162.     ios& setwidth(ios& i, int w ) { i.width( w ); return i; }
  163.     SAPP(int) setwidth( setwidth );
  164.  
  165.     This will create a manipulator 'setwidth' which works
  166.     like the library's 'setw'.
  167.  
  168. class SAPP(T) {
  169.     public:
  170.        SAPP(T) ( ios& (*f)( ios&, T ) );
  171.            Initializes a SAPP(T) to contain 'f'.
  172.  
  173.        SMANIP(T) operator() ( T d );
  174.            Creates and returns an SMANIP(T) using the 'f'
  175.            from the SAPP(T) and the 'd' argument.
  176.      };
  177.  
  178. The rest of the classes are the same except that the types of the
  179. user manipulating functions ('f') are different:
  180.  
  181.    IMANIP(T) and IAPP(T)  'f' is istream& (*f)( istream&, T)
  182.    OMANIP(T) and OAPP(T)  'f' is ostream& (*f)( ostream&, T)
  183.    IOMANIP(T) and IOAPP(T)  'f' is iostream& (*f)( iostream&, T)
  184.  
  185. IN addition IMANIP(T) does not have 'operator<<', and OMANIP(T) does
  186. not have an 'operator>>'.
  187.  
  188. */
  189. #ifndef __IOSTREAM_H
  190. #include <iostream.h>
  191. #endif
  192.  
  193. #define SMANIP(T) __smanip_ ## T
  194. #define SAPP(T)   __sapp_ ## T
  195. #define IMANIP(T) __imanip_ ## T
  196. #define IAPP(T)   __iapp_ ## T
  197. #define OMANIP(T) __omanip_ ## T
  198. #define OAPP(T)   __oapp_ ## T
  199. #define IOMANIP(T) __iomanip_ ## T
  200. #define IOAPP(T)   __ioapp_ ## T
  201.  
  202. #define IOMANIPdeclare(T)                                             \
  203.                                                                       \
  204.                                                                       \
  205. class SMANIP(T) {                                                     \
  206.     public:                                                           \
  207.       SMANIP(T) ( ios& (* _F)(ios&,T), T _D )                         \
  208.           : _FUNC(_F), _DATA(_D) {}                                   \
  209.                                                                       \
  210.       friend istream& operator>> ( istream& _I, const SMANIP(T)& _M ) \
  211.           {                                                           \
  212.           (*_M._FUNC)( _I, _M._DATA );                                \
  213.           return _I;                                                  \
  214.           }                                                           \
  215.                                                                       \
  216.       friend ostream& operator<< (ostream& _O, const SMANIP(T)& _M )  \
  217.           {                                                           \
  218.           (*_M._FUNC)( _O, _M._DATA );                                \
  219.           return _O;                                                  \
  220.           }                                                           \
  221.                                                                       \
  222.     private:                                                          \
  223.       T _DATA;                                                        \
  224.       ios& (*_FUNC)(ios&,T);                                          \
  225.     };                                                                \
  226.                                                                       \
  227. class SAPP(T) {                                                       \
  228.     public:                                                           \
  229.       SAPP(T) ( ios& (* _F)( ios&, T ) )                              \
  230.           : _FUNC(_F) {}                                              \
  231.                                                                       \
  232.       SMANIP(T) operator() ( T _D )                                   \
  233.           {                                                           \
  234.           return SMANIP(T)(_FUNC,_D);                                 \
  235.           }                                                           \
  236.                                                                       \
  237.     private:                                                          \
  238.       ios& (*_FUNC)(ios&, T );                                        \
  239.     };                                                                \
  240.                                                                       \
  241.                                                                       \
  242. class IMANIP(T) {                                                     \
  243.     public:                                                           \
  244.       IMANIP(T) ( istream& (* _F)(istream&,T), T _D )                 \
  245.           : _FUNC(_F), _DATA(_D) {}                                   \
  246.                                                                       \
  247.       friend istream& operator>> ( istream& _I, const IMANIP(T)& _M ) \
  248.           {                                                           \
  249.           (*_M._FUNC)( _I, _M._DATA );                                \
  250.           return _I;                                                  \
  251.           }                                                           \
  252.                                                                       \
  253.     private:                                                          \
  254.       T _DATA;                                                        \
  255.       istream& (*_FUNC)(istream&,T);                                  \
  256.     };                                                                \
  257.                                                                       \
  258. class IAPP(T) {                                                       \
  259.     public:                                                           \
  260.       IAPP(T) ( istream& (* _F)( istream&, T ) )                      \
  261.           : _FUNC(_F) {}                                              \
  262.                                                                       \
  263.       IMANIP(T) operator() ( T _D )                                   \
  264.           {                                                           \
  265.           return IMANIP(T)(_FUNC,_D);                                 \
  266.           }                                                           \
  267.                                                                       \
  268.     private:                                                          \
  269.       istream& (*_FUNC)(istream&, T );                                \
  270.     };                                                                \
  271.                                                                       \
  272.                                                                       \
  273. class OMANIP(T) {                                                     \
  274.     public:                                                           \
  275.       OMANIP(T) ( ostream& (* _F)(ostream&,T), T _D )                 \
  276.           : _FUNC(_F), _DATA(_D) {}                                   \
  277.                                                                       \
  278.       friend ostream& operator<< ( ostream& _I, const OMANIP(T)& _M ) \
  279.           {                                                           \
  280.           (*_M._FUNC)( _I, _M._DATA );                                \
  281.           return _I;                                                  \
  282.           }                                                           \
  283.                                                                       \
  284.     private:                                                          \
  285.       T _DATA;                                                        \
  286.       ostream& (*_FUNC)(ostream&,T);                                  \
  287.     };                                                                \
  288.                                                                       \
  289. class OAPP(T) {                                                       \
  290.     public:                                                           \
  291.       OAPP(T) ( ostream& (* _F)( ostream&, T ) )                      \
  292.           : _FUNC(_F) {}                                              \
  293.                                                                       \
  294.       OMANIP(T) operator() ( T _D )                                   \
  295.           {                                                           \
  296.           return OMANIP(T)(_FUNC,_D);                                 \
  297.           }                                                           \
  298.                                                                       \
  299.     private:                                                          \
  300.       ostream& (*_FUNC)(ostream&, T );                                \
  301.     };                                                                \
  302.                                                                       \
  303.                                                                       \
  304. class IOMANIP(T) {                                                    \
  305.     public:                                                           \
  306.       IOMANIP(T) ( iostream& (* _F)(iostream&,T), T _D )              \
  307.           : _FUNC(_F), _DATA(_D) {}                                   \
  308.                                                                       \
  309.    friend iostream& operator>> ( iostream& _I, const IOMANIP(T)& _M ) \
  310.           {                                                           \
  311.           (*_M._FUNC)( _I, _M._DATA );                                \
  312.           return _I;                                                  \
  313.           }                                                           \
  314.                                                                       \
  315.    friend iostream& operator<< (iostream& _O, const IOMANIP(T)& _M )  \
  316.           {                                                           \
  317.           (*_M._FUNC)( _O, _M._DATA );                                \
  318.           return _O;                                                  \
  319.           }                                                           \
  320.                                                                       \
  321.     private:                                                          \
  322.       T _DATA;                                                        \
  323.       iostream& (*_FUNC)(iostream&,T);                                \
  324.     };                                                                \
  325.                                                                       \
  326. class IOAPP(T) {                                                      \
  327.     public:                                                           \
  328.       IOAPP(T) ( iostream& (* _F)( iostream&, T ) )                   \
  329.           : _FUNC(_F) {}                                              \
  330.                                                                       \
  331.       IOMANIP(T) operator() ( T _D )                                  \
  332.           {                                                           \
  333.           return IOMANIP(T)(_FUNC,_D);                                \
  334.           }                                                           \
  335.                                                                       \
  336.     private:                                                          \
  337.       iostream& (*_FUNC)(iostream&, T );                              \
  338.     };                                                                \
  339. // end of IOMANIPdeclare(T) -- leave this comment
  340.  
  341.  
  342. IOMANIPdeclare(long);
  343. IOMANIPdeclare(int);
  344.  
  345.  
  346.  
  347. inline ios& setbase( ios& _IOS, int _ARG )
  348.     {
  349.     if ( _ARG == 16 )
  350.         return hex( _IOS );
  351.     else if ( _ARG == 8 )
  352.         return oct( _IOS );
  353.     else
  354.         return dec( _IOS );
  355.     }
  356.  
  357. inline SMANIP(int) setbase( int _ARG )
  358.     {
  359.     return SMANIP(int)( setbase, _ARG );
  360.     }
  361.  
  362.  
  363. inline ios& setw( ios& _IOS, int _ARG )
  364.     {
  365.     _IOS.width( _ARG );
  366.     return _IOS;
  367.     }
  368.  
  369. inline SMANIP(int) setw( int _ARG )
  370.     {
  371.     return SMANIP(int)( setw, _ARG );
  372.     }
  373.  
  374. inline ios& setfill( ios& _IOS, int _ARG )
  375.     {
  376.     _IOS.fill(_ARG);
  377.     return _IOS;
  378.     }
  379.  
  380. inline SMANIP(int) setfill( int _ARG )
  381.     {
  382.     return SMANIP(int)(setfill,_ARG);
  383.     }
  384.  
  385. inline ios& setprecision( ios& _IOS, int _ARG )
  386.     {
  387.     _IOS.precision( _ARG );
  388.     return _IOS;
  389.     }
  390.  
  391. inline SMANIP(int) setprecision( int _ARG )
  392.     {
  393.     return SMANIP(int)( setprecision, _ARG );
  394.     }
  395.  
  396. inline ios& setiosflags( ios& _IOS, long _ARG )
  397.     {
  398.     _IOS.setf( _ARG );
  399.     return _IOS;
  400.     }
  401.  
  402. inline SMANIP(long) setiosflags( long _ARG )
  403.     {
  404.     return SMANIP(long)( setiosflags, _ARG);
  405.     }
  406.  
  407. inline ios& resetiosflags( ios& _IOS, long _ARG )
  408.     {
  409.     _IOS.setf(0,_ARG);
  410.     return _IOS;
  411.     }
  412.  
  413. inline SMANIP(long) resetiosflags( long _ARG )
  414.     {
  415.     return SMANIP(long)( resetiosflags, _ARG);
  416.     }
  417.  
  418. #endif /* __IOMANIP_H */
  419.  
  420.