[go: up one dir, main page]

Menu

[r369]: / trunk / VTS3 / VTSQueue.cpp  Maximize  Restore  History

Download this file

156 lines (119 with data), 2.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "stdafx.h"
#include "vts.h"
#include "VTSQueue.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/* test app for VTSQueue template */
#ifdef LOCAL_TEST
#include "VTSQueue.h"
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
char *strings[] = {{"One"},{"Two"},{"Three"},{"Four"},{NULL}};
/* note - only tests the FIFO part, not the multithread safe part */
void main(void)
{
int index;
VTSQueue<char *> queue;
char **pWord;
index = 0;
while (strings[index]) {
queue.Write(&strings[index]);
index++;
}
index = 0;
do {
pWord = queue.Read();
if (pWord)
assert(strcmp(*pWord,strings[index]) == 0);
else
assert(strings[index] == NULL);
index++;
} while (pWord);
return;
}
//#endif
/////////////////////////////////////////////////////////////////////////////
// VTSQueue
//ScriptExecMsgQueue * pbogustemplatetest;
//
// VTSQueue<T>::VTSQueue
//
template<class T>
VTSQueue<T>::VTSQueue()
: qFirst(0), qLast(0)
{
}
//
// VTSQueue<T>::~VTSQueue
//
template<class T>
VTSQueue<T>::~VTSQueue()
{
if (qFirst)
TRACE0( "Warning: queue not empty\n" );
}
//
// VTSQueue<T>::Read
//
// This function returns a pointer to the first (oldest) element that was
// added to the queue. If the queue is empty it returns nil.
//
template<class T>
T* VTSQueue<T>::Read( void )
{
// lock the queue
qCS.Lock();
VTSQueueElem *cur = qFirst
;
T *rslt = 0
;
// if the queue not is empty, extract the first element
if (cur) {
// set result
rslt = cur->qElem;
// remove from list
qFirst = cur->qNext;
if (!qFirst)
qLast = 0;
// delete wrapper element
delete cur;
}
// unlock the queue
qCS.Unlock();
// fini
return rslt;
}
//
// VTSQueue<T>::Write
//
// This function adds a pointer to an element to the end of the queue.
//
template<class T>
void VTSQueue<T>::Write( T* tp )
{
// lock the queue
qCS.Lock();
VTSQueueElem *cur = new VTSQueueElem()
;
cur->qElem = tp;
cur->qNext = 0;
if (qFirst)
qLast->qNext = cur;
else
qFirst = cur;
qLast = cur;
// unlock the queue
qCS.Unlock();
}
template<class T>
void VTSQueue<T>::Fire( T* tp )
{
Write(tp);
::PostThreadMessage( AfxGetApp()->m_nThreadID, WM_VTS_EXECMSG, (WPARAM)0, (LPARAM)0 );
}
#endif