/* * Copyright (c) 2017-2023 zhllxt * * author : zhllxt * email : 37792738@qq.com * * Distributed under the Boost Software License, Version 1.0. (See accompanying * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) * * refrenced from : mqtt_cpp/include/mqtt/packet_id_manager.hpp */ #ifndef __ASIO2_MQTT_ID_MGR_HPP__ #define __ASIO2_MQTT_ID_MGR_HPP__ #if defined(_MSC_VER) && (_MSC_VER >= 1200) #pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include namespace asio2::mqtt { template class idmgr; template class idmgr> { public: using id_type = std::remove_cv_t>; static_assert(std::is_integral_v); idmgr() = default; ~idmgr() = default; /** * @brief Get a new unique id */ inline id_type get() noexcept { id_type r = id_.fetch_add(static_cast(1)); while (r == 0) { r = id_.fetch_add(static_cast(1)); } return r; } /** * @brief Checks whether contains the given id. */ inline bool contains(id_type id) const noexcept { return (id_.load() == id); } /** * @brief Release the id. */ inline void release(id_type id) noexcept { std::ignore = id; } /** * @brief Clear all ids. */ inline void clear() noexcept { id_.store(0); } private: std::atomic id_{ static_cast(1) }; }; template class idmgr> { public: using id_type = std::remove_cv_t>; static_assert(std::is_integral_v); idmgr() = default; ~idmgr() = default; /** * @brief Get a new unique id, return 0 if failed. */ id_type get() { if (used_.size() == (std::numeric_limits::max)()) return static_cast(0); id_type id; if (used_.empty()) { id = static_cast(1); } else { id = *(used_.rbegin()); for (;;) { id++; if (id == static_cast(0)) id++; if (used_.find(id) == used_.end()) break; } } used_.emplace(id); return id; } /** * @brief Checks whether contains the given id. */ bool contains(id_type id) { return (used_.find(id) != used_.end()); } /** * @brief Release the id. */ void release(id_type id) { used_.erase(id); } /** * @brief Clear all ids. */ void clear() { used_.clear(); } private: std::set used_{}; }; } #endif // !__ASIO2_MQTT_ID_MGR_HPP__