buffer_view.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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-2020 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 v2.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-v20.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 <iostream>
  25. #include "mqtt/types.h"
  26. namespace mqtt {
  27. /////////////////////////////////////////////////////////////////////////////
  28. /**
  29. * A reference to a contiguous sequence of items, with no ownership.
  30. * This simply contains a pointer to a const array of items, and a size.
  31. * This is a similar, but simplified version of the std::string_view
  32. * class(es) in the C++17 standard.
  33. */
  34. template <typename T>
  35. class buffer_view
  36. {
  37. public:
  38. /** The type of items to be held in the queue. */
  39. using value_type = T;
  40. /** The type used to specify number of items in the container. */
  41. using size_type = size_t;
  42. private:
  43. /** Const pointer to the data array */
  44. const value_type* data_;
  45. /** The size of the array */
  46. size_type sz_;
  47. public:
  48. /**
  49. * Constructs a buffer view.
  50. * @param data The data pointer
  51. * @param n The number of items
  52. */
  53. buffer_view(const value_type* data, size_type n) : 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(
  95. sizeof(char) == sizeof(T), "can only get string for char or byte buffers"
  96. );
  97. return string(reinterpret_cast<const char*>(data_), sz_);
  98. }
  99. };
  100. /**
  101. * Stream inserter for a buffer view.
  102. * This does a binary write of the data in the buffer.
  103. * @param os The output stream.
  104. * @param buf The buffer reference to write.
  105. * @return A reference to the output stream.
  106. */
  107. template <typename T>
  108. std::ostream& operator<<(std::ostream& os, const buffer_view<T>& buf) {
  109. if (buf.size() > 0)
  110. os.write(buf.data(), sizeof(T) * buf.size());
  111. return os;
  112. }
  113. /** A buffer view for character string data. */
  114. using string_view = buffer_view<char>;
  115. /** A buffer view for binary data */
  116. using binary_view = buffer_view<char>;
  117. /////////////////////////////////////////////////////////////////////////////
  118. // end namespace mqtt
  119. } // namespace mqtt
  120. #endif // __mqtt_buffer_view_h