mqtt_cpp
topic_alias_map.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_MAP_HPP)
8 #define MQTT_TOPIC_ALIAS_MAP_HPP
9 
10 #include <string>
11 #include <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/log.hpp>
23 
24 namespace MQTT_NS {
25 
26 using topic_alias_map_t = std::map<topic_alias_t, std::string>;
27 
29  BOOST_ASSERT(alias > 0); //alias <= topic_alias_max is always true
30 
31  MQTT_LOG("mqtt_impl", info)
32  << MQTT_ADD_VALUE(address, &m)
33  << "register_topic_alias"
34  << " topic:" << topic
35  << " alias:" << alias;
36 
37  if (topic.empty()) {
38  m.erase(alias);
39  }
40  else {
41  m[alias] = std::string(topic); // overwrite
42  }
43 }
44 
45 inline std::string find_topic_by_alias(topic_alias_map_t const& m, topic_alias_t alias) {
46  BOOST_ASSERT(alias > 0); //alias <= topic_alias_max is always true
47 
48  std::string topic;
49  auto it = m.find(alias);
50  if (it != m.end()) topic = it->second;
51 
52  MQTT_LOG("mqtt_impl", info)
53  << MQTT_ADD_VALUE(address, &m)
54  << "find_topic_by_alias"
55  << " alias:" << alias
56  << " topic:" << topic;
57 
58  return topic;
59 }
60 
62  MQTT_LOG("mqtt_impl", info)
63  << MQTT_ADD_VALUE(address, &m)
64  << "clear_topic_alias";
65 
66  m.clear();
67 }
68 
69 } // namespace MQTT_NS
70 
71 #endif // MQTT_TOPIC_ALIAS_MAP_HPP
#define MQTT_LOG(chan, sev)
Definition: log.hpp:135
#define MQTT_ADD_VALUE(name, val)
Definition: log.hpp:136
Definition: any.hpp:27
std::map< topic_alias_t, std::string > topic_alias_map_t
Definition: topic_alias_map.hpp:26
boost::string_ref string_view
Definition: string_view.hpp:64
void register_topic_alias(topic_alias_map_t &m, string_view topic, topic_alias_t alias)
Definition: topic_alias_map.hpp:28
std::uint16_t topic_alias_t
Definition: type.hpp:17
void clear_topic_alias(topic_alias_map_t &m)
Definition: topic_alias_map.hpp:61
std::string find_topic_by_alias(topic_alias_map_t const &m, topic_alias_t alias)
Definition: topic_alias_map.hpp:45