_stream_iterator.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #if !defined (_STLP_INTERNAL_STREAM_ITERATOR_H) && !defined (_STLP_USE_NO_IOSTREAMS)
  29. #define _STLP_INTERNAL_STREAM_ITERATOR_H
  30. #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
  31. # include <stl/_iterator_base.h>
  32. #endif
  33. // streambuf_iterators predeclarations must appear first
  34. #ifndef _STLP_INTERNAL_IOSFWD
  35. # include <stl/_iosfwd.h>
  36. #endif
  37. #ifndef _STLP_INTERNAL_ALGOBASE_H
  38. # include <stl/_algobase.h>
  39. #endif
  40. #ifndef _STLP_INTERNAL_OSTREAMBUF_ITERATOR_H
  41. # include <stl/_ostreambuf_iterator.h>
  42. #endif
  43. #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
  44. # include <stl/_istreambuf_iterator.h>
  45. #endif
  46. #ifndef _STLP_INTERNAL_ISTREAM
  47. # include <stl/_istream.h>
  48. #endif
  49. // istream_iterator and ostream_iterator look very different if we're
  50. // using new, templatized iostreams than if we're using the old cfront
  51. // version.
  52. _STLP_BEGIN_NAMESPACE
  53. #if !defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
  54. # define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _CharT, class _Traits, class _Dist
  55. # define __ISI_TMPL_ARGUMENTS _Tp, _CharT, _Traits, _Dist
  56. template <class _Tp,
  57. class _CharT = char, class _Traits = char_traits<_CharT>,
  58. class _Dist = ptrdiff_t>
  59. class istream_iterator : public iterator<input_iterator_tag, _Tp , _Dist,
  60. const _Tp*, const _Tp& > {
  61. #else
  62. # if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && !defined (_STLP_DEFAULT_TYPE_PARAM)
  63. # define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
  64. # define __ISI_TMPL_ARGUMENTS _Tp
  65. template <class _Tp>
  66. class istream_iterator : public iterator<input_iterator_tag, _Tp , ptrdiff_t,
  67. const _Tp*, const _Tp& > {
  68. # else
  69. # define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
  70. # define __ISI_TMPL_ARGUMENTS _Tp, _Dist
  71. template <class _Tp, _STLP_DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
  72. class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist ,
  73. const _Tp*, const _Tp& > {
  74. # endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
  75. #endif /* _STLP_LIMITED_DEFAULT_TEMPLATES */
  76. #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
  77. typedef char _CharT;
  78. typedef char_traits<char> _Traits;
  79. # if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && !defined (_STLP_DEFAULT_TYPE_PARAM)
  80. typedef ptrdiff_t _Dist;
  81. # endif
  82. #endif
  83. typedef istream_iterator< __ISI_TMPL_ARGUMENTS > _Self;
  84. public:
  85. typedef _CharT char_type;
  86. typedef _Traits traits_type;
  87. typedef basic_istream<_CharT, _Traits> istream_type;
  88. typedef input_iterator_tag iterator_category;
  89. typedef _Tp value_type;
  90. typedef _Dist difference_type;
  91. typedef const _Tp* pointer;
  92. typedef const _Tp& reference;
  93. istream_iterator() : _M_stream(0), _M_ok(false), _M_read_done(true) {}
  94. istream_iterator(istream_type& __s) : _M_stream(&__s), _M_ok(false), _M_read_done(false) {}
  95. reference operator*() const {
  96. if (!_M_read_done) {
  97. _M_read();
  98. }
  99. return _M_value;
  100. }
  101. _STLP_DEFINE_ARROW_OPERATOR
  102. _Self& operator++() {
  103. _M_read();
  104. return *this;
  105. }
  106. _Self operator++(int) {
  107. _Self __tmp = *this;
  108. _M_read();
  109. return __tmp;
  110. }
  111. bool _M_equal(const _Self& __x) const {
  112. if (!_M_read_done) {
  113. _M_read();
  114. }
  115. if (!__x._M_read_done) {
  116. __x._M_read();
  117. }
  118. return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream);
  119. }
  120. private:
  121. istream_type* _M_stream;
  122. mutable _Tp _M_value;
  123. mutable bool _M_ok;
  124. mutable bool _M_read_done;
  125. void _M_read() const {
  126. _STLP_MUTABLE(_Self, _M_ok) = ((_M_stream != 0) && !_M_stream->fail());
  127. if (_M_ok) {
  128. *_M_stream >> _STLP_MUTABLE(_Self, _M_value);
  129. _STLP_MUTABLE(_Self, _M_ok) = !_M_stream->fail();
  130. }
  131. _STLP_MUTABLE(_Self, _M_read_done) = true;
  132. }
  133. };
  134. #if !defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
  135. template <class _TpP,
  136. class _CharT = char, class _Traits = char_traits<_CharT> >
  137. #else
  138. template <class _TpP>
  139. #endif
  140. class ostream_iterator: public iterator<output_iterator_tag, void, void, void, void> {
  141. #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
  142. typedef char _CharT;
  143. typedef char_traits<char> _Traits;
  144. typedef ostream_iterator<_TpP> _Self;
  145. #else
  146. typedef ostream_iterator<_TpP, _CharT, _Traits> _Self;
  147. #endif
  148. public:
  149. typedef _CharT char_type;
  150. typedef _Traits traits_type;
  151. typedef basic_ostream<_CharT, _Traits> ostream_type;
  152. typedef output_iterator_tag iterator_category;
  153. ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
  154. ostream_iterator(ostream_type& __s, const _CharT* __c)
  155. : _M_stream(&__s), _M_string(__c) {}
  156. _Self& operator=(const _TpP& __val) {
  157. *_M_stream << __val;
  158. if (_M_string) *_M_stream << _M_string;
  159. return *this;
  160. }
  161. _Self& operator*() { return *this; }
  162. _Self& operator++() { return *this; }
  163. _Self& operator++(int) { return *this; }
  164. private:
  165. ostream_type* _M_stream;
  166. const _CharT* _M_string;
  167. };
  168. #if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
  169. # if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
  170. template <class _TpP>
  171. inline output_iterator_tag _STLP_CALL
  172. iterator_category(const ostream_iterator<_TpP>&) { return output_iterator_tag(); }
  173. # else
  174. template <class _TpP, class _CharT, class _Traits>
  175. inline output_iterator_tag _STLP_CALL
  176. iterator_category(const ostream_iterator<_TpP, _CharT, _Traits>&) { return output_iterator_tag(); }
  177. # endif
  178. #endif
  179. _STLP_END_NAMESPACE
  180. // form-independent definiotion of stream iterators
  181. _STLP_BEGIN_NAMESPACE
  182. template < __ISI_TMPL_HEADER_ARGUMENTS >
  183. inline bool _STLP_CALL
  184. operator==(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
  185. const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y)
  186. { return __x._M_equal(__y); }
  187. #if defined (_STLP_USE_SEPARATE_RELOPS_NAMESPACE)
  188. template < __ISI_TMPL_HEADER_ARGUMENTS >
  189. inline bool _STLP_CALL
  190. operator!=(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
  191. const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y)
  192. { return !__x._M_equal(__y); }
  193. #endif
  194. #if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
  195. template < __ISI_TMPL_HEADER_ARGUMENTS >
  196. inline input_iterator_tag _STLP_CALL
  197. iterator_category(const istream_iterator< __ISI_TMPL_ARGUMENTS >&)
  198. { return input_iterator_tag(); }
  199. template < __ISI_TMPL_HEADER_ARGUMENTS >
  200. inline _Tp* _STLP_CALL
  201. value_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Tp*) 0; }
  202. # if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && !defined (_STLP_DEFAULT_TYPE_PARAM)
  203. template < __ISI_TMPL_HEADER_ARGUMENTS >
  204. inline ptrdiff_t* _STLP_CALL
  205. distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (ptrdiff_t*)0; }
  206. # else
  207. template < __ISI_TMPL_HEADER_ARGUMENTS >
  208. inline _Dist* _STLP_CALL
  209. distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Dist*)0; }
  210. # endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
  211. #endif
  212. _STLP_END_NAMESPACE
  213. #undef __ISI_TMPL_HEADER_ARGUMENTS
  214. #undef __ISI_TMPL_ARGUMENTS
  215. #endif /* _STLP_INTERNAL_STREAM_ITERATOR_H */
  216. // Local Variables:
  217. // mode:C++
  218. // End: