From 6131a30530f3c0b3ad21afee953c65722cfdfb73 Mon Sep 17 00:00:00 2001 From: fpagliughi Date: Sat, 13 Jul 2024 18:19:10 -0400 Subject: [PATCH] Added properties indexer w/ example. Now uint8_t prints properly (as unsigned) --- examples/async_consume_v5.cpp | 20 ++++++++++++++++---- include/mqtt/properties.h | 19 +++++++++++++++++++ src/properties.cpp | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/examples/async_consume_v5.cpp b/examples/async_consume_v5.cpp index 54b2d4e3..f96d771b 100644 --- a/examples/async_consume_v5.cpp +++ b/examples/async_consume_v5.cpp @@ -43,7 +43,8 @@ using namespace std; const string DFLT_SERVER_URI{"mqtt://localhost:1883"}; const string CLIENT_ID{"PahoCppAsyncConsumeV5"}; -const string TOPIC{"hello"}; +// const string TOPIC{"hello"}; +const string TOPIC{"#"}; const int QOS = 1; ///////////////////////////////////////////////////////////////////////////// @@ -92,7 +93,7 @@ int main(int argc, char* argv[]) // We'll signal the consumer to exit from another thread. // (just to show that we can) thread([cli] { - this_thread::sleep_for(10s); + this_thread::sleep_for(60s); cout << "\nClosing the consumer." << endl; cli->stop_consuming(); }).detach(); @@ -111,8 +112,19 @@ int main(int argc, char* argv[]) if (const auto* p = evt.get_message_if()) { auto& msg = *p; - if (msg) - cout << msg->get_topic() << ": " << msg->to_string() << endl; + if (!msg) + continue; + + cout << msg->get_topic() << ": " << msg->to_string(); + + const auto& props = msg->get_properties(); + size_t n = props.size(); + if (n != 0) { + cout << "\n ["; + for (size_t i = 0; i < n - 1; ++i) cout << props[i] << ", "; + cout << props[n - 1] << "]"; + } + cout << endl; } else if (evt.is_connected()) { cout << "\n*** Connected ***" << endl; diff --git a/include/mqtt/properties.h b/include/mqtt/properties.h index c0d0f508..ee26689f 100644 --- a/include/mqtt/properties.h +++ b/include/mqtt/properties.h @@ -34,6 +34,7 @@ extern "C" { #include #include #include +#include #include "mqtt/buffer_ref.h" #include "mqtt/exception.h" @@ -404,6 +405,24 @@ class properties * the list contains any items. */ bool empty() const { return props_.count == 0; } + /** + * Gets the property at the specified index in the collection. + * @param i The index + * @return The property at the specified index. + */ + const property operator[](size_t i) const { + return property{props_.array[i]}; + } + /** + * Gets the property at the specified index in the collection. + * @param i The index + * @return The property at the specified index. + */ + const property at(size_t i) const { + if (i < size_t(props_.count)) + return property{props_.array[i]}; + throw std::out_of_range{"propery index"}; + } /** * Gets the numbers of property items in the list. * @return The number of property items in the list. diff --git a/src/properties.cpp b/src/properties.cpp index 13d0dc51..1833b675 100644 --- a/src/properties.cpp +++ b/src/properties.cpp @@ -201,7 +201,7 @@ std::ostream& operator<<(std::ostream& os, const property& prop) switch (::MQTTProperty_getType(MQTTPropertyCodes(prop.type()))) { case MQTTPROPERTY_TYPE_BYTE: - os << get(prop); + os << unsigned(get(prop)); break; case MQTTPROPERTY_TYPE_TWO_BYTE_INTEGER: