ostream_iterator.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef BOOST_ARCHIVE_ITERATORS_OSTREAM_ITERATOR_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_OSTREAM_ITERATOR_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // ostream_iterator.hpp
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. // note: this is a custom version of the standard ostream_iterator.
  15. // This is necessary as the standard version doesn't work as expected
  16. // for wchar_t based streams on systems for which wchar_t not a true
  17. // type but rather a synonym for some integer type.
  18. #include <ostream>
  19. #include <boost/iterator/iterator_facade.hpp>
  20. namespace boost {
  21. namespace archive {
  22. namespace iterators {
  23. // given a type, make an input iterator based on a pointer to that type
  24. template<class Elem>
  25. class ostream_iterator :
  26. public boost::iterator_facade<
  27. ostream_iterator<Elem>,
  28. Elem,
  29. std::output_iterator_tag,
  30. ostream_iterator<Elem> &
  31. >
  32. {
  33. friend class boost::iterator_core_access;
  34. typedef ostream_iterator this_t ;
  35. typedef Elem char_type;
  36. typedef std::basic_ostream<char_type> ostream_type;
  37. //emulate the behavior of std::ostream
  38. ostream_iterator & dereference() const {
  39. return const_cast<ostream_iterator &>(*this);
  40. }
  41. bool equal(const this_t & rhs) const {
  42. return m_ostream == rhs.m_ostream;
  43. }
  44. void increment(){}
  45. protected:
  46. ostream_type *m_ostream;
  47. void put_val(char_type e){
  48. if(NULL != m_ostream){
  49. m_ostream->put(e);
  50. if(! m_ostream->good())
  51. m_ostream = NULL;
  52. }
  53. }
  54. public:
  55. this_t & operator=(char_type c){
  56. put_val(c);
  57. return *this;
  58. }
  59. ostream_iterator(ostream_type & os) :
  60. m_ostream (& os)
  61. {}
  62. ostream_iterator() :
  63. m_ostream (NULL)
  64. {}
  65. ostream_iterator(const ostream_iterator & rhs) :
  66. m_ostream (rhs.m_ostream)
  67. {}
  68. };
  69. } // namespace iterators
  70. } // namespace archive
  71. } // namespace boost
  72. #endif // BOOST_ARCHIVE_ITERATORS_OSTREAM_ITERATOR_HPP