]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #ifndef BEAST_DETAIL_BUFFER_CONCEPTS_HPP | |
9 | #define BEAST_DETAIL_BUFFER_CONCEPTS_HPP | |
10 | ||
11 | #include <boost/asio/buffer.hpp> | |
12 | #include <iterator> | |
13 | #include <type_traits> | |
14 | ||
15 | namespace beast { | |
16 | namespace detail { | |
17 | ||
18 | // Types that meet the requirements, | |
19 | // for use with std::declval only. | |
20 | template<class BufferType> | |
21 | struct BufferSequence | |
22 | { | |
23 | using value_type = BufferType; | |
24 | using const_iterator = BufferType const*; | |
25 | ~BufferSequence(); | |
26 | BufferSequence(BufferSequence const&) = default; | |
27 | const_iterator begin() const noexcept; | |
28 | const_iterator end() const noexcept; | |
29 | }; | |
30 | using ConstBufferSequence = | |
31 | BufferSequence<boost::asio::const_buffer>; | |
32 | using MutableBufferSequence = | |
33 | BufferSequence<boost::asio::mutable_buffer>; | |
34 | ||
35 | template<class T, class BufferType> | |
36 | class is_BufferSequence | |
37 | { | |
38 | template<class U, class R = std::is_convertible< | |
39 | typename U::value_type, BufferType> > | |
40 | static R check1(int); | |
41 | template<class> | |
42 | static std::false_type check1(...); | |
43 | using type1 = decltype(check1<T>(0)); | |
44 | ||
45 | template<class U, class R = std::is_base_of< | |
46 | #if 0 | |
47 | std::bidirectional_iterator_tag, | |
48 | typename std::iterator_traits< | |
49 | typename U::const_iterator>::iterator_category>> | |
50 | #else | |
51 | // workaround: | |
52 | // boost::asio::detail::consuming_buffers::const_iterator | |
53 | // is not bidirectional | |
54 | std::forward_iterator_tag, | |
55 | typename std::iterator_traits< | |
56 | typename U::const_iterator>::iterator_category>> | |
57 | #endif | |
58 | static R check2(int); | |
59 | template<class> | |
60 | static std::false_type check2(...); | |
61 | using type2 = decltype(check2<T>(0)); | |
62 | ||
63 | template<class U, class R = typename | |
64 | std::is_convertible<decltype( | |
65 | std::declval<U>().begin()), | |
66 | typename U::const_iterator>::type> | |
67 | static R check3(int); | |
68 | template<class> | |
69 | static std::false_type check3(...); | |
70 | using type3 = decltype(check3<T>(0)); | |
71 | ||
72 | template<class U, class R = typename std::is_convertible<decltype( | |
73 | std::declval<U>().end()), | |
74 | typename U::const_iterator>::type> | |
75 | static R check4(int); | |
76 | template<class> | |
77 | static std::false_type check4(...); | |
78 | using type4 = decltype(check4<T>(0)); | |
79 | ||
80 | public: | |
81 | using type = std::integral_constant<bool, | |
82 | std::is_copy_constructible<T>::value && | |
83 | std::is_destructible<T>::value && | |
84 | type1::value && type2::value && | |
85 | type3::value && type4::value>; | |
86 | }; | |
87 | ||
88 | template<class B1, class... Bn> | |
89 | struct is_all_ConstBufferSequence | |
90 | : std::integral_constant<bool, | |
91 | is_BufferSequence<B1, boost::asio::const_buffer>::type::value && | |
92 | is_all_ConstBufferSequence<Bn...>::value> | |
93 | { | |
94 | }; | |
95 | ||
96 | template<class B1> | |
97 | struct is_all_ConstBufferSequence<B1> | |
98 | : is_BufferSequence<B1, boost::asio::const_buffer>::type | |
99 | { | |
100 | }; | |
101 | ||
102 | template<class T> | |
103 | class is_DynamicBuffer | |
104 | { | |
105 | // size() | |
106 | template<class U, class R = std::is_convertible<decltype( | |
107 | std::declval<U const>().size()), std::size_t>> | |
108 | static R check1(int); | |
109 | template<class> | |
110 | static std::false_type check1(...); | |
111 | using type1 = decltype(check1<T>(0)); | |
112 | ||
113 | // max_size() | |
114 | template<class U, class R = std::is_convertible<decltype( | |
115 | std::declval<U const>().max_size()), std::size_t>> | |
116 | static R check2(int); | |
117 | template<class> | |
118 | static std::false_type check2(...); | |
119 | using type2 = decltype(check2<T>(0)); | |
120 | ||
121 | // capacity() | |
122 | template<class U, class R = std::is_convertible<decltype( | |
123 | std::declval<U const>().capacity()), std::size_t>> | |
124 | static R check3(int); | |
125 | template<class> | |
126 | static std::false_type check3(...); | |
127 | using type3 = decltype(check3<T>(0)); | |
128 | ||
129 | // data() | |
130 | template<class U, class R = std::integral_constant< | |
131 | bool, is_BufferSequence<decltype( | |
132 | std::declval<U const>().data()), | |
133 | boost::asio::const_buffer>::type::value>> | |
134 | static R check4(int); | |
135 | template<class> | |
136 | static std::false_type check4(...); | |
137 | using type4 = decltype(check4<T>(0)); | |
138 | ||
139 | // prepare() | |
140 | template<class U, class R = std::integral_constant< | |
141 | bool, is_BufferSequence<decltype( | |
142 | std::declval<U>().prepare(1)), | |
143 | boost::asio::mutable_buffer>::type::value>> | |
144 | static R check5(int); | |
145 | template<class> | |
146 | static std::false_type check5(...); | |
147 | using type5 = decltype(check5<T>(0)); | |
148 | ||
149 | // commit() | |
150 | template<class U, class R = decltype( | |
151 | std::declval<U>().commit(1), std::true_type{})> | |
152 | static R check6(int); | |
153 | template<class> | |
154 | static std::false_type check6(...); | |
155 | using type6 = decltype(check6<T>(0)); | |
156 | ||
157 | // consume | |
158 | template<class U, class R = decltype( | |
159 | std::declval<U>().consume(1), std::true_type{})> | |
160 | static R check7(int); | |
161 | template<class> | |
162 | static std::false_type check7(...); | |
163 | using type7 = decltype(check7<T>(0)); | |
164 | ||
165 | public: | |
166 | using type = std::integral_constant<bool, | |
167 | type1::value | |
168 | && type2::value | |
169 | //&& type3::value // Networking TS | |
170 | && type4::value | |
171 | && type5::value | |
172 | && type6::value | |
173 | && type7::value | |
174 | >; | |
175 | }; | |
176 | ||
177 | } // detail | |
178 | } // beast | |
179 | ||
180 | #endif |