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
b5565720
Commit
b5565720
authored
Mar 06, 2019
by
Hauke Goldhammer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add first draft of caf::bb::file_reader actor and policy
parent
3b33fe79
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
147 additions
and
0 deletions
+147
-0
libcaf_bb/caf/bb/file_reader.hpp
libcaf_bb/caf/bb/file_reader.hpp
+105
-0
libcaf_bb/test/file_reader.cpp
libcaf_bb/test/file_reader.cpp
+42
-0
No files found.
libcaf_bb/caf/bb/file_reader.hpp
0 → 100644
View file @
b5565720
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <fstream>
#include <string>
#include "caf/behavior.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp"
#include "caf/unit.hpp"
namespace
caf
{
namespace
bb
{
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
;
/// Returns number of produced elements or an error.
expected
<
size_t
>
operator
()(
std
::
string
line
,
downstream
<
value_type
>
out
)
=
0
;
};
/// @relates file-reader
struct
file_reader_state
{
// -- constructors, destructors, and assignment operators --------------------
file_reader_state
()
:
name
(
"file-reader"
)
{
// nop
}
void
init
(
file_name
fname
)
{
file
.
open
(
fname
);
}
// -- properties -------------------------------------------------------------
size_t
at_end
()
const
{
return
file
.
eof
();
}
// -- member variables -------------------------------------------------------
/// Gives this actor a useful name in CAF logs.
const
char
*
name
;
/// File
std
::
ifstream
file
;
/// Caches the file 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
)
{
using
value_type
=
typename
Policy
::
value_type
;
self
->
state
.
init
(
fname
);
// Fail early if we got nothing to stream.
if
(
!
self
->
state
.
file
.
is_open
())
return
{};
// Spin up stream manager and connect the first sink.
auto
src
=
self
->
make_source
(
std
::
move
(
sink
),
[
&
](
unit_t
&
)
{
// nop
},
[
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
))
i
+=
pol
(
st
.
line
,
out
);
},
[
self
](
const
unit_t
&
)
{
return
self
->
state
.
at_end
();
});
// Add the remaining sinks.
std
::
initializer_list
<
unit_t
>
{
src
.
ptr
()
->
add_outbound_path
(
sinks
)...};
return
{};
}
}
// namespace bb
}
// namespace caf
libcaf_bb/test/file_reader.cpp
0 → 100644
View file @
b5565720
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE file_reader
#include "caf/bb/file_reader.hpp"
#include "caf/test/dsl.hpp"
#include "caf/actor_system.hpp"
#include "caf/actor_system_config.hpp"
using
namespace
caf
;
namespace
{
struct
fixture
{};
}
// namespace
CAF_TEST_FIXTURE_SCOPE
(
file_reader_tests
,
fixture
)
CAF_TEST
(
todo
)
{
// implement me
}
CAF_TEST_FIXTURE_SCOPE_END
()
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