Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
actor-incubator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
actor-incubator
Commits
cb66aed0
Commit
cb66aed0
authored
Mar 08, 2019
by
Hauke Goldhammer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change caf::bb::file_reader to a more generic stream_reader
parent
b5565720
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
22 deletions
+34
-22
libcaf_bb/caf/bb/stream_reader.hpp
libcaf_bb/caf/bb/stream_reader.hpp
+33
-21
libcaf_bb/test/stream_reader.cpp
libcaf_bb/test/stream_reader.cpp
+1
-1
No files found.
libcaf_bb/caf/bb/
file
_reader.hpp
→
libcaf_bb/caf/bb/
stream
_reader.hpp
View file @
cb66aed0
...
...
@@ -20,10 +20,12 @@
#include <fstream>
#include <string>
#include <vector>
#include "caf/behavior.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp"
#include "caf/string_algorithms.hpp"
#include "caf/unit.hpp"
namespace
caf
{
...
...
@@ -33,31 +35,37 @@ using file_name = std::string;
/// @relates file-reader
/// The Policy defines how the file-reader pares a line of the given file.
template
<
class
Stream_Output
>
class
Policy
{
public:
using
value_type
=
typename
Stream_Output
::
value_type
;
using
value_type
=
int
;
/// Returns number of produced elements or an error.
expected
<
size_t
>
operator
()(
std
::
string
line
,
downstream
<
value_type
>
out
)
=
0
;
expected
<
size_t
>
operator
()(
std
::
string
line
,
downstream
<
value_type
>
out
)
{
std
::
vector
<
std
::
string
>
tokens
;
split
(
tokens
,
line
,
' '
);
for
(
auto
&
token
:
tokens
)
out
.
push
(
std
::
stoi
(
token
));
return
tokens
.
size
();
}
};
/// @relates file-reader
struct
file_reader_state
{
/// @relates stream_reader
template
<
class
iStream
>
struct
stream_reader_state
{
// -- constructors, destructors, and assignment operators --------------------
file_reader_state
()
:
name
(
"file
-reader"
)
{
stream_reader_state
()
:
name
(
"stream
-reader"
)
{
// nop
}
void
init
(
file_name
fname
)
{
file
.
open
(
fname
);
void
init
(
iStream
&&
src_stream
)
{
stream
=
std
::
move
(
src_stream
);
}
// -- properties -------------------------------------------------------------
size_t
at_end
()
const
{
return
file
.
eof
();
return
stream
.
eof
();
}
// -- member variables -------------------------------------------------------
...
...
@@ -65,22 +73,26 @@ struct file_reader_state {
/// Gives this actor a useful name in CAF logs.
const
char
*
name
;
///
File
std
::
ifstream
file
;
///
Stream
iStream
stream
;
/// Caches the
file
line we are about to stream.
/// Caches the
stream
line we are about to stream.
std
::
string
line
;
};
/// Streams the content of given file `fname` line by line using the given
/// policy to all given stream sinks.
template
<
class
Policy
,
class
Handle
,
class
...
Handles
>
behavior
file_reader
(
stateful_actor
<
file_reader_state
>*
self
,
file_name
fname
,
Handle
sink
,
Handles
...
sinks
)
{
/// @relates stream_reader
template
<
class
iStream
>
using
stream_source_type
=
stateful_actor
<
stream_reader_state
<
iStream
>>
;
/// Streams the content of given istream line by line using the given policy to
/// all given stream sinks.
template
<
class
Policy
,
class
iStream
,
class
Handle
,
class
...
Handles
>
behavior
stream_reader
(
stream_source_type
<
iStream
>*
self
,
iStream
src_stream
,
Handle
sink
,
Handles
...
sinks
)
{
using
value_type
=
typename
Policy
::
value_type
;
self
->
state
.
init
(
fname
);
self
->
state
.
init
(
std
::
move
(
src_stream
)
);
// Fail early if we got nothing to stream.
if
(
!
self
->
state
.
file
.
is_open
())
if
(
!
self
->
state
.
at_end
())
return
{};
// Spin up stream manager and connect the first sink.
auto
src
=
self
->
make_source
(
...
...
@@ -91,8 +103,8 @@ behavior file_reader(stateful_actor<file_reader_state>* self, file_name fname,
[
self
](
unit_t
&
,
downstream
<
value_type
>&
out
,
size_t
hint
)
{
auto
&
st
=
self
->
state
;
Policy
pol
;
auto
i
=
0
;
while
(
i
<
hint
&&
getline
(
st
.
file
,
st
.
line
))
size_t
i
=
0
;
while
(
i
<
hint
&&
getline
(
st
.
stream
,
st
.
line
))
i
+=
pol
(
st
.
line
,
out
);
},
[
self
](
const
unit_t
&
)
{
return
self
->
state
.
at_end
();
});
...
...
libcaf_bb/test/
file
_reader.cpp
→
libcaf_bb/test/
stream
_reader.cpp
View file @
cb66aed0
...
...
@@ -18,7 +18,7 @@
#define CAF_SUITE file_reader
#include "caf/bb/
file
_reader.hpp"
#include "caf/bb/
stream
_reader.hpp"
#include "caf/test/dsl.hpp"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment