Loading...
Searching...
No Matches
string_collection.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2017-2024 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
*******************************************************************************/
#ifndef __mqtt_string_collection_h
#define __mqtt_string_collection_h
#include <map>
#include <memory>
#include <vector>
#include "MQTTAsync.h"
#include "mqtt/types.h"
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(); }
};
} // namespace mqtt
#endif // __mqtt_string_collection_h
mqtt::name_value_collection
Definition string_collection.h:267
mqtt::name_value_collection::operator=
name_value_collection & operator=(const name_value_collection &other)
Definition string_collection.h:337
mqtt::name_value_collection::const_ptr_t
std::shared_ptr< const name_value_collection > const_ptr_t
Definition string_collection.h:292
mqtt::name_value_collection::name_value_collection
name_value_collection(const collection_type &map)
Definition string_collection.h:303
mqtt::name_value_collection::name_value_collection
name_value_collection(const name_value_collection &other)
Definition string_collection.h:313
mqtt::name_value_collection::name_value_collection
name_value_collection()=default
mqtt::name_value_collection::operator=
name_value_collection & operator=(name_value_collection &&other)=default
mqtt::name_value_collection::ptr_t
std::shared_ptr< name_value_collection > ptr_t
Definition string_collection.h:290
mqtt::name_value_collection::name_value_collection
name_value_collection(collection_type &&map)
Definition string_collection.h:308
mqtt::name_value_collection::clear
void clear()
Definition string_collection.h:361
mqtt::name_value_collection::name_value_collection
name_value_collection(name_value_collection &&other)=default
mqtt::name_value_collection::insert
bool insert(const value_type &nvpair)
Definition string_collection.h:370
mqtt::name_value_collection::empty
bool empty() const
Definition string_collection.h:352
mqtt::name_value_collection::c_arr
const MQTTAsync_nameValue * c_arr() const
Definition string_collection.h:387
mqtt::name_value_collection::name_value_collection
name_value_collection(std::initializer_list< value_type > init)
Definition string_collection.h:330
mqtt::name_value_collection::value_type
collection_type::value_type value_type
Definition string_collection.h:294
mqtt::name_value_collection::size
size_t size() const
Definition string_collection.h:357
mqtt::string_collection
Definition string_collection.h:45
mqtt::string_collection::cbegin
const_iterator cbegin() const
Definition string_collection.h:203
mqtt::string_collection::operator=
string_collection & operator=(const string_collection &coll)
mqtt::string_collection::begin
const_iterator begin() const
Definition string_collection.h:193
mqtt::string_collection::string_collection
string_collection(std::initializer_list< const char * > sl)
mqtt::string_collection::size
size_t size() const
Definition string_collection.h:218
mqtt::string_collection::string_collection
string_collection(const string_collection &coll)
mqtt::string_collection::string_collection
string_collection(std::initializer_list< string > sl)
mqtt::string_collection::create
static ptr_t create(std::initializer_list< string > sl)
Definition string_collection.h:163
mqtt::string_collection::const_ptr_t
std::shared_ptr< const string_collection > const_ptr_t
Definition string_collection.h:55
mqtt::string_collection::string_collection
string_collection(string_collection &&coll)=default
mqtt::string_collection::string_collection
string_collection(const collection_type &vec)
mqtt::string_collection::ptr_t
std::shared_ptr< string_collection > ptr_t
Definition string_collection.h:53
mqtt::string_collection::push_back
void push_back(const string &str)
mqtt::string_collection::operator[]
const string & operator[](size_t i) const
Definition string_collection.h:238
mqtt::string_collection::string_collection
string_collection(const string &str)
mqtt::string_collection::create
static ptr_t create(const collection_type &vec)
Definition string_collection.h:145
mqtt::string_collection::create
static ptr_t create(std::initializer_list< const char * > sl)
Definition string_collection.h:172
mqtt::string_collection::clear
void clear()
mqtt::string_collection::string_collection
string_collection()=default
mqtt::string_collection::const_iterator
collection_type::const_iterator const_iterator
Definition string_collection.h:50
mqtt::string_collection::cend
const_iterator cend() const
Definition string_collection.h:208
mqtt::string_collection::push_back
void push_back(string &&str)
mqtt::string_collection::create
static ptr_t create(const string &str)
Definition string_collection.h:130
mqtt::string_collection::create
static ptr_t create(collection_type &&vec)
Definition string_collection.h:154
mqtt::string_collection::string_collection
string_collection(string &&str)
mqtt::string_collection::end
const_iterator end() const
Definition string_collection.h:198
mqtt::string_collection::string_collection
string_collection(collection_type &&vec)
mqtt::string_collection::operator=
string_collection & operator=(string_collection &&coll)=default
mqtt::string_collection::collection_type
std::vector< string > collection_type
Definition string_collection.h:48
mqtt::string_collection::empty
bool empty() const
Definition string_collection.h:213
mqtt::string_collection::create
static ptr_t create(string &&str)
Definition string_collection.h:139
mqtt::string_collection::c_arr
char *const * c_arr() const
Definition string_collection.h:250
mqtt
Definition async_client.h:60
mqtt::const_string_collection_ptr
string_collection::const_ptr_t const_string_collection_ptr
Definition string_collection.h:259
mqtt::string_collection_ptr
string_collection::ptr_t string_collection_ptr
Definition string_collection.h:256
types.h