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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*!
  2. * \file cont/equeue.h
  3. * \brief
  4. * A queue with event based callables based on edeque.
  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_EQUQUE_H_
  31. #define TBX_CONT_EQUQUE_H_
  32. #include <core/core.h>
  33. #include <cont/edeque.h>
  34. namespace tbx {
  35. /*!
  36. * \class equeue
  37. * \brief
  38. * A statically allocated queue based on edeque with size and data matching
  39. * event based callables.
  40. *
  41. * We use the \ref edeque::push_back() and \ref edeque::pop_front() pair from edeque's
  42. * functionality, so at the \c push the increment performed after the insertion.
  43. * Similarly at the \c pop the decrement performed before the exctraction. This way also
  44. * the \ref edeque::front() and \ref edeque::back() stay the same ;)
  45. *
  46. * We also provide stream operators.
  47. *
  48. * \tparam Data_t The char-like queued item type. Usually \c char
  49. * \tparam N The size of edeque
  50. * \tparam Fn The type of Callable
  51. */
  52. template <typename Data_t, size_t N, typename Fn = std::function<void()>>
  53. class equeue : public edeque<Data_t, N, Fn> {
  54. public:
  55. // meta-identity types
  56. using equeue_t = equeue<Data_t, N>;
  57. using base_type = edeque<Data_t, N, Fn>;
  58. // STL
  59. using value_type = typename base_type::value_type;
  60. using reference = typename base_type::reference;
  61. using const_reference = typename base_type::const_reference;
  62. using pointer = typename base_type::pointer;
  63. using const_pointer = typename base_type::const_pointer;
  64. using iterator = typename base_type::iterator;
  65. using const_iterator = typename base_type::const_iterator;
  66. using reverse_iterator = typename base_type::reverse_iterator;
  67. using const_reverse_iterator= typename base_type::const_reverse_iterator;
  68. //! \name Constructor / Destructor
  69. //! @{
  70. public:
  71. //! Default constructor
  72. constexpr equeue () noexcept : base_type() { }
  73. //! fill contructor
  74. constexpr equeue(const Data_t& value) noexcept : base_type(value) { }
  75. //! Initializer list contructor
  76. template <typename ...It>
  77. constexpr equeue(It&& ...it) noexcept : base_type(std::forward<It>(it)...) { }
  78. //! @}
  79. //! \name Member access
  80. //! @{
  81. public:
  82. //! \brief Push an item in the back of the queue
  83. //! \param it The item to push
  84. void push (const Data_t& it) {
  85. base_type::push_back(it);
  86. }
  87. //! \brief Extract an item from the front of the queue and remove it from the queue
  88. //! \param it The item to push
  89. Data_t pop () {
  90. return base_type::pop_front();
  91. }
  92. //! \brief Push an item in the back of the queue
  93. //! \param it The item to push
  94. equeue_t& operator<< (const Data_t& it) {
  95. push(it);
  96. return *this;
  97. }
  98. //! \brief Push an item in the back of the queue
  99. //! \param it The item to push
  100. equeue_t& operator>> (Data_t& it) {
  101. it = pop();
  102. return *this;
  103. }
  104. //! @}
  105. };
  106. /*!
  107. * \brief
  108. * Pop an item from the front of the queue.
  109. *
  110. * This definition enables the "data << equeue" syntax for pop operation
  111. *
  112. * \tparam Data_t The char-like queued item type. Usually \c char
  113. * \tparam N The size of queue
  114. * \tparam Fn The type of Callable
  115. *
  116. * \param it The item to write to
  117. * \param q The queue to read from
  118. * \return Reference to the returned item
  119. */
  120. template <typename Data_t, size_t N, typename Fn = std::function<void()>>
  121. Data_t& operator<< (Data_t& it, equeue<Data_t, N, Fn>& q) {
  122. it = q.pop();
  123. return it;
  124. }
  125. }
  126. #endif /* TBX_CONT_EQUQUE_H_ */