Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:38:19

0001 //
0002 // Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MQTT5_CHANNEL_TRAITS_HPP
0009 #define BOOST_MQTT5_CHANNEL_TRAITS_HPP
0010 
0011 #include <boost/asio/error.hpp>
0012 
0013 #include <deque>
0014 #include <type_traits>
0015 
0016 namespace boost::mqtt5::detail {
0017 
0018 namespace asio = boost::asio;
0019 using error_code = boost::system::error_code;
0020 
0021 template <typename Element>
0022 class bounded_deque {
0023     std::deque<Element> _buffer;
0024     static constexpr size_t MAX_SIZE = 65535;
0025 
0026 public:
0027     bounded_deque() = default;
0028     bounded_deque(size_t n) : _buffer(n) {}
0029 
0030     size_t size() const {
0031         return _buffer.size();
0032     }
0033 
0034     template <typename E>
0035     void push_back(E&& e) {
0036         if (_buffer.size() == MAX_SIZE)
0037             _buffer.pop_front();
0038         _buffer.push_back(std::forward<E>(e));
0039     }
0040 
0041     void pop_front() {
0042         _buffer.pop_front();
0043     }
0044 
0045     void clear() {
0046         _buffer.clear();
0047     }
0048 
0049     const auto& front() const noexcept {
0050         return _buffer.front();
0051     }
0052 
0053     auto& front() noexcept {
0054         return _buffer.front();
0055     }
0056 };
0057 
0058 template <typename... Signatures>
0059 struct channel_traits {
0060     template <typename... NewSignatures>
0061     struct rebind {
0062         using other = channel_traits<NewSignatures...>;
0063     };
0064 };
0065 
0066 template <typename R, typename... Args>
0067 struct channel_traits<R(error_code, Args...)> {
0068     static_assert(sizeof...(Args) > 0);
0069 
0070     template <typename... NewSignatures>
0071     struct rebind {
0072         using other = channel_traits<NewSignatures...>;
0073     };
0074 
0075     template <typename Element>
0076     struct container {
0077         using type = bounded_deque<Element>;
0078     };
0079 
0080     using receive_cancelled_signature = R(error_code, Args...);
0081 
0082     template <typename F>
0083     static void invoke_receive_cancelled(F f) {
0084         std::forward<F>(f)(
0085             asio::error::operation_aborted,
0086             typename std::decay_t<Args>()...
0087         );
0088     }
0089 
0090     using receive_closed_signature = R(error_code, Args...);
0091 
0092     template <typename F>
0093     static void invoke_receive_closed(F f) {
0094         std::forward<F>(f)(
0095             asio::error::operation_aborted,
0096             typename std::decay_t<Args>()...
0097         );
0098     }
0099 };
0100 
0101 } // namespace boost::mqtt5::detail
0102 
0103 #endif // !BOOST_MQTT5_CHANNEL_TRAITS_HPP