blob: f4eb39ee2de4f987a02351bb2e8ed60860190de2 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001// (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
6// See http://www.boost.org/libs/iostreams for documentation.
7
8#ifndef NDNBOOST_IOSTREAMS_ARRAY_HPP_INCLUDED
9#define NDNBOOST_IOSTREAMS_ARRAY_HPP_INCLUDED
10
11#if defined(_MSC_VER) && (_MSC_VER >= 1020)
12# pragma once
13#endif
14
15#include <ndnboost/config.hpp> // NDNBOOST_MSVC, make sure size_t is in std.
16#include <ndnboost/detail/workaround.hpp>
17#include <cstddef> // std::size_t.
18#include <utility> // pair.
19#include <ndnboost/iostreams/categories.hpp>
20#include <ndnboost/preprocessor/cat.hpp>
21#include <ndnboost/static_assert.hpp>
22#include <ndnboost/type_traits/is_convertible.hpp>
23#include <ndnboost/type_traits/is_same.hpp>
24
25namespace ndnboost { namespace iostreams {
26
27namespace detail {
28
29template<typename Mode, typename Ch>
30class array_adapter {
31public:
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 !NDNBOOST_WORKAROUND(NDNBOOST_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();
51private:
52 char_type* begin_;
53 char_type* end_;
54};
55
56} // End namespace detail.
57
58// Local macros, #undef'd below.
59#if !NDNBOOST_WORKAROUND(NDNBOOST_MSVC, <= 1300)
60# define NDNBOOST_IOSTREAMS_ARRAY_CTOR(name, ch) \
61 template<int N> \
62 NDNBOOST_PP_CAT(basic_, name)(ch (&ar)[N]) \
63 : base_type(ar) { } \
64 /**/
65#else
66# define NDNBOOST_IOSTREAMS_ARRAY_CTOR(name, ch)
67#endif
68#define NDNBOOST_IOSTREAMS_ARRAY(name, mode) \
69 template<typename Ch> \
70 struct NDNBOOST_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 NDNBOOST_PP_CAT(basic_, name)(char_type* begin, char_type* end) \
77 : base_type(begin, end) { } \
78 NDNBOOST_PP_CAT(basic_, name)(char_type* begin, std::size_t length) \
79 : base_type(begin, length) { } \
80 NDNBOOST_PP_CAT(basic_, name)(const char_type* begin, const char_type* end) \
81 : base_type(begin, end) { } \
82 NDNBOOST_PP_CAT(basic_, name)(const char_type* begin, std::size_t length) \
83 : base_type(begin, length) { } \
84 NDNBOOST_IOSTREAMS_ARRAY_CTOR(name, Ch) \
85 }; \
86 typedef NDNBOOST_PP_CAT(basic_, name)<char> name; \
87 typedef NDNBOOST_PP_CAT(basic_, name)<wchar_t> NDNBOOST_PP_CAT(w, name); \
88 /**/
89NDNBOOST_IOSTREAMS_ARRAY(array_source, input_seekable)
90NDNBOOST_IOSTREAMS_ARRAY(array_sink, output_seekable)
91NDNBOOST_IOSTREAMS_ARRAY(array, seekable)
92#undef NDNBOOST_IOSTREAMS_ARRAY_CTOR
93#undef NDNBOOST_IOSTREAMS_ARRAY
94
95
96//------------------Implementation of array_adapter---------------------------//
97
98namespace detail {
99
100template<typename Mode, typename Ch>
101array_adapter<Mode, Ch>::array_adapter
102 (char_type* begin, char_type* end)
103 : begin_(begin), end_(end)
104 { }
105
106template<typename Mode, typename Ch>
107array_adapter<Mode, Ch>::array_adapter
108 (char_type* begin, std::size_t length)
109 : begin_(begin), end_(begin + length)
110 { }
111
112template<typename Mode, typename Ch>
113array_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{ NDNBOOST_STATIC_ASSERT((!is_convertible<Mode, output>::value)); }
118
119template<typename Mode, typename Ch>
120array_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{ NDNBOOST_STATIC_ASSERT((!is_convertible<Mode, output>::value)); }
125
126template<typename Mode, typename Ch>
127typename array_adapter<Mode, Ch>::pair_type
128array_adapter<Mode, Ch>::input_sequence()
129{ NDNBOOST_STATIC_ASSERT((is_convertible<Mode, input>::value));
130 return pair_type(begin_, end_); }
131
132template<typename Mode, typename Ch>
133typename array_adapter<Mode, Ch>::pair_type
134array_adapter<Mode, Ch>::output_sequence()
135{ NDNBOOST_STATIC_ASSERT((is_convertible<Mode, output>::value));
136 return pair_type(begin_, end_); }
137
138} // End namespace detail.
139
140//----------------------------------------------------------------------------//
141
142} } // End namespaces iostreams, boost.
143
144#endif // #ifndef NDNBOOST_IOSTREAMS_ARRAY_HPP_INCLUDED