![]() |
PahoMqttCpp
MQTT C++ Client for POSIX and Windows
|
#include <topic_matcher.h>
Data Structures | |
class | const_iterator |
class | const_match_iterator |
class | iterator |
class | match_iterator |
Public Types | |
using | key_type = string |
using | mapped_type = T |
using | value_type = std::pair<key_type, mapped_type> |
using | reference = value_type |
using | const_reference = const value_type& |
using | value_ptr = std::unique_ptr<value_type> |
using | mapped_ptr = std::unique_ptr<mapped_type> |
Public Member Functions | |
topic_matcher () | |
topic_matcher (std::initializer_list< value_type > lst) | |
bool | empty () const |
void | insert (value_type &&val) |
void | insert (const value_type &val) |
mapped_ptr | remove (const key_type &filter) |
void | prune () |
iterator | begin () |
iterator | end () |
const_iterator | end () const noexcept |
const_iterator | cbegin () const |
const_iterator | cend () const noexcept |
iterator | find (const key_type &filter) |
const_iterator | find (const key_type &filter) const |
match_iterator | matches (const string &topic) |
const_match_iterator | matches (const string &topic) const |
const_match_iterator | matches_end () const noexcept |
const_match_iterator | matches_cend () const noexcept |
bool | has_match (const string &topic) |
A collection of MQTT topic filters mapped to arbitrary values.
This can be used to get an iterator to all filters in the collection that match a topic. A typical use case might be to match incoming messages to specific callback functions based on topics, such as
To test against a single filter, see [TopicFilter
](crate::TopicFilter). This collection is more commonly used when there are a number of filters and each needs to be associated with a particular action or piece of data. Note, however, that a single incoming topic could match against several items in the collection. For example, the topic:
Could match against the filters:
Thus, the collection gives an iterator for the items matching a topic.
A common use for this would be to store callbacks to process incoming messages based on topics.
This code was adapted from the Eclipse Python MQTTMatcher
class:
https://github.com/eclipse/paho.mqtt.python/blob/master/src/paho/mqtt/matcher.py
which use a prefix tree (trie) to store the values.
For example, if you had a topic_mapper<int>
and you inserted:
The collection would be built like:
Note that the collection has two types of iterators. The basic iterator
is a normal C++ iterator over all the items in the collection. It will visit every node in the collection and produce all items. This is not the typical use case for the collection, but can be used for diagnostics, etc, to show the full contents of the collection.
The more common use case is the match_iterator
, returned by the topic_matcher::matches(string)
method. This is an optimized search iterator for finding all the filters and values that match the specified topic string.
using mqtt::topic_matcher< T >::key_type = string |
using mqtt::topic_matcher< T >::mapped_type = T |
using mqtt::topic_matcher< T >::value_type = std::pair<key_type, mapped_type> |
using mqtt::topic_matcher< T >::reference = value_type |
using mqtt::topic_matcher< T >::const_reference = const value_type& |
using mqtt::topic_matcher< T >::value_ptr = std::unique_ptr<value_type> |
using mqtt::topic_matcher< T >::mapped_ptr = std::unique_ptr<mapped_type> |
|
inline |
Creates new, empty collection.
|
inline |
Creates a new collection with a list of key/value pairs.
This can be used to create a connection from a table of entries, as key/value pairs, like:
topic_matcher<int> matcher { { "#", -1 }, { "some/random/topic", 42 }, { "some/#", 99 } }
lst | The list of key/value pairs to populate the collection. |
|
inline |
Determines if the collection is empty.
|
inline |
Inserts a new key/value pair into the collection.
val | The value to place in the collection. |
|
inline |
Inserts a new value into the collection.
key | The topic/filter entry |
val | The value to associate with that entry. |
|
inline |
Removes an entry from the collection.
This removes the value from the internal node, but leaves the node in the collection, even if it is empty.
filter | The topic filter to remove. |
|
inline |
Removes the empty nodes in the collection.
|
inline |
Gets an iterator to the full collection of filters.
|
inline |
Gets an iterator to the end of the collection of filters.
|
inlinenoexcept |
Gets an iterator to the end of the collection of filters.
|
inline |
Gets a const iterator to the full collection of filters.
|
inlinenoexcept |
Gets a const iterator to the end of the collection of filters.
|
inline |
Gets a pointer to the value at the requested key.
filter | The topic filter entry to find. |
|
inline |
Gets a const pointer to the value at the requested key.
filter | The topic filter entry to find. |
|
inline |
Gets an match_iterator that can find the matches to the topic.
topic | The topic to search for matches. |
|
inline |
Gets a const iterator that can find the matches to the topic.
topic | The topic to search for matches. |
|
inlinenoexcept |
Gets an iterator for the end of the collection.
This simply returns an empty/null iterator which we can use to signal the end of the collection.
|
inlinenoexcept |
Gets an iterator for the end of the collection.
This simply returns an empty/null iterator which we can use to signal the end of the collection.
|
inline |
Determines if there are any matches for the specified topic.
topic | The topic to search for matches. |