SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
QueueSafe.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_core_QueueSafe_h
7 #define swcdb_core_QueueSafe_h
8 
9 
10 #include "swcdb/core/MutexSptd.h"
11 #include <queue>
12 
13 
14 namespace SWC { namespace Core {
15 
16 
17 template <class ItemT>
18 class QueueSafe : private std::queue<ItemT> {
19  typedef std::queue<ItemT> QBase;
20 
21  public:
22 
23  typedef ItemT value_type;
24 
26  explicit QueueSafe() noexcept: m_mutex() { }
27 
28  ~QueueSafe() noexcept { }
29 
30  QueueSafe(const QueueSafe&) = delete;
31 
32  QueueSafe(QueueSafe&& other) : m_mutex() {
33  MutexSptd::scope lock(other.m_mutex);
34  QBase::operator=(std::move(other));
35  }
36 
37  QueueSafe& operator=(const QueueSafe&) = delete;
38 
40 
42  void push(const ItemT& item) {
44  QBase::push(item);
45  }
46 
48  void push(ItemT&& item) {
50  QBase::push(std::move(item));
51  }
52 
54  bool push_and_is_1st(const ItemT& item) {
55  bool chk;
57  chk = QBase::empty();
58  QBase::push(item);
59  return chk;
60  }
61 
63  bool push_and_is_1st(ItemT&& item) {
64  bool chk;
66  chk = QBase::empty();
67  QBase::push(std::move(item));
68  return chk;
69  }
70 
72  ItemT& front() noexcept {
74  return QBase::front();
75  }
76 
78  bool empty() noexcept {
80  return QBase::empty();
81  }
82 
84  size_t size() noexcept {
86  return QBase::size();
87  }
88 
90  bool pop_and_more() {
92  QBase::pop();
93  return !QBase::empty();
94  }
95 
97  bool pop(ItemT* item) {
99  if(QBase::empty())
100  return false;
101  *item = std::move(QBase::front());
102  QBase::pop();
103  return true;
104  }
105 
106 
107  private:
108 
110 
111 };
112 
113 
114 
115 }} // namespace SWC::Core
116 
117 
118 #endif // swcdb_core_QueueSafe_h
SWC::Core::QueueSafe::QueueSafe
SWC_CAN_INLINE QueueSafe() noexcept
Definition: QueueSafe.h:26
SWC::Core::QueueSafe::push
SWC_CAN_INLINE void push(ItemT &&item)
Definition: QueueSafe.h:48
SWC::Core::QueueSafe::push_and_is_1st
SWC_CAN_INLINE bool push_and_is_1st(const ItemT &item)
Definition: QueueSafe.h:54
SWC::Core::QueueSafe::value_type
ItemT value_type
Definition: QueueSafe.h:23
SWC::Core::QueueSafe::m_mutex
MutexSptd m_mutex
Definition: QueueSafe.h:109
SWC::Core::MutexSptd::scope
Definition: MutexSptd.h:96
SWC::Core::QueueSafe::QueueSafe
QueueSafe(QueueSafe &&other)
Definition: QueueSafe.h:32
SWC::Core::QueueSafe::~QueueSafe
~QueueSafe() noexcept
Definition: QueueSafe.h:28
SWC::Core::QueueSafe::operator=
QueueSafe & operator=(QueueSafe &&)=delete
SWC::Core::QueueSafe::push
SWC_CAN_INLINE void push(const ItemT &item)
Definition: QueueSafe.h:42
SWC::Core::QueueSafe::front
SWC_CAN_INLINE ItemT & front() noexcept
Definition: QueueSafe.h:72
SWC::Core::QueueSafe::empty
SWC_CAN_INLINE bool empty() noexcept
Definition: QueueSafe.h:78
SWC::Core::QueueSafe::pop_and_more
SWC_CAN_INLINE bool pop_and_more()
Definition: QueueSafe.h:90
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Core::QueueSafe::QBase
std::queue< ItemT > QBase
Definition: QueueSafe.h:19
SWC::Core::QueueSafe::operator=
QueueSafe & operator=(const QueueSafe &)=delete
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
MutexSptd.h
SWC::Core::QueueSafe::push_and_is_1st
SWC_CAN_INLINE bool push_and_is_1st(ItemT &&item)
Definition: QueueSafe.h:63
SWC::Core::QueueSafe::size
SWC_CAN_INLINE size_t size() noexcept
Definition: QueueSafe.h:84
SWC::Core::MutexSptd
Definition: MutexSptd.h:16
SWC::Core::QueueSafe
Definition: QueueSafe.h:18
SWC::Core::QueueSafe::QueueSafe
QueueSafe(const QueueSafe &)=delete
SWC::Core::QueueSafe::pop
SWC_CAN_INLINE bool pop(ItemT *item)
Definition: QueueSafe.h:97