Loading...
Searching...
No Matches
properties.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2019-2024 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.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
*******************************************************************************/
#ifndef __mqtt_properties_h
#define __mqtt_properties_h
extern "C" {
#include "MQTTProperties.h"
}
#include <initializer_list>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string_view>
#include <tuple>
#include <typeinfo>
#include "mqtt/buffer_ref.h"
#include "mqtt/exception.h"
#include "mqtt/platform.h"
#include "mqtt/types.h"
namespace mqtt {
using string_pair = std::tuple<string, string>;
class property
{
MQTTProperty prop_;
// Make a deep copy of the property struct into this one.
// For string properties, this allocates memory and copied the string(s)
void copy(const MQTTProperty& other);
friend class properties;
property() {}
public:
enum code {
PAYLOAD_FORMAT_INDICATOR = 1,
MESSAGE_EXPIRY_INTERVAL = 2,
CONTENT_TYPE = 3,
RESPONSE_TOPIC = 8,
CORRELATION_DATA = 9,
SUBSCRIPTION_IDENTIFIER = 11,
SESSION_EXPIRY_INTERVAL = 17,
ASSIGNED_CLIENT_IDENTIFIER = 18,
SERVER_KEEP_ALIVE = 19,
AUTHENTICATION_METHOD = 21,
AUTHENTICATION_DATA = 22,
REQUEST_PROBLEM_INFORMATION = 23,
WILL_DELAY_INTERVAL = 24,
REQUEST_RESPONSE_INFORMATION = 25,
RESPONSE_INFORMATION = 26,
SERVER_REFERENCE = 28,
REASON_STRING = 31,
RECEIVE_MAXIMUM = 33,
TOPIC_ALIAS_MAXIMUM = 34,
TOPIC_ALIAS = 35,
MAXIMUM_QOS = 36,
RETAIN_AVAILABLE = 37,
USER_PROPERTY = 38,
MAXIMUM_PACKET_SIZE = 39,
WILDCARD_SUBSCRIPTION_AVAILABLE = 40,
SUBSCRIPTION_IDENTIFIERS_AVAILABLE = 41,
SHARED_SUBSCRIPTION_AVAILABLE = 42
};
PAHO_MQTTPP_EXPORT static const std::map<code, std::string_view> TYPE_NAME;
property(code c, int32_t val);
property(code c, uint32_t val) : property(c, int32_t(val)) {}
property(code c, string_ref val);
property(code c, string_ref name, string_ref val);
explicit property(const MQTTProperty& cprop) { copy(cprop); }
explicit property(MQTTProperty&& cprop) : prop_(cprop) {
memset(&cprop, 0, sizeof(MQTTProperty));
}
property(const property& other) { copy(other.prop_); }
property(property&& other);
~property();
property& operator=(const property& rhs);
property& operator=(property&& rhs);
const MQTTProperty& c_struct() const { return prop_; }
code type() const { return code(prop_.identifier); }
std::string_view type_name() const;
const std::type_info& value_type_id();
};
std::ostream& operator<<(std::ostream& os, const property& prop);
template <typename T>
inline T get(const property&) {
throw bad_cast();
}
template <>
inline uint8_t get<uint8_t>(const property& prop) {
return uint8_t(prop.c_struct().value.byte);
}
template <>
inline uint16_t get<uint16_t>(const property& prop) {
return uint16_t(prop.c_struct().value.integer2);
}
template <>
[[deprecated("Integer properties are unsigned. Use get<uint16_t>()")]] inline int16_t
get<int16_t>(const property& prop) {
return int16_t(prop.c_struct().value.integer2);
}
template <>
inline uint32_t get<uint32_t>(const property& prop) {
return uint32_t(prop.c_struct().value.integer4);
}
template <>
[[deprecated("Integer properties are unsigned. Use get<uint32_t>()")]] inline int32_t
get<int32_t>(const property& prop) {
return int32_t(prop.c_struct().value.integer4);
}
template <>
inline string get<string>(const property& prop) {
return (!prop.c_struct().value.data.data)
? string()
: string(prop.c_struct().value.data.data, prop.c_struct().value.data.len);
}
template <>
inline string_pair get<string_pair>(const property& prop) {
string name =
(!prop.c_struct().value.data.data)
? string()
: string(prop.c_struct().value.data.data, prop.c_struct().value.data.len);
string value =
(!prop.c_struct().value.value.data)
? string()
: string(prop.c_struct().value.value.data, prop.c_struct().value.value.len);
return std::make_tuple(std::move(name), std::move(value));
}
class properties
{
static constexpr MQTTProperties DFLT_C_STRUCT MQTTProperties_initializer;
MQTTProperties props_{DFLT_C_STRUCT};
template <typename T>
friend T get(const properties& props, property::code propid, size_t idx);
template <typename T>
friend T get(const properties& props, property::code propid);
public:
class const_iterator
{
const MQTTProperty* curr_;
mutable property prop_;
friend properties;
const_iterator(const MQTTProperty* curr) : curr_{curr} {}
public:
const property& operator*() const {
prop_ = property{*curr_};
return prop_;
}
const_iterator operator++(int) noexcept {
auto tmp = *this;
curr_++;
return tmp;
}
const_iterator& operator++() noexcept {
++curr_;
return *this;
}
bool operator!=(const const_iterator& other) const noexcept {
return curr_ != other.curr_;
}
};
properties() {}
properties(const properties& other) : props_(::MQTTProperties_copy(&other.props_)) {}
properties(properties&& other) : props_(other.props_) {
std::memset(&other.props_, 0, sizeof(MQTTProperties));
}
properties(const MQTTProperties& cprops) { props_ = ::MQTTProperties_copy(&cprops); }
properties(std::initializer_list<property> props);
~properties() { ::MQTTProperties_free(&props_); }
const MQTTProperties& c_struct() const { return props_; }
properties& operator=(const properties& rhs);
properties& operator=(properties&& rhs);
bool empty() const { return props_.count == 0; }
const property operator[](size_t i) const { return property{props_.array[i]}; }
const property at(size_t i) const {
if (i < size_t(props_.count))
return property{props_.array[i]};
throw std::out_of_range{"property index"};
}
size_t size() const { return size_t(props_.count); }
const_iterator begin() const { return const_iterator{props_.array}; }
const_iterator cbegin() const { return begin(); }
const_iterator end() const { return const_iterator{props_.array + size()}; }
const_iterator cend() const { return end(); }
void add(const property& prop) { ::MQTTProperties_add(&props_, &prop.c_struct()); }
void clear() { ::MQTTProperties_free(&props_); }
bool contains(property::code propid) const {
return ::MQTTProperties_hasProperty(
const_cast<MQTTProperties*>(&props_), MQTTPropertyCodes(propid)
) != 0;
}
size_t count(property::code propid) const {
return size_t(
::MQTTProperties_propertyCount(
const_cast<MQTTProperties*>(&props_), MQTTPropertyCodes(propid)
)
);
}
property get(property::code propid, size_t idx = 0) const;
};
// --------------------------------------------------------------------------
template <typename T>
inline T get(const properties& props, property::code propid, size_t idx) {
MQTTProperty* prop = MQTTProperties_getPropertyAt(
const_cast<MQTTProperties*>(&props.c_struct()), MQTTPropertyCodes(propid), int(idx)
);
if (!prop)
throw bad_cast();
return get<T>(property(*prop));
}
template <typename T>
inline T get(const properties& props, property::code propid) {
return get<T>(props, propid, 0);
}
} // namespace mqtt
#endif // __mqtt_properties_h
buffer_ref.h
mqtt::buffer_ref< char >
mqtt::properties::const_iterator
Definition properties.h:309
mqtt::properties::const_iterator::operator!=
bool operator!=(const const_iterator &other) const noexcept
Definition properties.h:349
mqtt::properties::const_iterator::operator++
const_iterator & operator++() noexcept
Definition properties.h:338
mqtt::properties::const_iterator::operator++
const_iterator operator++(int) noexcept
Definition properties.h:329
mqtt::properties::const_iterator::operator*
const property & operator*() const
Definition properties.h:321
mqtt::properties
Definition properties.h:293
mqtt::properties::operator[]
const property operator[](size_t i) const
Definition properties.h:413
mqtt::properties::contains
bool contains(property::code propid) const
Definition properties.h:463
mqtt::properties::at
const property at(size_t i) const
Definition properties.h:419
mqtt::properties::properties
properties(std::initializer_list< property > props)
mqtt::properties::end
const_iterator end() const
Definition properties.h:443
mqtt::properties::empty
bool empty() const
Definition properties.h:407
mqtt::properties::cend
const_iterator cend() const
Definition properties.h:448
mqtt::properties::properties
properties()
Definition properties.h:358
mqtt::properties::size
size_t size() const
Definition properties.h:428
mqtt::properties::get
property get(property::code propid, size_t idx=0) const
mqtt::properties::properties
properties(const properties &other)
Definition properties.h:363
mqtt::properties::~properties
~properties()
Definition properties.h:384
mqtt::properties::properties
properties(const MQTTProperties &cprops)
Definition properties.h:375
mqtt::properties::operator=
properties & operator=(properties &&rhs)
mqtt::properties::get
friend T get(const properties &props, property::code propid, size_t idx)
Definition properties.h:508
mqtt::properties::add
void add(const property &prop)
Definition properties.h:453
mqtt::properties::count
size_t count(property::code propid) const
Definition properties.h:478
mqtt::properties::operator=
properties & operator=(const properties &rhs)
mqtt::properties::c_struct
const MQTTProperties & c_struct() const
Definition properties.h:389
mqtt::properties::clear
void clear()
Definition properties.h:457
mqtt::properties::cbegin
const_iterator cbegin() const
Definition properties.h:438
mqtt::properties::properties
properties(properties &&other)
Definition properties.h:368
mqtt::properties::begin
const_iterator begin() const
Definition properties.h:433
mqtt::property
Definition properties.h:55
mqtt::property::operator=
property & operator=(const property &rhs)
mqtt::property::c_struct
const MQTTProperty & c_struct() const
Definition properties.h:174
mqtt::property::code
code
Definition properties.h:70
mqtt::property::MAXIMUM_QOS
@ MAXIMUM_QOS
Definition properties.h:91
mqtt::property::WILL_DELAY_INTERVAL
@ WILL_DELAY_INTERVAL
Definition properties.h:83
mqtt::property::SERVER_REFERENCE
@ SERVER_REFERENCE
Definition properties.h:86
mqtt::property::TOPIC_ALIAS
@ TOPIC_ALIAS
Definition properties.h:90
mqtt::property::PAYLOAD_FORMAT_INDICATOR
@ PAYLOAD_FORMAT_INDICATOR
Definition properties.h:71
mqtt::property::ASSIGNED_CLIENT_IDENTIFIER
@ ASSIGNED_CLIENT_IDENTIFIER
Definition properties.h:78
mqtt::property::RECEIVE_MAXIMUM
@ RECEIVE_MAXIMUM
Definition properties.h:88
mqtt::property::TOPIC_ALIAS_MAXIMUM
@ TOPIC_ALIAS_MAXIMUM
Definition properties.h:89
mqtt::property::REQUEST_PROBLEM_INFORMATION
@ REQUEST_PROBLEM_INFORMATION
Definition properties.h:82
mqtt::property::CONTENT_TYPE
@ CONTENT_TYPE
Definition properties.h:73
mqtt::property::AUTHENTICATION_DATA
@ AUTHENTICATION_DATA
Definition properties.h:81
mqtt::property::RETAIN_AVAILABLE
@ RETAIN_AVAILABLE
Definition properties.h:92
mqtt::property::RESPONSE_INFORMATION
@ RESPONSE_INFORMATION
Definition properties.h:85
mqtt::property::REQUEST_RESPONSE_INFORMATION
@ REQUEST_RESPONSE_INFORMATION
Definition properties.h:84
mqtt::property::SHARED_SUBSCRIPTION_AVAILABLE
@ SHARED_SUBSCRIPTION_AVAILABLE
Definition properties.h:97
mqtt::property::REASON_STRING
@ REASON_STRING
Definition properties.h:87
mqtt::property::SUBSCRIPTION_IDENTIFIER
@ SUBSCRIPTION_IDENTIFIER
Definition properties.h:76
mqtt::property::SERVER_KEEP_ALIVE
@ SERVER_KEEP_ALIVE
Definition properties.h:79
mqtt::property::WILDCARD_SUBSCRIPTION_AVAILABLE
@ WILDCARD_SUBSCRIPTION_AVAILABLE
Definition properties.h:95
mqtt::property::SESSION_EXPIRY_INTERVAL
@ SESSION_EXPIRY_INTERVAL
Definition properties.h:77
mqtt::property::AUTHENTICATION_METHOD
@ AUTHENTICATION_METHOD
Definition properties.h:80
mqtt::property::MAXIMUM_PACKET_SIZE
@ MAXIMUM_PACKET_SIZE
Definition properties.h:94
mqtt::property::RESPONSE_TOPIC
@ RESPONSE_TOPIC
Definition properties.h:74
mqtt::property::SUBSCRIPTION_IDENTIFIERS_AVAILABLE
@ SUBSCRIPTION_IDENTIFIERS_AVAILABLE
Definition properties.h:96
mqtt::property::MESSAGE_EXPIRY_INTERVAL
@ MESSAGE_EXPIRY_INTERVAL
Definition properties.h:72
mqtt::property::CORRELATION_DATA
@ CORRELATION_DATA
Definition properties.h:75
mqtt::property::USER_PROPERTY
@ USER_PROPERTY
Definition properties.h:93
mqtt::property::value_type_id
const std::type_info & value_type_id()
mqtt::property::type
code type() const
Definition properties.h:179
mqtt::property::operator=
property & operator=(property &&rhs)
mqtt::property::property
property(code c, int32_t val)
mqtt::property::property
property(const property &other)
Definition properties.h:147
mqtt::property::TYPE_NAME
static PAHO_MQTTPP_EXPORT const std::map< code, std::string_view > TYPE_NAME
Definition properties.h:101
mqtt::property::property
property(MQTTProperty &&cprop)
Definition properties.h:140
mqtt::property::~property
~property()
mqtt::property::property
property(code c, uint32_t val)
Definition properties.h:116
mqtt::property::property
property(const MQTTProperty &cprop)
Definition properties.h:134
mqtt::property::property
property(property &&other)
mqtt::property::property
property(code c, string_ref name, string_ref val)
mqtt::property::type_name
std::string_view type_name() const
mqtt::property::property
property(code c, string_ref val)
exception.h
PAHO_MQTTPP_EXPORT
#define PAHO_MQTTPP_EXPORT
Definition export.h:40
mqtt
Definition async_client.h:60
mqtt::get< int16_t >
int16_t get< int16_t >(const property &prop)
Definition properties.h:229
mqtt::get
T get(const property &)
Definition properties.h:199
mqtt::string_pair
std::tuple< string, string > string_pair
Definition properties.h:47
mqtt::get< string >
string get< string >(const property &prop)
Definition properties.h:259
mqtt::get< uint16_t >
uint16_t get< uint16_t >(const property &prop)
Definition properties.h:217
mqtt::bad_cast
std::bad_cast bad_cast
Definition exception.h:39
mqtt::string
std::string string
Definition types.h:43
mqtt::get< uint8_t >
uint8_t get< uint8_t >(const property &prop)
Definition properties.h:208
mqtt::get< int32_t >
int32_t get< int32_t >(const property &prop)
Definition properties.h:250
mqtt::operator<<
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition buffer_ref.h:286
mqtt::get< string_pair >
string_pair get< string_pair >(const property &prop)
Definition properties.h:270
mqtt::get< uint32_t >
uint32_t get< uint32_t >(const property &prop)
Definition properties.h:238
platform.h
types.h