home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _ostream.h < prev    next >
C/C++ Source or Header  |  2001-05-03  |  11KB  |  357 lines

  1. /*
  2.  * Copyright (c) 1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */ 
  18.  
  19.  
  20. #ifndef _STLP_INTERNAL_OSTREAM_H
  21. #define _STLP_INTERNAL_OSTREAM_H
  22.  
  23. #ifndef _STLP_INTERNAL_IOS_H
  24. # include <stl/_ios.h>                  // For basic_ios<>.  Includes <iosfwd>.
  25. #endif
  26.  
  27. #ifndef _STLP_INTERNAL_OSTREAMBUF_ITERATOR_H
  28. # include <stl/_ostreambuf_iterator.h>
  29. #endif
  30.  
  31. _STLP_BEGIN_NAMESPACE
  32.  
  33. template <class _CharT, class _Traits, class _Number> 
  34. basic_ostream<_CharT, _Traits>& _STLP_CALL
  35. _M_put_num(basic_ostream<_CharT, _Traits>& __os, _Number __x);
  36.  
  37. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  38. template <class _CharT, class _Traits>
  39. class _Osentry;
  40. # endif
  41.  
  42. template <class _CharT, class _Traits>
  43. bool
  44. _M_init(basic_ostream<_CharT, _Traits>& __str);
  45.  
  46. //----------------------------------------------------------------------
  47. // class basic_ostream<>
  48.  
  49. template <class _CharT, class _Traits>
  50. class basic_ostream : virtual public basic_ios<_CharT, _Traits>
  51. {
  52.   typedef basic_ostream<_CharT, _Traits> _Self;
  53.   
  54. public:                         // Types
  55.   typedef _CharT                     char_type;
  56.   typedef typename _Traits::int_type int_type;
  57.   typedef typename _Traits::pos_type pos_type;
  58.   typedef typename _Traits::off_type off_type;
  59.   typedef _Traits                    traits_type;
  60.   typedef basic_ios<_CharT, _Traits> _Basic_ios;
  61.  
  62. public:                         // Constructor and destructor.
  63.   explicit basic_ostream(basic_streambuf<_CharT, _Traits>* __buf);
  64.   ~basic_ostream();
  65.  
  66. public:                         // Hooks for manipulators.
  67.   typedef basic_ios<_CharT, _Traits>& (_STLP_CALL *__ios_fn)(basic_ios<_CharT, _Traits>&);
  68.   typedef ios_base& (_STLP_CALL *__ios_base_fn)(ios_base&);
  69.   typedef _Self& (_STLP_CALL *__ostream_fn)(_Self&);
  70.   _Self& operator<< (__ostream_fn __f) { return __f(*this); }
  71.   _Self & operator<< (__ios_base_fn __f) { __f(*this); return *this; }
  72.   _Self& operator<< (__ios_fn __ff) { __ff(*this); return *this; }
  73.  
  74. private:
  75.   bool _M_copy_buffered(basic_streambuf<_CharT, _Traits>* __from,
  76.                         basic_streambuf<_CharT, _Traits>* __to);
  77.   bool _M_copy_unbuffered(basic_streambuf<_CharT, _Traits>* __from,
  78.                           basic_streambuf<_CharT, _Traits>* __to);
  79.  
  80. public:
  81.   void _M_put_char(_CharT __c);
  82.  
  83.   void _M_put_nowiden(const _CharT* __s);
  84.   void _M_put_widen(const char* __s);
  85.   bool _M_put_widen_aux(const char* __s, streamsize __n);
  86.  
  87. public:                         // Unformatted output.
  88.   _Self& put(char_type __c);
  89.   _Self& write(const char_type* __s, streamsize __n);
  90.  
  91. public:                         // Formatted output.
  92.   // Formatted output from a streambuf.
  93.   _Self& operator<<(basic_streambuf<_CharT, _Traits>* __buf);
  94. # ifndef _STLP_NO_FUNCTION_TMPL_PARTIAL_ORDER
  95.   // this is needed for compiling with option char = unsigned
  96.   _Self& operator<<(unsigned char __x) { _M_put_char(__x); return *this; }
  97. # endif
  98.   _Self& operator<<(short __x) { return _M_put_num(*this,  __STATIC_CAST(long,__x)); }
  99.   _Self& operator<<(unsigned short __x) { return _M_put_num(*this,  __STATIC_CAST(unsigned long,__x)); }
  100.   _Self& operator<<(int __x) { return _M_put_num(*this,  __STATIC_CAST(long,__x)); }
  101.   _Self& operator<<(unsigned int __x) { return _M_put_num(*this,  __STATIC_CAST(unsigned long,__x)); }
  102.   _Self& operator<<(long __x) { return _M_put_num(*this,  __x); }
  103.   _Self& operator<<(unsigned long __x) { return _M_put_num(*this,  __x); }
  104. #ifdef _STLP_LONG_LONG
  105.   _Self& operator<< (_STLP_LONG_LONG __x)     { return _M_put_num(*this,  __x); }
  106.   _Self& operator<< (unsigned _STLP_LONG_LONG __x) { return _M_put_num(*this,  __x); }
  107. #endif 
  108.   _Self& operator<<(float __x)
  109.     { return _M_put_num(*this,  __STATIC_CAST(double,__x)); }
  110.   _Self& operator<<(double __x) { return _M_put_num(*this,  __x); }
  111. # ifndef _STLP_NO_LONG_DOUBLE
  112.   _Self& operator<<(long double __x) { return _M_put_num(*this,  __x); }
  113. # endif
  114.   _Self& operator<<(const void* __x) { return _M_put_num(*this,  __x); }
  115. # ifndef _STLP_NO_BOOL
  116.   _Self& operator<<(bool __x) { return _M_put_num(*this,  __x); }
  117. # endif
  118.  
  119. public:                         // Buffer positioning and manipulation.
  120.   _Self& flush() {
  121.     if (this->rdbuf())
  122.       if (this->rdbuf()->pubsync() == -1)
  123.         this->setstate(ios_base::badbit);
  124.     return *this;
  125.   }
  126.  
  127.   pos_type tellp() {
  128.     return this->rdbuf() && !this->fail()
  129.       ? this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out)
  130.       : pos_type(-1);
  131.   }
  132.  
  133.   _Self& seekp(pos_type __pos) {
  134.     if (this->rdbuf() && !this->fail())
  135.       this->rdbuf()->pubseekpos(__pos, ios_base::out);
  136.     return *this;
  137.   }
  138.  
  139.   _Self& seekp(off_type __off, ios_base::seekdir __dir) {
  140.     if (this->rdbuf() && !this->fail())
  141.       this->rdbuf()->pubseekoff(__off, __dir, ios_base::out);
  142.     return *this;
  143.   }
  144.  
  145. #if defined (_STLP_USE_TEMPLATE_EXPORT)
  146.   // If we are using DLL specs, we have not to use inner classes
  147.   // end class declaration here
  148.   typedef _Osentry<_CharT, _Traits>  sentry;
  149. };
  150. #  define sentry _Osentry
  151.   template <class _CharT, class _Traits>
  152.   class _Osentry {
  153.     typedef _Osentry<_CharT, _Traits> _Self;
  154. # else
  155.     class sentry {
  156.       typedef sentry _Self;
  157. # endif
  158.     private:
  159.       basic_ostream<_CharT, _Traits>& _M_str;
  160.       //      basic_streambuf<_CharT, _Traits>* _M_buf;
  161.       bool _M_ok;
  162.     public:
  163.       explicit sentry(basic_ostream<_CharT, _Traits>& __str)
  164.         : _M_str(__str), /* _M_buf(__str.rdbuf()), */ _M_ok(_M_init(__str))
  165.       {
  166.       }
  167.       
  168.       ~sentry() {
  169.         if (_M_str.flags() & ios_base::unitbuf)
  170. # ifndef _STLP_INCOMPLETE_EXCEPTION_HEADER 
  171.           if (!_STLP_VENDOR_EXCEPT_STD::uncaught_exception())
  172. # endif
  173.             _M_str.flush();
  174.       }
  175.  
  176.       operator bool() const { return _M_ok; }
  177.     private:                        // Disable assignment and copy constructor.
  178.       sentry(const _Self& __s) : _M_str (__s._M_str) {};
  179.       void operator=(const _Self&) {};
  180.     };
  181. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  182. #  undef sentry
  183. # else
  184.   // close basic_ostream class definition here    
  185. };
  186. # endif
  187.   
  188. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  189. _STLP_EXPORT_TEMPLATE_CLASS basic_ostream<char, char_traits<char> >;
  190. _STLP_EXPORT_TEMPLATE_CLASS _Osentry<char, char_traits<char> >;
  191. #  if !defined (_STLP_NO_WCHAR_T)
  192. _STLP_EXPORT_TEMPLATE_CLASS basic_ostream<wchar_t, char_traits<wchar_t> >;
  193. _STLP_EXPORT_TEMPLATE_CLASS _Osentry<wchar_t, char_traits<wchar_t> >;
  194. #  endif
  195. # endif /* _STLP_USE_TEMPLATE_EXPORT */
  196.  
  197. template <class _CharT, class _Traits>
  198. inline basic_streambuf<_CharT, _Traits>* _STLP_CALL 
  199. _M_get_ostreambuf(basic_ostream<_CharT, _Traits>& __St) 
  200. {
  201.   return __St.rdbuf();
  202. }
  203.  
  204. // Non-member functions.
  205.  
  206. template <class _CharT, class _Traits>
  207. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  208. operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) {
  209.   __os._M_put_char(__c);
  210.   return __os;
  211. }
  212.  
  213. template <class _CharT, class _Traits>
  214. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  215. operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __s) {
  216.   __os._M_put_nowiden(__s);
  217.   return __os;
  218. }
  219.  
  220. # ifdef _STLP_NO_FUNCTION_TMPL_PARTIAL_ORDER
  221. // some specializations
  222.  
  223. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  224. operator<<(basic_ostream<char, char_traits<char> >& __os, char __c) {
  225.   __os._M_put_char(__c);
  226.   return __os;
  227. }
  228.  
  229. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  230. operator<<(basic_ostream<char, char_traits<char> >& __os, signed char __c) {
  231.   __os._M_put_char(__c);
  232.   return __os;
  233. }
  234.  
  235. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  236. operator<<(basic_ostream<char, char_traits<char> >& __os, unsigned char __c) {
  237.   __os._M_put_char(__c);
  238.   return __os;
  239. }
  240.  
  241. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  242. operator<<(basic_ostream<char, char_traits<char> >& __os, const char* __s) {
  243.   __os._M_put_nowiden(__s);
  244.   return __os;
  245. }
  246.  
  247. inline basic_ostream<char, char_traits<char> >& _STLP_CALL
  248. operator<<(basic_ostream<char, char_traits<char> >& __os, const signed char* __s) {
  249.   __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s));
  250.   return __os;
  251. }
  252.  
  253. inline basic_ostream<char, char_traits<char> >&
  254. operator<<(basic_ostream<char, char_traits<char> >& __os, const unsigned char* __s) {
  255.   __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s));
  256.   return __os;
  257. }
  258.  
  259. # else
  260.  
  261. // also for compilers who might use that
  262. template <class _CharT, class _Traits>
  263. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  264. operator<<(basic_ostream<_CharT, _Traits>& __os, char __c) {
  265.   __os._M_put_char(__os.widen(__c));
  266.   return __os;
  267. }
  268.  
  269. template <class _Traits>
  270. inline basic_ostream<char, _Traits>& _STLP_CALL
  271. operator<<(basic_ostream<char, _Traits>& __os, char __c) {
  272.   __os._M_put_char(__c);
  273.   return __os;
  274. }
  275.  
  276. template <class _Traits>
  277. inline basic_ostream<char, _Traits>& _STLP_CALL
  278. operator<<(basic_ostream<char, _Traits>& __os, signed char __c) {
  279.   __os._M_put_char(__c);
  280.   return __os;
  281. }
  282.  
  283. template <class _Traits>
  284. inline basic_ostream<char, _Traits>& _STLP_CALL
  285. operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) {
  286.   __os._M_put_char(__c);
  287.   return __os;
  288. }
  289.  
  290. template <class _CharT, class _Traits>
  291. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  292. operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __s) {
  293.   __os._M_put_widen(__s);
  294.   return __os;
  295. }
  296.  
  297. template <class _Traits>
  298. inline basic_ostream<char, _Traits>& _STLP_CALL
  299. operator<<(basic_ostream<char, _Traits>& __os, const char* __s) {
  300.   __os._M_put_nowiden(__s);
  301.   return __os;
  302. }
  303.  
  304. template <class _Traits>
  305. inline basic_ostream<char, _Traits>& _STLP_CALL
  306. operator<<(basic_ostream<char, _Traits>& __os, const signed char* __s) {
  307.   __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s));
  308.   return __os;
  309. }
  310.  
  311. template <class _Traits>
  312. inline basic_ostream<char, _Traits>&
  313. operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __s) {
  314.   __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s));
  315.   return __os;
  316. }
  317. # endif /* _STLP_NO_FUNCTION_TMPL_PARTIAL_ORDER */
  318.  
  319. //----------------------------------------------------------------------
  320. // basic_ostream manipulators.
  321.  
  322. template <class _CharT, class _Traits>
  323. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  324. endl(basic_ostream<_CharT, _Traits>& __os) {
  325.   __os.put(__os.widen('\n'));
  326.   __os.flush();
  327.   return __os;
  328. }
  329.  
  330. template <class _CharT, class _Traits>
  331. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  332. ends(basic_ostream<_CharT, _Traits>& __os) {
  333.   __os.put(_STLP_DEFAULT_CONSTRUCTED(_CharT));
  334.   return __os;
  335. }
  336.  
  337. template <class _CharT, class _Traits>
  338. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  339. flush(basic_ostream<_CharT, _Traits>& __os) {
  340.   __os.flush();
  341.   return __os;
  342. }
  343.  
  344. _STLP_END_NAMESPACE
  345.  
  346. #  undef _STLP_MANIP_INLINE
  347.  
  348. #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  349. #  include <stl/_ostream.c>
  350. # endif
  351.  
  352. #endif /* _STLP_INTERNAL_OSTREAM_H */
  353.  
  354. // Local Variables:
  355. // mode:C++
  356. // End:
  357.