PahoMqttCpp
MQTT C++ Client for POSIX and Windows
Loading...
Searching...
No Matches
topic.h
Go to the documentation of this file.
1
7
8/*******************************************************************************
9 * Copyright (c) 2013-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_topic_h
25#define __mqtt_topic_h
26
27#include <vector>
28
29#include "MQTTAsync.h"
30#include "mqtt/delivery_token.h"
31#include "mqtt/message.h"
33#include "mqtt/types.h"
34
35namespace mqtt {
36
37class iasync_client;
38
40
44class topic
45{
47 iasync_client& cli_;
49 string name_;
51 int qos_;
53 bool retained_;
54
55public:
57 using ptr_t = std::shared_ptr<topic>;
59 using const_ptr_t = std::shared_ptr<const topic>;
60
69 iasync_client& cli, const string& name, int qos = message::DFLT_QOS,
70 bool retained = message::DFLT_RETAINED
71 )
72 : cli_(cli), name_(name), qos_(qos), retained_(retained) {}
81 static ptr_t create(
82 iasync_client& cli, const string& name, int qos = message::DFLT_QOS,
83 bool retained = message::DFLT_RETAINED
84 ) {
85 return std::make_shared<topic>(cli, name, qos, retained);
86 }
91 iasync_client& get_client() { return cli_; }
96 const string& get_name() const { return name_; }
103 static std::vector<std::string> split(const std::string& topic);
108 int get_qos() const { return qos_; }
113 bool get_retained() const { return retained_; }
118 void set_qos(int qos) {
120 qos_ = qos;
121 }
126 void set_retained(bool retained) { retained_ = retained; }
135 delivery_token_ptr publish(const void* payload, size_t n);
147 delivery_token_ptr publish(const void* payload, size_t n, int qos, bool retained);
166 delivery_token_ptr publish(binary_ref payload, int qos, bool retained);
176 string to_string() const { return name_; }
177};
178
181
184
186// Topic Filter
188
201{
203 std::vector<string> fields_;
204
205public:
213 explicit topic_filter(const string& filter);
219 static bool is_wildcard(char c) {
220 return c == '+' || c == '#';
221 }
227 static bool is_wildcard(const string& s) {
228 return s.size() == 1 && is_wildcard(s[0]);
229 }
237 static bool has_wildcards(const string& filter);
244 bool has_wildcards() const;
252 bool matches(const string& topic) const;
253};
254
256} // namespace mqtt
257
258#endif // __mqtt_topic_h
Definition iasync_client.h:60
static constexpr bool DFLT_RETAINED
Definition message.h:62
static void validate_qos(int qos)
Definition message.h:335
static constexpr int DFLT_QOS
Definition message.h:60
Definition subscribe_options.h:49
Definition topic.h:201
static bool has_wildcards(const string &filter)
static bool is_wildcard(const string &s)
Definition topic.h:227
bool has_wildcards() const
static bool is_wildcard(char c)
Definition topic.h:219
bool matches(const string &topic) const
topic_filter(const string &filter)
Definition topic.h:45
bool get_retained() const
Definition topic.h:113
static ptr_t create(iasync_client &cli, const string &name, int qos=message::DFLT_QOS, bool retained=message::DFLT_RETAINED)
Definition topic.h:81
iasync_client & get_client()
Definition topic.h:91
void set_qos(int qos)
Definition topic.h:118
delivery_token_ptr publish(const void *payload, size_t n)
delivery_token_ptr publish(const void *payload, size_t n, int qos, bool retained)
topic(iasync_client &cli, const string &name, int qos=message::DFLT_QOS, bool retained=message::DFLT_RETAINED)
Definition topic.h:68
int get_qos() const
Definition topic.h:108
std::shared_ptr< topic > ptr_t
Definition topic.h:57
void set_retained(bool retained)
Definition topic.h:126
token_ptr subscribe(const subscribe_options &opts=subscribe_options())
std::shared_ptr< const topic > const_ptr_t
Definition topic.h:59
delivery_token_ptr publish(binary_ref payload)
delivery_token_ptr publish(binary_ref payload, int qos, bool retained)
static std::vector< std::string > split(const std::string &topic)
const string & get_name() const
Definition topic.h:96
string to_string() const
Definition topic.h:176
Definition async_client.h:60
topic::const_ptr_t const_topic_ptr
Definition topic.h:183
token::ptr_t token_ptr
Definition token.h:513
topic::ptr_t topic_ptr
Definition topic.h:180
delivery_token::ptr_t delivery_token_ptr
Definition delivery_token.h:127