string_collection.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file string_collection.h
  3. /// Definition of the string_collection class for the Paho MQTT C++ library.
  4. /// @date April 23, 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_string_collection_h
  23. #define __mqtt_string_collection_h
  24. #include "mqtt/types.h"
  25. #include <vector>
  26. #include <memory>
  27. namespace mqtt {
  28. /////////////////////////////////////////////////////////////////////////////
  29. /**
  30. * Type for a collection of topics.
  31. * This acts like a collection of strings but carries an array of pointers
  32. * to the C strings for easy interactions with the Paho C library.
  33. */
  34. class string_collection
  35. {
  36. /** The type for the collection of strings */
  37. using collection_type = std::vector<string>;
  38. /** The type for the array of C pointers */
  39. using c_arr_type = std::vector<const char*>;
  40. /**
  41. * The collection of strings for the topics.
  42. */
  43. collection_type coll_;
  44. /**
  45. * A colleciton of pointers to NUL-terminated C strings for the topics.
  46. * This is what is required by the Paho C library, and thus the lifetime
  47. * of the pointers will remain consistent with the lifetime of the
  48. * object. The value is kept consistent with the current topics and
  49. * updated whenever topics are added or removed.
  50. */
  51. c_arr_type cArr_;
  52. /**
  53. * Updated the cArr_ object to agree with the values in coll_
  54. * This should be called any time the coll_ variable is modified
  55. * <i>in any way</i>.
  56. */
  57. void update_c_arr();
  58. public:
  59. /** Smart/shared pointer to an object of this type */
  60. using ptr_t = std::shared_ptr<string_collection>;
  61. /** Smart/shared pointer to a const object of this type */
  62. using const_ptr_t = std::shared_ptr<const string_collection>;
  63. /**
  64. * Construct an empty string collection.
  65. */
  66. string_collection() =default;
  67. /**
  68. * Construct a collection initially containing a single string.
  69. * @param str The string
  70. */
  71. string_collection(const string& str);
  72. /**
  73. * Construct a collection initially containing a single string.
  74. * @param str The string
  75. */
  76. string_collection(string&& str);
  77. /**
  78. * Constructs a string collection by copying a vector of strings.
  79. * @param vec A vector of strings.
  80. */
  81. string_collection(const collection_type& vec);
  82. /**
  83. * Constructs a string collection by moving a vector of strings.
  84. * @param vec A vector of strings.
  85. */
  86. string_collection(collection_type&& vec);
  87. /**
  88. * Copy constructor.
  89. * @param coll An existing string collection.
  90. */
  91. string_collection(const string_collection& coll);
  92. /**
  93. * Move constructor.
  94. * @param coll An existing string collection.
  95. */
  96. string_collection(string_collection&& coll) = default;
  97. /**
  98. * Construct a string collection from an initialization list of strings.
  99. * @param sl An initialization list of strings.
  100. */
  101. string_collection(std::initializer_list<string> sl);
  102. /**
  103. * Construct a string collection from an initialization list of C string
  104. * pointers.
  105. * @param sl An initialization list of C character arrays.
  106. */
  107. string_collection(std::initializer_list<const char*> sl);
  108. /**
  109. * Create an empty string collection on the heap.
  110. * @return A smart/shared pointer to a string collection.
  111. */
  112. static ptr_t create(const string& str) {
  113. return std::make_shared<string_collection>(str);
  114. }
  115. /**
  116. * Create a string collection on the heap, initially containing a single
  117. * string.
  118. * @param str The string
  119. * @return A smart/shared pointer to a string collection.
  120. */
  121. static ptr_t create(string&& str) {
  122. return std::make_shared<string_collection>(str);
  123. }
  124. /**
  125. * Creates a string collection on the heap by copying a vector of
  126. * strings.
  127. * @param vec A vector of strings.
  128. */
  129. static ptr_t create(const collection_type& vec) {
  130. return std::make_shared<string_collection>(vec);
  131. }
  132. /**
  133. * Creates a string collection on the heap by copying a vector of
  134. * strings.
  135. * @param vec A vector of strings.
  136. * @return A smart/shared pointer to a string collection.
  137. */
  138. static ptr_t create(collection_type&& vec) {
  139. return std::make_shared<string_collection>(vec);
  140. }
  141. /**
  142. * Create a string collection on the heap from an initialization list of
  143. * strings.
  144. * @param sl An initialization list of strings.
  145. * @return A smart/shared pointer to a string collection.
  146. */
  147. static ptr_t create(std::initializer_list<string> sl) {
  148. return std::make_shared<string_collection>(sl);
  149. }
  150. /**
  151. * Create a string collection on the heap from an initialization list of
  152. * C string pointers.
  153. * @param sl An initialization list of C character arrays.
  154. * @return A smart/shared pointer to a string collection.
  155. */
  156. static ptr_t create(std::initializer_list<const char*> sl) {
  157. return std::make_shared<string_collection>(sl);
  158. }
  159. /**
  160. * Copy assignment.
  161. * Copy another string collection to this one.
  162. * @param coll A string collection
  163. * @return A reference to this collection.
  164. */
  165. string_collection& operator=(const string_collection& coll);
  166. /**
  167. * Move assignment.
  168. * Move another string collection to this one.
  169. * @param coll A string collection
  170. * @return A reference to this collection.
  171. */
  172. string_collection& operator=(string_collection&& coll) = default;
  173. /**
  174. * Determines if the collection is empty.
  175. * @return @em true if the collection is empty, @em false if not.
  176. */
  177. bool empty() const { return coll_.empty(); }
  178. /**
  179. * Gets the number of strings in the collection.
  180. * @return The number of strings in the collection.
  181. */
  182. size_t size() const { return coll_.size(); }
  183. /**
  184. * Copies a string to the back of the collection.
  185. * @param str A string.
  186. */
  187. void push_back(const string& str);
  188. /**
  189. * Moves a string to the back of the collection.
  190. * @param str A string.
  191. */
  192. void push_back(string&& str);
  193. /**
  194. * Removes all the strings from the collection.
  195. */
  196. void clear();
  197. /**
  198. * Gets the n'th string in the collection.
  199. * @param i Index to the desired string.
  200. * @return A const reference to the string.
  201. */
  202. const string& operator[](size_t i) const { return coll_[i]; }
  203. /**
  204. * Gets a pointer to an array of NUL-terminated C string pointers.
  205. * This is the collection type supported by the underlying Paho C
  206. * library. The returned pointer is guaranteed valid so long as the
  207. * object is not updated. The return value may change if the object is
  208. * modified, so the application should not cache the return value, but
  209. * rather request the value when needed.
  210. * @return pointer to an array of NUL-terminated C string pointers of
  211. * the topics in the object.
  212. *
  213. */
  214. char* const* c_arr() const { return (char* const *) cArr_.data(); }
  215. //const char* const* c_arr() const { return cArr_.data(); }
  216. };
  217. /////////////////////////////////////////////////////////////////////////////
  218. /** Smart/shared pointer to a topic collection */
  219. using string_collection_ptr = string_collection::ptr_t;
  220. /** Smart/shared pointer to a const string_collection */
  221. using const_string_collection_ptr = string_collection::const_ptr_t;
  222. /////////////////////////////////////////////////////////////////////////////
  223. // end namespace mqtt
  224. }
  225. #endif // __mqtt_string_collection_h