Paho C++  1.0
The Paho MQTT C++ Client Library
 All Classes Files Functions Variables Typedefs Friends
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  *
11  * All rights reserved. This program and the accompanying materials
12  * are made available under the terms of the Eclipse Public License v1.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-v10.html
17  * and the Eclipse Distribution License is available at
18  * http://www.eclipse.org/org/documents/edl-v10.php.
19  *
20  * Contributors:
21  * Guilherme M. Ferreira - initial implementation and documentation
22  * Frank Pagliughi - added copy & move operations
23  *******************************************************************************/
24 
25 #ifndef __mqtt_will_options_h
26 #define __mqtt_will_options_h
27 
28 #include "MQTTAsync.h"
29 #include "mqtt/types.h"
30 #include "mqtt/message.h"
31 #include "mqtt/topic.h"
32 
33 namespace mqtt {
34 
35 class connect_options;
36 
38 
48 {
50  static constexpr int DFLT_QOS = 0;
52  static constexpr bool DFLT_RETAINED = false;
54  static const MQTTAsync_willOptions DFLT_C_STRUCT;
55 
57  MQTTAsync_willOptions opts_;
58 
60  string_ref topic_;
61 
63  binary_ref payload_;
64 
66  friend class connect_options;
67  friend class connect_options_test;
68  friend class will_options_test;
69 
81  const char* c_str(const string_ref& sr) {
82  return sr ? sr.to_string().c_str() : nullptr;
83  }
84 
85 public:
87  using ptr_t = std::shared_ptr<will_options>;
89  using const_ptr_t = std::shared_ptr<const will_options>;
90 
94  will_options();
104  will_options(string_ref top, const void *payload, size_t payload_len,
105  int qos=DFLT_QOS, bool retained=DFLT_RETAINED);
115  will_options(const topic& top, const void *payload, size_t payload_len,
116  int qos=DFLT_QOS, bool retained=DFLT_RETAINED);
126  will_options(string_ref top, binary_ref payload,
127  int qos=DFLT_QOS, bool retained=DFLT_RETAINED);
137  will_options(string_ref top, const string& payload,
138  int qos=DFLT_QOS, bool retained=DFLT_QOS);
143  will_options(const message& msg);
148  will_options(const will_options& opt);
153  will_options(will_options&& opt);
158  will_options& operator=(const will_options& opt);
168  string get_topic() const { return topic_ ? topic_.to_string() : string(); }
173  const binary_ref& get_payload() const { return payload_; }
178  string get_payload_str() const { return payload_ ? payload_.to_string() : string(); }
183  int get_qos() const { return opts_.qos; }
188  bool is_retained() const { return opts_.retained != 0; }
193  const_message_ptr get_message() const {
194  return message::create(topic_, payload_, opts_.qos, to_bool(opts_.retained));
195  }
200  void set_topic(string_ref top);
205  void set_payload(binary_ref msg);
210  void set_payload(string msg) { set_payload(binary_ref(std::move(msg))); }
215  void set_qos(const int qos) { opts_.qos = qos; }
221  void set_retained(bool retained) { opts_.retained = to_int(retained); }
222 };
223 
225 using will_options_ptr = will_options::ptr_t;
226 
228 using const_will_options_ptr = will_options::const_ptr_t;
229 
231 // end namespace mqtt
232 }
233 
234 #endif // __mqtt_will_options_h
235 
Basic types and type conversions for the Paho MQTT C++ library.
const_message_ptr get_message() const
Gets the LWT message as a message object.
Definition: will_options.h:193
string get_payload_str() const
Gets the LWT message payload as a string.
Definition: will_options.h:178
void set_payload(string msg)
Sets the LWT message text.
Definition: will_options.h:210
Holds the set of options that control how the client connects to a server.
Definition: connect_options.h:46
Represents a topic destination, used for publish/subscribe messaging.
Definition: topic.h:42
Declaration of MQTT message class.
bool is_retained() const
Gets the 'retained' flag for the LWT message.
Definition: will_options.h:188
void set_topic(string_ref top)
Sets the LWT message topic name.
will_options()
Constructs a new object using the default values.
void set_retained(bool retained)
Sets the retained flag.
Definition: will_options.h:221
std::shared_ptr< const will_options > const_ptr_t
Smart/shared pointer to a const object of this class.
Definition: will_options.h:89
An MQTT message holds everything required for an MQTT PUBLISH message.
Definition: message.h:52
std::shared_ptr< will_options > ptr_t
Smart/shared pointer to this class.
Definition: will_options.h:87
const binary_ref & get_payload() const
Gets the LWT message payload.
Definition: will_options.h:173
will_options & operator=(const will_options &opt)
Copy assignment for the LWT options.
string get_topic() const
Gets the LWT message topic name.
Definition: will_options.h:168
Declaration of MQTT topic class.
static ptr_t create(string_ref topic, const void *payload, size_t len, int qos, bool retained)
Constructs a message with the specified array as a payload, and all other values set to defaults...
Definition: message.h:164
int get_qos() const
Gets the QoS value for the LWT message.
Definition: will_options.h:183
Holds the set of options that govern the Last Will and Testament feature.
Definition: will_options.h:47
void set_payload(binary_ref msg)
Sets the LWT message text.
void set_qos(const int qos)
Sets the QoS value.
Definition: will_options.h:215
const blob & to_string() const
Gets the data buffer as a string.
Definition: buffer_ref.h:251