PahoMqttCpp
MQTT C++ Client for POSIX and Windows
Loading...
Searching...
No Matches
properties.h
Go to the documentation of this file.
1
7
8/*******************************************************************************
9 * Copyright (c) 2019-2024 Frank Pagliughi <fpagliughi@mindspring.com>
10 *
11 * All rights reserved. This program and the accompanying materials
12 * are made available under the terms of the Eclipse Public License v2.0
13 * and Eclipse Distribution License v1.0 which accompany this distribution.
14 *
15 * The Eclipse Public License is available at
16 * http://www.eclipse.org/legal/epl-v20.html
17 * and the Eclipse Distribution License is available at
18 * http://www.eclipse.org/org/documents/edl-v10.php.
19 *
20 * Contributors:
21 * Frank Pagliughi - initial implementation and documentation
22 *******************************************************************************/
23
24#ifndef __mqtt_properties_h
25#define __mqtt_properties_h
26
27extern "C" {
28#include "MQTTProperties.h"
29}
30
31#include <initializer_list>
32#include <iostream>
33#include <map>
34#include <stdexcept>
35#include <string_view>
36#include <tuple>
37#include <typeinfo>
38
39#include "mqtt/buffer_ref.h"
40#include "mqtt/exception.h"
41#include "mqtt/platform.h"
42#include "mqtt/types.h"
43
44namespace mqtt {
45
47using string_pair = std::tuple<string, string>;
48
50
55{
57 MQTTProperty prop_;
58
59 // Make a deep copy of the property struct into this one.
60 // For string properties, this allocates memory and copied the string(s)
61 void copy(const MQTTProperty& other);
62
63 friend class properties;
64 property() {}
65
66public:
99
101 PAHO_MQTTPP_EXPORT static const std::map<code, std::string_view> TYPE_NAME;
102
109 property(code c, int32_t val);
116 property(code c, uint32_t val) : property(c, int32_t(val)) {}
134 explicit property(const MQTTProperty& cprop) { copy(cprop); }
140 explicit property(MQTTProperty&& cprop) : prop_(cprop) {
141 memset(&cprop, 0, sizeof(MQTTProperty));
142 }
147 property(const property& other) { copy(other.prop_); }
162 property& operator=(const property& rhs);
168 property& operator=(property&& rhs);
174 const MQTTProperty& c_struct() const { return prop_; }
179 code type() const { return code(prop_.identifier); }
184 std::string_view type_name() const;
189 const std::type_info& value_type_id();
190};
191
192std::ostream& operator<<(std::ostream& os, const property& prop);
193
198template <typename T>
199inline T get(const property&) {
200 throw bad_cast();
201}
202
207template <>
208inline uint8_t get<uint8_t>(const property& prop) {
209 return uint8_t(prop.c_struct().value.byte);
210}
211
216template <>
217inline uint16_t get<uint16_t>(const property& prop) {
218 return uint16_t(prop.c_struct().value.integer2);
219}
220
227template <>
228[[deprecated("Integer properties are unsigned. Use get<uint16_t>()")]] inline int16_t
229get<int16_t>(const property& prop) {
230 return int16_t(prop.c_struct().value.integer2);
231}
232
237template <>
238inline uint32_t get<uint32_t>(const property& prop) {
239 return uint32_t(prop.c_struct().value.integer4);
240}
241
248template <>
249[[deprecated("Integer properties are unsigned. Use get<uint32_t>()")]] inline int32_t
250get<int32_t>(const property& prop) {
251 return int32_t(prop.c_struct().value.integer4);
252}
253
258template <>
259inline string get<string>(const property& prop) {
260 return (!prop.c_struct().value.data.data)
261 ? string()
262 : string(prop.c_struct().value.data.data, prop.c_struct().value.data.len);
263}
264
269template <>
271 string name =
272 (!prop.c_struct().value.data.data)
273 ? string()
274 : string(prop.c_struct().value.data.data, prop.c_struct().value.data.len);
275
276 string value =
277 (!prop.c_struct().value.value.data)
278 ? string()
279 : string(prop.c_struct().value.value.data, prop.c_struct().value.value.len);
280
281 return std::make_tuple(std::move(name), std::move(value));
282}
283
285
293{
295 static constexpr MQTTProperties DFLT_C_STRUCT MQTTProperties_initializer;
296
298 MQTTProperties props_{DFLT_C_STRUCT};
299
300 template <typename T>
301 friend T get(const properties& props, property::code propid, size_t idx);
302
303 template <typename T>
304 friend T get(const properties& props, property::code propid);
305
306public:
309 {
310 const MQTTProperty* curr_;
311 mutable property prop_;
312
313 friend properties;
314 const_iterator(const MQTTProperty* curr) : curr_{curr} {}
315
316 public:
321 const property& operator*() const {
322 prop_ = property{*curr_};
323 return prop_;
324 }
330 auto tmp = *this;
331 curr_++;
332 return tmp;
333 }
339 ++curr_;
340 return *this;
341 }
349 bool operator!=(const const_iterator& other) const noexcept {
350 return curr_ != other.curr_;
351 }
352 };
353
363 properties(const properties& other) : props_(::MQTTProperties_copy(&other.props_)) {}
368 properties(properties&& other) : props_(other.props_) {
369 std::memset(&other.props_, 0, sizeof(MQTTProperties));
370 }
375 properties(const MQTTProperties& cprops) { props_ = ::MQTTProperties_copy(&cprops); }
380 properties(std::initializer_list<property> props);
384 ~properties() { ::MQTTProperties_free(&props_); }
389 const MQTTProperties& c_struct() const { return props_; }
407 bool empty() const { return props_.count == 0; }
413 const property operator[](size_t i) const { return property{props_.array[i]}; }
419 const property at(size_t i) const {
420 if (i < size_t(props_.count))
421 return property{props_.array[i]};
422 throw std::out_of_range{"property index"};
423 }
428 size_t size() const { return size_t(props_.count); }
433 const_iterator begin() const { return const_iterator{props_.array}; }
438 const_iterator cbegin() const { return begin(); }
443 const_iterator end() const { return const_iterator{props_.array + size()}; }
448 const_iterator cend() const { return end(); }
453 void add(const property& prop) { ::MQTTProperties_add(&props_, &prop.c_struct()); }
457 void clear() { ::MQTTProperties_free(&props_); }
463 bool contains(property::code propid) const {
464 return ::MQTTProperties_hasProperty(
465 const_cast<MQTTProperties*>(&props_), MQTTPropertyCodes(propid)
466 ) != 0;
467 }
478 size_t count(property::code propid) const {
479 return size_t(
480 ::MQTTProperties_propertyCount(
481 const_cast<MQTTProperties*>(&props_), MQTTPropertyCodes(propid)
482 )
483 );
484 }
493 property get(property::code propid, size_t idx = 0) const;
494};
495
496// --------------------------------------------------------------------------
497
507template <typename T>
508inline T get(const properties& props, property::code propid, size_t idx) {
509 MQTTProperty* prop = MQTTProperties_getPropertyAt(
510 const_cast<MQTTProperties*>(&props.c_struct()), MQTTPropertyCodes(propid), int(idx)
511 );
512 if (!prop)
513 throw bad_cast();
514
515 return get<T>(property(*prop));
516}
517
525template <typename T>
526inline T get(const properties& props, property::code propid) {
527 return get<T>(props, propid, 0);
528}
529
531} // namespace mqtt
532
533#endif // __mqtt_properties_h
Definition properties.h:309
bool operator!=(const const_iterator &other) const noexcept
Definition properties.h:349
const_iterator & operator++() noexcept
Definition properties.h:338
const_iterator operator++(int) noexcept
Definition properties.h:329
const property & operator*() const
Definition properties.h:321
Definition properties.h:293
const property operator[](size_t i) const
Definition properties.h:413
bool contains(property::code propid) const
Definition properties.h:463
const property at(size_t i) const
Definition properties.h:419
properties(std::initializer_list< property > props)
const_iterator end() const
Definition properties.h:443
bool empty() const
Definition properties.h:407
const_iterator cend() const
Definition properties.h:448
properties()
Definition properties.h:358
size_t size() const
Definition properties.h:428
property get(property::code propid, size_t idx=0) const
properties(const properties &other)
Definition properties.h:363
~properties()
Definition properties.h:384
properties(const MQTTProperties &cprops)
Definition properties.h:375
properties & operator=(properties &&rhs)
friend T get(const properties &props, property::code propid, size_t idx)
Definition properties.h:508
void add(const property &prop)
Definition properties.h:453
size_t count(property::code propid) const
Definition properties.h:478
properties & operator=(const properties &rhs)
const MQTTProperties & c_struct() const
Definition properties.h:389
void clear()
Definition properties.h:457
const_iterator cbegin() const
Definition properties.h:438
properties(properties &&other)
Definition properties.h:368
const_iterator begin() const
Definition properties.h:433
Definition properties.h:55
property & operator=(const property &rhs)
const MQTTProperty & c_struct() const
Definition properties.h:174
code
Definition properties.h:70
@ MAXIMUM_QOS
Definition properties.h:91
@ WILL_DELAY_INTERVAL
Definition properties.h:83
@ SERVER_REFERENCE
Definition properties.h:86
@ TOPIC_ALIAS
Definition properties.h:90
@ PAYLOAD_FORMAT_INDICATOR
Definition properties.h:71
@ ASSIGNED_CLIENT_IDENTIFIER
Definition properties.h:78
@ RECEIVE_MAXIMUM
Definition properties.h:88
@ TOPIC_ALIAS_MAXIMUM
Definition properties.h:89
@ REQUEST_PROBLEM_INFORMATION
Definition properties.h:82
@ CONTENT_TYPE
Definition properties.h:73
@ AUTHENTICATION_DATA
Definition properties.h:81
@ RETAIN_AVAILABLE
Definition properties.h:92
@ RESPONSE_INFORMATION
Definition properties.h:85
@ REQUEST_RESPONSE_INFORMATION
Definition properties.h:84
@ SHARED_SUBSCRIPTION_AVAILABLE
Definition properties.h:97
@ REASON_STRING
Definition properties.h:87
@ SUBSCRIPTION_IDENTIFIER
Definition properties.h:76
@ SERVER_KEEP_ALIVE
Definition properties.h:79
@ WILDCARD_SUBSCRIPTION_AVAILABLE
Definition properties.h:95
@ SESSION_EXPIRY_INTERVAL
Definition properties.h:77
@ AUTHENTICATION_METHOD
Definition properties.h:80
@ MAXIMUM_PACKET_SIZE
Definition properties.h:94
@ RESPONSE_TOPIC
Definition properties.h:74
@ SUBSCRIPTION_IDENTIFIERS_AVAILABLE
Definition properties.h:96
@ MESSAGE_EXPIRY_INTERVAL
Definition properties.h:72
@ CORRELATION_DATA
Definition properties.h:75
@ USER_PROPERTY
Definition properties.h:93
const std::type_info & value_type_id()
code type() const
Definition properties.h:179
property & operator=(property &&rhs)
property(code c, int32_t val)
property(const property &other)
Definition properties.h:147
static PAHO_MQTTPP_EXPORT const std::map< code, std::string_view > TYPE_NAME
Definition properties.h:101
property(MQTTProperty &&cprop)
Definition properties.h:140
property(code c, uint32_t val)
Definition properties.h:116
property(const MQTTProperty &cprop)
Definition properties.h:134
property(property &&other)
property(code c, string_ref name, string_ref val)
std::string_view type_name() const
property(code c, string_ref val)
#define PAHO_MQTTPP_EXPORT
Definition export.h:40
Definition async_client.h:60
int16_t get< int16_t >(const property &prop)
Definition properties.h:229
T get(const property &)
Definition properties.h:199
string get< string >(const property &prop)
Definition properties.h:259
uint16_t get< uint16_t >(const property &prop)
Definition properties.h:217
std::bad_cast bad_cast
Definition exception.h:39
uint8_t get< uint8_t >(const property &prop)
Definition properties.h:208
std::string string
Definition types.h:43
std::tuple< string, string > string_pair
Definition properties.h:47
int32_t get< int32_t >(const property &prop)
Definition properties.h:250
std::ostream & operator<<(std::ostream &os, const buffer_ref< T > &buf)
Definition buffer_ref.h:286
string_pair get< string_pair >(const property &prop)
Definition properties.h:270
uint32_t get< uint32_t >(const property &prop)
Definition properties.h:238