paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
properties.h
Go to the documentation of this file.
1 
8 /*******************************************************************************
9  * Copyright (c) 2019-2023 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 
27 extern "C" {
28  #include "MQTTProperties.h"
29 }
30 
31 #include "mqtt/types.h"
32 #include "mqtt/buffer_ref.h"
33 #include "mqtt/exception.h"
34 #include "mqtt/platform.h"
35 #include <tuple>
36 #include <initializer_list>
37 
38 #include <iostream>
39 
40 namespace mqtt {
41 
43 using string_pair = std::tuple<string, string>;
44 
46 
50 class property
51 {
53  MQTTProperty prop_;
54 
55  // Make a deep copy of the property struct into this one.
56  // For string properties, this allocates memory and copied the string(s)
57  void copy(const MQTTProperty& other);
58 
59 public:
63  enum code {
91  };
92 
99  property(code c, int32_t val);
117  explicit property(const MQTTProperty& cprop);
123  explicit property(MQTTProperty&& cprop);
128  property(const property& other);
133  property(property&& other);
143  property& operator=(const property& rhs);
149  property& operator=(property&& rhs);
155  const MQTTProperty& c_struct() const { return prop_; }
160  code type() const { return code(prop_.identifier); }
165  const char* type_name() const {
166  return ::MQTTPropertyName(prop_.identifier);
167  }
168 };
169 
174 template <typename T>
175 inline T get(const property&) { throw bad_cast(); }
176 
181 template <>
182 inline uint8_t get<uint8_t>(const property& prop) {
183  return (uint8_t) prop.c_struct().value.byte;
184 }
185 
190 template <>
191 inline uint16_t get<uint16_t>(const property& prop) {
192  return (uint16_t) prop.c_struct().value.integer2;
193 }
194 
199 template <>
200 inline int16_t get<int16_t>(const property& prop) {
201  return (int16_t) prop.c_struct().value.integer2;
202 }
203 
208 template <>
209 inline uint32_t get<uint32_t>(const property& prop) {
210  return (uint32_t) prop.c_struct().value.integer4;
211 }
212 
217 template <>
218 inline int32_t get<int32_t>(const property& prop) {
219  return (int32_t) prop.c_struct().value.integer4;
220 }
221 
226 template <>
227 inline string get<string>(const property& prop) {
228  return (!prop.c_struct().value.data.data) ? string()
229  : string(prop.c_struct().value.data.data, prop.c_struct().value.data.len);
230 }
231 
236 template <>
237 inline string_pair get<string_pair>(const property& prop) {
238  string name = (!prop.c_struct().value.data.data) ? string()
239  : string(prop.c_struct().value.data.data, prop.c_struct().value.data.len);
240 
241  string value = (!prop.c_struct().value.value.data) ? string()
242  : string(prop.c_struct().value.value.data, prop.c_struct().value.value.len);
243 
244  return std::make_tuple(std::move(name), std::move(value));
245 }
246 
248 
256 {
258  PAHO_MQTTPP_EXPORT static const MQTTProperties DFLT_C_STRUCT;
259 
261  MQTTProperties props_;
262 
263  template<typename T>
264  friend T get(const properties& props, property::code propid, size_t idx);
265 
266  template<typename T>
267  friend T get(const properties& props, property::code propid);
268 
269 public:
279  properties(const properties& other)
280  : props_(::MQTTProperties_copy(&other.props_)) {}
285  properties(properties&& other) : props_(other.props_) {
286  std::memset(&other.props_, 0, sizeof(MQTTProperties));
287  }
292  properties(const MQTTProperties& cprops) {
293  props_ = ::MQTTProperties_copy(&cprops);
294  }
299  properties(std::initializer_list<property> props);
303  ~properties() { ::MQTTProperties_free(&props_); }
308  const MQTTProperties& c_struct() const { return props_; }
326  bool empty() const { return props_.count == 0; }
331  size_t size() const { return size_t(props_.count); }
336  void add(const property& prop) {
337  ::MQTTProperties_add(&props_, &prop.c_struct());
338  }
342  void clear() {
343  ::MQTTProperties_free(&props_);
344  }
350  bool contains(property::code propid) const {
351  return ::MQTTProperties_hasProperty(const_cast<MQTTProperties*>(&props_),
352  MQTTPropertyCodes(propid)) != 0;
353  }
364  size_t count(property::code propid) const {
365  return size_t(::MQTTProperties_propertyCount(
366  const_cast<MQTTProperties*>(&props_), MQTTPropertyCodes(propid)));
367  }
376  property get(property::code propid, size_t idx=0);
377 };
378 
379 // --------------------------------------------------------------------------
380 
390 template<typename T>
391 inline T get(const properties& props, property::code propid, size_t idx)
392 {
393  MQTTProperty* prop = MQTTProperties_getPropertyAt(
394  const_cast<MQTTProperties*>(&props.c_struct()),
395  MQTTPropertyCodes(propid), int(idx));
396  if (!prop)
397  throw bad_cast();
398 
399  return get<T>(property(*prop));
400 }
401 
409 template<typename T>
410 inline T get(const properties& props, property::code propid)
411 {
412  return get<T>(props, propid, 0);
413 }
414 
416 // end namespace mqtt
417 }
418 
419 #endif // __mqtt_properties_h
420 
Definition: properties.h:256
bool contains(property::code propid) const
Definition: properties.h:350
property get(property::code propid, size_t idx=0)
properties(std::initializer_list< property > props)
bool empty() const
Definition: properties.h:326
size_t size() const
Definition: properties.h:331
properties(const properties &other)
Definition: properties.h:279
~properties()
Definition: properties.h:303
properties(const MQTTProperties &cprops)
Definition: properties.h:292
const MQTTProperties & c_struct() const
Definition: properties.h:308
friend T get(const properties &props, property::code propid, size_t idx)
Definition: properties.h:391
void add(const property &prop)
Definition: properties.h:336
properties & operator=(properties &&rhs)
properties & operator=(const properties &rhs)
size_t count(property::code propid) const
Definition: properties.h:364
void clear()
Definition: properties.h:342
properties(properties &&other)
Definition: properties.h:285
Definition: properties.h:51
code
Definition: properties.h:63
@ MAXIMUM_QOS
Definition: properties.h:84
@ WILL_DELAY_INTERVAL
Definition: properties.h:76
@ SERVER_REFERENCE
Definition: properties.h:79
@ TOPIC_ALIAS
Definition: properties.h:83
@ ASSIGNED_CLIENT_IDENTIFER
Definition: properties.h:71
@ PAYLOAD_FORMAT_INDICATOR
Definition: properties.h:64
@ RECEIVE_MAXIMUM
Definition: properties.h:81
@ TOPIC_ALIAS_MAXIMUM
Definition: properties.h:82
@ REQUEST_PROBLEM_INFORMATION
Definition: properties.h:75
@ CONTENT_TYPE
Definition: properties.h:66
@ AUTHENTICATION_DATA
Definition: properties.h:74
@ RETAIN_AVAILABLE
Definition: properties.h:85
@ RESPONSE_INFORMATION
Definition: properties.h:78
@ REQUEST_RESPONSE_INFORMATION
Definition: properties.h:77
@ SHARED_SUBSCRIPTION_AVAILABLE
Definition: properties.h:90
@ REASON_STRING
Definition: properties.h:80
@ SUBSCRIPTION_IDENTIFIER
Definition: properties.h:69
@ SERVER_KEEP_ALIVE
Definition: properties.h:72
@ WILDCARD_SUBSCRIPTION_AVAILABLE
Definition: properties.h:88
@ SESSION_EXPIRY_INTERVAL
Definition: properties.h:70
@ AUTHENTICATION_METHOD
Definition: properties.h:73
@ MAXIMUM_PACKET_SIZE
Definition: properties.h:87
@ RESPONSE_TOPIC
Definition: properties.h:67
@ SUBSCRIPTION_IDENTIFIERS_AVAILABLE
Definition: properties.h:89
@ MESSAGE_EXPIRY_INTERVAL
Definition: properties.h:65
@ CORRELATION_DATA
Definition: properties.h:68
@ USER_PROPERTY
Definition: properties.h:86
code type() const
Definition: properties.h:160
property(code c, int32_t val)
property(const property &other)
property(MQTTProperty &&cprop)
const char * type_name() const
Definition: properties.h:165
property(const MQTTProperty &cprop)
property(property &&other)
property & operator=(const property &rhs)
property & operator=(property &&rhs)
property(code c, string_ref name, string_ref val)
property(code c, string_ref val)
const MQTTProperty & c_struct() const
Definition: properties.h:155
#define PAHO_MQTTPP_EXPORT
Definition: export.h:40
Definition: async_client.h:49
int16_t get< int16_t >(const property &prop)
Definition: properties.h:200
T get(const property &)
Definition: properties.h:175
std::tuple< string, string > string_pair
Definition: properties.h:43
string get< string >(const property &prop)
Definition: properties.h:227
uint16_t get< uint16_t >(const property &prop)
Definition: properties.h:191
std::bad_cast bad_cast
Definition: exception.h:38
std::string string
Definition: types.h:40
uint8_t get< uint8_t >(const property &prop)
Definition: properties.h:182
int32_t get< int32_t >(const property &prop)
Definition: properties.h:218
string_pair get< string_pair >(const property &prop)
Definition: properties.h:237
uint32_t get< uint32_t >(const property &prop)
Definition: properties.h:209