-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataBuffer.cc
85 lines (78 loc) · 1.75 KB
/
DataBuffer.cc
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
#include "DataBuffer.hh"
#include <errno.h>
DataBuffer::DataBuffer(Mode mode = LAST_RECEIVED) :
mutex_(recursive_mutex), mode_(mode), event_(NULL)
{
callEvents.connect(slot(this,&DataBuffer::call_all));
callOneEvent.connect(slot(this,&DataBuffer::call_all));
}
DataBuffer::~DataBuffer()
{
if ((NULL!=event_) && mutex_lock())
{
delete event_;
event_=NULL;
mutex_.unlock();
}
}
void DataBuffer::store_wrapped_event(Wrap* wrap)
{
if ((NULL!=wrap) && (event_!=wrap) && mutex_lock())
{
if (NULL!=event_)
{
if (FIRST_RECEIVED!=mode_)
{
delete event_;
event_=wrap;
wrap=NULL;
}
else
{
delete wrap;
}
}
else
{
event_=wrap;
}
mutex_.unlock();
}
}
void DataBuffer::call_all()
{
if ((NULL!=event_) && mutex_.lock())
{
if (REPEAT_LAST!=mode_) {
Wrap *fire = event_;
event_=NULL;
fire->re_emit();
delete fire;
}
else
{
event_->re_emit();
}
mutex_.unlock();
}
}
void DataBuffer::discard_all()
{
if ((NULL!=event_) && mutex_.lock())
{
delete event_;
mutex_.unlock();
}
}
bool DataBuffer::mutex_lock() {
int err=mutex_.lock();
if (0!=err) {
if (EINVAL==err) {
cerr << endl << "DataBuffer::mutex_lock() failed: " << endl;
cerr << "The lock returned EINVAL, so the mutex was not properly initialized" << endl;
cerr << "This is fatal, terminating with code 11" << endl;
exit(11);
}
}
return 0==err;
}