fix doc example typo
[boost.git] / boost / iostreams / device / array.hpp
blob3e08f04470e158802416d2893a121f328c0b96f8
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2004-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.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 #ifndef BOOST_IOSTREAMS_ARRAY_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_ARRAY_HPP_INCLUDED
11 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
12 # pragma once
13 #endif
15 #include <boost/config.hpp> // BOOST_MSVC, make sure size_t is in std.
16 #include <boost/detail/workaround.hpp>
17 #include <cstddef> // std::size_t.
18 #include <utility> // pair.
19 #include <boost/iostreams/categories.hpp>
20 #include <boost/preprocessor/cat.hpp>
21 #include <boost/static_assert.hpp>
22 #include <boost/type_traits/is_convertible.hpp>
23 #include <boost/type_traits/is_same.hpp>
25 namespace boost { namespace iostreams {
27 namespace detail {
29 template<typename Mode, typename Ch>
30 class array_adapter {
31 public:
32 typedef Ch char_type;
33 typedef std::pair<char_type*, char_type*> pair_type;
34 struct category
35 : public Mode,
36 public device_tag,
37 public direct_tag
38 { };
39 array_adapter(char_type* begin, char_type* end);
40 array_adapter(char_type* begin, std::size_t length);
41 array_adapter(const char_type* begin, const char_type* end);
42 array_adapter(const char_type* begin, std::size_t length);
43 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
44 template<int N>
45 array_adapter(char_type (&ar)[N])
46 : begin_(ar), end_(ar + N)
47 { }
48 #endif
49 pair_type input_sequence();
50 pair_type output_sequence();
51 private:
52 char_type* begin_;
53 char_type* end_;
56 } // End namespace detail.
58 // Local macros, #undef'd below.
59 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
60 # define BOOST_IOSTREAMS_ARRAY_CTOR(name, ch) \
61 template<int N> \
62 BOOST_PP_CAT(basic_, name)(ch (&ar)[N]) \
63 : base_type(ar) { } \
64 /**/
65 #else
66 # define BOOST_IOSTREAMS_ARRAY_CTOR(name, ch)
67 #endif
68 #define BOOST_IOSTREAMS_ARRAY(name, mode) \
69 template<typename Ch> \
70 struct BOOST_PP_CAT(basic_, name) : detail::array_adapter<mode, Ch> { \
71 private: \
72 typedef detail::array_adapter<mode, Ch> base_type; \
73 public: \
74 typedef typename base_type::char_type char_type; \
75 typedef typename base_type::category category; \
76 BOOST_PP_CAT(basic_, name)(char_type* begin, char_type* end) \
77 : base_type(begin, end) { } \
78 BOOST_PP_CAT(basic_, name)(char_type* begin, std::size_t length) \
79 : base_type(begin, length) { } \
80 BOOST_PP_CAT(basic_, name)(const char_type* begin, const char_type* end) \
81 : base_type(begin, end) { } \
82 BOOST_PP_CAT(basic_, name)(const char_type* begin, std::size_t length) \
83 : base_type(begin, length) { } \
84 BOOST_IOSTREAMS_ARRAY_CTOR(name, Ch) \
85 }; \
86 typedef BOOST_PP_CAT(basic_, name)<char> name; \
87 typedef BOOST_PP_CAT(basic_, name)<wchar_t> BOOST_PP_CAT(w, name); \
88 /**/
89 BOOST_IOSTREAMS_ARRAY(array_source, input_seekable)
90 BOOST_IOSTREAMS_ARRAY(array_sink, output_seekable)
91 BOOST_IOSTREAMS_ARRAY(array, seekable)
92 #undef BOOST_IOSTREAMS_ARRAY_CTOR
93 #undef BOOST_IOSTREAMS_ARRAY
96 //------------------Implementation of array_adapter---------------------------//
98 namespace detail {
100 template<typename Mode, typename Ch>
101 array_adapter<Mode, Ch>::array_adapter
102 (char_type* begin, char_type* end)
103 : begin_(begin), end_(end)
106 template<typename Mode, typename Ch>
107 array_adapter<Mode, Ch>::array_adapter
108 (char_type* begin, std::size_t length)
109 : begin_(begin), end_(begin + length)
112 template<typename Mode, typename Ch>
113 array_adapter<Mode, Ch>::array_adapter
114 (const char_type* begin, const char_type* end)
115 : begin_(const_cast<char_type*>(begin)), // Treated as read-only.
116 end_(const_cast<char_type*>(end)) // Treated as read-only.
117 { BOOST_STATIC_ASSERT((!is_convertible<Mode, output>::value)); }
119 template<typename Mode, typename Ch>
120 array_adapter<Mode, Ch>::array_adapter
121 (const char_type* begin, std::size_t length)
122 : begin_(const_cast<char_type*>(begin)), // Treated as read-only.
123 end_(const_cast<char_type*>(begin) + length) // Treated as read-only.
124 { BOOST_STATIC_ASSERT((!is_convertible<Mode, output>::value)); }
126 template<typename Mode, typename Ch>
127 typename array_adapter<Mode, Ch>::pair_type
128 array_adapter<Mode, Ch>::input_sequence()
129 { BOOST_STATIC_ASSERT((is_convertible<Mode, input>::value));
130 return pair_type(begin_, end_); }
132 template<typename Mode, typename Ch>
133 typename array_adapter<Mode, Ch>::pair_type
134 array_adapter<Mode, Ch>::output_sequence()
135 { BOOST_STATIC_ASSERT((is_convertible<Mode, output>::value));
136 return pair_type(begin_, end_); }
138 } // End namespace detail.
140 //----------------------------------------------------------------------------//
142 } } // End namespaces iostreams, boost.
144 #endif // #ifndef BOOST_IOSTREAMS_ARRAY_HPP_INCLUDED