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

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Copyright (c) 1996-1998
  7.  * Silicon Graphics Computer Systems, Inc.
  8.  *
  9.  * Copyright (c) 1997
  10.  * Moscow Center for SPARC Technology
  11.  *
  12.  * Copyright (c) 1999 
  13.  * Boris Fomitchev
  14.  *
  15.  * This material is provided "as is", with absolutely no warranty expressed
  16.  * or implied. Any use is at your own risk.
  17.  *
  18.  * Permission to use or copy this software for any purpose is hereby granted 
  19.  * without fee, provided the above notices are retained on all copies.
  20.  * Permission to modify the code and to distribute modified code is granted,
  21.  * provided the above notices are retained, and a notice that the code was
  22.  * modified is included with the above copyright notice.
  23.  *
  24.  */
  25.  
  26. /* NOTE: This is an internal header file, included by other STL headers.
  27.  *   You should not attempt to use it directly.
  28.  */
  29.  
  30. #if !defined (_STLP_INTERNAL_STREAM_ITERATOR_H) && ! defined (_STLP_USE_NO_IOSTREAMS)
  31. #define _STLP_INTERNAL_STREAM_ITERATOR_H
  32.  
  33. #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
  34. # include <stl/_iterator_base.h>
  35. #endif
  36.  
  37. // streambuf_iterators predeclarations must appear first
  38. #ifndef _STLP_IOSFWD
  39. # include <iosfwd>
  40. #endif
  41.  
  42. #ifndef _STLP_INTERNAL_ALGOBASE_H
  43. #include <stl/_algobase.h>
  44. #endif
  45.  
  46. #if defined (_STLP_OWN_IOSTREAMS)
  47.  
  48. #ifndef _STLP_INTERNAL_OSTREAMBUF_ITERATOR_H
  49. # include <stl/_ostreambuf_iterator.h>
  50. #endif
  51.  
  52. #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
  53. # include <stl/_istreambuf_iterator.h>
  54. #endif
  55.  
  56. #ifndef _STLP_INTERNAL_ISTREAM_H
  57. # include <stl/_istream.h>
  58. #endif
  59. #endif /* _STLP_OWN_IOSTREAMS */
  60.  
  61. // istream_iterator and ostream_iterator look very different if we're
  62. // using new, templatized iostreams than if we're using the old cfront
  63. // version.
  64.  
  65. # if defined (_STLP_USE_NEW_IOSTREAMS) 
  66.  
  67. _STLP_BEGIN_NAMESPACE
  68.  
  69. #  ifndef _STLP_LIMITED_DEFAULT_TEMPLATES
  70. template <class _Tp, 
  71.           class _CharT = _STLP_DEFAULTCHAR, class _Traits = char_traits<_CharT>,
  72.           class _Dist = ptrdiff_t> 
  73. #   define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _CharT, class _Traits, class _Dist
  74. #   define __ISI_TMPL_ARGUMENTS _Tp, _CharT, _Traits, _Dist
  75. class istream_iterator : public iterator<input_iterator_tag, _Tp , _Dist,
  76.                          const _Tp*, const _Tp& > {
  77. #  else
  78.  
  79. #   if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
  80. #    define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
  81. #    define __ISI_TMPL_ARGUMENTS        _Tp
  82. template <class _Tp>
  83. class istream_iterator : public iterator<input_iterator_tag, _Tp , ptrdiff_t, 
  84.                          const _Tp*, const _Tp& > {
  85. #   else
  86. #    define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
  87. #    define __ISI_TMPL_ARGUMENTS        _Tp, _Dist
  88. template <class _Tp,__DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
  89. class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist , 
  90.                                          const _Tp*, const _Tp& > {
  91. #   endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
  92.  
  93. #  endif /* _STLP_LIMITED_DEFAULT_TEMPLATES */
  94.  
  95. # ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
  96.   typedef char _CharT;
  97.   typedef char_traits<char> _Traits;
  98. #  if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
  99.   typedef ptrdiff_t _Dist;
  100. #  endif
  101. # endif
  102.  
  103.   typedef istream_iterator< __ISI_TMPL_ARGUMENTS > _Self;
  104. public:
  105.   typedef _CharT                         char_type;
  106.   typedef _Traits                        traits_type;
  107.   typedef basic_istream<_CharT, _Traits> istream_type;
  108.  
  109.   typedef input_iterator_tag             iterator_category;
  110.   typedef _Tp                            value_type;
  111.   typedef _Dist                          difference_type;
  112.   typedef const _Tp*                     pointer;
  113.   typedef const _Tp&                     reference;
  114.  
  115.   istream_iterator() : _M_stream(0), _M_ok(false) {}
  116.   istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
  117.  
  118.   reference operator*() const { return _M_value; }
  119.  
  120.   _STLP_DEFINE_ARROW_OPERATOR
  121.  
  122.   _Self& operator++() { 
  123.     _M_read(); 
  124.     return *this;
  125.   }
  126.   _Self operator++(int)  {
  127.     _Self __tmp = *this;
  128.     _M_read();
  129.     return __tmp;
  130.   }
  131.  
  132.   bool _M_equal(const _Self& __x) const
  133.     { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
  134.  
  135. private:
  136.   istream_type* _M_stream;
  137.   _Tp _M_value;
  138.   bool _M_ok;
  139.  
  140.   void _M_read() {
  141.     _M_ok = (_M_stream && *_M_stream) ? true : false;
  142.     if (_M_ok) {
  143.       *_M_stream >> _M_value;
  144.       _M_ok = *_M_stream ? true : false;
  145.     }
  146.   }
  147. };
  148.  
  149. #ifndef _STLP_LIMITED_DEFAULT_TEMPLATES
  150. template <class _TpP,
  151.           class _CharT = _STLP_DEFAULTCHAR, class _Traits = char_traits<_CharT> >
  152. #else
  153. template <class _TpP>
  154. #endif
  155. class ostream_iterator: public iterator<output_iterator_tag, void, void, void, void> {
  156. # ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
  157.   typedef char _CharT;
  158.   typedef char_traits<char> _Traits;
  159.   typedef ostream_iterator<_TpP> _Self;
  160. # else
  161.   typedef ostream_iterator<_TpP, _CharT, _Traits> _Self;
  162. # endif
  163. public:
  164.   typedef _CharT                         char_type;
  165.   typedef _Traits                        traits_type;
  166.   typedef basic_ostream<_CharT, _Traits> ostream_type;
  167.  
  168.   typedef output_iterator_tag            iterator_category;
  169.  
  170.   ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
  171.   ostream_iterator(ostream_type& __s, const _CharT* __c) 
  172.     : _M_stream(&__s), _M_string(__c)  {}
  173.   _Self& operator=(const _TpP& __val) { 
  174.     *_M_stream << __val;
  175.     if (_M_string) *_M_stream << _M_string;
  176.     return *this;
  177.   }
  178.   _Self& operator*() { return *this; }
  179.   _Self& operator++() { return *this; } 
  180.   _Self& operator++(int) { return *this; } 
  181. private:
  182.   ostream_type* _M_stream;
  183.   const _CharT* _M_string;
  184. };
  185.  
  186. # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
  187. #  ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
  188. template <class _TpP>
  189. inline output_iterator_tag _STLP_CALL 
  190. iterator_category(const ostream_iterator<_TpP>&) { return output_iterator_tag(); }
  191. # else
  192. template <class _TpP, class _CharT, class _Traits>
  193. inline output_iterator_tag _STLP_CALL 
  194. iterator_category(const ostream_iterator<_TpP, _CharT, _Traits>&) { return output_iterator_tag(); }
  195. #  endif
  196. # endif
  197.  
  198. _STLP_END_NAMESPACE
  199.  
  200. # elif ! defined(_STLP_USE_NO_IOSTREAMS)
  201.  
  202. _STLP_BEGIN_NAMESPACE
  203.  
  204. #  if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
  205. #  define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
  206. #  define __ISI_TMPL_ARGUMENTS        _Tp
  207. template <class _Tp>
  208. class istream_iterator : public iterator<input_iterator_tag, _Tp, ptrdiff_t, 
  209.                          const _Tp*, const _Tp& > {
  210. #  else
  211. #  define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
  212. #  define __ISI_TMPL_ARGUMENTS        _Tp, _Dist
  213. template <class _Tp, __DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
  214. class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist, 
  215.                          const _Tp*, const _Tp& > {
  216. #  endif
  217.  
  218. protected:
  219.   istream* _M_stream;
  220.   _Tp _M_value;
  221.   bool _M_end_marker;
  222.   void _M_read() {
  223.     _M_end_marker = (*_M_stream) ? true : false;
  224.     if (_M_end_marker) *_M_stream >> _M_value;
  225.     _M_end_marker = (*_M_stream) ? true : false;
  226. }
  227. public:
  228.   typedef input_iterator_tag  iterator_category;
  229.   typedef _Tp                 value_type;
  230.   typedef _Dist               difference_type;
  231.   typedef const _Tp*          pointer;
  232.   typedef const _Tp&          reference;
  233.  
  234.   istream_iterator() : _M_stream(&cin), _M_end_marker(false) {}
  235.   istream_iterator(istream& __s) : _M_stream(&__s) { _M_read(); }
  236.   reference operator*() const { return _M_value; }
  237.  
  238.   _STLP_DEFINE_ARROW_OPERATOR
  239.  
  240.   istream_iterator< __ISI_TMPL_ARGUMENTS >& operator++() { 
  241.     _M_read(); 
  242.     return *this;
  243.   }
  244.   istream_iterator< __ISI_TMPL_ARGUMENTS > operator++(int)  {
  245.     istream_iterator< __ISI_TMPL_ARGUMENTS > __tmp = *this;
  246.     _M_read();
  247.     return __tmp;
  248.   }
  249.   inline bool _M_equal(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) const {
  250.     return (_M_stream == __y._M_stream &&
  251.         _M_end_marker == __y._M_end_marker) ||
  252.       _M_end_marker == false && __y._M_end_marker == false;
  253.   }
  254. };
  255.  
  256. template <class _Tp>
  257. class ostream_iterator {
  258. protected:
  259.   ostream* _M_stream;
  260.   const char* _M_string;
  261. public:
  262.   typedef output_iterator_tag iterator_category;
  263. # ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
  264.   typedef void                value_type;
  265.   typedef void                difference_type;
  266.   typedef void                pointer;
  267.   typedef void                reference;
  268. # endif
  269.   ostream_iterator(ostream& __s) : _M_stream(&__s), _M_string(0) {}
  270.   ostream_iterator(ostream& __s, const char* __c) 
  271.     : _M_stream(&__s), _M_string(__c)  {}
  272.   ostream_iterator<_Tp>& operator=(const _Tp& __val) { 
  273.     *_M_stream << __val;
  274.     if (_M_string) *_M_stream << _M_string;
  275.     return *this;
  276.   }
  277.   ostream_iterator<_Tp>& operator*() { return *this; }
  278.   ostream_iterator<_Tp>& operator++() { return *this; } 
  279.   ostream_iterator<_Tp>& operator++(int) { return *this; } 
  280. };
  281.  
  282. # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
  283. template <class _Tp> inline output_iterator_tag 
  284. iterator_category(const ostream_iterator<_Tp>&) { return output_iterator_tag(); }
  285. #endif
  286.  
  287. _STLP_END_NAMESPACE
  288.  
  289. #endif /* _STLP_USE_NEW_IOSTREAMS */
  290.  
  291. // form-independent definiotion of stream iterators
  292. _STLP_BEGIN_NAMESPACE
  293.  
  294. template < __ISI_TMPL_HEADER_ARGUMENTS >
  295. inline bool _STLP_CALL 
  296. operator==(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
  297.            const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) {
  298.   return __x._M_equal(__y);
  299. }
  300.  
  301. #  ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
  302.  
  303. template < __ISI_TMPL_HEADER_ARGUMENTS >
  304. inline bool _STLP_CALL 
  305. operator!=(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
  306.            const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) {
  307.   return !__x._M_equal(__y);
  308. }
  309.  
  310. #  endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
  311.  
  312. # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
  313. template < __ISI_TMPL_HEADER_ARGUMENTS >
  314. inline input_iterator_tag _STLP_CALL 
  315. iterator_category(const istream_iterator< __ISI_TMPL_ARGUMENTS >&)
  316. { return input_iterator_tag(); }
  317. template < __ISI_TMPL_HEADER_ARGUMENTS >
  318. inline _Tp* _STLP_CALL 
  319. value_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Tp*) 0; }
  320.  
  321. #  if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
  322. template < __ISI_TMPL_HEADER_ARGUMENTS >
  323. inline ptrdiff_t* _STLP_CALL 
  324. distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (ptrdiff_t*)0; }
  325. #  else
  326. template < __ISI_TMPL_HEADER_ARGUMENTS >
  327. inline _Dist* _STLP_CALL 
  328. distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Dist*)0; }
  329. #  endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
  330.  
  331. # endif 
  332.  
  333. _STLP_END_NAMESPACE
  334.  
  335. #  undef __ISI_TMPL_HEADER_ARGUMENTS
  336. #  undef __ISI_TMPL_ARGUMENTS
  337.  
  338.  
  339. #endif /* _STLP_INTERNAL_STREAM_ITERATOR_H */
  340.  
  341. // Local Variables:
  342. // mode:C++
  343. // End:
  344.