Added create_options assignment operators.

This commit is contained in:
fpagliughi 2024-06-22 20:28:20 -04:00
parent d386d30be9
commit 2ca93a0384
4 changed files with 38 additions and 16 deletions

View File

@ -48,12 +48,12 @@ const string PERSIST_DIR{"./persist"};
const string TOPIC{"hello"};
const char* PAYLOAD1 = "Hello World!";
const char* PAYLOAD2 = "Hi there!";
const char* PAYLOAD3 = "Is anyone listening?";
const char* PAYLOAD4 = "Someone is always listening.";
const string PAYLOAD1{"Hello World!"};
const string PAYLOAD2{"Hi there!"};
const string PAYLOAD3{"Is anyone listening?"};
const string PAYLOAD4{"Someone is always listening."};
const char* LWT_PAYLOAD = "Last will and testament.";
const string LWT_PAYLOAD{"Last will and testament."};
const int QOS = 1;
@ -143,11 +143,10 @@ int main(int argc, char* argv[])
callback cb;
client.set_callback(cb);
auto connOpts =
mqtt::connect_options_builder()
.clean_session()
.will(mqtt::message(TOPIC, LWT_PAYLOAD, strlen(LWT_PAYLOAD), QOS, false))
.finalize();
auto connOpts = mqtt::connect_options_builder()
.clean_session()
.will(mqtt::message(TOPIC, LWT_PAYLOAD, QOS, false))
.finalize();
cout << " ...OK" << endl;
@ -170,7 +169,7 @@ int main(int argc, char* argv[])
cout << "\nSending next message..." << endl;
mqtt::delivery_token_ptr pubtok;
pubtok = client.publish(TOPIC, PAYLOAD2, strlen(PAYLOAD2), QOS, false);
pubtok = client.publish(TOPIC, PAYLOAD2, QOS, false);
cout << " ...with token: " << pubtok->get_message_id() << endl;
cout << " ...for message with " << pubtok->get_message()->get_payload().size()
<< " bytes" << endl;

View File

@ -37,7 +37,6 @@
#include "mqtt/client.h"
const std::string SERVER_ADDRESS{"mqtt://localhost:1883"};
const std::string CLIENT_ID{"sync_publish_cpp"};
const std::string TOPIC{"hello"};
const std::string PAYLOAD1{"Hello World!"};
@ -168,7 +167,7 @@ int main(int argc, char* argv[])
{
std::cout << "Initializing..." << std::endl;
sample_mem_persistence persist;
mqtt::client client(SERVER_ADDRESS, CLIENT_ID, &persist);
mqtt::client client(SERVER_ADDRESS, "", &persist);
user_callback cb;
client.set_callback(cb);

View File

@ -184,6 +184,9 @@ public:
serverURI_{std::move(opts.serverURI_)},
clientId_{std::move(opts.clientId_)},
persistence_{std::move(opts.persistence_)} {}
create_options& operator=(const create_options& rhs);
create_options& operator=(create_options&& rhs);
/**
* Set the address of the server to connect to, specified as a URI
* @param serverURI The URI of the server.

View File

@ -22,9 +22,6 @@ namespace mqtt {
/////////////////////////////////////////////////////////////////////////////
// const MQTTAsync_createOptions create_options::DFLT_C_STRUCT =
// MQTTAsync_createOptions_initializer5;
create_options::create_options(int mqttVersion, int maxBufferedMessages)
{
opts_.MQTTVersion = mqttVersion;
@ -35,5 +32,29 @@ create_options::create_options(int mqttVersion, int maxBufferedMessages)
}
}
// --------------------------------------------------------------------------
create_options& create_options::operator=(const create_options& rhs)
{
if (&rhs != this) {
opts_ = rhs.opts_;
serverURI_ = rhs.serverURI_;
clientId_ = rhs.clientId_;
persistence_ = rhs.persistence_;
}
return *this;
}
create_options& create_options::operator=(create_options&& rhs)
{
if (&rhs != this) {
opts_ = std::move(rhs.opts_);
serverURI_ = std::move(rhs.serverURI_);
clientId_ = std::move(rhs.clientId_);
persistence_ = std::move(rhs.persistence_);
}
return *this;
}
/////////////////////////////////////////////////////////////////////////////
} // end namespace mqtt