buffer_view.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file buffer_view.h
  3. /// Buffer reference type for the Paho MQTT C++ library.
  4. /// @date April 18, 2017
  5. /// @author Frank Pagliughi
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2017 Frank Pagliughi <fpagliughi@mindspring.com>
  9. *
  10. * All rights reserved. This program and the accompanying materials
  11. * are made available under the terms of the Eclipse Public License v1.0
  12. * and Eclipse Distribution License v1.0 which accompany this distribution.
  13. *
  14. * The Eclipse Public License is available at
  15. * http://www.eclipse.org/legal/epl-v10.html
  16. * and the Eclipse Distribution License is available at
  17. * http://www.eclipse.org/org/documents/edl-v10.php.
  18. *
  19. * Contributors:
  20. * Frank Pagliughi - initial implementation and documentation
  21. *******************************************************************************/
  22. #ifndef __mqtt_buffer_view_h
  23. #define __mqtt_buffer_view_h
  24. #include "mqtt/types.h"
  25. namespace mqtt {
  26. /////////////////////////////////////////////////////////////////////////////
  27. /**
  28. * A reference to a contiguous sequence of items, with no ownership.
  29. * This simply contains a pointer to a const array of items, and a size.
  30. * This is a similar, but simplified version of the std::string_view
  31. * class(es) in the C++17 standard.
  32. */
  33. template <typename T>
  34. class buffer_view
  35. {
  36. public:
  37. /** The type of items to be held in the queue. */
  38. using value_type = T;
  39. /** The type used to specify number of items in the container. */
  40. using size_type = size_t;
  41. private:
  42. /** Const pointer to the data array */
  43. const value_type* data_;
  44. /** The size of the array */
  45. size_type sz_;
  46. public:
  47. /**
  48. * Constructs a buffer view.
  49. * @param data The data pointer
  50. * @param n The number of items
  51. */
  52. buffer_view(const value_type* data, size_type n)
  53. : data_(data), sz_(n) {}
  54. /**
  55. * Constructs a buffer view to a whole string.
  56. * This the starting pointer and length of the whole string.
  57. * @param str The string.
  58. */
  59. buffer_view(const std::basic_string<value_type>& str)
  60. : data_(str.data()), sz_(str.size()) {}
  61. /**
  62. * Gets a pointer the first item in the view.
  63. * @return A const pointer the first item in the view.
  64. */
  65. const value_type* data() const { return data_; }
  66. /**
  67. * Gets the number of items in the view.
  68. * @return The number of items in the view.
  69. */
  70. size_type size() const { return sz_; }
  71. /**
  72. * Gets the number of items in the view.
  73. * @return The number of items in the view.
  74. */
  75. size_type length() const { return sz_; }
  76. /**
  77. * Access an item in the view.
  78. * @param i The index of the item.
  79. * @return A const reference to the requested item.
  80. */
  81. const value_type& operator[](size_t i) const { return data_[i]; }
  82. /**
  83. * Gets a copy of the view as a string.
  84. * @return A copy of the view as a string.
  85. */
  86. std::basic_string<value_type> str() const {
  87. return std::basic_string<value_type>(data_, sz_);
  88. }
  89. /**
  90. * Gets a copy of the view as a string.
  91. * @return A copy of the view as a string.
  92. */
  93. string to_string() const {
  94. static_assert(sizeof(char) == sizeof(T), "can only get string for char or byte buffers");
  95. return string(reinterpret_cast<const char*>(data_), sz_);
  96. }
  97. };
  98. /** A buffer view for character string data. */
  99. using string_view = buffer_view<char>;
  100. /** A buffer view for binary data */
  101. using binary_view = buffer_view<char>;
  102. /////////////////////////////////////////////////////////////////////////////
  103. // end namespace mqtt
  104. }
  105. #endif // __mqtt_buffer_view_h