paho-mqtt-cpp
MQTT C++ Client for POSIX and Windows
Loading...
Searching...
No Matches
Data Structures | Public Types | Public Member Functions
mqtt::topic_matcher< T > Class Template Reference

#include <topic_matcher.h>

Data Structures

class  const_iterator
 
class  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&
 

Public Member Functions

 topic_matcher ()
 
 topic_matcher (std::initializer_list< value_type > lst)
 
void insert (value_type &&val)
 
void insert (const value_type &val)
 
iterator find (const key_type &key)
 
const_iterator find (const key_type &key) const
 
iterator matches (const string &topic)
 
const_iterator matches (const string &topic) const
 
const_iterator end () const noexcept
 
const_iterator cend () const noexcept
 

Detailed Description

template<typename T>
class mqtt::topic_matcher< T >

This can be used to get an iterator to all items that have a filter that matches a topic. 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, though, that a single incoming topic could match against several items in the collection. For example, the topic: data/temperature/engine

Could match against the filters: data/temperature/# data/+/engine

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: insert({"some/random/topic", 42}) insert({"some/#", 99}) insert({"some/+/topic", 33})

The collection would be built like: "some" -> <null> "random" -> <null> "topic" -> <42> "#" -> <99> "+" -> <null> "topic" -> <33>

Member Typedef Documentation

◆ key_type

template<typename T >
using mqtt::topic_matcher< T >::key_type = string

◆ mapped_type

template<typename T >
using mqtt::topic_matcher< T >::mapped_type = T

◆ value_type

template<typename T >
using mqtt::topic_matcher< T >::value_type = std::pair<key_type, mapped_type>

◆ reference

template<typename T >
using mqtt::topic_matcher< T >::reference = value_type

◆ const_reference

template<typename T >
using mqtt::topic_matcher< T >::const_reference = const value_type&

Constructor & Destructor Documentation

◆ topic_matcher() [1/2]

template<typename T >
mqtt::topic_matcher< T >::topic_matcher ( )
inline

Creates new, empty collection.

◆ topic_matcher() [2/2]

template<typename T >
mqtt::topic_matcher< T >::topic_matcher ( std::initializer_list< value_type > lst)
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 }
}
Parameters
lstThe list of key/value pairs to populate the collection.

Member Function Documentation

◆ insert() [1/2]

template<typename T >
void mqtt::topic_matcher< T >::insert ( value_type && val)
inline

Inserts a new key/value pair into the collection.

Parameters
valThe value to place in the collection.

◆ insert() [2/2]

template<typename T >
void mqtt::topic_matcher< T >::insert ( const value_type & val)
inline

Inserts a new value into the collection.

Parameters
keyThe topic/filter entry
valThe value to associate with that entry.

◆ find() [1/2]

template<typename T >
iterator mqtt::topic_matcher< T >::find ( const key_type & key)
inline

Gets a pointer to the value at the requested key.

Parameters
keyThe topic/filter entry to find.
Returns
An iterator to the value if found, end() if not found.

◆ find() [2/2]

template<typename T >
const_iterator mqtt::topic_matcher< T >::find ( const key_type & key) const
inline

Gets a const pointer to the value at the requested key.

Parameters
keyThe topic/filter entry to find.
Returns
A const pointer to the value if found, nullptr if not found.

◆ matches() [1/2]

template<typename T >
iterator mqtt::topic_matcher< T >::matches ( const string & topic)
inline

Gets an iterator that can find the matches to the topic.

Parameters
topicThe topic to search for matches.
Returns
An iterator that can find the matches to the topic.

◆ matches() [2/2]

template<typename T >
const_iterator mqtt::topic_matcher< T >::matches ( const string & topic) const
inline

Gets a const iterator that can find the matches to the topic.

Parameters
topicThe topic to search for matches.
Returns
A const iterator that can find the matches to the topic.

◆ end()

template<typename T >
const_iterator mqtt::topic_matcher< T >::end ( ) const
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.

Returns
An empty/null iterator indicating the end of the collection.

◆ cend()

template<typename T >
const_iterator mqtt::topic_matcher< T >::cend ( ) const
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.

Returns
An empty/null iterator indicating the end of the collection.

The documentation for this class was generated from the following file: