Skip to content

Commit

Permalink
Added cbegin() and cend() to string_collection, and fixed docs for th…
Browse files Browse the repository at this point in the history
…e class.
  • Loading branch information
fpagliughi committed Dec 9, 2023
1 parent a513e06 commit 1980bd3
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions src/mqtt/string_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,38 @@ namespace mqtt {
/////////////////////////////////////////////////////////////////////////////

/**
* Type for a collection of topics.
* This acts like a collection of strings but carries an array of pointers
* to the C strings for easy interactions with the Paho C library.
* Type for a collection of strings, such as MQTT topics.
*
* This acts like a standard collection of strings but carries an array of
* pointers to the C strings for easy interactions with the Paho C library.
*/
class string_collection
{
public:
/** The type for the collection of strings */
using collection_type = std::vector<string>;
/** Iterator over const items */
using const_iterator = collection_type::const_iterator;

/** Smart/shared pointer to an object of this type */
using ptr_t = std::shared_ptr<string_collection>;
/** Smart/shared pointer to a const object of this type */
using const_ptr_t = std::shared_ptr<const string_collection>;

private:
/** The type for the array of C pointers */
using c_arr_type = std::vector<const char*>;

/**
* The collection of strings for the topics.
* The collection of strings.
*/
collection_type coll_;
/**
* A collection of pointers to NUL-terminated C strings for the topics.
* A collection of pointers to NUL-terminated C strings.
* This is what is required by the Paho C library, and thus the lifetime
* of the pointers will remain consistent with the lifetime of the
* object. The value is kept consistent with the current topics and
* updated whenever topics are added or removed.
* object. The value is kept consistent with the current stringss and
* updated whenever strings are added or removed.
*/
c_arr_type cArr_;
/**
Expand All @@ -66,11 +77,6 @@ class string_collection
void update_c_arr();

public:
/** Smart/shared pointer to an object of this type */
using ptr_t = std::shared_ptr<string_collection>;
/** Smart/shared pointer to a const object of this type */
using const_ptr_t = std::shared_ptr<const string_collection>;

/**
* Construct an empty string collection.
*/
Expand Down Expand Up @@ -104,7 +110,7 @@ class string_collection
* Move constructor.
* @param coll An existing string collection.
*/
string_collection(string_collection&& coll) = default;
string_collection(string_collection&& coll) =default;
/**
* Construct a string collection from an initialization list of strings.
* @param sl An initialization list of strings.
Expand Down Expand Up @@ -180,7 +186,27 @@ class string_collection
* @param coll A string collection
* @return A reference to this collection.
*/
string_collection& operator=(string_collection&& coll) = default;
string_collection& operator=(string_collection&& coll) =default;
/**
* Gets a const iterator to the begining of the collecion.
* @return A const iterator to the begining of the collecion.
*/
const_iterator begin() const { return coll_.begin(); }
/**
* Gets a const iterator to the end of the collecion.
* @return A const iterator to the end of the collecion.
*/
const_iterator end() const { return coll_.end(); }
/**
* Gets a const iterator to the begining of the collecion.
* @return A const iterator to the begining of the collecion.
*/
const_iterator cbegin() const { return coll_.cbegin(); }
/**
* Gets a const iterator to the end of the collecion.
* @return A const iterator to the end of the collecion.
*/
const_iterator cend() const { return coll_.cend(); }
/**
* Determines if the collection is empty.
* @return @em true if the collection is empty, @em false if not.
Expand All @@ -192,12 +218,12 @@ class string_collection
*/
size_t size() const { return coll_.size(); }
/**
* Copies a string to the back of the collection.
* Copies a string onto the back of the collection.
* @param str A string.
*/
void push_back(const string& str);
/**
* Moves a string to the back of the collection.
* Moves a string onto the back of the collection.
* @param str A string.
*/
void push_back(string&& str);
Expand All @@ -219,21 +245,15 @@ class string_collection
* modified, so the application should not cache the return value, but
* rather request the value when needed.
* @return pointer to an array of NUL-terminated C string pointers of
* the topics in the object.
* the C++ strings in the object.
*
*/
char* const* c_arr() const { return (char* const *) cArr_.data(); }

using const_iterator = collection_type::const_iterator;

const_iterator begin() const { return coll_.begin(); }

const_iterator end() const { return coll_.end(); }
};

/////////////////////////////////////////////////////////////////////////////

/** Smart/shared pointer to a topic collection */
/** Smart/shared pointer to a string collection */
using string_collection_ptr = string_collection::ptr_t;

/** Smart/shared pointer to a const string_collection */
Expand Down Expand Up @@ -303,7 +323,7 @@ class name_value_collection
* Move constructor.
* @param other Another collection of name/value pairs
*/
name_value_collection(name_value_collection&& other) = default;
name_value_collection(name_value_collection&& other) =default;
/**
* Constructs the collection with an initializer list.
*
Expand All @@ -330,7 +350,7 @@ class name_value_collection
* Move constructor.
* @param other Another collection of name/value pairs
*/
name_value_collection& operator=(name_value_collection&& other) = default;
name_value_collection& operator=(name_value_collection&& other) =default;
/**
* Determines if the collection is empty.
* @return @em true if the container is empty, @em false if it contains
Expand Down

0 comments on commit 1980bd3

Please sign in to comment.