OpendTect-6_4  6.4
queue.h
Go to the documentation of this file.
1 #ifndef queue_h
2 #define queue_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: A.H.Lammertink
9  Date: March 2004
10  RCS: $Id$
11 ________________*_______________________________________________________
12 
13 -*/
14 
15 #include "threadlock.h"
16 
17 
22 template <class T>
24 {
25 public:
26  QueueEntry( T item )
27  : value( item ), next( 0 ) {}
28 
29  T value;
31 };
32 
33 
38 template <class T>
40 {
41 public:
42  ObjQueue() : head(0), tail(0), lock_(true) {}
43 
45  void add( T* item )
46  {
47  Threads::Locker lckr( lock_ );
48  if ( !tail )
49  head = tail = new QueueEntry<T*>( item );
50  else
51  {
52  tail->next = new QueueEntry<T*>( item );
53  tail = tail->next;
54  }
55  }
56 
58  T* next()
59  {
60  if ( !head ) return 0;
61  Threads::Locker lckr( lock_ );
62 
63  T* value = head->value;
64 
65  QueueEntry<T*>* old = head;
66  head = head->next;
67  delete old;
68 
69  if ( !head ) tail = 0;
70  return value;
71  }
72 protected:
73 
75 
78 
79 };
80 
81 
86 template <class T>
88 {
89 public:
90  TypeQueue() : head(0), tail(0), lock_(true) {}
91 
92  void add( T item )
93  {
94  Threads::Locker lckr( lock_ );
95  if ( !tail )
96  head = tail = new QueueEntry<T>( item );
97  else
98  {
99  tail->next = new QueueEntry<T>( item );
100  tail = tail->next;
101  }
102  }
103 
104  bool empty() { return !head; }
105 
106  T next()
107  {
108  if ( empty() ) return 0;
109  Threads::Locker lckr( lock_ );
110 
111  T value = head->value;
112 
113  QueueEntry<T>* old = head;
114  head = head->next;
115  delete old;
116 
117  if ( !head ) tail = 0;
118 
119  return value;
120  }
121 protected:
122 
124 
127 
128 };
129 
130 
131 #endif
QueueEntry(T item)
Definition: queue.h:26
Locks the lock, shutting out access from other threads if needed.
Definition: threadlock.h:85
QueueEntry< T > * head
Definition: queue.h:125
TypeQueue()
Definition: queue.h:90
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class...
Definition: threadlock.h:53
QueueEntry< T * > * tail
Definition: queue.h:77
bool empty()
Definition: queue.h:104
ObjQueue()
Definition: queue.h:42
QueueEntry< T > * tail
Definition: queue.h:126
Queue of objects.
Definition: queue.h:39
QueueEntry< T > * next
Definition: queue.h:30
QueueEntry< T * > * head
Definition: queue.h:76
T * next()
becomes YOURS!
Definition: queue.h:58
One single queue entry.
Definition: queue.h:23
Queue of basic data types.
Definition: queue.h:87
T next()
Definition: queue.h:106
void add(T item)
Definition: queue.h:92
#define mClass(module)
Definition: commondefs.h:164
void add(T *item)
item becomes MINE!
Definition: queue.h:45
Threads::Lock lock_
Definition: queue.h:123
Threads::Lock lock_
Definition: queue.h:74
T value
Definition: queue.h:29

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019