123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- #ifndef __mqtt_string_collection_h
- #define __mqtt_string_collection_h
- #include "MQTTAsync.h"
- #include "mqtt/types.h"
- #include <vector>
- #include <map>
- #include <memory>
- namespace mqtt {
- class string_collection
- {
- public:
-
- using collection_type = std::vector<string>;
-
- using const_iterator = collection_type::const_iterator;
-
- using ptr_t = std::shared_ptr<string_collection>;
-
- using const_ptr_t = std::shared_ptr<const string_collection>;
- private:
-
- using c_arr_type = std::vector<const char*>;
-
- collection_type coll_;
-
- c_arr_type cArr_;
-
- void update_c_arr();
- public:
-
- string_collection() =default;
-
- string_collection(const string& str);
-
- string_collection(string&& str);
-
- string_collection(const collection_type& vec);
-
- string_collection(collection_type&& vec);
-
- string_collection(const string_collection& coll);
-
- string_collection(string_collection&& coll) =default;
-
- string_collection(std::initializer_list<string> sl);
-
- string_collection(std::initializer_list<const char*> sl);
-
- static ptr_t create(const string& str) {
- return std::make_shared<string_collection>(str);
- }
-
- static ptr_t create(string&& str) {
- return std::make_shared<string_collection>(str);
- }
-
- static ptr_t create(const collection_type& vec) {
- return std::make_shared<string_collection>(vec);
- }
-
- static ptr_t create(collection_type&& vec) {
- return std::make_shared<string_collection>(vec);
- }
-
- static ptr_t create(std::initializer_list<string> sl) {
- return std::make_shared<string_collection>(sl);
- }
-
- static ptr_t create(std::initializer_list<const char*> sl) {
- return std::make_shared<string_collection>(sl);
- }
-
- string_collection& operator=(const string_collection& coll);
-
- string_collection& operator=(string_collection&& coll) =default;
-
- const_iterator begin() const { return coll_.begin(); }
-
- const_iterator end() const { return coll_.end(); }
-
- const_iterator cbegin() const { return coll_.cbegin(); }
-
- const_iterator cend() const { return coll_.cend(); }
-
- bool empty() const { return coll_.empty(); }
-
- size_t size() const { return coll_.size(); }
-
- void push_back(const string& str);
-
- void push_back(string&& str);
-
- void clear();
-
- const string& operator[](size_t i) const { return coll_[i]; }
-
- char* const* c_arr() const { return (char* const *) cArr_.data(); }
- };
- using string_collection_ptr = string_collection::ptr_t;
- using const_string_collection_ptr = string_collection::const_ptr_t;
- class name_value_collection
- {
-
- using collection_type = std::map<string, string>;
-
- using c_arr_type = std::vector<MQTTAsync_nameValue>;
-
- collection_type map_;
-
- c_arr_type cArr_;
-
- void update_c_arr();
- public:
-
- using ptr_t = std::shared_ptr<name_value_collection>;
-
- using const_ptr_t = std::shared_ptr<const name_value_collection>;
-
- using value_type = collection_type::value_type;
-
- name_value_collection() =default;
-
- name_value_collection(const collection_type& map) : map_(map) {
- update_c_arr();
- }
-
- name_value_collection(collection_type&& map) : map_(std::move(map)) {
- update_c_arr();
- }
-
- name_value_collection(const name_value_collection& other)
- : map_(other.map_) {
- update_c_arr();
- }
-
- name_value_collection(name_value_collection&& other) =default;
-
- name_value_collection(std::initializer_list<value_type> init)
- : map_{ init } {
- update_c_arr();
- }
-
- name_value_collection& operator=(const name_value_collection& other) {
- map_ = other.map_;
- update_c_arr();
- return *this;
- }
-
- name_value_collection& operator=(name_value_collection&& other) =default;
-
- bool empty() const { return map_.empty(); }
-
- size_t size() const { return map_.size(); }
-
- void clear() {
- map_.clear();
- update_c_arr();
- }
-
- bool insert(const value_type& nvpair) {
- if (map_.insert(nvpair).second) {
- update_c_arr();
- return true;
- }
- return false;
- }
-
- const MQTTAsync_nameValue* c_arr() const { return cArr_.data(); }
- };
- }
- #endif
|