Jeff Thompson | 86b6d64 | 2013-10-17 15:01:56 -0700 | [diff] [blame^] | 1 | // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) |
| 2 | // (C) Copyright 2005-2007 Jonathan Turkanis |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) |
| 5 | |
| 6 | // See http://www.boost.org/libs/iostreams for documentation. |
| 7 | |
| 8 | namespace ndnboost { namespace iostreams { |
| 9 | |
| 10 | namespace detail { |
| 11 | |
| 12 | template<typename T> |
| 13 | struct write_device_impl; |
| 14 | |
| 15 | template<typename T> |
| 16 | struct write_filter_impl; |
| 17 | |
| 18 | } // End namespace detail. |
| 19 | |
| 20 | template<typename T> |
| 21 | bool put(T& t, typename char_type_of<T>::type c) |
| 22 | { |
| 23 | typedef typename detail::unwrapped_type<T>::type unwrapped; |
| 24 | return detail::write_device_impl<T>::inner<unwrapped>::put(detail::unwrap(t), c); |
| 25 | } |
| 26 | |
| 27 | template<typename T> |
| 28 | inline std::streamsize write |
| 29 | (T& t, const typename char_type_of<T>::type* s, std::streamsize n) |
| 30 | { |
| 31 | typedef typename detail::unwrapped_type<T>::type unwrapped; |
| 32 | return detail::write_device_impl<T>::inner<unwrapped>::write(detail::unwrap(t), s, n); |
| 33 | } |
| 34 | |
| 35 | template<typename T, typename Sink> |
| 36 | inline std::streamsize |
| 37 | write( T& t, Sink& snk, const typename char_type_of<T>::type* s, |
| 38 | std::streamsize n ) |
| 39 | { |
| 40 | typedef typename detail::unwrapped_type<T>::type unwrapped; |
| 41 | return detail::write_filter_impl<T>::inner<unwrapped>::write(detail::unwrap(t), snk, s, n); |
| 42 | } |
| 43 | |
| 44 | namespace detail { |
| 45 | |
| 46 | //------------------Definition of write_device_impl---------------------------// |
| 47 | |
| 48 | template<typename T> |
| 49 | struct write_device_impl |
| 50 | : mpl::if_< |
| 51 | is_custom<T>, |
| 52 | operations<T>, |
| 53 | write_device_impl< |
| 54 | NDNBOOST_DEDUCED_TYPENAME |
| 55 | dispatch< |
| 56 | T, ostream_tag, streambuf_tag, output |
| 57 | >::type |
| 58 | > |
| 59 | >::type |
| 60 | { }; |
| 61 | |
| 62 | template<> |
| 63 | struct write_device_impl<ostream_tag> { |
| 64 | template<typename T> |
| 65 | struct inner { |
| 66 | static bool put(T& t, typename char_type_of<T>::type c) |
| 67 | { |
| 68 | typedef typename char_type_of<T>::type char_type; |
| 69 | typedef NDNBOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type; |
| 70 | return !traits_type::eq_int_type( t.rdbuf()->s.sputc(), |
| 71 | traits_type::eof() ); |
| 72 | } |
| 73 | |
| 74 | static std::streamsize write |
| 75 | (T& t, const typename char_type_of<T>::type* s, std::streamsize n) |
| 76 | { return t.rdbuf()->sputn(s, n); } |
| 77 | }; |
| 78 | }; |
| 79 | |
| 80 | template<> |
| 81 | struct write_device_impl<streambuf_tag> { |
| 82 | template<typename T> |
| 83 | struct inner { |
| 84 | static bool put(T& t, typename char_type_of<T>::type c) |
| 85 | { |
| 86 | typedef typename char_type_of<T>::type char_type; |
| 87 | typedef NDNBOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type; |
| 88 | return !traits_type::eq_int_type(t.sputc(c), traits_type::eof()); |
| 89 | } |
| 90 | |
| 91 | template<typename T> |
| 92 | static std::streamsize write |
| 93 | (T& t, const typename char_type_of<T>::type* s, std::streamsize n) |
| 94 | { return t.sputn(s, n); } |
| 95 | }; |
| 96 | }; |
| 97 | |
| 98 | template<> |
| 99 | struct write_device_impl<output> { |
| 100 | template<typename T> |
| 101 | struct inner { |
| 102 | static bool put(T& t, typename char_type_of<T>::type c) |
| 103 | { return t.write(&c, 1) == 1; } |
| 104 | |
| 105 | template<typename T> |
| 106 | static std::streamsize |
| 107 | write(T& t, const typename char_type_of<T>::type* s, std::streamsize n) |
| 108 | { return t.write(s, n); } |
| 109 | }; |
| 110 | }; |
| 111 | |
| 112 | //------------------Definition of write_filter_impl---------------------------// |
| 113 | |
| 114 | template<typename T> |
| 115 | struct write_filter_impl |
| 116 | : mpl::if_< |
| 117 | is_custom<T>, |
| 118 | operations<T>, |
| 119 | write_filter_impl< |
| 120 | NDNBOOST_DEDUCED_TYPENAME |
| 121 | dispatch< |
| 122 | T, multichar_tag, any_tag |
| 123 | >::type |
| 124 | > |
| 125 | >::type |
| 126 | { }; |
| 127 | |
| 128 | template<> |
| 129 | struct write_filter_impl<multichar_tag> { |
| 130 | template<typename T> |
| 131 | struct inner { |
| 132 | template<typename Sink> |
| 133 | static std::streamsize |
| 134 | write( T& t, Sink& snk, const typename char_type_of<T>::type* s, |
| 135 | std::streamsize n ) |
| 136 | { return t.write(snk, s, n); } |
| 137 | }; |
| 138 | }; |
| 139 | |
| 140 | template<> |
| 141 | struct write_filter_impl<any_tag> { |
| 142 | template<typename T> |
| 143 | struct inner { |
| 144 | template<typename Sink> |
| 145 | static std::streamsize |
| 146 | write( T& t, Sink& snk, const typename char_type_of<T>::type* s, |
| 147 | std::streamsize n ) |
| 148 | { |
| 149 | for (std::streamsize off = 0; off < n; ++off) |
| 150 | if (!t.put(snk, s[off])) |
| 151 | return off; |
| 152 | return n; |
| 153 | } |
| 154 | }; |
| 155 | }; |
| 156 | |
| 157 | } // End namespace detail. |
| 158 | |
| 159 | } } // End namespaces iostreams, boost. |