24#ifndef __mqtt_topic_matcher_h
25#define __mqtt_topic_matcher_h
31#include <forward_list>
32#include <initializer_list>
92 using ptr_t = std::unique_ptr<node>;
93 using map_t = std::map<string, ptr_t>;
96 std::unique_ptr<value_type> content;
100 static ptr_t create() {
103 return std::unique_ptr<node>(
new node{});
106 using node_ptr =
typename node::ptr_t;
107 using node_map =
typename node::map_t;
121 std::forward_list<string> syms_;
123 search_node() : node_(
nullptr) {}
124 search_node(node* nd,
const std::forward_list<string>& sy)
125 : node_(nd), syms_(sy) {}
126 search_node(node* nd, std::forward_list<string>&& sy)
127 : node_(nd), syms_(std::move(sy)) {}
135 std::vector<search_node> nodes_;
151 if (snode_.syms_.empty()) {
152 pval_ = snode_.node_->content.get();
155 typename node_map::iterator child;
156 auto map_end = snode_.node_->children.end();
157 auto sym = snode_.syms_.front();
159 if ((child = snode_.node_->children.find(sym)) != map_end) {
160 auto syms = snode_.syms_;
162 nodes_.push_back({child->second.get(), std::move(syms)});
164 if ((child = snode_.node_->children.find(
"+")) != map_end) {
165 auto syms = snode_.syms_;
167 nodes_.push_back({child->second.get(), std::move(syms)});
169 if ((child = snode_.node_->children.find(
"#")) != map_end) {
170 pval_ = child->second->content.get();
174 if (!nodes_.empty()) {
176 snode_ = nodes_.back();
184 snode_ = search_node();
192 iterator(node* root,
const string&
topic) : pval_(nullptr) {
194 std::forward_list<string> syms(v.begin(), v.end());
195 snode_ = search_node(root, std::move(syms));
254 return pval_ != other.pval_ || snode_.node_ != other.snode_.node_;
288 : root_(node::create()) {}
304 : root_(node::create()) {
305 for (
const auto& v : lst) {
314 auto nd = root_.get();
317 for (
auto& field : fields) {
318 auto it = nd->children.find(field);
319 if (it == nd->children.end()) {
320 nd->children[field] = node::create();
321 it = nd->children.find(field);
323 nd = it->second.get();
327 nd->content = std::unique_ptr<value_type>(
new value_type{std::move(val)});
336 this->
insert(std::move(v));
344 auto nd = root_.get();
347 for (
auto& field : fields) {
348 auto it = nd->children.find(field);
349 if (it == nd->children.end())
352 nd = it->second.get();
354 return iterator{ nd->content.get() };
Definition topic_matcher.h:261
const value_type * operator->() const noexcept
Definition topic_matcher.h:279
const_reference operator*() const noexcept
Definition topic_matcher.h:272
Definition topic_matcher.h:113
const value_type * operator->() const noexcept
Definition topic_matcher.h:225
iterator operator++(int) noexcept
Definition topic_matcher.h:232
reference operator*() noexcept
Definition topic_matcher.h:204
const_reference operator*() const noexcept
Definition topic_matcher.h:211
iterator & operator++() noexcept
Definition topic_matcher.h:241
bool operator!=(const iterator &other) const noexcept
Definition topic_matcher.h:252
value_type * operator->() noexcept
Definition topic_matcher.h:218
Definition topic_matcher.h:78
value_type reference
Definition topic_matcher.h:83
void insert(const value_type &val)
Definition topic_matcher.h:334
const_iterator cend() const noexcept
Definition topic_matcher.h:400
string key_type
Definition topic_matcher.h:80
void insert(value_type &&val)
Definition topic_matcher.h:313
std::pair< key_type, mapped_type > value_type
Definition topic_matcher.h:82
const_iterator end() const noexcept
Definition topic_matcher.h:389
T mapped_type
Definition topic_matcher.h:81
const_iterator find(const key_type &key) const
Definition topic_matcher.h:362
topic_matcher(std::initializer_list< value_type > lst)
Definition topic_matcher.h:303
const_iterator matches(const string &topic) const
Definition topic_matcher.h:378
iterator matches(const string &topic)
Definition topic_matcher.h:370
iterator find(const key_type &key)
Definition topic_matcher.h:343
const value_type & const_reference
Definition topic_matcher.h:84
topic_matcher()
Definition topic_matcher.h:287
static std::vector< std::string > split(const std::string &topic)
Definition async_client.h:49
std::string string
Definition types.h:40