2 #include "NPLMessage.h" 4 #include <boost/circular_buffer.hpp> 5 #include <boost/thread.hpp> 6 #include "util/mutex.h" 21 void wait(T type_) {};
25 template <
typename Data,
typename Condition = boost::condition_variable>
35 template<
typename Data,
typename Condition>
39 typedef boost::circular_buffer<Data> container_type;
40 typedef typename container_type::size_type size_type;
41 typedef typename container_type::value_type value_type;
43 mutable boost::mutex m_mutex;
44 Condition m_condition_variable;
52 container_type m_container;
71 boost::mutex::scoped_lock lock(m_mutex);
72 m_use_event = bUseEvent;
83 boost::mutex::scoped_lock lock(m_mutex);
84 BufferStatus bufferStatus = m_container.empty() ? BufferFirst : BufferNormal;
85 if(m_container.full())
87 bufferStatus = BufferOverFlow;
91 m_container.push_back(item);
92 bufferStatus = m_container.full() ? BufferFull : bufferStatus;
97 m_condition_variable.notify_one();
109 boost::mutex::scoped_lock lock(m_mutex);
110 BufferStatus bufferStatus = m_container.empty() ? BufferFirst : BufferNormal;
111 if(m_container.full())
113 bufferStatus = BufferOverFlow;
117 m_container.push_back(item);
118 *ppFrontItem = &(m_container.front());
119 bufferStatus = m_container.full() ? BufferFull : bufferStatus;
124 m_condition_variable.notify_one();
134 boost::mutex::scoped_lock lock(m_mutex);
135 m_container.push_back(data);
139 m_condition_variable.notify_one();
149 boost::mutex::scoped_lock lock(m_mutex);
150 m_container.push_front(data);
154 m_condition_variable.notify_one();
157 bool try_pop(value_type& popped_value)
159 boost::mutex::scoped_lock lock(m_mutex);
160 if(m_container.empty())
165 popped_value=m_container.front();
166 m_container.pop_front();
170 void wait_and_pop(value_type& popped_value)
172 boost::mutex::scoped_lock lock(m_mutex);
173 while(m_container.empty())
175 m_condition_variable.wait(lock);
177 popped_value=m_container.front();
178 m_container.pop_front();
185 void wait(
int nMessageCount = -1)
187 boost::mutex::scoped_lock lock(m_mutex);
188 if (nMessageCount >= 0 && (nMessageCount != (
int)m_container.size() || nMessageCount>= (int)m_container.capacity()))
190 m_condition_variable.wait(lock);
197 value_type
peek(size_type nIndex)
199 boost::mutex::scoped_lock lock(m_mutex);
200 if (nIndex < m_container.size())
202 return m_container.at(nIndex);
212 boost::mutex::scoped_lock lock(m_mutex);
213 if (nIndex < m_container.size())
217 popped_value = m_container.front();
221 popped_value = m_container[nIndex];
222 for (size_type i = nIndex; i >= 1; i--)
224 m_container[i] = m_container[i-1];
227 m_container.pop_front();
239 boost::mutex::scoped_lock lock(m_mutex);
240 return m_container.empty() ? NULL : &(m_container.front());
251 boost::mutex::scoped_lock lock(m_mutex);
252 if(m_container.empty())
258 m_container.pop_front();
259 if(ppValueFront!=0 && !m_container.empty())
261 *ppValueFront = &(m_container.front());
269 boost::mutex::scoped_lock lock(m_mutex);
270 return m_container.empty();
273 boost::mutex::scoped_lock lock(m_mutex);
274 return m_container.full();
280 boost::mutex::scoped_lock lock(m_mutex);
281 return m_container.size();
287 return m_container.capacity();
293 boost::mutex::scoped_lock lock(m_mutex);
294 m_container.set_capacity(new_capacity);
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
bool try_next(value_type **ppValueFront)
try pop from the front of the queue and return the front object after the pop.
Definition: NPLMessageQueue.h:249
size_type size() const
Get the number of elements currently stored in the circular_buffer.
Definition: NPLMessageQueue.h:278
it implements a producer/consumer(s) queue design pattern.
Definition: NPLMessageQueue.h:26
void set_capacity(size_type new_capacity)
Set the max number of elements that can be stored in the circular_buffer.
Definition: NPLMessageQueue.h:291
void push(value_type &data)
add a data item to the back of the queue.
Definition: NPLMessageQueue.h:132
void wait(int nMessageCount=-1)
simply wait for the next message to arrive.
Definition: NPLMessageQueue.h:185
bool m_use_event
whether to use event to inform consumer when new data items are added to the queue.
Definition: NPLMessageQueue.h:50
value_type * try_front()
Definition: NPLMessageQueue.h:237
Message queue implementation.
Definition: NPLMessageQueue.h:301
size_type capacity() const
Get the number of elements that can be stored in the circular_buffer.
Definition: NPLMessageQueue.h:285
dummy condition variable
Definition: NPLMessageQueue.h:14
void notify_one()
notify one dummy
Definition: NPLMessageQueue.h:18
void push_front(value_type &data)
add a data item to the front of the queue.
Definition: NPLMessageQueue.h:147
bool try_pop_at(size_type nIndex, value_type &popped_value)
pop message at given index.
Definition: NPLMessageQueue.h:210
BufferStatus try_push(value_type &item)
try push to back of the queue.
Definition: NPLMessageQueue.h:81
void SetUseEvent(bool bUseEvent)
whether to use event to inform consumer when new data items are added to the queue.
Definition: NPLMessageQueue.h:69
BufferStatus try_push_get_front(value_type &item, value_type **ppFrontItem)
same as try_push, except that it also returns pointer to the front object.
Definition: NPLMessageQueue.h:107
value_type peek(size_type nIndex)
Definition: NPLMessageQueue.h:197