paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
will_options.h
Go to the documentation of this file.
1 
8 /*******************************************************************************
9  * Copyright (c) 2016 Guilherme M. Ferreira <guilherme.maciel.ferreira@gmail.com>
10  * Copyright (c) 2016-2023 Frank Pagliughi <fpagliughi@mindspring.com>
11  *
12  * All rights reserved. This program and the accompanying materials
13  * are made available under the terms of the Eclipse Public License v2.0
14  * and Eclipse Distribution License v1.0 which accompany this distribution.
15  *
16  * The Eclipse Public License is available at
17  * http://www.eclipse.org/legal/epl-v20.html
18  * and the Eclipse Distribution License is available at
19  * http://www.eclipse.org/org/documents/edl-v10.php.
20  *
21  * Contributors:
22  * Guilherme M. Ferreira - initial implementation and documentation
23  * Frank Pagliughi - added copy & move operations
24  *******************************************************************************/
25 
26 #ifndef __mqtt_will_options_h
27 #define __mqtt_will_options_h
28 
29 #include "MQTTAsync.h"
30 #include "mqtt/types.h"
31 #include "mqtt/message.h"
32 #include "mqtt/topic.h"
33 #include "mqtt/platform.h"
34 
35 namespace mqtt {
36 
37 class connect_options;
38 
40 
50 {
51 public:
53  PAHO_MQTTPP_EXPORT static const int DFLT_QOS; // =0;
55  PAHO_MQTTPP_EXPORT static const bool DFLT_RETAINED; // =false;
56 
57 private:
59  PAHO_MQTTPP_EXPORT static const MQTTAsync_willOptions DFLT_C_STRUCT;
60 
62  MQTTAsync_willOptions opts_;
63 
65  string_ref topic_;
66 
68  binary_ref payload_;
69 
77  properties props_;
78 
80  friend class connect_options;
81 
93  const char* c_str(const string_ref& sr) {
94  return sr ? sr.to_string().c_str() : nullptr;
95  }
96 
97 public:
99  using ptr_t = std::shared_ptr<will_options>;
101  using const_ptr_t = std::shared_ptr<const will_options>;
103  using unique_ptr_t = std::unique_ptr<will_options>;
104 
119  will_options(string_ref top, const void *payload, size_t payload_len,
120  int qos=DFLT_QOS, bool retained=DFLT_RETAINED,
121  const properties& props=properties());
132  will_options(const topic& top, const void *payload, size_t payload_len,
133  int qos=DFLT_QOS, bool retained=DFLT_RETAINED,
134  const properties& props=properties());
146  int qos=DFLT_QOS, bool retained=DFLT_RETAINED,
147  const properties& props=properties());
158  will_options(string_ref top, const string& payload,
159  int qos=DFLT_QOS, bool retained=DFLT_QOS,
160  const properties& props=properties());
165  will_options(const message& msg);
189  #if defined(UNIT_TESTS)
190  const MQTTAsync_willOptions& c_struct() const { return opts_; }
191  #endif
196  string get_topic() const { return topic_ ? topic_.to_string() : string(); }
201  const binary_ref& get_payload() const { return payload_; }
206  string get_payload_str() const { return payload_ ? payload_.to_string() : string(); }
211  int get_qos() const { return opts_.qos; }
216  bool is_retained() const { return opts_.retained != 0; }
222  return message::create(topic_, payload_, opts_.qos, to_bool(opts_.retained));
223  }
238  void set_payload(string msg) { set_payload(binary_ref(std::move(msg))); }
243  void set_qos(const int qos) { opts_.qos = qos; }
249  void set_retained(bool retained) { opts_.retained = to_int(retained); }
254  const properties& get_properties() const { return props_; }
259  void set_properties(const properties& props) { props_ = props; }
264  void set_properties(properties&& props) { props_ = std::move(props); }
265 };
266 
269 
272 
275 
277 // end namespace mqtt
278 }
279 
280 #endif // __mqtt_will_options_h
281 
const blob & to_string() const
Definition: buffer_ref.h:251
Definition: connect_options.h:49
Definition: message.h:56
static ptr_t create(string_ref topic, const void *payload, size_t len, int qos, bool retained, const properties &props=properties())
Definition: message.h:168
Definition: properties.h:256
Definition: topic.h:44
Definition: will_options.h:50
const properties & get_properties() const
Definition: will_options.h:254
static PAHO_MQTTPP_EXPORT const int DFLT_QOS
Definition: will_options.h:53
void set_payload(binary_ref msg)
will_options(string_ref top, binary_ref payload, int qos=DFLT_QOS, bool retained=DFLT_RETAINED, const properties &props=properties())
const binary_ref & get_payload() const
Definition: will_options.h:201
will_options & operator=(will_options &&opt)
void set_qos(const int qos)
Definition: will_options.h:243
will_options & operator=(const will_options &opt)
bool is_retained() const
Definition: will_options.h:216
will_options(const message &msg)
void set_retained(bool retained)
Definition: will_options.h:249
will_options(const topic &top, const void *payload, size_t payload_len, int qos=DFLT_QOS, bool retained=DFLT_RETAINED, const properties &props=properties())
void set_topic(string_ref top)
void set_payload(string msg)
Definition: will_options.h:238
const_message_ptr get_message() const
Definition: will_options.h:221
will_options(will_options &&opt)
will_options(string_ref top, const string &payload, int qos=DFLT_QOS, bool retained=DFLT_QOS, const properties &props=properties())
string get_payload_str() const
Definition: will_options.h:206
void set_properties(const properties &props)
Definition: will_options.h:259
string get_topic() const
Definition: will_options.h:196
static PAHO_MQTTPP_EXPORT const bool DFLT_RETAINED
Definition: will_options.h:55
int get_qos() const
Definition: will_options.h:211
will_options(string_ref top, const void *payload, size_t payload_len, int qos=DFLT_QOS, bool retained=DFLT_RETAINED, const properties &props=properties())
std::shared_ptr< will_options > ptr_t
Definition: will_options.h:99
will_options(const will_options &opt)
std::shared_ptr< const will_options > const_ptr_t
Definition: will_options.h:101
std::unique_ptr< will_options > unique_ptr_t
Definition: will_options.h:103
void set_properties(properties &&props)
Definition: will_options.h:264
#define PAHO_MQTTPP_EXPORT
Definition: export.h:40
Definition: async_client.h:49
bool to_bool(int n)
Definition: types.h:161
will_options::ptr_t will_options_ptr
Definition: will_options.h:268
will_options::unique_ptr_t will_options_unique_ptr
Definition: will_options.h:274
will_options::const_ptr_t const_will_options_ptr
Definition: will_options.h:271
std::string string
Definition: types.h:40
buffer_ref< char > binary_ref
Definition: buffer_ref.h:298
message::const_ptr_t const_message_ptr
Definition: message.h:368
int to_int(bool b)
Definition: types.h:167