buffer_ref.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file buffer_ref.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_ref_h
  23. #define __mqtt_buffer_ref_h
  24. #include "mqtt/types.h"
  25. #include <iostream>
  26. #include <cstring>
  27. namespace mqtt {
  28. /////////////////////////////////////////////////////////////////////////////
  29. /**
  30. * A reference object for holding immutable data buffers, with cheap copy
  31. * semantics and lifetime management.
  32. *
  33. * Each object of this class contains a reference-counted pointer to an
  34. * immutable data buffer. Objects can be copied freely and easily, even
  35. * across threads, since all instances promise not to modify the contents
  36. * of the buffer.
  37. *
  38. * The buffer is immutable but the reference itself acts like a normal
  39. * variable. It can be reassigned to point to a different buffer.
  40. *
  41. * If no value has been assigned to a reference, then it is in a default
  42. * "null" state. It is not safe to call any member functions on a null
  43. * reference, other than to check if the object is null or empty.
  44. * @verbatim
  45. * string_ref sr;
  46. * if (!sr)
  47. * cout << "null reference" << endl;
  48. * else
  49. * cout.write(sr.data(), sr.size());
  50. * @endverbatim
  51. */
  52. template <typename T>
  53. class buffer_ref
  54. {
  55. public:
  56. /**
  57. * The underlying type for the buffer.
  58. * Normally byte-wide data (char or uint8_t) for Paho.
  59. */
  60. using value_type = T;
  61. /**
  62. * The type for the buffer.
  63. * We use basic_string for compatibility with string data.
  64. */
  65. using blob = std::basic_string<value_type>;
  66. /**
  67. * The pointer we use.
  68. * Note that it is a pointer to a _const_ blob.
  69. */
  70. using pointer_type = std::shared_ptr<const blob>;
  71. private:
  72. /** Our data is a shared pointer to a const buffer */
  73. pointer_type data_;
  74. public:
  75. /**
  76. * Default constructor creates a null reference.
  77. */
  78. buffer_ref() =default;
  79. /**
  80. * Copy constructor only copies a shared pointer.
  81. * @param buf Another buffer reference.
  82. */
  83. buffer_ref(const buffer_ref& buf) =default;
  84. /**
  85. * Move constructor only moves a shared pointer.
  86. * @param buf Another buffer reference.
  87. */
  88. buffer_ref(buffer_ref&& buf) =default;
  89. /**
  90. * Creates a reference to a new buffer by copying data.
  91. * @param b A string from which to create a new buffer.
  92. */
  93. buffer_ref(const blob& b) : data_{std::make_shared<blob>(b)} {}
  94. /**
  95. * Creates a reference to a new buffer by moving a string into the
  96. * buffer.
  97. * @param b A string from which to create a new buffer.
  98. */
  99. buffer_ref(blob&& b) : data_{std::make_shared<blob>(std::move(b))} {}
  100. /**
  101. * Creates a reference to an existing buffer by copying the shared
  102. * pointer.
  103. * Note that it is up to the caller to insure that there are no mutable
  104. * references to the buffer.
  105. * @param p A shared pointer to a string.
  106. */
  107. buffer_ref(const pointer_type& p) : data_(p) {}
  108. /**
  109. * Creates a reference to an existing buffer by moving the shared
  110. * pointer.
  111. * Note that it is up to the caller to insure that there are no mutable
  112. * references to the buffer.
  113. * @param p A shared pointer to a string.
  114. */
  115. buffer_ref(pointer_type&& p) : data_(std::move(p)) {}
  116. /**
  117. * Creates a reference to a new buffer containing a copy of the data.
  118. * @param buf The memory to copy
  119. * @param n The number of bytes to copy.
  120. */
  121. buffer_ref(const value_type* buf, size_t n) : data_{std::make_shared<blob>(buf,n)} {}
  122. /**
  123. * Creates a reference to a new buffer containing a copy of the
  124. * NUL-terminated char array.
  125. * @param buf A NUL-terminated char array (C string).
  126. */
  127. buffer_ref(const char* buf) : buffer_ref(reinterpret_cast<const value_type*>(buf),
  128. std::strlen(buf)) {
  129. static_assert(sizeof(char) == sizeof(T), "can only use C arr with char or byte buffers");
  130. }
  131. /**
  132. * Copy the reference to the buffer.
  133. * @param rhs Another buffer
  134. * @return A reference to this object
  135. */
  136. buffer_ref& operator=(const buffer_ref& rhs) =default;
  137. /**
  138. * Move a reference to a buffer.
  139. * @param rhs The other reference to move.
  140. * @return A reference to this object.
  141. */
  142. buffer_ref& operator=(buffer_ref&& rhs) =default;
  143. /**
  144. * Copy a string into this object, creating a new buffer.
  145. * Modifies the reference for this object, pointing it to a
  146. * newly-created buffer. Other references to the old object remain
  147. * unchanges, so this follows copy-on-write semantics.
  148. * @param b A new blob/string to copy.
  149. * @return A reference to this object.
  150. */
  151. buffer_ref& operator=(const blob& b) {
  152. data_.reset(new blob(b));
  153. return *this;
  154. }
  155. /**
  156. * Move a string into this object, creating a new buffer.
  157. * Modifies the reference for this object, pointing it to a
  158. * newly-created buffer. Other references to the old object remain
  159. * unchanges, so this follows copy-on-write semantics.
  160. * @param b A new blob/string to move.
  161. * @return A reference to this object.
  162. */
  163. buffer_ref& operator=(blob&& b) {
  164. data_.reset(new blob(std::move(b)));
  165. return *this;
  166. }
  167. /**
  168. * Copy a NUL-terminated C char array into a new buffer
  169. * @param cstr A NUL-terminated C string.
  170. * @return A reference to this object
  171. */
  172. buffer_ref& operator=(const char* cstr) {
  173. static_assert(sizeof(char) == sizeof(T), "can only use C arr with char or byte buffers");
  174. data_.reset(new blob(reinterpret_cast<const value_type*>(cstr), strlen(cstr)));
  175. return *this;
  176. }
  177. /**
  178. * Copy another type of buffer reference to this one.
  179. * This can copy a buffer of different types, provided that the size of
  180. * the data elements are the same. This is typically used to convert
  181. * from char to byte, where the data is the same, but the interpretation
  182. * is different. Note that this copies the underlying buffer.
  183. * @param rhs A reference to a different type of buffer.
  184. * @return A reference to this object.
  185. */
  186. template <typename OT>
  187. buffer_ref& operator=(const buffer_ref<OT>& rhs) {
  188. static_assert(sizeof(OT) == sizeof(T), "Can only assign buffers if values the same size");
  189. data_.reset(new blob(reinterpret_cast<const value_type*>(rhs.data()), rhs.size()));
  190. return *this;
  191. }
  192. /**
  193. * Clears the reference to nil.
  194. */
  195. void reset() { data_.reset(); }
  196. /**
  197. * Determines if the reference is valid.
  198. * If the reference is invalid then it is not safe to call @em any
  199. * member functions other than @ref is_null() and @ref empty()
  200. * @return @em true if referring to a valid buffer, @em false if the
  201. * reference (pointer) is null.
  202. */
  203. explicit operator bool() const { return bool(data_); }
  204. /**
  205. * Determines if the reference is invalid.
  206. * If the reference is invalid then it is not safe to call @em any
  207. * member functions other than @ref is_null() and @ref empty()
  208. * @return @em true if the reference is null, @em false if it is
  209. * referring to a valid buffer,
  210. */
  211. bool is_null() const { return !data_; }
  212. /**
  213. * Determines if the buffer is empty.
  214. * @return @em true if the buffer is empty or thr reference is null, @em
  215. * false if the buffer contains data.
  216. */
  217. bool empty() const { return !data_ || data_->empty(); }
  218. /**
  219. * Gets a const pointer to the data buffer.
  220. * @return A pointer to the data buffer.
  221. */
  222. const value_type* data() const { return data_->data(); }
  223. /**
  224. * Gets the size of the data buffer.
  225. * @return The size of the data buffer.
  226. */
  227. size_t size() const { return data_->size(); }
  228. /**
  229. * Gets the size of the data buffer.
  230. * @return The size of the data buffer.
  231. */
  232. size_t length() const { return data_->length(); }
  233. /**
  234. * Gets the data buffer as a string.
  235. * @return The data buffer as a string.
  236. */
  237. const blob& str() const { return *data_; }
  238. /**
  239. * Gets the data buffer as a string.
  240. * @return The data buffer as a string.
  241. */
  242. const blob& to_string() const { return str(); }
  243. /**
  244. * Gets the data buffer as NUL-terminated C string.
  245. * Note that the reference must be set to call this function.
  246. * @return The data buffer as a string.
  247. */
  248. const char* c_str() const { return data_->c_str(); }
  249. /**
  250. * Gets a shared pointer to the (const) data buffer.
  251. * @return A shared pointer to the (const) data buffer.
  252. */
  253. const pointer_type& ptr() const { return data_; }
  254. /**
  255. * Gets elemental access to the data buffer (read only)
  256. * @param i The index into the buffer.
  257. * @return The value at the specified index.
  258. */
  259. const value_type& operator[](size_t i) const { return (*data_)[i]; }
  260. };
  261. /**
  262. * Stream inserter for a buffer reference.
  263. * This does a binary write of the data in the buffer.
  264. * @param os The output stream.
  265. * @param buf The buffer reference to write.
  266. * @return A reference to the output stream.
  267. */
  268. template <typename T>
  269. std::ostream& operator<<(std::ostream& os, const buffer_ref<T>& buf) {
  270. if (!buf.empty())
  271. os.write(buf.data(), buf.size());
  272. return os;
  273. }
  274. /////////////////////////////////////////////////////////////////////////////
  275. /**
  276. * A refernce to a text buffer.
  277. */
  278. using string_ref = buffer_ref<char>;
  279. /**
  280. * A reference to a binary buffer.
  281. * Note that we're using char for the underlying data type to allow
  282. * efficient moves to and from std::string's. Using a separate type
  283. * indicates that the data may be arbitrary binary.
  284. */
  285. using binary_ref = buffer_ref<char>;
  286. /////////////////////////////////////////////////////////////////////////////
  287. // end namespace mqtt
  288. }
  289. #endif // __mqtt_buffer_ref_h