File indexing completed on 2025-01-18 09:38:50
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_IOSTREAMS_ARRAY_HPP_INCLUDED
0009 #define BOOST_IOSTREAMS_ARRAY_HPP_INCLUDED
0010
0011 #if defined(_MSC_VER)
0012 # pragma once
0013 #endif
0014
0015 #include <boost/config.hpp> // BOOST_MSVC, make sure size_t is in std.
0016 #include <boost/detail/workaround.hpp>
0017 #include <cstddef> // std::size_t.
0018 #include <utility> // pair.
0019 #include <boost/iostreams/categories.hpp>
0020 #include <boost/preprocessor/cat.hpp>
0021 #include <boost/static_assert.hpp>
0022 #include <boost/type_traits/is_convertible.hpp>
0023 #include <boost/type_traits/is_same.hpp>
0024
0025 namespace boost { namespace iostreams {
0026
0027 namespace detail {
0028
0029 template<typename Mode, typename Ch>
0030 class array_adapter {
0031 public:
0032 typedef Ch char_type;
0033 typedef std::pair<char_type*, char_type*> pair_type;
0034 struct category
0035 : public Mode,
0036 public device_tag,
0037 public direct_tag
0038 { };
0039 array_adapter(char_type* begin, char_type* end);
0040 array_adapter(char_type* begin, std::size_t length);
0041 array_adapter(const char_type* begin, const char_type* end);
0042 array_adapter(const char_type* begin, std::size_t length);
0043 template<int N>
0044 array_adapter(char_type (&ar)[N])
0045 : begin_(ar), end_(ar + N)
0046 { }
0047 pair_type input_sequence();
0048 pair_type output_sequence();
0049 private:
0050 char_type* begin_;
0051 char_type* end_;
0052 };
0053
0054 }
0055
0056 #define BOOST_IOSTREAMS_ARRAY(name, mode) \
0057 template<typename Ch> \
0058 struct BOOST_PP_CAT(basic_, name) : detail::array_adapter<mode, Ch> { \
0059 private: \
0060 typedef detail::array_adapter<mode, Ch> base_type; \
0061 public: \
0062 typedef typename base_type::char_type char_type; \
0063 typedef typename base_type::category category; \
0064 BOOST_PP_CAT(basic_, name)(char_type* begin, char_type* end) \
0065 : base_type(begin, end) { } \
0066 BOOST_PP_CAT(basic_, name)(char_type* begin, std::size_t length) \
0067 : base_type(begin, length) { } \
0068 BOOST_PP_CAT(basic_, name)(const char_type* begin, const char_type* end) \
0069 : base_type(begin, end) { } \
0070 BOOST_PP_CAT(basic_, name)(const char_type* begin, std::size_t length) \
0071 : base_type(begin, length) { } \
0072 template<int N> \
0073 BOOST_PP_CAT(basic_, name)(Ch (&ar)[N]) \
0074 : base_type(ar) { } \
0075 }; \
0076 typedef BOOST_PP_CAT(basic_, name)<char> name; \
0077 typedef BOOST_PP_CAT(basic_, name)<wchar_t> BOOST_PP_CAT(w, name); \
0078
0079 BOOST_IOSTREAMS_ARRAY(array_source, input_seekable)
0080 BOOST_IOSTREAMS_ARRAY(array_sink, output_seekable)
0081 BOOST_IOSTREAMS_ARRAY(array, seekable)
0082 #undef BOOST_IOSTREAMS_ARRAY
0083
0084
0085
0086
0087 namespace detail {
0088
0089 template<typename Mode, typename Ch>
0090 array_adapter<Mode, Ch>::array_adapter
0091 (char_type* begin, char_type* end)
0092 : begin_(begin), end_(end)
0093 { }
0094
0095 template<typename Mode, typename Ch>
0096 array_adapter<Mode, Ch>::array_adapter
0097 (char_type* begin, std::size_t length)
0098 : begin_(begin), end_(begin + length)
0099 { }
0100
0101 template<typename Mode, typename Ch>
0102 array_adapter<Mode, Ch>::array_adapter
0103 (const char_type* begin, const char_type* end)
0104 : begin_(const_cast<char_type*>(begin)),
0105 end_(const_cast<char_type*>(end))
0106 { BOOST_STATIC_ASSERT((!is_convertible<Mode, output>::value)); }
0107
0108 template<typename Mode, typename Ch>
0109 array_adapter<Mode, Ch>::array_adapter
0110 (const char_type* begin, std::size_t length)
0111 : begin_(const_cast<char_type*>(begin)),
0112 end_(const_cast<char_type*>(begin) + length)
0113 { BOOST_STATIC_ASSERT((!is_convertible<Mode, output>::value)); }
0114
0115 template<typename Mode, typename Ch>
0116 typename array_adapter<Mode, Ch>::pair_type
0117 array_adapter<Mode, Ch>::input_sequence()
0118 { BOOST_STATIC_ASSERT((is_convertible<Mode, input>::value));
0119 return pair_type(begin_, end_); }
0120
0121 template<typename Mode, typename Ch>
0122 typename array_adapter<Mode, Ch>::pair_type
0123 array_adapter<Mode, Ch>::output_sequence()
0124 { BOOST_STATIC_ASSERT((is_convertible<Mode, output>::value));
0125 return pair_type(begin_, end_); }
0126
0127 }
0128
0129
0130
0131 } }
0132
0133 #endif