7#if !defined(ASYNC_MQTT_TOPIC_ALIAS_RECV_HPP)
8#define ASYNC_MQTT_TOPIC_ALIAS_RECV_HPP
11#include <unordered_map>
14#include <boost/assert.hpp>
15#include <boost/multi_index_container.hpp>
16#include <boost/multi_index/ordered_index.hpp>
17#include <boost/multi_index/key.hpp>
19#include <async_mqtt/constant.hpp>
20#include <async_mqtt/type.hpp>
21#include <async_mqtt/log.hpp>
22#include <async_mqtt/util/move.hpp>
23#include <async_mqtt/util/string_view.hpp>
27namespace mi = boost::multi_index;
29class topic_alias_recv {
31 topic_alias_recv(topic_alias_t max)
34 void insert_or_update(string_view topic, topic_alias_t alias) {
35 ASYNC_MQTT_LOG(
"mqtt_impl", trace)
36 << ASYNC_MQTT_ADD_VALUE(address,
this)
37 <<
"topic_alias_recv insert"
39 <<
" alias:" << alias;
40 BOOST_ASSERT(!topic.empty() && alias >= min_ && alias <= max_);
41 auto it = aliases_.lower_bound(alias);
42 if (it == aliases_.end() || it->alias != alias) {
43 aliases_.emplace_hint(it, std::string(topic), alias);
49 e.topic = std::string{topic};
51 [](
auto&) { BOOST_ASSERT(
false); }
57 std::string find(topic_alias_t alias)
const {
58 BOOST_ASSERT(alias >= min_ && alias <= max_);
60 auto it = aliases_.find(alias);
61 if (it != aliases_.end()) topic = it->topic;
63 ASYNC_MQTT_LOG(
"mqtt_impl", info)
64 << ASYNC_MQTT_ADD_VALUE(address,
this)
65 <<
"find_topic_by_alias"
67 <<
" topic:" << topic;
73 ASYNC_MQTT_LOG(
"mqtt_impl", info)
74 << ASYNC_MQTT_ADD_VALUE(address,
this)
75 <<
"clear_topic_alias";
79 topic_alias_t max()
const {
return max_; }
82 static constexpr topic_alias_t min_ = 1;
86 entry(std::string topic, topic_alias_t alias)
87 : topic{force_move(topic)}, alias{alias} {}
92 using mi_topic_alias = mi::multi_index_container<
96 mi::key<&entry::alias>
101 mi_topic_alias aliases_;