string_view.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #if !defined(MQTT_STRING_VIEW_HPP)
  7. #define MQTT_STRING_VIEW_HPP
  8. #include <iterator>
  9. #include <mqtt/namespace.hpp>
  10. #ifdef MQTT_STD_STRING_VIEW
  11. #include <string_view>
  12. namespace MQTT_NS {
  13. using std::string_view;
  14. using std::basic_string_view;
  15. } // namespace MQTT_NS
  16. #else // MQTT_STD_STRING_VIEW
  17. #include <boost/version.hpp>
  18. #include <boost/utility/string_view.hpp>
  19. #include <boost/container_hash/hash_fwd.hpp>
  20. namespace MQTT_NS {
  21. using string_view = boost::string_view;
  22. template<class CharT, class Traits = std::char_traits<CharT> >
  23. using basic_string_view = boost::basic_string_view<CharT, Traits>;
  24. } // namespace MQTT_NS
  25. #endif // !defined(MQTT_STD_STRING_VIEW)
  26. namespace MQTT_NS {
  27. namespace detail {
  28. template<class T>
  29. T* to_address(T* p) noexcept
  30. {
  31. return p;
  32. }
  33. template<class T>
  34. auto to_address(const T& p) noexcept
  35. {
  36. return detail::to_address(p.operator->());
  37. }
  38. } // namespace detail
  39. // Make a string_view from a pair of iterators.
  40. template<typename Begin, typename End>
  41. string_view make_string_view(Begin begin, End end) {
  42. return string_view(detail::to_address(begin), static_cast<string_view::size_type>(std::distance(begin, end)));
  43. }
  44. } // namespace MQTT_NS
  45. #endif // MQTT_STRING_VIEW_HPP