Skip to content

Commit

Permalink
Added properties indexer w/ example. Now uint8_t prints properly (as …
Browse files Browse the repository at this point in the history
…unsigned)
  • Loading branch information
fpagliughi committed Jul 13, 2024
1 parent 3f2f7c2 commit 6131a30
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
20 changes: 16 additions & 4 deletions examples/async_consume_v5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions include/mqtt/properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern "C" {
#include <string_view>
#include <tuple>
#include <typeinfo>
#include <stdexcept>

#include "mqtt/buffer_ref.h"
#include "mqtt/exception.h"
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(prop);
os << unsigned(get<uint8_t>(prop));
break;

case MQTTPROPERTY_TYPE_TWO_BYTE_INTEGER:
Expand Down

0 comments on commit 6131a30

Please sign in to comment.