iterator_sink.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boist.org/LICENSE_1_0.txt)
  5. #if !defined(BOOST_SPIRIT_ITERATOR_SINK_MAY_27_2007_0133PM)
  6. #define BOOST_SPIRIT_ITERATOR_SINK_MAY_27_2007_0133PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/iostreams/stream.hpp>
  11. #include <boost/spirit/home/karma/detail/generate_to.hpp>
  12. ///////////////////////////////////////////////////////////////////////////////
  13. namespace boost { namespace spirit { namespace karma { namespace detail
  14. {
  15. ///////////////////////////////////////////////////////////////////////////
  16. template <
  17. typename OutputIterator, typename Char, typename CharEncoding
  18. , typename Tag
  19. >
  20. struct iterator_sink
  21. {
  22. typedef boost::iostreams::sink_tag category;
  23. typedef Char char_type;
  24. iterator_sink (OutputIterator& sink_)
  25. : sink(sink_)
  26. {}
  27. // Write up to n characters from the buffer s to the output sequence,
  28. // returning the number of characters written
  29. std::streamsize write (Char const* s, std::streamsize n)
  30. {
  31. std::streamsize bytes_written = 0;
  32. while (n--) {
  33. if (!generate_to(sink, *s, CharEncoding(), Tag()))
  34. break;
  35. ++s; ++bytes_written;
  36. }
  37. return bytes_written;
  38. }
  39. OutputIterator& sink;
  40. // silence MSVC warning C4512: assignment operator could not be generated
  41. BOOST_DELETED_FUNCTION(iterator_sink& operator= (iterator_sink const&))
  42. };
  43. }}}}
  44. #endif