Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 09:43:48

0001 //
0002 // detail/op_queue.hpp
0003 // ~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_OP_QUEUE_HPP
0012 #define BOOST_ASIO_DETAIL_OP_QUEUE_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/noncopyable.hpp>
0019 
0020 #include <boost/asio/detail/push_options.hpp>
0021 
0022 namespace boost {
0023 namespace asio {
0024 namespace detail {
0025 
0026 template <typename Operation>
0027 class op_queue;
0028 
0029 class op_queue_access
0030 {
0031 public:
0032   template <typename Operation>
0033   static Operation* next(Operation* o)
0034   {
0035     return static_cast<Operation*>(o->next_);
0036   }
0037 
0038   template <typename Operation1, typename Operation2>
0039   static void next(Operation1*& o1, Operation2* o2)
0040   {
0041     o1->next_ = o2;
0042   }
0043 
0044   template <typename Operation>
0045   static void destroy(Operation* o)
0046   {
0047     o->destroy();
0048   }
0049 
0050   template <typename Operation>
0051   static Operation*& front(op_queue<Operation>& q)
0052   {
0053     return q.front_;
0054   }
0055 
0056   template <typename Operation>
0057   static Operation*& back(op_queue<Operation>& q)
0058   {
0059     return q.back_;
0060   }
0061 };
0062 
0063 template <typename Operation>
0064 class op_queue
0065   : private noncopyable
0066 {
0067 public:
0068   // Constructor.
0069   op_queue()
0070     : front_(0),
0071       back_(0)
0072   {
0073   }
0074 
0075   // Destructor destroys all operations.
0076   ~op_queue()
0077   {
0078     while (Operation* op = front_)
0079     {
0080       pop();
0081       op_queue_access::destroy(op);
0082     }
0083   }
0084 
0085   // Get the operation at the front of the queue.
0086   Operation* front()
0087   {
0088     return front_;
0089   }
0090 
0091   // Pop an operation from the front of the queue.
0092   void pop()
0093   {
0094     if (front_)
0095     {
0096       Operation* tmp = front_;
0097       front_ = op_queue_access::next(front_);
0098       if (front_ == 0)
0099         back_ = 0;
0100       op_queue_access::next(tmp, static_cast<Operation*>(0));
0101     }
0102   }
0103 
0104   // Push an operation on to the back of the queue.
0105   void push(Operation* h)
0106   {
0107     op_queue_access::next(h, static_cast<Operation*>(0));
0108     if (back_)
0109     {
0110       op_queue_access::next(back_, h);
0111       back_ = h;
0112     }
0113     else
0114     {
0115       front_ = back_ = h;
0116     }
0117   }
0118 
0119   // Push all operations from another queue on to the back of the queue. The
0120   // source queue may contain operations of a derived type.
0121   template <typename OtherOperation>
0122   void push(op_queue<OtherOperation>& q)
0123   {
0124     if (Operation* other_front = op_queue_access::front(q))
0125     {
0126       if (back_)
0127         op_queue_access::next(back_, other_front);
0128       else
0129         front_ = other_front;
0130       back_ = op_queue_access::back(q);
0131       op_queue_access::front(q) = 0;
0132       op_queue_access::back(q) = 0;
0133     }
0134   }
0135 
0136   // Whether the queue is empty.
0137   bool empty() const
0138   {
0139     return front_ == 0;
0140   }
0141 
0142   // Test whether an operation is already enqueued.
0143   bool is_enqueued(Operation* o) const
0144   {
0145     return op_queue_access::next(o) != 0 || back_ == o;
0146   }
0147 
0148 private:
0149   friend class op_queue_access;
0150 
0151   // The front of the queue.
0152   Operation* front_;
0153 
0154   // The back of the queue.
0155   Operation* back_;
0156 };
0157 
0158 } // namespace detail
0159 } // namespace asio
0160 } // namespace boost
0161 
0162 #include <boost/asio/detail/pop_options.hpp>
0163 
0164 #endif // BOOST_ASIO_DETAIL_OP_QUEUE_HPP