A C++ toolbox repo until the pair uTL/dTL arives
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
3 роки тому
3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*!
  2. * \file cont/queue.h
  3. * \brief
  4. * A statically allocated queue based on deque.
  5. *
  6. * \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
  7. *
  8. * <dl class=\"section copyright\"><dt>License</dt><dd>
  9. * The MIT License (MIT)
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. * </dd></dl>
  29. */
  30. #ifndef TBX_CONT_QUQUE_H_
  31. #define TBX_CONT_QUQUE_H_
  32. #include <core/core.h>
  33. #include <cont/deque.h>
  34. namespace tbx {
  35. /*!
  36. * \class queue
  37. * \brief
  38. * A statically allocated queue based on deque.
  39. *
  40. * We use the \ref deque::push_back() and \ref deque::pop_front() pair from deque's
  41. * functionality, so at the \c push the increment performed after the insertion.
  42. * Similarly at the \c pop the decrement performed before the exctraction. This way also
  43. * the \ref deque::front() and \ref deque::back() stay the same ;)
  44. *
  45. * We also provide stream operators.
  46. *
  47. * \tparam Data_t The char-like queued item type. Usually \c char
  48. * \tparam N The size of queue
  49. * \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
  50. * \note
  51. * SemiAtomic means it is safe to for one thread to push only from front and another can pop.
  52. */
  53. template <typename Data_t, size_t N, bool SemiAtomic =false>
  54. class queue : public deque<Data_t, N, SemiAtomic> {
  55. public:
  56. // meta-identity types
  57. using queue_t = queue<Data_t, N, SemiAtomic>;
  58. using base_type = deque<Data_t, N, SemiAtomic>;
  59. using range_t = typename base_type::range_t;
  60. // STL
  61. using value_type = typename base_type::value_type;
  62. using reference = typename base_type::reference;
  63. using const_reference = typename base_type::const_reference;
  64. using pointer = typename base_type::pointer;
  65. using const_pointer = typename base_type::const_pointer;
  66. using iterator = typename base_type::iterator;
  67. using const_iterator = typename base_type::const_iterator;
  68. using reverse_iterator = typename base_type::reverse_iterator;
  69. using const_reverse_iterator= typename base_type::const_reverse_iterator;
  70. //! \name Constructor / Destructor
  71. //! @{
  72. public:
  73. //! Default constructor
  74. constexpr queue () noexcept : base_type() { }
  75. //! fill contructor
  76. constexpr queue(const Data_t& value) noexcept : base_type(value) { }
  77. //! Initializer list contructor
  78. template <typename ...It>
  79. constexpr queue(It&& ...it) noexcept : base_type(std::forward<It>(it)...) { }
  80. //! @}
  81. //! \name Member access
  82. //! @{
  83. public:
  84. //! \brief Push an item in the back of the queue
  85. //! \param it The item to push
  86. constexpr void push (const Data_t& it) {
  87. base_type::push_back(it);
  88. }
  89. //! \brief Extract an item from the front of the queue and remove it from the queue
  90. //! \param it The item to push
  91. constexpr Data_t pop () {
  92. return base_type::pop_front();
  93. }
  94. //! \brief Push an item in the back of the queue
  95. //! \param it The item to push
  96. constexpr queue_t& operator<< (const Data_t& it) {
  97. push(it);
  98. return *this;
  99. }
  100. //! \brief Pop an item from the front of the queue
  101. //! \param it The item to write to
  102. constexpr queue_t& operator>> (Data_t& it) {
  103. it = pop();
  104. return *this;
  105. }
  106. //! @}
  107. };
  108. /*!
  109. * \brief
  110. * Pop an item from the front of the queue.
  111. *
  112. * This definition enables the "data << queue" syntax for pop operation
  113. *
  114. * \tparam Data_t The char-like queued item type. Usually \c char
  115. * \tparam N The size of queue
  116. * \tparam SemiAtomic True for semi-atomic operation. In that case the \c ring_iterator is also atomic.
  117. *
  118. * \param it The item to write to
  119. * \param q The queue to read from
  120. * \return Reference to the returned item
  121. */
  122. template <typename Data_t, size_t N, bool SemiAtomic =false>
  123. constexpr Data_t& operator<< (Data_t& it, queue<Data_t, N, SemiAtomic>& q) {
  124. it = q.pop();
  125. return it;
  126. }
  127. } // namespace tbx
  128. #endif /* TBX_CONT_QUQUE_H_ */