Paho C++  1.0
The Paho MQTT C++ Client Library
 All Classes Files Functions Variables Typedefs Friends
string_collection.h
Go to the documentation of this file.
1 
8 /*******************************************************************************
9  * Copyright (c) 2017 Frank Pagliughi <fpagliughi@mindspring.com>
10  *
11  * All rights reserved. This program and the accompanying materials
12  * are made available under the terms of the Eclipse Public License v1.0
13  * and Eclipse Distribution License v1.0 which accompany this distribution.
14  *
15  * The Eclipse Public License is available at
16  * http://www.eclipse.org/legal/epl-v10.html
17  * and the Eclipse Distribution License is available at
18  * http://www.eclipse.org/org/documents/edl-v10.php.
19  *
20  * Contributors:
21  * Frank Pagliughi - initial implementation and documentation
22  *******************************************************************************/
23 
24 #ifndef __mqtt_string_collection_h
25 #define __mqtt_string_collection_h
26 
27 #include "mqtt/types.h"
28 #include <vector>
29 #include <memory>
30 
31 namespace mqtt {
32 
34 
41 {
43  using collection_type = std::vector<string>;
45  using c_arr_type = std::vector<const char*>;
46 
50  collection_type coll_;
58  c_arr_type cArr_;
64  void update_c_arr();
65 
66 public:
68  using ptr_t = std::shared_ptr<string_collection>;
70  using const_ptr_t = std::shared_ptr<const string_collection>;
71 
75  string_collection() =default;
80  string_collection(const string& str);
85  string_collection(string&& str);
90  string_collection(const collection_type& vec);
95  string_collection(collection_type&& vec);
105  string_collection(string_collection&& coll) = default;
110  string_collection(std::initializer_list<string> sl);
116  string_collection(std::initializer_list<const char*> sl);
121  static ptr_t create(const string& str) {
122  return std::make_shared<string_collection>(str);
123  }
130  static ptr_t create(string&& str) {
131  return std::make_shared<string_collection>(str);
132  }
138  static ptr_t create(const collection_type& vec) {
139  return std::make_shared<string_collection>(vec);
140  }
147  static ptr_t create(collection_type&& vec) {
148  return std::make_shared<string_collection>(vec);
149  }
156  static ptr_t create(std::initializer_list<string> sl) {
157  return std::make_shared<string_collection>(sl);
158  }
165  static ptr_t create(std::initializer_list<const char*> sl) {
166  return std::make_shared<string_collection>(sl);
167  }
181  string_collection& operator=(string_collection&& coll) = default;
186  bool empty() const { return coll_.empty(); }
191  size_t size() const { return coll_.size(); }
196  void push_back(const string& str);
201  void push_back(string&& str);
205  void clear();
211  const string& operator[](size_t i) const { return coll_[i]; }
212 
224  char* const* c_arr() const { return (char* const *) cArr_.data(); }
225  //const char* const* c_arr() const { return cArr_.data(); }
226 };
227 
229 
231 using string_collection_ptr = string_collection::ptr_t;
232 
234 using const_string_collection_ptr = string_collection::const_ptr_t;
235 
237 // end namespace mqtt
238 }
239 
240 #endif // __mqtt_string_collection_h
241 
std::shared_ptr< string_collection > ptr_t
Smart/shared pointer to an object of this type.
Definition: string_collection.h:68
size_t size() const
Gets the number of strings in the collection.
Definition: string_collection.h:191
Basic types and type conversions for the Paho MQTT C++ library.
static ptr_t create(collection_type &&vec)
Creates a string collection on the heap by copying a vector of strings.
Definition: string_collection.h:147
static ptr_t create(std::initializer_list< const char * > sl)
Create a string collection on the heap from an initialization list of C string pointers.
Definition: string_collection.h:165
void push_back(const string &str)
Copies a string to the back of the collection.
bool empty() const
Determines if the collection is empty.
Definition: string_collection.h:186
string_collection & operator=(const string_collection &coll)
Copy assignment.
static ptr_t create(const collection_type &vec)
Creates a string collection on the heap by copying a vector of strings.
Definition: string_collection.h:138
Type for a collection of topics.
Definition: string_collection.h:40
static ptr_t create(std::initializer_list< string > sl)
Create a string collection on the heap from an initialization list of strings.
Definition: string_collection.h:156
char *const * c_arr() const
Gets a pointer to an array of NUL-terminated C string pointers.
Definition: string_collection.h:224
static ptr_t create(string &&str)
Create a string collection on the heap, initially containing a single string.
Definition: string_collection.h:130
std::shared_ptr< const string_collection > const_ptr_t
Smart/shared pointer to a const object of this type.
Definition: string_collection.h:70
static ptr_t create(const string &str)
Create an empty string collection on the heap.
Definition: string_collection.h:121
string_collection()=default
Construct an empty string collection.
const string & operator[](size_t i) const
Gets the n'th string in the collection.
Definition: string_collection.h:211
void clear()
Removes all the strings from the collection.