Core Library 1.7.0.0 Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.

BoundedBuffer.h
Go to the documentation of this file.
1 // This file is part of CoreLibrary containing useful reusable utility
2 // classes.
3 //
4 // Copyright (C) 2014 to present, Duncan Crutchley
5 // Contact <dac1976github@outlook.com>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License and GNU Lesser General Public License
16 // for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // and GNU Lesser General Public License along with this program. If
20 // not, see <http://www.gnu.org/licenses/>.
21
27 #ifndef BOUNDEDBUFFER
28 #define BOUNDEDBUFFER
29
30 #include <mutex>
31 #include <condition_variable>
32 #include <functional>
33 #include <boost/circular_buffer.hpp>
34 #include <boost/call_traits.hpp>
35
37 namespace core_lib
38 {
40 namespace threads
41 {
42
54 template <typename T> class BoundedBuffer final
55 {
56 public:
58 using container_type = boost::circular_buffer<T>;
60 using size_type = typename container_type::size_type;
62 using value_type = typename container_type::value_type;
64 using param_type = typename boost::call_traits<value_type>::param_type;
69 explicit BoundedBuffer(size_type capacity)
70 : m_container{capacity}
71 {
72 }
74 ~BoundedBuffer() = default;
76 BoundedBuffer(const BoundedBuffer&) = delete;
78 BoundedBuffer& operator=(const BoundedBuffer&) = delete;
80 BoundedBuffer(BoundedBuffer&&) = delete;
82 BoundedBuffer& operator=(BoundedBuffer&&) = delete;
90 void PushFront(param_type item)
91 {
92 {
93 std::unique_lock<std::mutex> lock{m_mutex};
94 m_notFullEvent.wait(lock, std::bind(&BoundedBuffer<value_type>::IsNotFull, this));
95 m_container.push_front(item);
96 ++m_unreadCount;
97 }
98
99 m_notEmptyEvent.notify_one();
100 }
108 void PopBack(value_type& item)
109 {
110 {
111 std::unique_lock<std::mutex> lock{m_mutex};
112 m_notEmptyEvent.wait(lock, std::bind(&BoundedBuffer<value_type>::IsNotEmpty, this));
113 item = m_container[--m_unreadCount];
114 }
115
116 m_notFullEvent.notify_one();
117 }
118
119 private:
121 std::mutex m_mutex;
123 std::condition_variable m_notEmptyEvent;
125 std::condition_variable m_notFullEvent;
127 size_type m_unreadCount{0};
129 container_type m_container{};
134 bool IsNotEmpty() const
135 {
136 return m_unreadCount > 0;
137 }
142 bool IsNotFull() const
143 {
144 return m_unreadCount < m_container.capacity();
145 }
146 };
147
148 } // namespace threads
149 } // namespace core_lib
150 #endif // BOUNDEDBUFFER
core_lib::threads::BoundedBuffer::operator=
BoundedBuffer & operator=(const BoundedBuffer &)=delete
Copy assignment operator deleted.
core_lib::threads::BoundedBuffer::IsNotFull
bool IsNotFull() const
Test if buffer not full.
Definition: BoundedBuffer.h:142
core_lib::threads::BoundedBuffer::~BoundedBuffer
~BoundedBuffer()=default
Default destructor.
core_lib::threads::BoundedBuffer::m_unreadCount
size_type m_unreadCount
Unread count.
Definition: BoundedBuffer.h:127
core_lib::threads::BoundedBuffer::param_type
typename boost::call_traits< value_type >::param_type param_type
Typedef for container param type.
Definition: BoundedBuffer.h:64
core_lib::threads::BoundedBuffer::size_type
typename container_type::size_type size_type
Typedef for container size type.
Definition: BoundedBuffer.h:60
core_lib::threads::BoundedBuffer::IsNotEmpty
bool IsNotEmpty() const
Test if buffer not empty.
Definition: BoundedBuffer.h:134
core_lib::threads::BoundedBuffer::m_mutex
std::mutex m_mutex
Synchronization mutex.
Definition: BoundedBuffer.h:121
core_lib::threads::BoundedBuffer::m_container
container_type m_container
Circular buffer.
Definition: BoundedBuffer.h:129
core_lib::threads::BoundedBuffer::value_type
typename container_type::value_type value_type
Typedef for container value type.
Definition: BoundedBuffer.h:62
core_lib
The core_lib namespace.
Definition: AsioDefines.h:59
core_lib::threads::BoundedBuffer::container_type
boost::circular_buffer< T > container_type
Typedef for container type.
Definition: BoundedBuffer.h:58
core_lib::threads::BoundedBuffer::m_notFullEvent
std::condition_variable m_notFullEvent
Condition variable to flag not full.
Definition: BoundedBuffer.h:125
core_lib::threads::BoundedBuffer::PopBack
void PopBack(value_type &item)
Pop item from the back.
Definition: BoundedBuffer.h:108
core_lib::threads::BoundedBuffer::PushFront
void PushFront(param_type item)
Push new item to the front.
Definition: BoundedBuffer.h:90
core_lib::threads::BoundedBuffer::m_notEmptyEvent
std::condition_variable m_notEmptyEvent
Condition variable to flag not empty.
Definition: BoundedBuffer.h:123
core_lib::threads::BoundedBuffer
Class defining a bounded buffer.
Definition: BoundedBuffer.h:54
core_lib::threads::BoundedBuffer::BoundedBuffer
BoundedBuffer(size_type capacity)
Constructor.
Definition: BoundedBuffer.h:69