1
0
mirror of https://github.com/eclipse/paho.mqtt.cpp.git synced 2025-05-10 03:39:07 +08:00

Added cbegin() and cend() to string_collection, and fixed docs for the class.

This commit is contained in:
fpagliughi 2023-12-09 17:50:26 -05:00
parent a513e06d5b
commit 1980bd3e17

View File

@ -35,27 +35,38 @@ namespace mqtt {
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
/** /**
* Type for a collection of topics. * Type for a collection of strings, such as MQTT 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. * 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 class string_collection
{ {
public:
/** The type for the collection of strings */ /** The type for the collection of strings */
using collection_type = std::vector<string>; 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 */ /** The type for the array of C pointers */
using c_arr_type = std::vector<const char*>; using c_arr_type = std::vector<const char*>;
/** /**
* The collection of strings for the topics. * The collection of strings.
*/ */
collection_type coll_; 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 * 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 * of the pointers will remain consistent with the lifetime of the
* object. The value is kept consistent with the current topics and * object. The value is kept consistent with the current stringss and
* updated whenever topics are added or removed. * updated whenever strings are added or removed.
*/ */
c_arr_type cArr_; c_arr_type cArr_;
/** /**
@ -66,11 +77,6 @@ class string_collection
void update_c_arr(); void update_c_arr();
public: 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. * Construct an empty string collection.
*/ */
@ -104,7 +110,7 @@ public:
* Move constructor. * Move constructor.
* @param coll An existing string collection. * @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. * Construct a string collection from an initialization list of strings.
* @param sl An initialization list of strings. * @param sl An initialization list of strings.
@ -180,7 +186,27 @@ public:
* @param coll A string collection * @param coll A string collection
* @return A reference to this 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. * Determines if the collection is empty.
* @return @em true if the collection is empty, @em false if not. * @return @em true if the collection is empty, @em false if not.
@ -192,12 +218,12 @@ public:
*/ */
size_t size() const { return coll_.size(); } 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. * @param str A string.
*/ */
void push_back(const string& str); 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. * @param str A string.
*/ */
void push_back(string&& str); void push_back(string&& str);
@ -219,21 +245,15 @@ public:
* modified, so the application should not cache the return value, but * modified, so the application should not cache the return value, but
* rather request the value when needed. * rather request the value when needed.
* @return pointer to an array of NUL-terminated C string pointers of * @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(); } 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; using string_collection_ptr = string_collection::ptr_t;
/** Smart/shared pointer to a const string_collection */ /** Smart/shared pointer to a const string_collection */
@ -303,7 +323,7 @@ public:
* Move constructor. * Move constructor.
* @param other Another collection of name/value pairs * @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. * Constructs the collection with an initializer list.
* *
@ -330,7 +350,7 @@ public:
* Move constructor. * Move constructor.
* @param other Another collection of name/value pairs * @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. * Determines if the collection is empty.
* @return @em true if the container is empty, @em false if it contains * @return @em true if the container is empty, @em false if it contains