forked from eclipse-paho/paho.mqtt.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_collection.cpp
116 lines (94 loc) · 2.67 KB
/
string_collection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// string_collection.cpp
/*******************************************************************************
* Copyright (c) 2017-2020 Frank Pagliughi <[email protected]>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.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
*******************************************************************************/
#include "mqtt/string_collection.h"
namespace mqtt {
/////////////////////////////////////////////////////////////////////////////
string_collection::string_collection(const string& str) : coll_{ str }
{
update_c_arr();
}
string_collection::string_collection(string&& str) : coll_{ std::move(str) }
{
update_c_arr();
}
string_collection::string_collection(const collection_type& vec) : coll_{ vec }
{
update_c_arr();
}
string_collection::string_collection(collection_type&& vec) : coll_{ std::move(vec) }
{
update_c_arr();
}
string_collection::string_collection(const string_collection& coll) : coll_ { coll.coll_ }
{
update_c_arr();
}
string_collection::string_collection(std::initializer_list<string> sl)
{
for (const auto& s : sl)
coll_.push_back(s);
update_c_arr();
}
string_collection::string_collection(std::initializer_list<const char*> sl)
{
for (const auto& s : sl)
coll_.push_back(string(s));
update_c_arr();
}
void string_collection::update_c_arr()
{
cArr_.clear();
cArr_.reserve(coll_.size());
for (const auto& s : coll_)
cArr_.push_back(s.c_str());
}
string_collection& string_collection::operator=(const string_collection& coll)
{
coll_ = coll.coll_;
update_c_arr();
return *this;
}
void string_collection::push_back(const string& str)
{
coll_.push_back(str);
update_c_arr();
}
void string_collection::push_back(string&& str)
{
coll_.push_back(str);
update_c_arr();
}
void string_collection::clear()
{
coll_.clear();
cArr_.clear();
}
/////////////////////////////////////////////////////////////////////////////
void name_value_collection::update_c_arr()
{
cArr_.clear();
cArr_.reserve(map_.size()+1);
for (const auto& m : map_) {
cArr_.push_back(
MQTTAsync_nameValue{ m.first.c_str(), m.second.c_str() }
);
}
cArr_.push_back(MQTTAsync_nameValue{ nullptr, nullptr });
}
/////////////////////////////////////////////////////////////////////////////
// end namespace mqtt
}