From 2ca93a038468f8af206ea8d67268cd253b4bf54a Mon Sep 17 00:00:00 2001 From: fpagliughi Date: Sat, 22 Jun 2024 20:28:20 -0400 Subject: [PATCH] Added create_options assignment operators. --- examples/async_publish.cpp | 21 ++++++++++----------- examples/sync_publish.cpp | 3 +-- include/mqtt/create_options.h | 3 +++ src/create_options.cpp | 27 ++++++++++++++++++++++++--- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/examples/async_publish.cpp b/examples/async_publish.cpp index 62a2cd9..8f57a8f 100644 --- a/examples/async_publish.cpp +++ b/examples/async_publish.cpp @@ -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; diff --git a/examples/sync_publish.cpp b/examples/sync_publish.cpp index e031ffb..fd975f5 100644 --- a/examples/sync_publish.cpp +++ b/examples/sync_publish.cpp @@ -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); diff --git a/include/mqtt/create_options.h b/include/mqtt/create_options.h index a642f7d..3ba1126 100644 --- a/include/mqtt/create_options.h +++ b/include/mqtt/create_options.h @@ -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. diff --git a/src/create_options.cpp b/src/create_options.cpp index 1329041..1c3d3f4 100644 --- a/src/create_options.cpp +++ b/src/create_options.cpp @@ -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