ewmscp
..
src
getlineWithTimeout.h
Go to the documentation of this file.
1
#ifndef __getlineWithTimeout_h__
2
#define __getlineWithTimeout_h__
3
#include <
unwindCheck.h
>
4
#include <condition_variable>
5
#include <mutex>
6
#include <thread>
7
8
9
class
getlineWithTimeout
:
private
unwindCheck
{
10
std::istream&
stream
;
11
char
delimiter
;
12
bool
waitingForData
;
13
std::string
line
;
14
std::mutex
lineMutex
;
15
std::condition_variable
cv
;
16
std::thread
readerThread
;
17
bool
running
;
18
void
reader
() {
19
running
=
true
;
20
while
(
stream
) {
21
std::unique_lock<decltype(
lineMutex
)> lock(
lineMutex
);
22
while
(!
waitingForData
) {
23
cv
.wait(lock);
24
}
25
std::getline(
stream
,
line
,
delimiter
);
26
waitingForData
=
false
;
27
cv
.notify_all();
28
}
29
cv
.notify_all();
30
running
=
false
;
31
}
32
public
:
33
getlineWithTimeout
(std::istream& aStream,
34
char
aDelimiter) :
35
stream
(aStream),
delimiter
(aDelimiter),
waitingForData
(true) {
36
running
=
true
;
37
readerThread
= std::thread(&
getlineWithTimeout::reader
,
this
);
38
};
39
~getlineWithTimeout
() {
40
if
(!
isUnwinding
()) {
41
readerThread
.join();
42
}
43
};
44
45
bool
active
() {
46
return
running
;
47
}
48
49
class
nocopyLine
{
50
getlineWithTimeout
&
lineGetter
;
51
std::unique_lock<decltype(
getlineWithTimeout::lineMutex
)>
lock
;
52
public
:
53
template
<
typename
durationType>
nocopyLine
(
getlineWithTimeout
& aLineGetter, durationType timeout):
54
lineGetter
(aLineGetter),
lock
(
lineGetter
.
lineMutex
) {
55
while
(
lineGetter
.
waitingForData
) {
56
if
(
lineGetter
.
cv
.wait_for(
lock
, timeout) == std::cv_status::timeout) {
57
return
;
58
}
59
}
60
return
;
61
}
62
bool
timedOut
()
const
{
63
return
lineGetter
.
waitingForData
;
64
}
65
std::string&
line
() {
66
return
lineGetter
.
line
;
67
}
68
~nocopyLine
() {
69
lineGetter
.
waitingForData
=
true
;
70
lineGetter
.
cv
.notify_all();
71
}
72
};
73
};
74
75
76
77
#endif
getlineWithTimeout::~getlineWithTimeout
~getlineWithTimeout()
Definition:
getlineWithTimeout.h:39
getlineWithTimeout::lineMutex
std::mutex lineMutex
Definition:
getlineWithTimeout.h:14
unwindCheck::isUnwinding
bool isUnwinding() const
Definition:
unwindCheck.h:19
getlineWithTimeout::cv
std::condition_variable cv
Definition:
getlineWithTimeout.h:15
getlineWithTimeout::nocopyLine::lock
std::unique_lock< decltype(getlineWithTimeout::lineMutex)> lock
Definition:
getlineWithTimeout.h:51
getlineWithTimeout::delimiter
char delimiter
Definition:
getlineWithTimeout.h:11
unwindCheck
Definition:
unwindCheck.h:5
getlineWithTimeout
Definition:
getlineWithTimeout.h:9
getlineWithTimeout::waitingForData
bool waitingForData
Definition:
getlineWithTimeout.h:12
getlineWithTimeout::getlineWithTimeout
getlineWithTimeout(std::istream &aStream, char aDelimiter)
Definition:
getlineWithTimeout.h:33
getlineWithTimeout::nocopyLine::line
std::string & line()
Definition:
getlineWithTimeout.h:65
getlineWithTimeout::nocopyLine::lineGetter
getlineWithTimeout & lineGetter
Definition:
getlineWithTimeout.h:50
getlineWithTimeout::active
bool active()
Definition:
getlineWithTimeout.h:45
getlineWithTimeout::nocopyLine
Definition:
getlineWithTimeout.h:49
getlineWithTimeout::line
std::string line
Definition:
getlineWithTimeout.h:13
getlineWithTimeout::stream
std::istream & stream
Definition:
getlineWithTimeout.h:10
getlineWithTimeout::nocopyLine::nocopyLine
nocopyLine(getlineWithTimeout &aLineGetter, durationType timeout)
Definition:
getlineWithTimeout.h:53
getlineWithTimeout::running
bool running
Definition:
getlineWithTimeout.h:17
getlineWithTimeout::nocopyLine::timedOut
bool timedOut() const
Definition:
getlineWithTimeout.h:62
getlineWithTimeout::reader
void reader()
Definition:
getlineWithTimeout.h:18
unwindCheck.h
getlineWithTimeout::readerThread
std::thread readerThread
Definition:
getlineWithTimeout.h:16
getlineWithTimeout::nocopyLine::~nocopyLine
~nocopyLine()
Definition:
getlineWithTimeout.h:68
Generated by
1.8.17