A C++ toolbox repo until the pair uTL/dTL arives
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. */
  50. template <typename Data_t, size_t N>
  51. class queue : public deque<Data_t, N> {
  52. public:
  53. // meta-identity types
  54. using queue_t = queue<Data_t, N>;
  55. using base_type = deque<Data_t, N>;
  56. // STL
  57. using value_type = typename base_type::value_type;
  58. using reference = typename base_type::reference;
  59. using const_reference = typename base_type::const_reference;
  60. using pointer = typename base_type::pointer;
  61. using const_pointer = typename base_type::const_pointer;
  62. using iterator = typename base_type::iterator;
  63. using const_iterator = typename base_type::const_iterator;
  64. using reverse_iterator = typename base_type::reverse_iterator;
  65. using const_reverse_iterator= typename base_type::const_reverse_iterator;
  66. //! \name Constructor / Destructor
  67. //! @{
  68. public:
  69. //! Default constructor
  70. constexpr queue () noexcept : base_type() { }
  71. //! fill contructor
  72. constexpr queue(const Data_t& value) noexcept : base_type(value) { }
  73. //! Initializer list contructor
  74. template <typename ...It>
  75. constexpr queue(It&& ...it) noexcept : base_type(std::forward<It>(it)...) { }
  76. //! @}
  77. //! \name Member access
  78. //! @{
  79. public:
  80. //! \brief Push an item in the back of the queue
  81. //! \param it The item to push
  82. constexpr void push (const Data_t& it) {
  83. base_type::push_back(it);
  84. }
  85. //! \brief Extract an item from the front of the queue and remove it from the queue
  86. //! \param it The item to push
  87. constexpr Data_t pop () {
  88. return base_type::pop_front();
  89. }
  90. //! \brief Push an item in the back of the queue
  91. //! \param it The item to push
  92. constexpr queue_t& operator<< (const Data_t& it) {
  93. push(it);
  94. return *this;
  95. }
  96. //! \brief Pop an item from the front of the queue
  97. //! \param it The item to write to
  98. constexpr queue_t& operator>> (Data_t& it) {
  99. it = pop();
  100. return *this;
  101. }
  102. //! @}
  103. };
  104. /*!
  105. * \brief
  106. * Pop an item from the front of the queue.
  107. *
  108. * This definition enables the "data << queue" syntax for pop operation
  109. *
  110. * \tparam Data_t The char-like queued item type. Usually \c char
  111. * \tparam N The size of queue
  112. *
  113. * \param it The item to write to
  114. * \param q The queue to read from
  115. * \return Reference to the returned item
  116. */
  117. template <typename Data_t, size_t N>
  118. constexpr Data_t& operator<< (Data_t& it, queue<Data_t, N>& q) {
  119. it = q.pop();
  120. return it;
  121. }
  122. } // namespace tbx
  123. #endif /* TBX_CONT_QUQUE_H_ */