home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / cplus261.zip / gcc2 / gppincl / iomanip.h < prev    next >
C/C++ Source or Header  |  1994-11-03  |  5KB  |  152 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. #ifndef _IOMANIP_H
  26. //
  27. // Not specifying `pragma interface' causes the compiler to emit the 
  28. // template definitions in the files, where they are used.
  29. //
  30. //#ifdef __GNUG__
  31. //#pragma interface
  32. //#endif
  33. #define _IOMANIP_H
  34.  
  35. #include <iostream.h>
  36.  
  37. //-----------------------------------------------------------------------------
  38. //    Parametrized Manipulators as specified by ANSI draft
  39. //-----------------------------------------------------------------------------
  40.  
  41. //-----------------------------------------------------------------------------
  42. //    Stream Manipulators
  43. //-----------------------------------------------------------------------------
  44. //
  45. template<class TP> class smanip; // TP = Type Param
  46.  
  47. template<class TP> class sapp {
  48.     ios& (*_f)(ios&, TP);
  49. public: 
  50.     sapp(ios& (*f)(ios&, TP)) : _f(f) {}
  51.     //
  52.     smanip<TP> operator()(TP a) 
  53.       { return smanip<TP>(_f, a); }
  54. };
  55.  
  56. template <class TP> class smanip {
  57.     ios& (*_f)(ios&, TP);
  58.     TP _a;
  59. public:
  60.     smanip(ios& (*f)(ios&, TP), TP a) : _f(f), _a(a) {}
  61.     //
  62.     friend 
  63.       istream& operator>>(istream& i, const smanip<TP>& m);
  64.     friend
  65.       ostream& operator<<(ostream& o, const smanip<TP>& m);
  66. };
  67.  
  68. template<class TP>
  69. inline istream& operator>>(istream& i, const smanip<TP>& m)
  70.     { (*m._f)(i, m._a); return i; }
  71.  
  72. template<class TP>
  73. inline ostream& operator<<(ostream& o, const smanip<TP>& m)
  74.     { (*m._f)(o, m._a); return o;}
  75.  
  76. //-----------------------------------------------------------------------------
  77. //    Input-Stream Manipulators
  78. //-----------------------------------------------------------------------------
  79. //
  80. template<class TP> class imanip; 
  81.  
  82. template<class TP> class iapp {
  83.     istream& (*_f)(istream&, TP);
  84. public: 
  85.     iapp(ostream& (*f)(istream&,TP)) : _f(f) {}
  86.     //
  87.     imanip<TP> operator()(TP a)
  88.        { return imanip<TP>(_f, a); }
  89. };
  90.  
  91. template <class TP> class imanip {
  92.     istream& (*_f)(istream&, TP);
  93.     TP _a;
  94. public:
  95.     imanip(istream& (*f)(istream&, TP), TP a) : _f(f), _a(a) {}
  96.     //
  97.     friend 
  98.       istream& operator>>(istream& i, const imanip<TP>& m)
  99.     { return (*m._f)( i, m._a); }
  100. };
  101.  
  102.  
  103. //-----------------------------------------------------------------------------
  104. //    Output-Stream Manipulators
  105. //-----------------------------------------------------------------------------
  106. //
  107. template<class TP> class omanip; 
  108.  
  109. template<class TP> class oapp {
  110.     ostream& (*_f)(ostream&, TP);
  111. public: 
  112.     oapp(ostream& (*f)(ostream&,TP)) : _f(f) {}
  113.     //
  114.     omanip<TP> operator()(TP a)
  115.       { return omanip<TP>(_f, a); }
  116. };
  117.  
  118. template <class TP> class omanip {
  119.     ostream& (*_f)(ostream&, TP);
  120.     TP _a;
  121. public:
  122.     omanip(ostream& (*f)(ostream&, TP), TP a) : _f(f), _a(a) {}
  123.     //
  124.     friend
  125.       ostream& operator<<(ostream& o, omanip<TP>& m)
  126.     { return (*m._f)(o, m._a); }
  127. };
  128.  
  129.  
  130. //-----------------------------------------------------------------------------
  131. //    Available Manipulators
  132. //-----------------------------------------------------------------------------
  133.  
  134. //
  135. // Macro to define an iomanip function, with one argument
  136. // The underlying function is `__iomanip_<name>' 
  137. //
  138. #define __DEFINE_IOMANIP_FN1(type,param,function)         \
  139.     extern ios& __iomanip_##function (ios&, param); \
  140.     inline type<param> function (param n)           \
  141.                 { return type<param> (__iomanip_##function, n); }
  142.  
  143. __DEFINE_IOMANIP_FN1( smanip, int, setbase)
  144. __DEFINE_IOMANIP_FN1( smanip, int, setfill)
  145. __DEFINE_IOMANIP_FN1( smanip, int, setprecision)
  146. __DEFINE_IOMANIP_FN1( smanip, int, setw)
  147.  
  148. __DEFINE_IOMANIP_FN1( smanip, ios::fmtflags, resetiosflags)
  149. __DEFINE_IOMANIP_FN1( smanip, ios::fmtflags, setiosflags)
  150.  
  151. #endif /*!_IOMANIP_H*/
  152.