Skip to content

Commit 1980bd3

Browse files
committed
Added cbegin() and cend() to string_collection, and fixed docs for the class.
1 parent a513e06 commit 1980bd3

File tree

1 file changed

+46
-26
lines changed

1 file changed

+46
-26
lines changed

src/mqtt/string_collection.h

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,38 @@ namespace mqtt {
3535
/////////////////////////////////////////////////////////////////////////////
3636

3737
/**
38-
* Type for a collection of topics.
39-
* This acts like a collection of strings but carries an array of pointers
40-
* to the C strings for easy interactions with the Paho C library.
38+
* Type for a collection of strings, such as MQTT topics.
39+
*
40+
* This acts like a standard collection of strings but carries an array of
41+
* pointers to the C strings for easy interactions with the Paho C library.
4142
*/
4243
class string_collection
4344
{
45+
public:
4446
/** The type for the collection of strings */
4547
using collection_type = std::vector<string>;
48+
/** Iterator over const items */
49+
using const_iterator = collection_type::const_iterator;
50+
51+
/** Smart/shared pointer to an object of this type */
52+
using ptr_t = std::shared_ptr<string_collection>;
53+
/** Smart/shared pointer to a const object of this type */
54+
using const_ptr_t = std::shared_ptr<const string_collection>;
55+
56+
private:
4657
/** The type for the array of C pointers */
4758
using c_arr_type = std::vector<const char*>;
4859

4960
/**
50-
* The collection of strings for the topics.
61+
* The collection of strings.
5162
*/
5263
collection_type coll_;
5364
/**
54-
* A collection of pointers to NUL-terminated C strings for the topics.
65+
* A collection of pointers to NUL-terminated C strings.
5566
* This is what is required by the Paho C library, and thus the lifetime
5667
* of the pointers will remain consistent with the lifetime of the
57-
* object. The value is kept consistent with the current topics and
58-
* updated whenever topics are added or removed.
68+
* object. The value is kept consistent with the current stringss and
69+
* updated whenever strings are added or removed.
5970
*/
6071
c_arr_type cArr_;
6172
/**
@@ -66,11 +77,6 @@ class string_collection
6677
void update_c_arr();
6778

6879
public:
69-
/** Smart/shared pointer to an object of this type */
70-
using ptr_t = std::shared_ptr<string_collection>;
71-
/** Smart/shared pointer to a const object of this type */
72-
using const_ptr_t = std::shared_ptr<const string_collection>;
73-
7480
/**
7581
* Construct an empty string collection.
7682
*/
@@ -104,7 +110,7 @@ class string_collection
104110
* Move constructor.
105111
* @param coll An existing string collection.
106112
*/
107-
string_collection(string_collection&& coll) = default;
113+
string_collection(string_collection&& coll) =default;
108114
/**
109115
* Construct a string collection from an initialization list of strings.
110116
* @param sl An initialization list of strings.
@@ -180,7 +186,27 @@ class string_collection
180186
* @param coll A string collection
181187
* @return A reference to this collection.
182188
*/
183-
string_collection& operator=(string_collection&& coll) = default;
189+
string_collection& operator=(string_collection&& coll) =default;
190+
/**
191+
* Gets a const iterator to the begining of the collecion.
192+
* @return A const iterator to the begining of the collecion.
193+
*/
194+
const_iterator begin() const { return coll_.begin(); }
195+
/**
196+
* Gets a const iterator to the end of the collecion.
197+
* @return A const iterator to the end of the collecion.
198+
*/
199+
const_iterator end() const { return coll_.end(); }
200+
/**
201+
* Gets a const iterator to the begining of the collecion.
202+
* @return A const iterator to the begining of the collecion.
203+
*/
204+
const_iterator cbegin() const { return coll_.cbegin(); }
205+
/**
206+
* Gets a const iterator to the end of the collecion.
207+
* @return A const iterator to the end of the collecion.
208+
*/
209+
const_iterator cend() const { return coll_.cend(); }
184210
/**
185211
* Determines if the collection is empty.
186212
* @return @em true if the collection is empty, @em false if not.
@@ -192,12 +218,12 @@ class string_collection
192218
*/
193219
size_t size() const { return coll_.size(); }
194220
/**
195-
* Copies a string to the back of the collection.
221+
* Copies a string onto the back of the collection.
196222
* @param str A string.
197223
*/
198224
void push_back(const string& str);
199225
/**
200-
* Moves a string to the back of the collection.
226+
* Moves a string onto the back of the collection.
201227
* @param str A string.
202228
*/
203229
void push_back(string&& str);
@@ -219,21 +245,15 @@ class string_collection
219245
* modified, so the application should not cache the return value, but
220246
* rather request the value when needed.
221247
* @return pointer to an array of NUL-terminated C string pointers of
222-
* the topics in the object.
248+
* the C++ strings in the object.
223249
*
224250
*/
225251
char* const* c_arr() const { return (char* const *) cArr_.data(); }
226-
227-
using const_iterator = collection_type::const_iterator;
228-
229-
const_iterator begin() const { return coll_.begin(); }
230-
231-
const_iterator end() const { return coll_.end(); }
232252
};
233253

234254
/////////////////////////////////////////////////////////////////////////////
235255

236-
/** Smart/shared pointer to a topic collection */
256+
/** Smart/shared pointer to a string collection */
237257
using string_collection_ptr = string_collection::ptr_t;
238258

239259
/** Smart/shared pointer to a const string_collection */
@@ -303,7 +323,7 @@ class name_value_collection
303323
* Move constructor.
304324
* @param other Another collection of name/value pairs
305325
*/
306-
name_value_collection(name_value_collection&& other) = default;
326+
name_value_collection(name_value_collection&& other) =default;
307327
/**
308328
* Constructs the collection with an initializer list.
309329
*
@@ -330,7 +350,7 @@ class name_value_collection
330350
* Move constructor.
331351
* @param other Another collection of name/value pairs
332352
*/
333-
name_value_collection& operator=(name_value_collection&& other) = default;
353+
name_value_collection& operator=(name_value_collection&& other) =default;
334354
/**
335355
* Determines if the collection is empty.
336356
* @return @em true if the container is empty, @em false if it contains

0 commit comments

Comments
 (0)