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);
105  property(code c, string_ref val);
112  property(code c, string_ref name, string_ref val);
117  explicit property(const MQTTProperty& cprop);
123  explicit property(MQTTProperty&& cprop);
128  property(const property& other);
133  property(property&& other);
137  ~property();
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:
274  properties();
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_; }
314  properties& operator=(const properties& rhs);
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 
const MQTTProperty & c_struct() const
Definition: properties.h:155
std::bad_cast bad_cast
Definition: exception.h:38
properties(const MQTTProperties &cprops)
Definition: properties.h:292
int16_t get< int16_t >(const property &prop)
Definition: properties.h:200
std::string string
Definition: types.h:40
Definition: properties.h:73
string_pair get< string_pair >(const property &prop)
Definition: properties.h:237
uint8_t get< uint8_t >(const property &prop)
Definition: properties.h:182
Definition: properties.h:86
Definition: properties.h:80
Definition: properties.h:81
std::tuple< string, string > string_pair
Definition: properties.h:43
Definition: properties.h:69
Definition: properties.h:78
int32_t get< int32_t >(const property &prop)
Definition: properties.h:218
Definition: properties.h:50
Definition: properties.h:82
properties & operator=(const properties &rhs)
Definition: properties.h:72
~properties()
Definition: properties.h:303
void add(const property &prop)
Definition: properties.h:336
Definition: properties.h:74
string get< string >(const property &prop)
Definition: properties.h:227
Definition: properties.h:83
Definition: properties.h:255
Definition: properties.h:84
property & operator=(const property &rhs)
const MQTTProperties & c_struct() const
Definition: properties.h:308
size_t size() const
Definition: properties.h:331
bool empty() const
Definition: properties.h:326
Definition: properties.h:67
Definition: properties.h:85
Definition: properties.h:87
void clear()
Definition: properties.h:342
code
Definition: properties.h:63
size_t count(property::code propid) const
Definition: properties.h:364
#define PAHO_MQTTPP_EXPORT
Definition: export.h:40
uint32_t get< uint32_t >(const property &prop)
Definition: properties.h:209
property(code c, int32_t val)
bool contains(property::code propid) const
Definition: properties.h:350
Definition: properties.h:70
Definition: properties.h:66
Definition: properties.h:76
properties(const properties &other)
Definition: properties.h:279
properties(properties &&other)
Definition: properties.h:285
uint16_t get< uint16_t >(const property &prop)
Definition: properties.h:191
Definition: async_client.h:49
const char * type_name() const
Definition: properties.h:165
Definition: properties.h:68
code type() const
Definition: properties.h:160
Definition: properties.h:64
Definition: properties.h:79
Definition: properties.h:65