SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
QueueSafeStated.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_QueueSafeStated_h
7 #define swcdb_core_QueueSafeStated_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 QueueSafeStated final : private std::queue<ItemT> {
19  public:
20 
22  explicit QueueSafeStated() noexcept: m_mutex() { }
23 
24  ~QueueSafeStated() noexcept { }
25 
26  QueueSafeStated(const QueueSafeStated&) = delete;
27 
28  QueueSafeStated(const QueueSafeStated&&) = delete;
29 
31 
32 
34  void push(const ItemT& item) {
36  QBase::push(item);
37  }
38 
40  void push(ItemT&& item) {
42  QBase::push(std::move(item));
43  }
44 
45 
47  ItemT& front() noexcept {
49  return QBase::front();
50  }
51 
53  bool empty() noexcept {
55  return QBase::empty();
56  }
57 
59  size_t size() noexcept {
61  return QBase::size();
62  }
63 
65  bool is_active() noexcept {
67  return m_state;
68  }
69 
71  bool activating() noexcept {
73  return (m_state || QBase::empty()) ? false : (m_state = true);
74  }
75 
77  bool deactivating() {
79  QBase::pop();
80  if(QBase::empty())
81  m_state = false;
82  return !m_state;
83  }
84 
86  void deactivate() noexcept {
88  m_state = false;
89  }
90 
91 
93  bool activating(const ItemT& item) {
95  if(m_state) {
96  QBase::push(item);
97  return false;
98  }
99  return m_state = true;
100  }
101 
103  bool activating(ItemT&& item) {
105  if(m_state) {
106  QBase::push(std::move(item));
107  return false;
108  }
109  return m_state = true;
110  }
111 
113  bool deactivating(ItemT& item) {
115  if(QBase::empty()) {
116  m_state = false;
117  } else {
118  item = std::move(QBase::front());
119  QBase::pop();
120  }
121  return !m_state;
122  }
123 
124 
125  private:
127  bool m_state = false;
128 
129  typedef std::queue<ItemT> QBase;
130 };
131 
132 
133 
134 }} // namespace SWC::Core
135 
136 
137 #endif // swcdb_core_QueueSafeStated_h
SWC::Core::QueueSafeStated::is_active
SWC_CAN_INLINE bool is_active() noexcept
Definition: QueueSafeStated.h:65
SWC::Core::QueueSafeStated::activating
SWC_CAN_INLINE bool activating(const ItemT &item)
Definition: QueueSafeStated.h:93
SWC::Core::QueueSafeStated::~QueueSafeStated
~QueueSafeStated() noexcept
Definition: QueueSafeStated.h:24
SWC::Core::MutexSptd::scope
Definition: MutexSptd.h:96
SWC::Core::QueueSafeStated::size
SWC_CAN_INLINE size_t size() noexcept
Definition: QueueSafeStated.h:59
SWC::Core::QueueSafeStated::QueueSafeStated
QueueSafeStated(const QueueSafeStated &)=delete
SWC::Core::QueueSafeStated
Definition: QueueSafeStated.h:18
SWC::Core::QueueSafeStated::front
SWC_CAN_INLINE ItemT & front() noexcept
Definition: QueueSafeStated.h:47
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Core::QueueSafeStated::deactivate
SWC_CAN_INLINE void deactivate() noexcept
Definition: QueueSafeStated.h:86
SWC::Core::QueueSafeStated::push
SWC_CAN_INLINE void push(ItemT &&item)
Definition: QueueSafeStated.h:40
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
size
uint32_t size
Buffer size.
Definition: HeaderBufferInfo.h:47
SWC::Core::QueueSafeStated::push
SWC_CAN_INLINE void push(const ItemT &item)
Definition: QueueSafeStated.h:34
SWC::Core::QueueSafeStated::QueueSafeStated
SWC_CAN_INLINE QueueSafeStated() noexcept
Definition: QueueSafeStated.h:22
SWC::Core::QueueSafeStated::deactivating
SWC_CAN_INLINE bool deactivating()
Definition: QueueSafeStated.h:77
SWC::Core::QueueSafeStated::activating
SWC_CAN_INLINE bool activating(ItemT &&item)
Definition: QueueSafeStated.h:103
SWC::Core::QueueSafeStated::m_mutex
MutexSptd m_mutex
Definition: QueueSafeStated.h:126
MutexSptd.h
SWC::Core::QueueSafeStated::empty
SWC_CAN_INLINE bool empty() noexcept
Definition: QueueSafeStated.h:53
SWC::Core::QueueSafeStated::QBase
std::queue< ItemT > QBase
Definition: QueueSafeStated.h:129
SWC::Core::QueueSafeStated::QueueSafeStated
QueueSafeStated(const QueueSafeStated &&)=delete
SWC::Core::MutexSptd
Definition: MutexSptd.h:16
SWC::Core::QueueSafeStated::m_state
bool m_state
Definition: QueueSafeStated.h:127
SWC::Core::QueueSafeStated::activating
SWC_CAN_INLINE bool activating() noexcept
Definition: QueueSafeStated.h:71
SWC::Core::QueueSafeStated::deactivating
SWC_CAN_INLINE bool deactivating(ItemT &item)
Definition: QueueSafeStated.h:113
SWC::Core::QueueSafeStated::operator=
QueueSafeStated & operator=(const QueueSafeStated &)=delete