libt3widget
keybuffer.h
1 /* Copyright (C) 2011-2012 G.P. Halkes
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License version 3, as
4  published by the Free Software Foundation.
5 
6  This program is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  GNU General Public License for more details.
10 
11  You should have received a copy of the GNU General Public License
12  along with this program. If not, see <http://www.gnu.org/licenses/>.
13 */
14 #ifndef T3_WIDGET_KEYBUFFER_H
15 #define T3_WIDGET_KEYBUFFER_H
16 
17 #ifndef _T3_WIDGET_INTERNAL
18 #error This header file is for internal use _only_!!
19 #endif
20 
21 /* Buffer for storing keys that ensures thread-safe operation by means of a
22  mutex. It is implemented by means of a double ended queue. */
23 
24 #include <algorithm>
25 #include <deque>
26 
27 #include <t3widget/key.h>
28 #include <t3widget/thread.h>
29 
30 namespace t3_widget {
31 
33 template <class T>
34 class T3_WIDGET_LOCAL item_buffer_t {
35  protected:
37  std::deque<T> items;
42 
43  public:
45  void push_back(T item) {
47  /* Catch all exceptions, to enusre that unlocking of the mutex is
48  performed. The only real exception that can occur here is bad_alloc,
49  and there is not much we can do about that anyway. */
50  try {
51  items.push_back(item);
52  } catch (...) {
53  }
54  cond.notify_one();
55  }
56 
58  T pop_front(void) {
59  T result;
61  while (items.empty())
62  cond.wait(l);
63  result = items.front();
64  items.pop_front();
65  return result;
66  }
67 };
68 
70 class T3_WIDGET_LOCAL key_buffer_t : public item_buffer_t<key_t> {
71  public:
73  void push_back_unique(key_t key) {
75 
76  // Return without adding if the key is already queued
77  if (find(items.begin(), items.end(), key) != items.end())
78  return;
79 
80  /* Catch all exceptions, to enusre that unlocking of the mutex is
81  performed. The only real exception that can occur here is bad_alloc,
82  and there is not much we can do about that anyway. */
83  try {
84  items.push_back(key);
85  } catch (...) {
86  }
87  cond.notify_one();
88  }
89 };
90 
91 typedef item_buffer_t<mouse_event_t> mouse_event_buffer_t;
92 
93 }; // namespace
94 #endif
The t3_widget namespace is contains all classes, functions and global variables in the libt3widget li...
Definition: autocompleter.cc:18
Class implmementing a mutex-protected queue of key symbols.
Definition: keybuffer.h:70
long key_t
Integer type holding a single key symbol.
Definition: key.h:24
Definition: thread.h:94
T pop_front(void)
Retrieve and remove the item at the front of the queue.
Definition: keybuffer.h:58
std::deque< T > items
The list of item symbols.
Definition: keybuffer.h:37
thread::mutex lock
The mutex used for the critical section.
Definition: keybuffer.h:39
void push_back(T item)
Append an item to the list.
Definition: keybuffer.h:45
void push_back_unique(key_t key)
Append an item to the list, but only if it is not already in the queue.
Definition: keybuffer.h:73
thread::condition_variable cond
The condition variable used to signal addition to the #keys list.
Definition: keybuffer.h:41
Class implmementing a mutex-protected queue of items.
Definition: keybuffer.h:34