Orocos Real-Time Toolkit  2.8.3
LockedQueue.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:08 CEST 2009 LockedQueue.hpp
3 
4  LockedQueue.hpp - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Peter Soetens
8  email : peter@thesourcworks.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef ORO_LOCKED_QUEUE_HPP
40 #define ORO_LOCKED_QUEUE_HPP
41 
42 #include <deque>
43 #include "../os/Mutex.hpp"
44 #include "../os/MutexLock.hpp"
45 
46 namespace RTT
47 { namespace internal {
48 
57  template< class T>
59  {
60  public:
61  typedef T value_t;
62  private:
63  typedef std::deque<value_t> BufferType;
64  typedef typename BufferType::iterator Iterator;
65  typedef typename BufferType::const_iterator CIterator;
66  mutable os::Mutex lock;
67  BufferType data;
68 
69  unsigned int cap;
70  public:
71  typedef unsigned int size_type;
72 
77  LockedQueue(unsigned int lsize)
78  : cap(lsize)
79  {
80  data.resize(lsize);
81  data.resize(0);
82  }
83 
85  }
86 
87  size_type capacity() const
88  {
89  return cap;
90  }
91 
92  size_type size() const
93  {
94  os::MutexLock locker(lock);
95  return data.size();
96  }
97 
102  bool isEmpty() const
103  {
104  os::MutexLock locker(lock);
105  return data.empty();
106  }
107 
112  bool isFull() const
113  {
114  os::MutexLock locker(lock);
115  return data.size() == cap;
116  }
117 
118  void clear()
119  {
120  os::MutexLock locker(lock);
121  data.clear();
122  }
123 
129  bool enqueue(const T& value)
130  {
131  {
132  os::MutexLock locker(lock);
133  if (cap == data.size() )
134  return false;
135  data.push_back(value);
136  }
137  return true;
138  }
139 
145  bool dequeue( T& result )
146  {
147  {
148  os::MutexLock locker(lock);
149  if ( data.empty() )
150  return false;
151  result = data.front();
152  data.pop_front();
153  }
154  return true;
155  }
156 
160  value_t front() const
161  {
162  os::MutexLock locker(lock);
163  value_t item = value_t();
164  if ( !data.empty() )
165  item = data.front();
166  return item;
167  }
168 
172  value_t back() const
173  {
174  os::MutexLock locker(lock);
175  value_t item = value_t();
176  if ( !data.empty() )
177  item = data.back();
178  return item;
179  }
180 
181  };
182 
183 }}
184 
185 #endif
bool enqueue(const T &value)
Enqueue an item.
size_type size() const
Definition: LockedQueue.hpp:92
size_type capacity() const
Definition: LockedQueue.hpp:87
LockedQueue(unsigned int lsize)
Create a lock-based queue wich can store lsize elements.
Definition: LockedQueue.hpp:77
bool isEmpty() const
Inspect if the Queue is empty.
value_t front() const
Returns the first element of the queue.
value_t back() const
Returns the last element of the queue.
bool isFull() const
Inspect if the Queue is full.
A lock-based queue implementation to enqueue or dequeue a pointer of type T.
Definition: LockedQueue.hpp:58
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:82
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
bool dequeue(T &result)
Dequeue an item.
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51