Loading...
Searching...
No Matches
topic.h
Go to the documentation of this file.
/*******************************************************************************
* Copyright (c) 2013-2024 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
*******************************************************************************/
#ifndef __mqtt_topic_h
#define __mqtt_topic_h
#include <vector>
#include <variant>
#include "MQTTAsync.h"
#include "mqtt/delivery_token.h"
#include "mqtt/message.h"
#include "mqtt/subscribe_options.h"
#include "mqtt/types.h"
namespace mqtt {
class iasync_client;
class topic
{
iasync_client& cli_;
string name_;
int qos_;
bool retained_;
public:
using ptr_t = std::shared_ptr<topic>;
using const_ptr_t = std::shared_ptr<const topic>;
topic(
iasync_client& cli, const string& name, int qos = message::DFLT_QOS,
bool retained = message::DFLT_RETAINED
)
: cli_(cli), name_(name), qos_(qos), retained_(retained) {}
static ptr_t create(
iasync_client& cli, const string& name, int qos = message::DFLT_QOS,
bool retained = message::DFLT_RETAINED
) {
return std::make_shared<topic>(cli, name, qos, retained);
}
iasync_client& get_client() { return cli_; }
const string& get_name() const { return name_; }
static std::vector<std::string> split(const std::string& topic);
int get_qos() const { return qos_; }
bool get_retained() const { return retained_; }
void set_qos(int qos) {
message::validate_qos(qos);
qos_ = qos;
}
void set_retained(bool retained) { retained_ = retained; }
delivery_token_ptr publish(const void* payload, size_t n);
delivery_token_ptr publish(const void* payload, size_t n, int qos, bool retained);
delivery_token_ptr publish(binary_ref payload);
delivery_token_ptr publish(binary_ref payload, int qos, bool retained);
token_ptr subscribe(const subscribe_options& opts = subscribe_options());
string to_string() const { return name_; }
};
using topic_ptr = topic::ptr_t;
using const_topic_ptr = topic::const_ptr_t;
// Topic Filter
class topic_filter
{
std::variant<string, std::vector<string>> filter_;
public:
explicit topic_filter(const string& filter);
static bool is_wildcard(char c) { return c == '+' || c == '#'; }
static bool is_wildcard(const string& s) { return s.size() == 1 && is_wildcard(s[0]); }
static bool has_wildcards(const string& filter);
bool has_wildcards() const;
bool matches(const string& topic) const;
string to_string() const;
};
} // namespace mqtt
#endif // __mqtt_topic_h
mqtt::iasync_client
Definition iasync_client.h:60
mqtt::message::DFLT_RETAINED
static constexpr bool DFLT_RETAINED
Definition message.h:62
mqtt::message::validate_qos
static void validate_qos(int qos)
Definition message.h:330
mqtt::message::DFLT_QOS
static constexpr int DFLT_QOS
Definition message.h:60
mqtt::subscribe_options
Definition subscribe_options.h:49
mqtt::topic_filter::has_wildcards
static bool has_wildcards(const string &filter)
mqtt::topic_filter::is_wildcard
static bool is_wildcard(const string &s)
Definition topic.h:242
mqtt::topic_filter::has_wildcards
bool has_wildcards() const
mqtt::topic_filter::is_wildcard
static bool is_wildcard(char c)
Definition topic.h:236
mqtt::topic_filter::matches
bool matches(const string &topic) const
mqtt::topic_filter::to_string
string to_string() const
mqtt::topic_filter::topic_filter
topic_filter(const string &filter)
mqtt::topic
Definition topic.h:54
mqtt::topic::get_retained
bool get_retained() const
Definition topic.h:122
mqtt::topic::create
static ptr_t create(iasync_client &cli, const string &name, int qos=message::DFLT_QOS, bool retained=message::DFLT_RETAINED)
Definition topic.h:90
mqtt::topic::get_client
iasync_client & get_client()
Definition topic.h:100
mqtt::topic::set_qos
void set_qos(int qos)
Definition topic.h:127
mqtt::topic::publish
delivery_token_ptr publish(const void *payload, size_t n)
mqtt::topic::publish
delivery_token_ptr publish(const void *payload, size_t n, int qos, bool retained)
mqtt::topic::topic
topic(iasync_client &cli, const string &name, int qos=message::DFLT_QOS, bool retained=message::DFLT_RETAINED)
Definition topic.h:77
mqtt::topic::get_qos
int get_qos() const
Definition topic.h:117
mqtt::topic::ptr_t
std::shared_ptr< topic > ptr_t
Definition topic.h:66
mqtt::topic::set_retained
void set_retained(bool retained)
Definition topic.h:135
mqtt::topic::subscribe
token_ptr subscribe(const subscribe_options &opts=subscribe_options())
mqtt::topic::const_ptr_t
std::shared_ptr< const topic > const_ptr_t
Definition topic.h:68
mqtt::topic::publish
delivery_token_ptr publish(binary_ref payload)
mqtt::topic::publish
delivery_token_ptr publish(binary_ref payload, int qos, bool retained)
mqtt::topic::split
static std::vector< std::string > split(const std::string &topic)
mqtt::topic::get_name
const string & get_name() const
Definition topic.h:105
mqtt::topic::to_string
string to_string() const
Definition topic.h:185
delivery_token.h
message.h
mqtt
Definition async_client.h:60
mqtt::binary_ref
buffer_ref< char > binary_ref
Definition buffer_ref.h:305
mqtt::topic_ptr
topic::ptr_t topic_ptr
Definition topic.h:189
mqtt::delivery_token_ptr
delivery_token::ptr_t delivery_token_ptr
Definition delivery_token.h:127
mqtt::token_ptr
token::ptr_t token_ptr
Definition token.h:513
mqtt::const_topic_ptr
topic::const_ptr_t const_topic_ptr
Definition topic.h:192
subscribe_options.h
types.h