mqtt_cpp
buffer.hpp
Go to the documentation of this file.
1 // Copyright Takatoshi Kondo 2019
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #if !defined(MQTT_BUFFER_HPP)
8 #define MQTT_BUFFER_HPP
9 
10 #include <stdexcept>
11 #include <utility>
12 #include <type_traits>
13 
14 #include <boost/asio/buffer.hpp>
15 
16 #include <mqtt/namespace.hpp>
17 #include <mqtt/string_view.hpp>
19 #include <mqtt/move.hpp>
20 
21 namespace MQTT_NS {
22 
23 namespace as = boost::asio;
24 
30 class buffer : public string_view {
31 public:
38  explicit constexpr buffer(string_view sv = string_view())
39  : string_view(force_move(sv)) {}
40 
47  explicit buffer(std::string) = delete; // to avoid misuse
48 
56  : string_view(force_move(sv)),
57  lifetime_(force_move(spa)) {
58  }
59 
68  buffer substr(std::size_t offset, std::size_t length = string_view::npos) const& {
69  // range is checked in string_view::substr.
70  return buffer(string_view::substr(offset, length), lifetime_);
71  }
72 
81  buffer substr(std::size_t offset, std::size_t length = string_view::npos) && {
82  // range is checked in string_view::substr.
83  return buffer(string_view::substr(offset, length), force_move(lifetime_));
84  }
85 
91  bool has_life() const {
92  return lifetime_.get();
93  }
94 
95 private:
96  const_shared_ptr_array lifetime_;
97 };
98 
99 inline namespace literals {
100 
113 inline buffer operator""_mb(char const* str, std::size_t length) {
114  return buffer(string_view(str, length));
115 }
116 
117 } // namespace literals
118 
129 template <typename Iterator>
130 inline buffer allocate_buffer(Iterator b, Iterator e) {
131  auto size = static_cast<std::size_t>(std::distance(b, e));
132  auto spa = make_shared_ptr_array(size);
133  std::copy(b, e, spa.get());
134  auto view = string_view(spa.get(), size);
135  return buffer(view, force_move(spa));
136 }
137 
148  return allocate_buffer(sv.begin(), sv.end());
149 }
150 
151 inline buffer const* buffer_sequence_begin(buffer const& buf) {
152  return std::addressof(buf);
153 }
154 
155 inline buffer const* buffer_sequence_end(buffer const& buf) {
156  return std::addressof(buf) + 1;
157 }
158 
159 template <typename Col>
160 inline typename Col::const_iterator buffer_sequence_begin(Col const& col) {
161  return col.cbegin();
162 }
163 
164 template <typename Col>
165 inline typename Col::const_iterator buffer_sequence_end(Col const& col) {
166  return col.cend();
167 }
168 
169 namespace detail {
170 
171 template <typename>
173 
174 template <typename T>
176  T* t,
177  typename std::enable_if<
178  !std::is_same<
179  decltype(buffer_sequence_begin(*t)),
180  void
181  >::value
182  >::type*)
183 )[2];
184 
185 template <typename>
187 
188 template <typename T>
190  T* t,
191  typename std::enable_if<
192  !std::is_same<
193  decltype(buffer_sequence_end(*t)),
194  void
195  >::value
196  >::type*)
197 )[2];
198 
199 template <typename, typename>
201 
202 template <typename T, typename Buffer>
204  T* t,
205  typename std::enable_if<
206  std::is_convertible<
207  decltype(*buffer_sequence_begin(*t)),
208  Buffer
209  >::value
210  >::type*
211 );
212 
213 template <typename T, typename Buffer>
215  : std::integral_constant<bool,
216  sizeof(buffer_sequence_begin_helper<T>(0, 0)) != 1 &&
217  sizeof(buffer_sequence_end_helper<T>(0, 0)) != 1 &&
218  sizeof(buffer_sequence_element_type_helper<T, Buffer>(0, 0)) == 1>
219 {
220 };
221 
222 } // namespace detail
223 
224 template <typename T>
226  std::conditional<
227  std::is_class<T>::value,
228  detail::is_buffer_sequence_class<T, buffer>,
229  std::false_type
230  >::type
231 {
232 };
233 
234 template <>
235 struct is_buffer_sequence<buffer> : std::true_type
236 {
237 };
238 
239 } // namespace MQTT_NS
240 
241 namespace boost {
242 namespace asio {
243 
253 inline const_buffer buffer(MQTT_NS::buffer const& data) {
254  return buffer(data.data(), data.size());
255 }
256 
257 } // namespace asio
258 } // namespace boost
259 
260 #endif // MQTT_BUFFER_HPP
buffer that has string_view interface This class provides string_view interface. This class hold stri...
Definition: buffer.hpp:30
bool has_life() const
check the buffer has lifetime.
Definition: buffer.hpp:91
buffer(std::string)=delete
string constructor (deleted)
buffer substr(std::size_t offset, std::size_t length=string_view::npos) const &
get substring The returned buffer ragnge is the same as std::string_view::substr()....
Definition: buffer.hpp:68
buffer substr(std::size_t offset, std::size_t length=string_view::npos) &&
get substring The returned buffer ragnge is the same as std::string_view::substr()....
Definition: buffer.hpp:81
constexpr buffer(string_view sv=string_view())
string_view constructor
Definition: buffer.hpp:38
buffer(string_view sv, const_shared_ptr_array spa)
string_view and lifetime constructor
Definition: buffer.hpp:55
char buffer_sequence_end_helper(...)
char(& buffer_sequence_element_type_helper(...))[2]
char buffer_sequence_begin_helper(...)
Definition: any.hpp:27
boost::string_ref string_view
Definition: string_view.hpp:64
constexpr std::remove_reference_t< T > && force_move(T &&t)
Definition: move.hpp:20
buffer allocate_buffer(Iterator b, Iterator e)
create buffer from the pair of iterators It copies string that from b to e into shared_ptr_array....
Definition: buffer.hpp:130
buffer const * buffer_sequence_end(buffer const &buf)
Definition: buffer.hpp:155
buffer const * buffer_sequence_begin(buffer const &buf)
Definition: buffer.hpp:151
std::size_t size(basic_message_variant< PacketIdBytes > const &mv)
Definition: message_variant.hpp:93
Definition: buffer.hpp:242
const_buffer buffer(MQTT_NS::buffer const &data)
create boost::asio::const_buffer from the MQTT_NS::buffer boost::asio::const_buffer is a kind of view...
Definition: buffer.hpp:253
Definition: buffer.hpp:241
shared_ptr_array make_shared_ptr_array(std::size_t size)
shared_ptr_array creating function. You can choose the target type.
std::shared_ptr< char const[]> const_shared_ptr_array
Definition: shared_ptr_array.hpp:21
Definition: buffer.hpp:231