blob: b0398e110dcaa0f5e391f1dc0583d7795438405a [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001// (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
8namespace ndnboost { namespace iostreams {
9
10namespace detail {
11
12template<typename T>
13struct write_device_impl;
14
15template<typename T>
16struct write_filter_impl;
17
18} // End namespace detail.
19
20template<typename T>
21bool 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
27template<typename T>
28inline 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
35template<typename T, typename Sink>
36inline std::streamsize
37write( 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
44namespace detail {
45
46//------------------Definition of write_device_impl---------------------------//
47
48template<typename T>
49struct 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
62template<>
63struct 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
80template<>
81struct 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
98template<>
99struct 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
114template<typename T>
115struct 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
128template<>
129struct 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
140template<>
141struct 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.