async_mqtt 9.0.1
Loading...
Searching...
No Matches
packet_id_manager.hpp
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(ASYNC_MQTT_UTIL_PACKET_ID_MANAGER_HPP)
8#define ASYNC_MQTT_UTIL_PACKET_ID_MANAGER_HPP
9
10#include <optional>
11
12#include <async_mqtt/util/value_allocator.hpp>
13
14namespace async_mqtt {
15
16template <typename PacketId>
17class packet_id_manager {
18 using packet_id_type = PacketId;
19
20public:
21
30 std::optional<packet_id_type> acquire_unique_id() {
31 return va_.allocate();
32 }
33
41 bool register_id(packet_id_type packet_id) {
42 return va_.use(packet_id);
43 }
44
49 bool is_used_id(packet_id_type packet_id) const {
50 return va_.is_used(packet_id);
51 }
52
59 void release_id(packet_id_type packet_id) {
60 va_.deallocate(packet_id);
61 }
62
66 void clear() {
67 va_.clear();
68 }
69
70private:
71 value_allocator<packet_id_type> va_ {1, std::numeric_limits<packet_id_type>::max()};
72};
73
74} // namespace async_mqtt
75
76#endif // ASYNC_MQTT_UTIL_PACKET_ID_MANAGER_HPP