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

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