async_mqtt 5.0.0
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_PACKET_ID_MANAGER_HPP)
8#define ASYNC_MQTT_PACKET_ID_MANAGER_HPP
9
10#include <async_mqtt/util/value_allocator.hpp>
11
12namespace async_mqtt {
13
14template <typename PacketId>
15class packet_id_manager {
16 using packet_id_type = PacketId;
17
18public:
19
28 optional<packet_id_type> acquire_unique_id() {
29 return va_.allocate();
30 }
31
39 bool register_id(packet_id_type packet_id) {
40 return va_.use(packet_id);
41 }
42
47 bool is_used_id(packet_id_type packet_id) const {
48 return va_.is_used(packet_id);
49 }
50
57 void release_id(packet_id_type packet_id) {
58 va_.deallocate(packet_id);
59 }
60
64 void clear() {
65 va_.clear();
66 }
67
68private:
69 value_allocator<packet_id_type> va_ {1, std::numeric_limits<packet_id_type>::max()};
70};
71
72} // namespace async_mqtt
73
74#endif // ASYNC_MQTT_PACKET_ID_MANAGER_HPP