mqtt_cpp
packet_id_manager.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_PACKET_ID_MANAGER_HPP)
8 #define MQTT_PACKET_ID_MANAGER_HPP
9 
10 #include <mqtt/config.hpp> // should be top to configure variant limit
11 
12 #include <mqtt/optional.hpp>
13 #include <mqtt/value_allocator.hpp>
14 
15 namespace MQTT_NS {
16 
17 template <typename PacketId>
19  using packet_id_t = PacketId;
20 
21 public:
22 
31  optional<packet_id_t> acquire_unique_id() {
32  return va_.allocate();
33  }
34 
42  bool register_id(packet_id_t packet_id) {
43  return va_.use(packet_id);
44  }
45 
52  void release_id(packet_id_t packet_id) {
53  va_.deallocate(packet_id);
54  }
55 
59  void clear() {
60  va_.clear();
61  }
62 
63 private:
64  value_allocator<packet_id_t> va_ {1, std::numeric_limits<packet_id_t>::max()};
65 };
66 
67 } // namespace MQTT_NS
68 
69 #endif // MQTT_PACKET_ID_MANAGER_HPP
Definition: packet_id_manager.hpp:18
void clear()
Clear all packet ids.
Definition: packet_id_manager.hpp:59
optional< packet_id_t > acquire_unique_id()
Acquire the new unique packet id. If all packet ids are already in use, then returns nullopt After ac...
Definition: packet_id_manager.hpp:31
bool register_id(packet_id_t packet_id)
Register packet_id to the library. After registering the packet_id, you can call acquired_* functions...
Definition: packet_id_manager.hpp:42
void release_id(packet_id_t packet_id)
Release packet_id.
Definition: packet_id_manager.hpp:52
void deallocate(value_type value)
Dellocate one value.
Definition: value_allocator.hpp:138
optional< value_type > allocate()
Allocate one value.
Definition: value_allocator.hpp:98
void clear()
Clear all allocated or used values.
Definition: value_allocator.hpp:255
bool use(value_type value)
Declare the value as used.
Definition: value_allocator.hpp:237
Definition: any.hpp:27