buffer_view.h 4.0 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 "mqtt/types.h"
  25. #include <iostream>
  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)
  54. : data_(data), sz_(n) {}
  55. /**
  56. * Constructs a buffer view to a whole string.
  57. * This the starting pointer and length of the whole string.
  58. * @param str The string.
  59. */
  60. buffer_view(const std::basic_string<value_type>& str)
  61. : data_(str.data()), sz_(str.size()) {}
  62. /**
  63. * Gets a pointer the first item in the view.
  64. * @return A const pointer the first item in the view.
  65. */
  66. const value_type* data() const { return data_; }
  67. /**
  68. * Gets the number of items in the view.
  69. * @return The number of items in the view.
  70. */
  71. size_type size() const { return sz_; }
  72. /**
  73. * Gets the number of items in the view.
  74. * @return The number of items in the view.
  75. */
  76. size_type length() const { return sz_; }
  77. /**
  78. * Access an item in the view.
  79. * @param i The index of the item.
  80. * @return A const reference to the requested item.
  81. */
  82. const value_type& operator[](size_t i) const { return data_[i]; }
  83. /**
  84. * Gets a copy of the view as a string.
  85. * @return A copy of the view as a string.
  86. */
  87. std::basic_string<value_type> str() const {
  88. return std::basic_string<value_type>(data_, sz_);
  89. }
  90. /**
  91. * Gets a copy of the view as a string.
  92. * @return A copy of the view as a string.
  93. */
  94. string to_string() const {
  95. static_assert(sizeof(char) == sizeof(T), "can only get string for char or byte buffers");
  96. return string(reinterpret_cast<const char*>(data_), sz_);
  97. }
  98. };
  99. /**
  100. * Stream inserter for a buffer view.
  101. * This does a binary write of the data in the buffer.
  102. * @param os The output stream.
  103. * @param buf The buffer reference to write.
  104. * @return A reference to the output stream.
  105. */
  106. template <typename T>
  107. std::ostream& operator<<(std::ostream& os, const buffer_view<T>& buf) {
  108. if (buf.size() > 0)
  109. os.write(buf.data(), sizeof(T)*buf.size());
  110. return os;
  111. }
  112. /** A buffer view for character string data. */
  113. using string_view = buffer_view<char>;
  114. /** A buffer view for binary data */
  115. using binary_view = buffer_view<char>;
  116. /////////////////////////////////////////////////////////////////////////////
  117. // end namespace mqtt
  118. }
  119. #endif // __mqtt_buffer_view_h