async_mqtt 5.0.0
Loading...
Searching...
No Matches
string_view.hpp
1// Copyright Takatoshi Kondo 2016
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(ASYNC_MQTT_UTIL_STRING_VIEW_HPP)
8#define ASYNC_MQTT_UTIL_STRING_VIEW_HPP
9
10#include <iterator>
11#include <string_view>
12#include <type_traits>
13
14namespace async_mqtt {
15
16using std::string_view;
17using std::basic_string_view;
18
19namespace detail {
20
21template<class T>
22T* to_address(T* p) noexcept
23{
24 return p;
25}
26
27template<class T>
28auto to_address(const T& p) noexcept
29{
30 return detail::to_address(p.operator->());
31}
32
33} // namespace detail
34
35// Make a string_view from a pair of iterators.
36template<typename Begin, typename End>
37string_view make_string_view(Begin begin, End end) {
38 static_assert(
39 std::is_same_v<
40 typename std::iterator_traits<Begin>::iterator_category,
41 std::random_access_iterator_tag
42 >
43 );
44 return string_view(detail::to_address(begin), static_cast<string_view::size_type>(std::distance(begin, end)));
45}
46
47} // namespace async_mqtt
48
49#endif // ASYNC_MQTT_UTIL_STRING_VIEW_HPP