mqtt_cpp
topic_alias_recv.hpp
Go to the documentation of this file.
1 // Copyright Takatoshi Kondo 2020
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #if !defined(MQTT_TOPIC_ALIAS_RECV_HPP)
8 #define MQTT_TOPIC_ALIAS_RECV_HPP
9 
10 #include <string>
11 #include <unordered_map>
12 #include <array>
13 
14 #include <boost/multi_index_container.hpp>
15 #include <boost/multi_index/ordered_index.hpp>
16 #include <boost/multi_index/member.hpp>
17 
18 #include <mqtt/namespace.hpp>
19 #include <mqtt/string_view.hpp>
20 #include <mqtt/constant.hpp>
21 #include <mqtt/type.hpp>
22 #include <mqtt/move.hpp>
23 #include <mqtt/log.hpp>
24 
25 namespace MQTT_NS {
26 
27 namespace mi = boost::multi_index;
28 
30 public:
32  :max_{max} {}
33 
35  MQTT_LOG("mqtt_impl", trace)
36  << MQTT_ADD_VALUE(address, this)
37  << "topic_alias_recv insert"
38  << " topic:" << topic
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);
44  }
45  else {
46  aliases_.modify(
47  it,
48  [&](entry& e) {
49  e.topic = std::string{topic};
50  },
51  [](auto&) { BOOST_ASSERT(false); }
52  );
53 
54  }
55  }
56 
57  std::string find(topic_alias_t alias) const {
58  BOOST_ASSERT(alias >= min_ && alias <= max_);
59  std::string topic;
60  auto it = aliases_.find(alias);
61  if (it != aliases_.end()) topic = it->topic;
62 
63  MQTT_LOG("mqtt_impl", info)
64  << MQTT_ADD_VALUE(address, this)
65  << "find_topic_by_alias"
66  << " alias:" << alias
67  << " topic:" << topic;
68 
69  return topic;
70  }
71 
72  void clear() {
73  MQTT_LOG("mqtt_impl", info)
74  << MQTT_ADD_VALUE(address, this)
75  << "clear_topic_alias";
76  aliases_.clear();
77  }
78 
79  topic_alias_t max() const { return max_; }
80 
81 private:
82  static constexpr topic_alias_t min_ = 1;
83  topic_alias_t max_;
84 
85  struct entry {
86  entry(std::string topic, topic_alias_t alias)
87  : topic{force_move(topic)}, alias{alias} {}
88 
89  std::string topic;
90  topic_alias_t alias;
91  };
92  using mi_topic_alias = mi::multi_index_container<
93  entry,
94  mi::indexed_by<
95  mi::ordered_unique<
96  BOOST_MULTI_INDEX_MEMBER(entry, topic_alias_t, alias)
97  >
98  >
99  >;
100 
101  mi_topic_alias aliases_;
102 };
103 
104 } // namespace MQTT_NS
105 
106 #endif // MQTT_TOPIC_ALIAS_RECV_HPP
Definition: topic_alias_recv.hpp:29
topic_alias_t max() const
Definition: topic_alias_recv.hpp:79
topic_alias_recv(topic_alias_t max)
Definition: topic_alias_recv.hpp:31
std::string find(topic_alias_t alias) const
Definition: topic_alias_recv.hpp:57
void insert_or_update(string_view topic, topic_alias_t alias)
Definition: topic_alias_recv.hpp:34
void clear()
Definition: topic_alias_recv.hpp:72
#define MQTT_LOG(chan, sev)
Definition: log.hpp:135
#define MQTT_ADD_VALUE(name, val)
Definition: log.hpp:136
Definition: any.hpp:27
boost::string_ref string_view
Definition: string_view.hpp:64
constexpr std::remove_reference_t< T > && force_move(T &&t)
Definition: move.hpp:20
std::uint16_t topic_alias_t
Definition: type.hpp:17