array.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. #ifndef BOOST_IOSTREAMS_ARRAY_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_ARRAY_HPP_INCLUDED
  8. #if defined(_MSC_VER)
  9. # pragma once
  10. #endif
  11. #include <boost/config.hpp> // BOOST_MSVC, make sure size_t is in std.
  12. #include <boost/detail/workaround.hpp>
  13. #include <cstddef> // std::size_t.
  14. #include <utility> // pair.
  15. #include <boost/iostreams/categories.hpp>
  16. #include <boost/preprocessor/cat.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/type_traits/is_convertible.hpp>
  19. #include <boost/type_traits/is_same.hpp>
  20. namespace boost { namespace iostreams {
  21. namespace detail {
  22. template<typename Mode, typename Ch>
  23. class array_adapter {
  24. public:
  25. typedef Ch char_type;
  26. typedef std::pair<char_type*, char_type*> pair_type;
  27. struct category
  28. : public Mode,
  29. public device_tag,
  30. public direct_tag
  31. { };
  32. array_adapter(char_type* begin, char_type* end);
  33. array_adapter(char_type* begin, std::size_t length);
  34. array_adapter(const char_type* begin, const char_type* end);
  35. array_adapter(const char_type* begin, std::size_t length);
  36. template<int N>
  37. array_adapter(char_type (&ar)[N])
  38. : begin_(ar), end_(ar + N)
  39. { }
  40. pair_type input_sequence();
  41. pair_type output_sequence();
  42. private:
  43. char_type* begin_;
  44. char_type* end_;
  45. };
  46. } // End namespace detail.
  47. #define BOOST_IOSTREAMS_ARRAY(name, mode) \
  48. template<typename Ch> \
  49. struct BOOST_PP_CAT(basic_, name) : detail::array_adapter<mode, Ch> { \
  50. private: \
  51. typedef detail::array_adapter<mode, Ch> base_type; \
  52. public: \
  53. typedef typename base_type::char_type char_type; \
  54. typedef typename base_type::category category; \
  55. BOOST_PP_CAT(basic_, name)(char_type* begin, char_type* end) \
  56. : base_type(begin, end) { } \
  57. BOOST_PP_CAT(basic_, name)(char_type* begin, std::size_t length) \
  58. : base_type(begin, length) { } \
  59. BOOST_PP_CAT(basic_, name)(const char_type* begin, const char_type* end) \
  60. : base_type(begin, end) { } \
  61. BOOST_PP_CAT(basic_, name)(const char_type* begin, std::size_t length) \
  62. : base_type(begin, length) { } \
  63. template<int N> \
  64. BOOST_PP_CAT(basic_, name)(Ch (&ar)[N]) \
  65. : base_type(ar) { } \
  66. }; \
  67. typedef BOOST_PP_CAT(basic_, name)<char> name; \
  68. typedef BOOST_PP_CAT(basic_, name)<wchar_t> BOOST_PP_CAT(w, name); \
  69. /**/
  70. BOOST_IOSTREAMS_ARRAY(array_source, input_seekable)
  71. BOOST_IOSTREAMS_ARRAY(array_sink, output_seekable)
  72. BOOST_IOSTREAMS_ARRAY(array, seekable)
  73. #undef BOOST_IOSTREAMS_ARRAY
  74. //------------------Implementation of array_adapter---------------------------//
  75. namespace detail {
  76. template<typename Mode, typename Ch>
  77. array_adapter<Mode, Ch>::array_adapter
  78. (char_type* begin, char_type* end)
  79. : begin_(begin), end_(end)
  80. { }
  81. template<typename Mode, typename Ch>
  82. array_adapter<Mode, Ch>::array_adapter
  83. (char_type* begin, std::size_t length)
  84. : begin_(begin), end_(begin + length)
  85. { }
  86. template<typename Mode, typename Ch>
  87. array_adapter<Mode, Ch>::array_adapter
  88. (const char_type* begin, const char_type* end)
  89. : begin_(const_cast<char_type*>(begin)), // Treated as read-only.
  90. end_(const_cast<char_type*>(end)) // Treated as read-only.
  91. { BOOST_STATIC_ASSERT((!is_convertible<Mode, output>::value)); }
  92. template<typename Mode, typename Ch>
  93. array_adapter<Mode, Ch>::array_adapter
  94. (const char_type* begin, std::size_t length)
  95. : begin_(const_cast<char_type*>(begin)), // Treated as read-only.
  96. end_(const_cast<char_type*>(begin) + length) // Treated as read-only.
  97. { BOOST_STATIC_ASSERT((!is_convertible<Mode, output>::value)); }
  98. template<typename Mode, typename Ch>
  99. typename array_adapter<Mode, Ch>::pair_type
  100. array_adapter<Mode, Ch>::input_sequence()
  101. { BOOST_STATIC_ASSERT((is_convertible<Mode, input>::value));
  102. return pair_type(begin_, end_); }
  103. template<typename Mode, typename Ch>
  104. typename array_adapter<Mode, Ch>::pair_type
  105. array_adapter<Mode, Ch>::output_sequence()
  106. { BOOST_STATIC_ASSERT((is_convertible<Mode, output>::value));
  107. return pair_type(begin_, end_); }
  108. } // End namespace detail.
  109. //----------------------------------------------------------------------------//
  110. } } // End namespaces iostreams, boost.
  111. #endif // #ifndef BOOST_IOSTREAMS_ARRAY_HPP_INCLUDED